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 |  109 ++++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 80 insertions(+), 29 deletions(-)

diff --git a/util/simpleCV.go b/util/simpleCV.go
index 892af23..74c59d3 100644
--- a/util/simpleCV.go
+++ b/util/simpleCV.go
@@ -1,11 +1,17 @@
 package util
 
 import (
+	"basic.com/pubsub/protomsg.git"
+	"bufio"
 	"fmt"
+	"gocv.io/x/gocv"
 	"image"
 	"image/color"
-
-	"gocv.io/x/gocv"
+	"os"
+	"ruleprocess/cache"
+	"ruleprocess/logger"
+	"ruleprocess/ruleserver"
+	"strings"
 )
 
 func CvRTSP() {
@@ -149,34 +155,79 @@
 	}
 }
 
-func DrawPolygon(){
-	// draw the rook
-	rook := gocv.NewMatWithSize(w, w, gocv.MatTypeCV8UC3)
+func DrawPolygonOnImage(cameraId string, img protomsg.Image, results []ruleserver.Result) (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()
+
+	red := color.RGBA{255, 0, 0, 0}
 	green := color.RGBA{0, 255, 0, 0}
-	points := [][]image.Point{
-		{
-			image.Pt(w/4., 7*w/8.),
-			image.Pt(3*w/4., 7*w/8.),
-			image.Pt(3*w/4., 13*w/16.),
-			image.Pt(11*w/16., 13*w/16.),
-			image.Pt(19*w/32., 3*w/8.),
-			image.Pt(3*w/4., 3*w/8.),
-			image.Pt(3*w/4., w/8.),
-			image.Pt(26*w/40., w/8.),
-			image.Pt(26*w/40., w/4.),
-			image.Pt(22*w/40., w/4.),
-			image.Pt(22*w/40., w/8.),
-			image.Pt(18*w/40., w/8.),
-			image.Pt(18*w/40., w/4.),
-			image.Pt(14*w/40., w/4.),
-			image.Pt(14*w/40., w/8.),
-			image.Pt(w/4., w/8.),
-			image.Pt(w/4., 3*w/8.),
-			image.Pt(13*w/32., 3*w/8.),
-			image.Pt(5*w/16., 13*w/16.),
-			image.Pt(w/4., 13*w/16.),
-		},
+
+	// 鍒嗗壊鍖哄煙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)
+			}
+		}
 	}
-	gocv.FillPoly(&rook, points, green)
+	// 鎶婄洰鏍囨鍑烘潵
+	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)), green, 1)
+		}
+	}
+	//return nil,nil
+	maps,err0 = UploadFromMat(rook)
+	return
+}
+
+// 鎶婂浘鐗囪浆鎴愪簩杩涘埗娴�
+func RetrieveROM(filename string) ([]byte, error) {
+	file, err := os.Open(filename)
+
+	if err != nil {
+		return nil, err
+	}
+	defer file.Close()
+
+	stats, statsErr := file.Stat()
+	if statsErr != nil {
+		return nil, statsErr
+	}
+
+	var size int64 = stats.Size()
+	bytes := make([]byte, size)
+
+	bufr := bufio.NewReader(file)
+	_, err = bufr.Read(bytes)
+
+	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