mirror of https://bitbucket.org/ausocean/av.git
making mog filter that only dectects motion on every 5 frame (will make this a var)
This commit is contained in:
parent
949c4ee2af
commit
b39a440105
|
@ -46,17 +46,22 @@ type MOGFilter struct {
|
||||||
knl gocv.Mat
|
knl gocv.Mat
|
||||||
debug bool
|
debug bool
|
||||||
windows []*gocv.Window
|
windows []*gocv.Window
|
||||||
|
hold [][]byte
|
||||||
|
hf int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var hfCount int = 0
|
||||||
|
|
||||||
// NewMOGFilter returns a pointer to a new MOGFilter struct.
|
// NewMOGFilter returns a pointer to a new MOGFilter struct.
|
||||||
func NewMOGFilter(dst io.WriteCloser, area, threshold float64, history int, debug bool) *MOGFilter {
|
func NewMOGFilter(dst io.WriteCloser, area, threshold float64, history int, debug bool, hf int) *MOGFilter {
|
||||||
bs := gocv.NewBackgroundSubtractorMOG2WithParams(history, threshold, false)
|
bs := gocv.NewBackgroundSubtractorMOG2WithParams(history, threshold, false)
|
||||||
k := gocv.GetStructuringElement(gocv.MorphRect, image.Pt(3, 3))
|
k := gocv.GetStructuringElement(gocv.MorphRect, image.Pt(3, 3))
|
||||||
var windows []*gocv.Window
|
var windows []*gocv.Window
|
||||||
if debug {
|
if debug {
|
||||||
windows = []*gocv.Window{gocv.NewWindow("MOG: Bounding boxes"), gocv.NewWindow("MOG: Motion")}
|
windows = []*gocv.Window{gocv.NewWindow("MOG: Bounding boxes"), gocv.NewWindow("MOG: Motion")}
|
||||||
}
|
}
|
||||||
return &MOGFilter{dst, area, &bs, k, debug, windows}
|
hold := make([][]byte, hf-1)
|
||||||
|
return &MOGFilter{dst, area, &bs, k, debug, windows, hold, hf}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implements io.Closer.
|
// Implements io.Closer.
|
||||||
|
@ -75,6 +80,11 @@ func (m *MOGFilter) Close() error {
|
||||||
// Write applies the motion filter to the video stream. Only frames with motion
|
// Write applies the motion filter to the video stream. Only frames with motion
|
||||||
// are written to the destination encoder, frames without are discarded.
|
// are written to the destination encoder, frames without are discarded.
|
||||||
func (m *MOGFilter) Write(f []byte) (int, error) {
|
func (m *MOGFilter) Write(f []byte) (int, error) {
|
||||||
|
if hfCount < (m.hf - 1) {
|
||||||
|
m.hold[hfCount] = f
|
||||||
|
hfCount++
|
||||||
|
return -1, nil
|
||||||
|
} else {
|
||||||
img, err := gocv.IMDecode(f, gocv.IMReadColor)
|
img, err := gocv.IMDecode(f, gocv.IMReadColor)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, fmt.Errorf("image can't be decoded: %w", err)
|
return 0, fmt.Errorf("image can't be decoded: %w", err)
|
||||||
|
@ -128,6 +138,16 @@ func (m *MOGFilter) Write(f []byte) (int, error) {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write to destination.
|
// Write to destination, past 4 frames then current frame.
|
||||||
|
for _, h := range m.hold {
|
||||||
|
_, err := m.dst.Write(h)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
hfCount = 0
|
||||||
return m.dst.Write(f)
|
return m.dst.Write(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -85,6 +85,7 @@ const (
|
||||||
defaultClipDuration = 0
|
defaultClipDuration = 0
|
||||||
defaultAudioInputCodec = codecutil.ADPCM
|
defaultAudioInputCodec = codecutil.ADPCM
|
||||||
defaultPSITime = 2
|
defaultPSITime = 2
|
||||||
|
defaultFilterFrames = 1
|
||||||
|
|
||||||
// Ring buffer defaults.
|
// Ring buffer defaults.
|
||||||
defaultRBMaxElements = 10000
|
defaultRBMaxElements = 10000
|
||||||
|
@ -275,6 +276,7 @@ type Config struct {
|
||||||
HorizontalFlip bool // HorizontalFlip flips video horizontally for Raspivid input.
|
HorizontalFlip bool // HorizontalFlip flips video horizontally for Raspivid input.
|
||||||
VerticalFlip bool // VerticalFlip flips video vertically for Raspivid input.
|
VerticalFlip bool // VerticalFlip flips video vertically for Raspivid input.
|
||||||
Filters []int // Defines the methods of filtering to be used in between lexing and encoding.
|
Filters []int // Defines the methods of filtering to be used in between lexing and encoding.
|
||||||
|
FilterFrames int // Sets the number of frames that are held before the filter is used (on the nth frame)
|
||||||
PSITime int // Sets the time between a packet being sent.
|
PSITime int // Sets the time between a packet being sent.
|
||||||
|
|
||||||
// Ring buffer parameters.
|
// Ring buffer parameters.
|
||||||
|
@ -460,6 +462,10 @@ func (c *Config) Validate() error {
|
||||||
c.Logger.Log(logger.Info, pkg+"PSITime bad or unset, defaulting", "PSITime", defaultPSITime)
|
c.Logger.Log(logger.Info, pkg+"PSITime bad or unset, defaulting", "PSITime", defaultPSITime)
|
||||||
c.PSITime = defaultPSITime
|
c.PSITime = defaultPSITime
|
||||||
}
|
}
|
||||||
|
if c.FilterFrames <= 0 {
|
||||||
|
c.Logger.Log(logger.Info, pkg+"FilterFrames bad or unset, defaulting", "FilterFrames", defaultFilterFrames)
|
||||||
|
c.FilterFrames = defaultFilterFrames
|
||||||
|
}
|
||||||
|
|
||||||
if c.MinFPS <= 0 {
|
if c.MinFPS <= 0 {
|
||||||
c.Logger.Log(logger.Info, pkg+"MinFPS bad or unset, defaulting", "MinFPS", defaultMinFPS)
|
c.Logger.Log(logger.Info, pkg+"MinFPS bad or unset, defaulting", "MinFPS", defaultMinFPS)
|
||||||
|
|
|
@ -63,6 +63,24 @@ const (
|
||||||
rtmpConnectionTimeout = 10
|
rtmpConnectionTimeout = 10
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Motion filter parameters.
|
||||||
|
const minFPS = 1.0
|
||||||
|
|
||||||
|
// KNN specific parameters.
|
||||||
|
const (
|
||||||
|
knnMinArea = 25.0
|
||||||
|
knnThreshold = 300
|
||||||
|
knnHistory = 300
|
||||||
|
knnKernel = 9
|
||||||
|
)
|
||||||
|
|
||||||
|
// MOG specific parameters.
|
||||||
|
const (
|
||||||
|
mogMinArea = 50
|
||||||
|
mogThreshold = 100
|
||||||
|
mogHistory = 100
|
||||||
|
)
|
||||||
|
|
||||||
const pkg = "revid: "
|
const pkg = "revid: "
|
||||||
|
|
||||||
type Logger interface {
|
type Logger interface {
|
||||||
|
@ -337,17 +355,16 @@ func (r *Revid) setupPipeline(mtsEnc func(dst io.WriteCloser, rate float64) (io.
|
||||||
case config.FilterNoOp:
|
case config.FilterNoOp:
|
||||||
r.filters[i] = filter.NewNoOp(dst)
|
r.filters[i] = filter.NewNoOp(dst)
|
||||||
case config.FilterMOG:
|
case config.FilterMOG:
|
||||||
r.filters[i] = filter.NewMOGFilter(dst, r.cfg.MOGMinArea, r.cfg.MOGThreshold, int(r.cfg.MOGHistory), r.cfg.ShowWindows)
|
r.filters[i] = filter.NewMOGFilter(dst, mogMinArea, mogThreshold, mogHistory, r.cfg.ShowWindows, r.cfg.FilterFrames)
|
||||||
case config.FilterVariableFPS:
|
case config.FilterVariableFPS:
|
||||||
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.filters[i] = filter.NewVariableFPSFilter(dst, minFPS, filter.NewMOGFilter(r.encoders, mogMinArea, mogThreshold, mogHistory, r.cfg.ShowWindows, r.cfg.FilterFrames))
|
||||||
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.filters[i] = filter.NewKNNFilter(dst, knnMinArea, knnThreshold, knnHistory, knnKernel, r.cfg.ShowWindows)
|
||||||
default:
|
default:
|
||||||
panic("Undefined Filter")
|
panic("Undefined Filter")
|
||||||
}
|
}
|
||||||
dst = r.filters[i]
|
dst = r.filters[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch r.cfg.Input {
|
switch r.cfg.Input {
|
||||||
|
@ -671,6 +688,13 @@ func (r *Revid) Update(vars map[string]string) error {
|
||||||
}
|
}
|
||||||
r.cfg.Filters[i] = v
|
r.cfg.Filters[i] = v
|
||||||
}
|
}
|
||||||
|
case "FilterFrames":
|
||||||
|
v, err := strconv.Atoi(value)
|
||||||
|
if err != nil || v < 0 {
|
||||||
|
r.cfg.Logger.Log(logger.Warning, pkg+"invalid FilterFrames var", "value", value)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
r.cfg.FilterFrames = v
|
||||||
case "PSITime":
|
case "PSITime":
|
||||||
v, err := strconv.Atoi(value)
|
v, err := strconv.Atoi(value)
|
||||||
if err != nil || v < 0 {
|
if err != nil || v < 0 {
|
||||||
|
|
Loading…
Reference in New Issue