-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathopencv.go
85 lines (68 loc) · 2.52 KB
/
opencv.go
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
package opencv
import (
"fmt"
"image"
"image/color"
"gocv.io/x/gocv"
_ "golang.org/x/image/bmp"
)
type ImageProcessorInterface interface {
CaptureStreamVideo(input interface{}, runDetectionCallback func(currentFrame gocv.Mat)) error
EncodeImageToBMP(currentFrame gocv.Mat) (output *gocv.NativeByteBuffer, err error)
DrawRectangle(currentImage gocv.Mat, coordinates image.Rectangle)
DrawText(currentImage gocv.Mat, text string, coordinates image.Point)
}
type OpenCVImageProcessor struct{}
func NewOpenCVImageProcessor() *OpenCVImageProcessor {
return &OpenCVImageProcessor{}
}
// Start capturing video frames by Device ID, video file, RTSP/URL, or GStreamer pipeline
func (oip *OpenCVImageProcessor) CaptureStreamVideo(input interface{}, runDetectionCallback func(currentFrame gocv.Mat)) error {
videoStream, err := gocv.OpenVideoCapture(input)
if err != nil {
return fmt.Errorf("unable to open video capture: %v", err)
}
frame := gocv.NewMat()
window := gocv.NewWindow("Golang Object Detection")
if ok := videoStream.Read(&frame); !ok {
return fmt.Errorf("unable to capture image from camera")
}
for {
videoStream.Read(&frame)
if frame.Empty() {
continue
}
runDetectionCallback(frame)
window.IMShow(frame)
window.WaitKey(1)
}
}
// Compresses the image and stores it in the returned memory buffer, using .bmp extension
func (oip *OpenCVImageProcessor) EncodeImageToBMP(currentFrame gocv.Mat) (output *gocv.NativeByteBuffer, err error) {
buf, err := gocv.IMEncode(".bmp", currentFrame)
if err != nil {
return nil, err
}
return buf, nil
}
// Draws a rectangle on the processed frame at the passed coordinates
func (oip *OpenCVImageProcessor) DrawRectangle(currentImage gocv.Mat, coordinates image.Rectangle) {
gocv.Rectangle(
¤tImage, // image
coordinates, // rectangle
color.RGBA{R: 64, G: 255, B: 134, A: 1}, // rgba color
2, // thickness
)
}
// Draws a text on the processed frame at the passed coordinates
func (oip *OpenCVImageProcessor) DrawText(currentImage gocv.Mat, text string, coordinates image.Point) {
gocv.PutText(
¤tImage, //image
text, // text
coordinates, // image point
gocv.FontHersheySimplex, // font face
0.6, // font scale
color.RGBA{R: 64, G: 255, B: 134, A: 1}, // color
2, // thickness
)
}