---
panlei
2019-07-06 4f3c77d4bee93f15d72a1d12a19af68428c29151
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package util
 
import (
    "basic.com/pubsub/protomsg.git"
    "bufio"
    "bytes"
    "encoding/json"
    "errors"
    "fmt"
    "gocv.io/x/gocv"
    "image"
    "image/color"
    "io"
    "log"
    "mime/multipart"
    "net/http"
    "os"
    "ruleprocess/cache"
    "ruleprocess/ruleserver"
    "time"
)
 
func CvRTSP() {
    url := `rtsp://admin:a1234567@192.168.1.188:554/h264/ch1/main/av_stream`
 
    webcam, _ := gocv.OpenVideoCapture(url)
    window := gocv.NewWindow("Hello")
    img := gocv.NewMat()
    for {
        webcam.Read(&img)
        window.IMShow(img)
        window.WaitKey(1)
    }
}
 
func cvFaceDetect() {
    // set to use a video capture device 0
    deviceID := 0
    url := `rtsp://admin:a1234567@192.168.1.188:554/h264/ch1/main/av_stream`
 
    // open webcam
    webcam, err := gocv.OpenVideoCapture(url)
    if err != nil {
        fmt.Println(err)
        return
    }
    defer webcam.Close()
 
    // open display window
    window := gocv.NewWindow("Face Detect")
    defer window.Close()
 
    // prepare image matrix
    img := gocv.NewMat()
    defer img.Close()
 
    // color for the rect when faces detected
    blue := color.RGBA{0, 0, 255, 0}
 
    // load classifier to recognize faces
    classifier := gocv.NewCascadeClassifier()
    defer classifier.Close()
 
    if !classifier.Load("data/haarcascade_frontalface_default.xml") {
        fmt.Println("Error reading cascade file: data/haarcascade_frontalface_default.xml")
        return
    }
 
    fmt.Printf("start reading camera device: %v\n", deviceID)
    for {
        if ok := webcam.Read(&img); !ok {
            fmt.Printf("cannot read device %v\n", deviceID)
            return
        }
        if img.Empty() {
            continue
        }
 
        // detect faces
        rects := classifier.DetectMultiScale(img)
        fmt.Printf("found %d faces\n", len(rects))
 
        // draw a rectangle around each face on the original image
        for _, r := range rects {
            gocv.Rectangle(&img, r, blue, 3)
        }
 
        // show the image in the window, and wait 1 millisecond
        window.IMShow(img)
        window.WaitKey(1)
    }
}
 
var w = 400
 
// CVDraw demo
func CVDraw() {
    windowA := gocv.NewWindow("basic drawing: atom")
    windowR := gocv.NewWindow("basic drawing: rook")
    defer windowA.Close()
    defer windowR.Close()
 
    atom := gocv.NewMatWithSize(w, w, gocv.MatTypeCV8UC3)
    defer atom.Close()
 
    rook := gocv.NewMatWithSize(w, w, gocv.MatTypeCV8UC3)
    defer rook.Close()
 
    black := color.RGBA{0, 0, 0, 0}
    blue := color.RGBA{0, 0, 255, 0}
    red := color.RGBA{255, 0, 0, 0}
    white := color.RGBA{255, 255, 255, 0}
    yellow := color.RGBA{255, 255, 0, 0}
 
    // draw the atom
    gocv.Ellipse(&atom, image.Pt(w/2., w/2.), image.Pt(w/4.0, w/16.0), 90., 0, 360, blue, 2)
    gocv.Ellipse(&atom, image.Pt(w/2., w/2.), image.Pt(w/4.0, w/16.0), 0., 0, 360, blue, 2)
    gocv.Ellipse(&atom, image.Pt(w/2., w/2.), image.Pt(w/4.0, w/16.0), 45., 0, 360, blue, 2)
    gocv.Ellipse(&atom, image.Pt(w/2., w/2.), image.Pt(w/4.0, w/16.0), -45., 0, 360, blue, 2)
    gocv.Circle(&atom, image.Pt(w/2., w/2.), w/32., red, -1)
 
    // 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.),
        },
    }
    gocv.FillPoly(&rook, points, white)
    gocv.Rectangle(&rook, image.Rect(0, 7*w/8.0, w, w), yellow, -1)
    gocv.Line(&rook, image.Pt(0, 15*w/16), image.Pt(w, 15*w/16), black, 2)
    gocv.Line(&rook, image.Pt(w/4, 7*w/8), image.Pt(w/4, w), black, 2)
    gocv.Line(&rook, image.Pt(w/2, 7*w/8), image.Pt(w/2, w), black, 2)
    gocv.Line(&rook, image.Pt(3*w/4, 7*w/8), image.Pt(3*w/4, w), black, 2)
 
    for {
        windowA.IMShow(atom)
        windowR.IMShow(rook)
 
        if windowA.WaitKey(10) >= 0 || windowR.WaitKey(10) >= 0 {
            break
        }
    }
}
 
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}
    // 查到摄像机所有的区域并画框
    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)
            }
 
        }
    }
    // 把目标框出来
    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
    // 上传
    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
}