From 716810adb573f7e6b40ab52887b5b158d9824817 Mon Sep 17 00:00:00 2001
From: panlei <2799247126@qq.com>
Date: 星期一, 16 九月 2019 17:04:03 +0800
Subject: [PATCH] 读公共的配置文件

---
 util/simpleCV.go |  135 ++++++++++++++++++---------------------------
 1 files changed, 54 insertions(+), 81 deletions(-)

diff --git a/util/simpleCV.go b/util/simpleCV.go
index 1d617dd..6ce97a2 100644
--- a/util/simpleCV.go
+++ b/util/simpleCV.go
@@ -1,13 +1,17 @@
 package util
 
 import (
+	"basic.com/pubsub/protomsg.git"
 	"bufio"
-	"bytes"
 	"fmt"
 	"gocv.io/x/gocv"
 	"image"
 	"image/color"
 	"os"
+	"ruleprocess/cache"
+	"ruleprocess/logger"
+	"ruleprocess/ruleserver"
+	"strings"
 )
 
 func CvRTSP() {
@@ -151,89 +155,35 @@
 	}
 }
 
-func DrawPolygonOnImage()(maps map[string]interface{}, err0 error){
-	// draw the rook
-	//rook := gocv.NewMatWithSize(w, w, gocv.MatTypeCV8UC3)
-	//imgs := gocv.NewMat()
-	ddd, err := RetrieveROM("/home/user/workspace/ruleprocess/util/105.jpg")
-	if err != nil {
-		fmt.Println("瑙g爜鏈夎",err)
-	}
-	bbb := bytes.NewBuffer(ddd)  // 蹇呴』鍔犱竴涓猙uffer 涓嶇劧娌℃湁read鏂规硶灏变細鎶ラ敊
+func DrawPolygonOnImage(cameraId string, img protomsg.Image, results []ruleserver.Result,url string) (maps map[string]interface{}, err0 error) {
 
-
-
-
-	rook, _ := gocv.NewMatFromBytes(500, 500, gocv.MatTypeCV8UC3, bbb.Bytes())
+	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()
-	green := color.RGBA{0, 255, 0, 0}
-	points := [][]image.Point{
-		{
-			image.Pt(100., 100),
-			image.Pt(100., 400),
-			image.Pt(400, 400),
-			image.Pt(400, 100),
-		},
-	}
-	gocv.FillPoly(&rook, points, green)
 
-	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
-	//}
+	yellow := color.RGBA{255, 255, 0, 0}
+	red := color.RGBA{255, 0, 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,yellow)
+			}
+		}
+	}
+	// 鎶婄洰鏍囨鍑烘潵
+	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
+	maps,err0 = UploadFromMat(url,rook)
+	return
 }
 
 // 鎶婂浘鐗囪浆鎴愪簩杩涘埗娴�
@@ -258,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