diff --git a/exp/gocv-exp/gocv-exp b/exp/gocv-exp/gocv-exp new file mode 100644 index 00000000..7a51493d Binary files /dev/null and b/exp/gocv-exp/gocv-exp differ diff --git a/exp/gocv-exp/leatherjacket.mp4 b/exp/gocv-exp/leatherjacket.mp4 new file mode 100644 index 00000000..55815d30 Binary files /dev/null and b/exp/gocv-exp/leatherjacket.mp4 differ diff --git a/exp/gocv-exp/littleFish.mp4 b/exp/gocv-exp/littleFish.mp4 new file mode 100644 index 00000000..4b8d9d9d Binary files /dev/null and b/exp/gocv-exp/littleFish.mp4 differ diff --git a/exp/gocv-exp/littleFish2.mp4 b/exp/gocv-exp/littleFish2.mp4 new file mode 100644 index 00000000..41817558 Binary files /dev/null and b/exp/gocv-exp/littleFish2.mp4 differ diff --git a/exp/gocv-exp/main.go b/exp/gocv-exp/main.go new file mode 100644 index 00000000..9de10184 --- /dev/null +++ b/exp/gocv-exp/main.go @@ -0,0 +1,117 @@ +// What it does: +// +// This example detects motion using a delta threshold from the first frame, +// and then finds contours to determine where the object is located. +// +// Very loosely based on Adrian Rosebrock code located at: +// http://www.pyimagesearch.com/2015/06/01/home-surveillance-and-motion-detection-with-the-raspberry-pi-python-and-opencv/ + +package main + +import ( + "fmt" + "image" + "image/color" + "os" + "strconv" + "time" + + "gocv.io/x/gocv" +) + +const MinimumArea = 1000 + +func main() { + if len(os.Args) < 2 { + fmt.Println("How to run:\n\tmotion-detect [camera ID]") + return + } + + // parse args + deviceID := os.Args[1] + minArea, _ := strconv.Atoi(os.Args[2]) + + webcam, err := gocv.OpenVideoCapture(deviceID) + if err != nil { + fmt.Printf("Error opening video capture device: %v\n", deviceID) + return + } + defer webcam.Close() + + window1 := gocv.NewWindow("Motion Window") + defer window1.Close() + + window2 := gocv.NewWindow("Threshold window") + defer window2.Close() + + time.Sleep(2 * time.Second) + + img := gocv.NewMat() + defer img.Close() + + imgDelta := gocv.NewMat() + defer imgDelta.Close() + + imgThresh := gocv.NewMat() + defer imgThresh.Close() + + mog2 := gocv.NewBackgroundSubtractorMOG2() + defer mog2.Close() + + status := "Ready" + + fmt.Printf("Start reading device: %v\n", deviceID) + for { + if ok := webcam.Read(&img); !ok { + fmt.Printf("Device closed: %v\n", deviceID) + return + } + if img.Empty() { + continue + } + + status = "READY" + statusColor := color.RGBA{0, 255, 0, 0} + + // first phase of cleaning up image, obtain foreground only + mog2.Apply(img, &imgDelta) + + // remaining cleanup of the image to use for finding contours. + // first use threshold + gocv.Threshold(imgDelta, &imgThresh, 25, 255, gocv.ThresholdBinary) + + // then dilate + kernel := gocv.GetStructuringElement(gocv.MorphRect, image.Pt(3, 3)) + defer kernel.Close() + + // Erode + gocv.Erode(imgThresh, &imgThresh, kernel) + // Dilate + gocv.Dilate(imgThresh, &imgThresh, kernel) + + // now find contours + contours := gocv.FindContours(imgThresh, gocv.RetrievalExternal, gocv.ChainApproxSimple) + for _, c := range contours { + area := gocv.ContourArea(c) + if area < float64(minArea) { + continue + } + + status = "MOTION DETECTED" + statusColor = color.RGBA{255, 0, 0, 0} + + //gocv.DrawContours(&img, contours, i, statusColor, 2) + + rect := gocv.BoundingRect(c) + gocv.Rectangle(&img, rect, color.RGBA{0, 0, 255, 0}, 1) + } + + gocv.PutText(&img, status, image.Pt(10, 20), gocv.FontHersheyPlain, 1.2, statusColor, 2) + + window1.IMShow(img) + window2.IMShow(imgThresh) + if window1.WaitKey(1) == 27 { + break + } + } +} diff --git a/exp/gocv-exp/portJackson.mp4 b/exp/gocv-exp/portJackson.mp4 new file mode 100644 index 00000000..0563f292 Binary files /dev/null and b/exp/gocv-exp/portJackson.mp4 differ diff --git a/exp/gocv-exp/school.mp4 b/exp/gocv-exp/school.mp4 new file mode 100644 index 00000000..581e06b0 Binary files /dev/null and b/exp/gocv-exp/school.mp4 differ diff --git a/exp/gocv-exp/squid.mp4 b/exp/gocv-exp/squid.mp4 new file mode 100644 index 00000000..6a2ffedf Binary files /dev/null and b/exp/gocv-exp/squid.mp4 differ diff --git a/exp/gocv-exp/squid2.mp4 b/exp/gocv-exp/squid2.mp4 new file mode 100644 index 00000000..4d29057d Binary files /dev/null and b/exp/gocv-exp/squid2.mp4 differ diff --git a/exp/gocv-exp/squid3.mp4 b/exp/gocv-exp/squid3.mp4 new file mode 100644 index 00000000..169181b0 Binary files /dev/null and b/exp/gocv-exp/squid3.mp4 differ diff --git a/go.mod b/go.mod index a1860670..ec014617 100644 --- a/go.mod +++ b/go.mod @@ -12,5 +12,6 @@ require ( github.com/mewkiz/flac v1.0.5 github.com/pkg/errors v0.8.1 github.com/yobert/alsa v0.0.0-20180630182551-d38d89fa843e + gocv.io/x/gocv v0.21.0 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect ) diff --git a/go.sum b/go.sum index 7870ba89..d82a7b0c 100644 --- a/go.sum +++ b/go.sum @@ -52,6 +52,8 @@ go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/ go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= go.uber.org/zap v1.10.0 h1:ORx85nbTijNz8ljznvCMR1ZBIPKFn3jQrag10X2AsuM= go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= +gocv.io/x/gocv v0.21.0 h1:dVjagrupZrfCRY0qPEaYWgoNMRpBel6GYDH4mvQOK8Y= +gocv.io/x/gocv v0.21.0/go.mod h1:Rar2PS6DV+T4FL+PM535EImD/h13hGVaHhnCu1xarBs= golang.org/x/sys v0.0.0-20190913121621-c3b328c6e5a7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=