From 509506fd53635830ff43572df0d7845debf376f9 Mon Sep 17 00:00:00 2001 From: panlei <2799247126@qq.com> Date: 星期五, 12 七月 2019 20:29:48 +0800 Subject: [PATCH] 改造定时器 --- util/simpleCV.go | 112 +++++++++++++++++++++++++++++++++++++++++--------------- 1 files changed, 82 insertions(+), 30 deletions(-) diff --git a/util/simpleCV.go b/util/simpleCV.go index 9513826..5b33210 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() { @@ -14,7 +20,6 @@ webcam, _ := gocv.OpenVideoCapture(url) window := gocv.NewWindow("Hello") img := gocv.NewMat() - for { webcam.Read(&img) window.IMShow(img) @@ -93,7 +98,6 @@ defer atom.Close() rook := gocv.NewMatWithSize(w, w, gocv.MatTypeCV8UC3) - gocv.NewMatW defer rook.Close() black := color.RGBA{0, 0, 0, 0} @@ -151,31 +155,79 @@ } } -func DrawPolygon(){ - // draw the rook - 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.), - }, +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} + + // 鍒嗗壊鍖哄煙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, white) + // 鎶婄洰鏍囨鍑烘潵 + 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