mirror of https://bitbucket.org/ausocean/av.git
Added gocv experimental code with testing files
This commit is contained in:
parent
f8da6b0609
commit
e1712773e1
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1
go.mod
1
go.mod
|
@ -12,5 +12,6 @@ require (
|
||||||
github.com/mewkiz/flac v1.0.5
|
github.com/mewkiz/flac v1.0.5
|
||||||
github.com/pkg/errors v0.8.1
|
github.com/pkg/errors v0.8.1
|
||||||
github.com/yobert/alsa v0.0.0-20180630182551-d38d89fa843e
|
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
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
|
||||||
)
|
)
|
||||||
|
|
2
go.sum
2
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.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
|
||||||
go.uber.org/zap v1.10.0 h1:ORx85nbTijNz8ljznvCMR1ZBIPKFn3jQrag10X2AsuM=
|
go.uber.org/zap v1.10.0 h1:ORx85nbTijNz8ljznvCMR1ZBIPKFn3jQrag10X2AsuM=
|
||||||
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
|
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=
|
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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
|
Loading…
Reference in New Issue