| | |
| | | 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() { |
| | |
| | | webcam, _ := gocv.OpenVideoCapture(url) |
| | | window := gocv.NewWindow("Hello") |
| | | img := gocv.NewMat() |
| | | gocv.add |
| | | for { |
| | | webcam.Read(&img) |
| | | window.IMShow(img) |
| | |
| | | } |
| | | } |
| | | |
| | | 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集合并根据id查询区域然后画框 |
| | | 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) |
| | | } |
| | | } |
| | | } |