From 56beffe0aa33aaaea37f8f58f38cc7c90cbe6bdd Mon Sep 17 00:00:00 2001 From: panlei <2799247126@qq.com> Date: 星期五, 12 七月 2019 15:23:30 +0800 Subject: [PATCH] --- --- util/simpleCV.go | 114 +++++++++++++++++++-------------------------------------- 1 files changed, 38 insertions(+), 76 deletions(-) diff --git a/util/simpleCV.go b/util/simpleCV.go index 04c9340..74c59d3 100644 --- a/util/simpleCV.go +++ b/util/simpleCV.go @@ -3,21 +3,15 @@ import ( "basic.com/pubsub/protomsg.git" "bufio" - "bytes" - "encoding/json" - "errors" "fmt" "gocv.io/x/gocv" "image" "image/color" - "io" - "log" - "mime/multipart" - "net/http" "os" "ruleprocess/cache" + "ruleprocess/logger" "ruleprocess/ruleserver" - "time" + "strings" ) func CvRTSP() { @@ -168,83 +162,28 @@ defer rook.Close() red := color.RGBA{255, 0, 0, 0} - // 鏌ュ埌鎽勫儚鏈烘墍鏈夌殑鍖哄煙骞剁敾妗� - var cameraPolygons []protomsg.CameraPolygon - cameraPolygons = cache.GetPolygonsByCameraId(cameraId) - for _, polygon := range cameraPolygons { - points := ruleserver.Json2points(polygon.Polygon) - for index := 0; index < len(points); index++ { - if index == len(points)-1 { // 闂悎鍥惧舰 - gocv.Line(&rook, image.Pt(int(points[index].X), int(points[index].Y)), image.Pt(int(points[0].X), int(points[0].Y)), red, 2) - } else { - gocv.Line(&rook, image.Pt(int(points[index].X), int(points[index].Y)), image.Pt(int(points[index+1].X), int(points[index+1].Y)), red, 2) - } + green := color.RGBA{0, 255, 0, 0} + // 鍒嗗壊鍖哄煙id闆嗗悎骞舵牴鎹甶d鏌ヨ鍖哄煙鐒跺悗鐢绘 + for _,result := range results { + polygonIds := strings.Split(result.AlarmPolygon,",") + logger.Info("-----------------------鐪嬬湅鎶ヨ鍖哄煙id锛�",polygonIds) + for i := 0; i < len(polygonIds)-1; i++ { + polygon := getPolygonById(polygonIds[i],cameraId) + if polygon.Polygon != "" { + DrawAPolygon(&rook,polygon.Polygon,red) + } } } // 鎶婄洰鏍囨鍑烘潵 for _,result := range results { for _,rect := range result.Location { - gocv.Rectangle(&rook, image.Rect(int(rect.X), int(rect.Y), int(rect.X+rect.Width), int(rect.Y+rect.Height)), red, 1) + gocv.Rectangle(&rook, image.Rect(int(rect.X), int(rect.Y), int(rect.X+rect.Width), int(rect.Y+rect.Height)), green, 1) } } //return nil,nil - // 涓婁紶 - fdata, _ := gocv.IMEncode(".jpg", rook) - body := &bytes.Buffer{} - writer := multipart.NewWriter(body) - _, err1 := writer.CreateFormFile("file", "fasjuierf") - if err1 != nil { - return nil, err1 - } - boundary := writer.Boundary() - //close_string := fmt.Sprintf("\r\n--%s--\r\n", boundary) - close_buf := bytes.NewBufferString(fmt.Sprintf("\r\n--%s--\r\n", boundary)) - file := bytes.NewBuffer(fdata) - request_reader := io.MultiReader(body, file, close_buf) - //_, err = io.Copy(part, file) - //writer.WriteField(key, val) - request, err := http.NewRequest("POST", "http://192.168.1.182:6333/submit", request_reader) - request.Header.Add("Content-Type", writer.FormDataContentType()) - timeout := time.Duration(5 * time.Second) //瓒呮椂鏃堕棿50ms - client := &http.Client{Timeout: timeout} - resp, err := client.Do(request) - if err != nil { - log.Fatal(err) - return nil, err - } - defer func() { - if r := recover(); r != nil { - fmt.Printf("panic鐨勫唴瀹�%v\n", r) - msg := "涓婁紶鍥剧墖鏈嶅姟鍣ㄥ紓甯�" - if _, ok := r.(error); ok { - msg = r.(error).Error() - fmt.Println("panic--recover()寰楀埌鐨勬槸error绫诲瀷") - } - if _, ok := r.(string); ok { - msg = r.(string) - fmt.Println("panic--recover()寰楀埌鐨勬槸string绫诲瀷") - } - err0 = errors.New(msg) - } - }() - defer resp.Body.Close() - { - body := &bytes.Buffer{} - _, err := body.ReadFrom(resp.Body) - if err != nil { - log.Fatal(err) - } - fmt.Println(resp.StatusCode) - //fmt.Println(resp.Header) - fmt.Println(body) - //decoder := json.NewDecoder(strings.NewReader(body.String())) - decoder := make(map[string]interface{}) - if err := json.Unmarshal([]byte(body.String()), &decoder); err != nil { - return nil, err - } - return decoder, nil - } + maps,err0 = UploadFromMat(rook) + return } // 鎶婂浘鐗囪浆鎴愪簩杩涘埗娴� @@ -269,3 +208,26 @@ return bytes, err } +// 鏍规嵁id鍘荤紦瀛橀噷鏌ヨ澶氳竟褰� +func getPolygonById(polygonId string, cameraId string) (protomsg.CameraPolygon){ + // 鏌ュ埌鎽勫儚鏈烘墍鏈夌殑鍖哄煙骞剁敾妗� + var cameraPolygons []protomsg.CameraPolygon + cameraPolygons = cache.GetPolygonsByCameraId(cameraId) + for _, polygon := range cameraPolygons { + if polygon.Id == polygonId { + return polygon + } + } + return protomsg.CameraPolygon{} +} +// 鍦ㄥ浘涓婄敾涓�涓 +func DrawAPolygon(rook *gocv.Mat,polygonString string, color color.RGBA) { + points := ruleserver.Json2points(polygonString) + for index := 0; index < len(points); index++ { + if index == len(points)-1 { // 闂悎鍥惧舰 + gocv.Line(rook, image.Pt(int(points[index].X), int(points[index].Y)), image.Pt(int(points[0].X), int(points[0].Y)), color, 2) + } else { + gocv.Line(rook, image.Pt(int(points[index].X), int(points[index].Y)), image.Pt(int(points[index+1].X), int(points[index+1].Y)), color, 2) + } + } +} -- Gitblit v1.8.0