panlei
2019-07-02 1ccfcd8575bf706f5c4086a945c7429889317da2
util/simpleCV.go
@@ -1,11 +1,23 @@
package util
import (
   "basic.com/pubsub/protomsg.git"
   "bufio"
   "bytes"
   "encoding/json"
   "errors"
   "fmt"
   "gocv.io/x/gocv"
   "image"
   "image/color"
   "gocv.io/x/gocv"
   "io"
   "log"
   "mime/multipart"
   "net/http"
   "os"
   "ruleprocess/cache"
   "ruleprocess/ruleserver"
   "time"
)
func CvRTSP() {
@@ -149,34 +161,121 @@
   }
}
func DrawPolygon(){
func DrawPolygonOnImage(cameraId string, img protomsg.Image) (maps map[string]interface{}, err0 error) {
   // draw the rook
   rook := gocv.NewMatWithSize(w, w, gocv.MatTypeCV8UC3)
   //ddd, err := RetrieveROM("/home/user/workspace/ruleprocess/util/105.jpg")
   //if err != nil {
   //   fmt.Println("解码有误",err)
   //}
   //bbb := bytes.NewBuffer(ddd)  // 必须加一个buffer 不然没有read方法就会报错
   //rook,_ := gocv.IMDecode(bbb.Bytes(),gocv.IMReadColor)
   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(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.),
      },
   red := color.RGBA{255, 0, 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)
   // 查到摄像机所有的区域并画框
   var cameraPolygons []protomsg.CameraPolygon
   cameraPolygons = cache.GetPolygonsByCameraId(cameraId)
   for _, polygon := range cameraPolygons {
      points := ruleserver.Json2points(polygon.Polygon)
      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)), red, 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)), red, 2)
         }
      }
   }
   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
   }
}
// 把图片转成二进制流
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
}