From 788358c03d90f6371fa7a7d238850e2cb114a95f Mon Sep 17 00:00:00 2001
From: panlei <2799247126@qq.com>
Date: 星期一, 19 八月 2019 16:41:18 +0800
Subject: [PATCH] 对于或规则的一些改进

---
 util/simpleCV.go |  120 +++++++++++++++++++++--------------------------------------
 1 files changed, 43 insertions(+), 77 deletions(-)

diff --git a/util/simpleCV.go b/util/simpleCV.go
index 3a0bd38..6ce97a2 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() {
@@ -161,86 +155,35 @@
 	}
 }
 
-func DrawPolygonOnImage(cameraId string, img protomsg.Image) (maps map[string]interface{}, err0 error) {
+func DrawPolygonOnImage(cameraId string, img protomsg.Image, results []ruleserver.Result,url string) (maps map[string]interface{}, err0 error) {
 
 	rook, _ := gocv.NewMatFromBytes(int(img.Height), int(img.Width), gocv.MatTypeCV8UC3, img.Data)
 	//rook := gocv.IMRead("/home/user/workspace/ruleprocess/util/105.jpg",gocv.IMReadColor)
 	defer rook.Close()
 
+	yellow := color.RGBA{255, 255, 0, 0}
 	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)
-			}
 
+	// 鍒嗗壊鍖哄煙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,yellow)
+			}
 		}
 	}
 	// 鎶婄洰鏍囨鍑烘潵
-	//gocv.Rectangle()
+	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)
+		}
+	}
 	//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(url,rook)
+	return
 }
 
 // 鎶婂浘鐗囪浆鎴愪簩杩涘埗娴�
@@ -265,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*1.33), int(points[index].Y*1.33)), image.Pt(int(points[0].X*1.33), int(points[0].Y*1.33)), color, 2)
+		} else {
+			gocv.Line(rook, image.Pt(int(points[index].X*1.33), int(points[index].Y*1.33)), image.Pt(int(points[index+1].X*1.33), int(points[index+1].Y*1.33)), color, 2)
+		}
+	}
+}

--
Gitblit v1.8.0