From ced8727c070b2077366ab4329e2e639a6923098a Mon Sep 17 00:00:00 2001 From: Scott Date: Wed, 22 Jan 2020 12:06:14 +1030 Subject: [PATCH 1/6] filter: create a simple difference motion filter using gocv --- filter/difference.go | 114 +++++++++++++++++++++++++++++++++++++++++ revid/config/config.go | 12 +++-- revid/revid.go | 11 +++- 3 files changed, 132 insertions(+), 5 deletions(-) create mode 100644 filter/difference.go diff --git a/filter/difference.go b/filter/difference.go new file mode 100644 index 00000000..97df94b9 --- /dev/null +++ b/filter/difference.go @@ -0,0 +1,114 @@ +// +build !circleci + +/* +DESCRIPTION + A filter that detects motion and discards frames without motion. The + algorithm calculates the absolute difference for each pixel between + two frames, then finds the mean. If the mean is above a given threshold, + then it is considered motion. + +AUTHORS + Scott Barnard + +LICENSE + difference.go is Copyright (C) 2020 the Australian Ocean Lab (AusOcean) + + It is free software: you can redistribute it and/or modify them + under the terms of the GNU General Public License as published by the + Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + It is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + in gpl.txt. If not, see http://www.gnu.org/licenses. +*/ + +package filter + +import ( + "fmt" + "image" + "image/color" + "io" + + "gocv.io/x/gocv" +) + +// DiffFilter is a filter that provides basic motion detection. DiffFilter calculates +// the absolute difference for each pixel between two frames, then finds the mean. If +// the mean is above a given threshold, then it is considered motion. +type DiffFilter struct { + dst io.WriteCloser + thresh float64 + prev gocv.Mat + debug bool + windows []*gocv.Window +} + +// NewDiffFilter returns a pointer to a new DiffFilter. +func NewDiffFilter(dst io.WriteCloser, debug bool, threshold float64) *DiffFilter { + var windows []*gocv.Window + if debug { + windows = []*gocv.Window{gocv.NewWindow("Diff: Bounding boxes"), gocv.NewWindow("Diff: Motion")} + } + return &DiffFilter{dst, threshold, gocv.NewMat(), debug, windows} +} + +// Implements io.Closer. +// Close frees resources used by gocv, because it has to be done manually, due to +// it using c-go. +func (d *DiffFilter) Close() error { + d.prev.Close() + for _, window := range d.windows { + window.Close() + } + return nil +} + +// Implements io.Writer. +// Write applies the motion filter to the video stream. Only frames with motion +// are written to the destination encoder, frames without are discarded. +func (d *DiffFilter) Write(f []byte) (int, error) { + if d.prev.Empty() { + d.prev, _ = gocv.IMDecode(f, gocv.IMReadColor) + return 0, nil + } + + img, _ := gocv.IMDecode(f, gocv.IMReadColor) + defer img.Close() + + imgDelta := gocv.NewMat() + defer imgDelta.Close() + + // Seperate foreground and background. + gocv.AbsDiff(img, d.prev, &imgDelta) + gocv.CvtColor(imgDelta, &imgDelta, gocv.ColorBGRToGray) + + mean := imgDelta.Mean().Val1 + + // Update History + d.prev = img.Clone() + + // Draw debug information. + if d.debug { + if mean >= d.thresh { + gocv.PutText(&img, fmt.Sprintf("motion - mean:%f", mean), image.Pt(32, 32), gocv.FontHersheyPlain, 2.0, color.RGBA{255, 0, 0, 0}, 2) + } + + d.windows[0].IMShow(img) + d.windows[1].IMShow(imgDelta) + d.windows[0].WaitKey(1) + } + + // Don't write to destination if there is no motion. + if mean < d.thresh { + return 0, nil + } + + // Write to destination. + return d.dst.Write(f) +} diff --git a/revid/config/config.go b/revid/config/config.go index b70a01b3..0192b1bd 100644 --- a/revid/config/config.go +++ b/revid/config/config.go @@ -127,6 +127,7 @@ const ( FilterMOG FilterVariableFPS FilterKNN + FilterDifference ) // OS names @@ -304,6 +305,8 @@ type Config struct { // Defines the rate at which frames from a file source are processed. FileFPS int + // Difference filter parameters. + DiffThreshold float64 // Intensity value from the Difference motion detection algorithm that is considered motion. } // TypeData contains information about all of the variables that @@ -317,9 +320,10 @@ var TypeData = map[string]string{ "CameraIP": "string", "CBR": "bool", "ClipDuration": "uint", + "DiffThreshold": "float", "Exposure": "enum:auto,night,nightpreview,backlight,spotlight,sports,snow,beach,verylong,fixedfps,antishake,fireworks", "FileFPS": "int", - "Filters": "enums:NoOp,MOG,VariableFPS,KNN", + "Filters": "enums:NoOp,MOG,VariableFPS,KNN,Difference", "FrameRate": "uint", "Height": "uint", "HorizontalFlip": "bool", @@ -340,13 +344,13 @@ var TypeData = map[string]string{ "MOGMinArea": "float", "MOGThreshold": "float", "MotionInterval": "int", - "RBCapacity": "uint", - "RBMaxElements": "uint", - "RBWriteTimeout": "uint", "Output": "enum:File,Http,Rtmp,Rtp", "OutputPath": "string", "Outputs": "enums:File,Http,Rtmp,Rtp", "Quantization": "uint", + "RBCapacity": "uint", + "RBMaxElements": "uint", + "RBWriteTimeout": "uint", "Rotation": "uint", "RTMPURL": "string", "RTPAddress": "string", diff --git a/revid/revid.go b/revid/revid.go index 94bd4a6f..fd7614f1 100644 --- a/revid/revid.go +++ b/revid/revid.go @@ -342,6 +342,8 @@ func (r *Revid) setupPipeline(mtsEnc func(dst io.WriteCloser, rate float64) (io. r.filters[i] = filter.NewVariableFPSFilter(dst, r.cfg.MinFPS, filter.NewMOGFilter(dst, r.cfg.MOGMinArea, r.cfg.MOGThreshold, int(r.cfg.MOGHistory), r.cfg.ShowWindows, r.cfg.MotionInterval)) case config.FilterKNN: r.filters[i] = filter.NewKNNFilter(dst, r.cfg.KNNMinArea, r.cfg.KNNThreshold, int(r.cfg.KNNHistory), int(r.cfg.KNNKernel), r.cfg.ShowWindows, r.cfg.MotionInterval) + case config.FilterDifference: + r.filters[i] = filter.NewDiffFilter(dst, r.cfg.ShowWindows, r.cfg.DiffThreshold) default: panic("Undefined Filter") } @@ -663,7 +665,7 @@ func (r *Revid) Update(vars map[string]string) error { } case "Filters": filters := strings.Split(value, ",") - m := map[string]int{"NoOp": config.FilterNoOp, "MOG": config.FilterMOG, "VariableFPS": config.FilterVariableFPS, "KNN": config.FilterKNN} + m := map[string]int{"NoOp": config.FilterNoOp, "MOG": config.FilterMOG, "VariableFPS": config.FilterVariableFPS, "KNN": config.FilterKNN, "Difference": config.FilterDifference} r.cfg.Filters = make([]int, len(filters)) for i, filter := range filters { v, ok := m[filter] @@ -811,6 +813,13 @@ func (r *Revid) Update(vars map[string]string) error { break } r.cfg.KNNThreshold = v + case "DiffThreshold": + v, err := strconv.ParseFloat(value, 64) + if err != nil { + r.cfg.Logger.Log(logger.Warning, pkg+"invalid DiffThreshold var", "value", value) + break + } + r.cfg.DiffThreshold = v case "KNNKernel": v, err := strconv.Atoi(value) if err != nil { From 458933babb95bb105a7f6ffc7a39b509787ea27f Mon Sep 17 00:00:00 2001 From: Scott Date: Thu, 23 Jan 2020 14:03:08 +1030 Subject: [PATCH 2/6] filter: create function for satisfying circleci tests --- filter/filters_circleci.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/filter/filters_circleci.go b/filter/filters_circleci.go index 374e8b0a..d4c427d7 100644 --- a/filter/filters_circleci.go +++ b/filter/filters_circleci.go @@ -36,6 +36,12 @@ func NewMOGFilter(dst io.WriteCloser, area, threshold float64, history int, debu return &NoOp{dst: dst} } +// NewKNNFilter returns a pointer to a new NoOp struct for testing purposes only. func NewKNNFilter(dst io.WriteCloser, area, threshold float64, history, kernelSize int, debug bool, hf int) *NoOp { return &NoOp{dst: dst} } + +// NewDiffFilter returns a pointer to a new NoOp struct for testing purposes only. +func NewDiffFilter(dst io.WriteCloser, debug bool, threshold float64) *NoOp { + return &NoOp{dst: dst} +} From e0fa47490671fdbca93d70023e6c25cb97ca568d Mon Sep 17 00:00:00 2001 From: Scott Date: Fri, 24 Jan 2020 16:05:35 +1030 Subject: [PATCH 3/6] =?UTF-8?q?filter:=20DiffFilter=20=E2=86=92=20Differen?= =?UTF-8?q?ce?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- filter/difference.go | 14 +++++++------- filter/filters_circleci.go | 4 ++-- revid/revid.go | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/filter/difference.go b/filter/difference.go index 97df94b9..00de000a 100644 --- a/filter/difference.go +++ b/filter/difference.go @@ -38,10 +38,10 @@ import ( "gocv.io/x/gocv" ) -// DiffFilter is a filter that provides basic motion detection. DiffFilter calculates +// Difference is a filter that provides basic motion detection. Difference calculates // the absolute difference for each pixel between two frames, then finds the mean. If // the mean is above a given threshold, then it is considered motion. -type DiffFilter struct { +type Difference struct { dst io.WriteCloser thresh float64 prev gocv.Mat @@ -49,19 +49,19 @@ type DiffFilter struct { windows []*gocv.Window } -// NewDiffFilter returns a pointer to a new DiffFilter. -func NewDiffFilter(dst io.WriteCloser, debug bool, threshold float64) *DiffFilter { +// NewDifference returns a pointer to a new Difference struct. +func NewDifference(dst io.WriteCloser, debug bool, threshold float64) *Difference { var windows []*gocv.Window if debug { windows = []*gocv.Window{gocv.NewWindow("Diff: Bounding boxes"), gocv.NewWindow("Diff: Motion")} } - return &DiffFilter{dst, threshold, gocv.NewMat(), debug, windows} + return &Difference{dst, threshold, gocv.NewMat(), debug, windows} } // Implements io.Closer. // Close frees resources used by gocv, because it has to be done manually, due to // it using c-go. -func (d *DiffFilter) Close() error { +func (d *Difference) Close() error { d.prev.Close() for _, window := range d.windows { window.Close() @@ -72,7 +72,7 @@ func (d *DiffFilter) Close() error { // Implements io.Writer. // Write applies the motion filter to the video stream. Only frames with motion // are written to the destination encoder, frames without are discarded. -func (d *DiffFilter) Write(f []byte) (int, error) { +func (d *Difference) Write(f []byte) (int, error) { if d.prev.Empty() { d.prev, _ = gocv.IMDecode(f, gocv.IMReadColor) return 0, nil diff --git a/filter/filters_circleci.go b/filter/filters_circleci.go index d4c427d7..aaca83f6 100644 --- a/filter/filters_circleci.go +++ b/filter/filters_circleci.go @@ -41,7 +41,7 @@ func NewKNNFilter(dst io.WriteCloser, area, threshold float64, history, kernelSi return &NoOp{dst: dst} } -// NewDiffFilter returns a pointer to a new NoOp struct for testing purposes only. -func NewDiffFilter(dst io.WriteCloser, debug bool, threshold float64) *NoOp { +// NewDiffference returns a pointer to a new NoOp struct for testing purposes only. +func NewDifference(dst io.WriteCloser, debug bool, threshold float64) *NoOp { return &NoOp{dst: dst} } diff --git a/revid/revid.go b/revid/revid.go index fd7614f1..b5f31a6e 100644 --- a/revid/revid.go +++ b/revid/revid.go @@ -343,7 +343,7 @@ func (r *Revid) setupPipeline(mtsEnc func(dst io.WriteCloser, rate float64) (io. case config.FilterKNN: r.filters[i] = filter.NewKNNFilter(dst, r.cfg.KNNMinArea, r.cfg.KNNThreshold, int(r.cfg.KNNHistory), int(r.cfg.KNNKernel), r.cfg.ShowWindows, r.cfg.MotionInterval) case config.FilterDifference: - r.filters[i] = filter.NewDiffFilter(dst, r.cfg.ShowWindows, r.cfg.DiffThreshold) + r.filters[i] = filter.NewDifference(dst, r.cfg.ShowWindows, r.cfg.DiffThreshold) default: panic("Undefined Filter") } From b15b649151d394d25d4313d8f66182595ada3d8b Mon Sep 17 00:00:00 2001 From: Scott Date: Fri, 24 Jan 2020 16:10:08 +1030 Subject: [PATCH 4/6] filter: check for errors after decoding --- filter/difference.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/filter/difference.go b/filter/difference.go index 00de000a..581cc8bf 100644 --- a/filter/difference.go +++ b/filter/difference.go @@ -74,12 +74,16 @@ func (d *Difference) Close() error { // are written to the destination encoder, frames without are discarded. func (d *Difference) Write(f []byte) (int, error) { if d.prev.Empty() { - d.prev, _ = gocv.IMDecode(f, gocv.IMReadColor) - return 0, nil + var err error + d.prev, err = gocv.IMDecode(f, gocv.IMReadColor) + return 0, err } - img, _ := gocv.IMDecode(f, gocv.IMReadColor) + img, err := gocv.IMDecode(f, gocv.IMReadColor) defer img.Close() + if err != nil { + return 0, err + } imgDelta := gocv.NewMat() defer imgDelta.Close() From fb43e9214ae5fb11d67e92ea4e2c1cec0ee9d40f Mon Sep 17 00:00:00 2001 From: Scott Date: Fri, 24 Jan 2020 16:29:06 +1030 Subject: [PATCH 5/6] filter: change formatting of code --- filter/difference.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/filter/difference.go b/filter/difference.go index 581cc8bf..4504c6ef 100644 --- a/filter/difference.go +++ b/filter/difference.go @@ -100,7 +100,15 @@ func (d *Difference) Write(f []byte) (int, error) { // Draw debug information. if d.debug { if mean >= d.thresh { - gocv.PutText(&img, fmt.Sprintf("motion - mean:%f", mean), image.Pt(32, 32), gocv.FontHersheyPlain, 2.0, color.RGBA{255, 0, 0, 0}, 2) + gocv.PutText( + &img, + fmt.Sprintf("motion - mean:%f", mean), + image.Pt(32, 32), + gocv.FontHersheyPlain, + 2.0, + color.RGBA{255, 0, 0, 0}, + 2, + ) } d.windows[0].IMShow(img) From a111f214279966885d1af40766dabd0ee9867453 Mon Sep 17 00:00:00 2001 From: Scott Date: Mon, 27 Jan 2020 11:23:56 +1030 Subject: [PATCH 6/6] filter: return length from the filter's Write method --- filter/difference.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/filter/difference.go b/filter/difference.go index 4504c6ef..d01cf0a7 100644 --- a/filter/difference.go +++ b/filter/difference.go @@ -76,7 +76,10 @@ func (d *Difference) Write(f []byte) (int, error) { if d.prev.Empty() { var err error d.prev, err = gocv.IMDecode(f, gocv.IMReadColor) - return 0, err + if err != nil { + return 0, err + } + return len(f), nil } img, err := gocv.IMDecode(f, gocv.IMReadColor) @@ -94,7 +97,7 @@ func (d *Difference) Write(f []byte) (int, error) { mean := imgDelta.Mean().Val1 - // Update History + // Update History. d.prev = img.Clone() // Draw debug information. @@ -118,7 +121,7 @@ func (d *Difference) Write(f []byte) (int, error) { // Don't write to destination if there is no motion. if mean < d.thresh { - return 0, nil + return len(f), nil } // Write to destination.