From 28ce7ab311185202eb188151a8d3d61ed94974be Mon Sep 17 00:00:00 2001
From: panlei <2799247126@qq.com>
Date: 星期一, 01 七月 2019 11:27:11 +0800
Subject: [PATCH] 添加一个gocv和一个测试函数
---
util/simpleCV.go | 151 ++++++++++++++++++++++++++++++++++++++++++++++++++
util/simpleCV_test.go | 9 +++
2 files changed, 160 insertions(+), 0 deletions(-)
diff --git a/util/simpleCV.go b/util/simpleCV.go
new file mode 100644
index 0000000..05827b1
--- /dev/null
+++ b/util/simpleCV.go
@@ -0,0 +1,151 @@
+package util
+
+import (
+ "fmt"
+ "image"
+ "image/color"
+
+ "gocv.io/x/gocv"
+)
+
+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
+ }
+ }
+}
diff --git a/util/simpleCV_test.go b/util/simpleCV_test.go
new file mode 100644
index 0000000..7030d18
--- /dev/null
+++ b/util/simpleCV_test.go
@@ -0,0 +1,9 @@
+package util
+
+import (
+ "testing"
+)
+
+func TestCvRTSP(t *testing.T) {
+ CvRTSP()
+}
\ No newline at end of file
--
Gitblit v1.8.0