mirror of https://bitbucket.org/ausocean/av.git
filter: create a simple difference motion filter using gocv
This commit is contained in:
parent
cfd3f0fc3c
commit
ced8727c07
|
@ -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 <scott@ausocean.org>
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
|
@ -127,6 +127,7 @@ const (
|
||||||
FilterMOG
|
FilterMOG
|
||||||
FilterVariableFPS
|
FilterVariableFPS
|
||||||
FilterKNN
|
FilterKNN
|
||||||
|
FilterDifference
|
||||||
)
|
)
|
||||||
|
|
||||||
// OS names
|
// OS names
|
||||||
|
@ -304,6 +305,8 @@ type Config struct {
|
||||||
|
|
||||||
// Defines the rate at which frames from a file source are processed.
|
// Defines the rate at which frames from a file source are processed.
|
||||||
FileFPS int
|
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
|
// TypeData contains information about all of the variables that
|
||||||
|
@ -317,9 +320,10 @@ var TypeData = map[string]string{
|
||||||
"CameraIP": "string",
|
"CameraIP": "string",
|
||||||
"CBR": "bool",
|
"CBR": "bool",
|
||||||
"ClipDuration": "uint",
|
"ClipDuration": "uint",
|
||||||
|
"DiffThreshold": "float",
|
||||||
"Exposure": "enum:auto,night,nightpreview,backlight,spotlight,sports,snow,beach,verylong,fixedfps,antishake,fireworks",
|
"Exposure": "enum:auto,night,nightpreview,backlight,spotlight,sports,snow,beach,verylong,fixedfps,antishake,fireworks",
|
||||||
"FileFPS": "int",
|
"FileFPS": "int",
|
||||||
"Filters": "enums:NoOp,MOG,VariableFPS,KNN",
|
"Filters": "enums:NoOp,MOG,VariableFPS,KNN,Difference",
|
||||||
"FrameRate": "uint",
|
"FrameRate": "uint",
|
||||||
"Height": "uint",
|
"Height": "uint",
|
||||||
"HorizontalFlip": "bool",
|
"HorizontalFlip": "bool",
|
||||||
|
@ -340,13 +344,13 @@ var TypeData = map[string]string{
|
||||||
"MOGMinArea": "float",
|
"MOGMinArea": "float",
|
||||||
"MOGThreshold": "float",
|
"MOGThreshold": "float",
|
||||||
"MotionInterval": "int",
|
"MotionInterval": "int",
|
||||||
"RBCapacity": "uint",
|
|
||||||
"RBMaxElements": "uint",
|
|
||||||
"RBWriteTimeout": "uint",
|
|
||||||
"Output": "enum:File,Http,Rtmp,Rtp",
|
"Output": "enum:File,Http,Rtmp,Rtp",
|
||||||
"OutputPath": "string",
|
"OutputPath": "string",
|
||||||
"Outputs": "enums:File,Http,Rtmp,Rtp",
|
"Outputs": "enums:File,Http,Rtmp,Rtp",
|
||||||
"Quantization": "uint",
|
"Quantization": "uint",
|
||||||
|
"RBCapacity": "uint",
|
||||||
|
"RBMaxElements": "uint",
|
||||||
|
"RBWriteTimeout": "uint",
|
||||||
"Rotation": "uint",
|
"Rotation": "uint",
|
||||||
"RTMPURL": "string",
|
"RTMPURL": "string",
|
||||||
"RTPAddress": "string",
|
"RTPAddress": "string",
|
||||||
|
|
|
@ -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))
|
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:
|
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)
|
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:
|
default:
|
||||||
panic("Undefined Filter")
|
panic("Undefined Filter")
|
||||||
}
|
}
|
||||||
|
@ -663,7 +665,7 @@ func (r *Revid) Update(vars map[string]string) error {
|
||||||
}
|
}
|
||||||
case "Filters":
|
case "Filters":
|
||||||
filters := strings.Split(value, ",")
|
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))
|
r.cfg.Filters = make([]int, len(filters))
|
||||||
for i, filter := range filters {
|
for i, filter := range filters {
|
||||||
v, ok := m[filter]
|
v, ok := m[filter]
|
||||||
|
@ -811,6 +813,13 @@ func (r *Revid) Update(vars map[string]string) error {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
r.cfg.KNNThreshold = v
|
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":
|
case "KNNKernel":
|
||||||
v, err := strconv.Atoi(value)
|
v, err := strconv.Atoi(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue