| | |
| | | package util |
| | | |
| | | import ( |
| | | "basic.com/pubsub/protomsg.git" |
| | | "bufio" |
| | | "fmt" |
| | | "gocv.io/x/gocv" |
| | | "image" |
| | | "image/color" |
| | | |
| | | "gocv.io/x/gocv" |
| | | "os" |
| | | "ruleprocess/cache" |
| | | "basic.com/valib/logger.git" |
| | | "ruleprocess/ruleserver" |
| | | "ruleprocess/structure" |
| | | "strings" |
| | | ) |
| | | |
| | | func CvRTSP() { |
| | |
| | | webcam, _ := gocv.OpenVideoCapture(url) |
| | | window := gocv.NewWindow("Hello") |
| | | img := gocv.NewMat() |
| | | |
| | | for { |
| | | webcam.Read(&img) |
| | | window.IMShow(img) |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | func DrawPolygonOnImageForYolo(cameraId string, img protomsg.Image, results []structure.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} |
| | | scale := float64(img.Width) / 960 // 画图比例 |
| | | logger.Info("width:",img.Width,"--画图比例:",scale) |
| | | // 分割区域id集合并根据id查询区域然后画框 |
| | | for _,result := range results { |
| | | polygonIds := strings.Split(result.AlarmPolygon,",") |
| | | logger.Info("-----------------------看看报警区域id:",polygonIds) |
| | | for i := 0; i < len(polygonIds); i++ { |
| | | polygon := getPolygonById(polygonIds[i],cameraId) |
| | | if polygon.Polygon != "[]" && polygon.Polygon != ""{ |
| | | logger.Debug("所画区域:",polygon.Polygon) |
| | | DrawAPolygon(&rook,polygon.Polygon,yellow,scale) |
| | | } |
| | | } |
| | | } |
| | | // 把目标框出来 |
| | | 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 |
| | | } |
| | | |
| | | func DrawPolygonOnImageForFace(cameraId string, img protomsg.Image, results []structure.FaceResult,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} |
| | | scale := float64(img.Width) / 960 // 画图比例 |
| | | // 分割区域id集合并根据id查询区域然后画框 |
| | | for _,result := range results { |
| | | polygonIds := strings.Split(result.AlarmPolygon,",") |
| | | logger.Info("-----------------------看看报警区域id:",polygonIds) |
| | | for i := 0; i < len(polygonIds); i++ { |
| | | polygon := getPolygonById(polygonIds[i],cameraId) |
| | | logger.Info("----查到的报警框:",polygon) |
| | | if polygon.Polygon != "[]" && polygon.Polygon != ""{ |
| | | logger.Debug("所画区域:",polygon.Polygon) |
| | | DrawAPolygon(&rook,polygon.Polygon,yellow,scale) |
| | | } |
| | | } |
| | | } |
| | | //return nil,nil |
| | | maps,err0 = UploadFromMat(url,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) |
| | | logger.Info("根据id查到的报警框:",cameraPolygons) |
| | | for _, polygon := range cameraPolygons { |
| | | logger.Info("查到的区域id:",polygon.Id,"--要匹配的数据id:",polygonId,"---是否相等:",strings.Contains(polygonId,polygon.Id)) |
| | | if strings.Contains(polygonId,polygon.Id) { |
| | | logger.Info("进来即相等") |
| | | return polygon |
| | | } |
| | | } |
| | | return protomsg.CameraPolygon{} |
| | | } |
| | | // 在图上画一个框 |
| | | func DrawAPolygon(rook *gocv.Mat,polygonString string, color color.RGBA,scale float64) { |
| | | 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 * scale), int(points[index].Y * scale)), image.Pt(int(points[0].X * scale), int(points[0].Y * scale)), color, 2) |
| | | } else { |
| | | gocv.Line(rook, image.Pt(int(points[index].X * scale), int(points[index].Y * scale)), image.Pt(int(points[index+1].X * scale), int(points[index+1].Y * scale)), color, 2) |
| | | } |
| | | } |
| | | } |