2020-01-22 04:36:14 +03:00
|
|
|
// +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 (
|
2020-02-07 04:12:27 +03:00
|
|
|
"fmt"
|
2020-01-22 04:36:14 +03:00
|
|
|
"io"
|
|
|
|
|
2020-02-06 02:12:12 +03:00
|
|
|
"bitbucket.org/ausocean/av/revid/config"
|
2020-01-22 04:36:14 +03:00
|
|
|
"gocv.io/x/gocv"
|
|
|
|
)
|
|
|
|
|
2020-02-06 02:12:12 +03:00
|
|
|
const defaultDiffThreshold = 3
|
|
|
|
|
2020-01-24 08:35:35 +03:00
|
|
|
// Difference is a filter that provides basic motion detection. Difference calculates
|
2020-01-22 04:36:14 +03:00
|
|
|
// 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.
|
2020-01-24 08:35:35 +03:00
|
|
|
type Difference struct {
|
2020-02-05 09:06:09 +03:00
|
|
|
debugging debugWindows
|
|
|
|
dst io.WriteCloser
|
|
|
|
thresh float64
|
|
|
|
prev gocv.Mat
|
2020-01-22 04:36:14 +03:00
|
|
|
}
|
|
|
|
|
2020-01-24 08:35:35 +03:00
|
|
|
// NewDifference returns a pointer to a new Difference struct.
|
2020-02-06 02:12:12 +03:00
|
|
|
func NewDifference(dst io.WriteCloser, c config.Config) *Difference {
|
|
|
|
// Validate parameters.
|
|
|
|
if c.MotionThreshold <= 0 {
|
|
|
|
c.LogInvalidField("MotionThreshold", defaultDiffThreshold)
|
|
|
|
c.MotionThreshold = defaultDiffThreshold
|
|
|
|
}
|
|
|
|
|
2020-02-05 09:06:09 +03:00
|
|
|
return &Difference{
|
|
|
|
dst: dst,
|
2020-02-06 02:12:12 +03:00
|
|
|
thresh: c.MotionThreshold,
|
2020-02-05 09:06:09 +03:00
|
|
|
prev: gocv.NewMat(),
|
|
|
|
debugging: newWindows("DIFF"),
|
2020-01-22 04:36:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements io.Closer.
|
|
|
|
// Close frees resources used by gocv, because it has to be done manually, due to
|
|
|
|
// it using c-go.
|
2020-01-24 08:35:35 +03:00
|
|
|
func (d *Difference) Close() error {
|
2020-01-22 04:36:14 +03:00
|
|
|
d.prev.Close()
|
2020-02-05 09:06:09 +03:00
|
|
|
d.debugging.close()
|
2020-01-22 04:36:14 +03:00
|
|
|
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.
|
2020-01-24 08:35:35 +03:00
|
|
|
func (d *Difference) Write(f []byte) (int, error) {
|
2020-01-22 04:36:14 +03:00
|
|
|
if d.prev.Empty() {
|
2020-01-24 08:40:08 +03:00
|
|
|
var err error
|
|
|
|
d.prev, err = gocv.IMDecode(f, gocv.IMReadColor)
|
2020-01-27 03:53:56 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return len(f), nil
|
2020-01-22 04:36:14 +03:00
|
|
|
}
|
|
|
|
|
2020-01-24 08:40:08 +03:00
|
|
|
img, err := gocv.IMDecode(f, gocv.IMReadColor)
|
2020-01-22 04:36:14 +03:00
|
|
|
defer img.Close()
|
2020-01-24 08:40:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-01-22 04:36:14 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-01-27 03:53:56 +03:00
|
|
|
// Update History.
|
2020-01-22 04:36:14 +03:00
|
|
|
d.prev = img.Clone()
|
|
|
|
|
|
|
|
// Draw debug information.
|
2020-02-07 04:12:27 +03:00
|
|
|
d.debugging.show(img, imgDelta, mean > d.thresh, nil, fmt.Sprintf("Mean: %f", mean), fmt.Sprintf("Threshold: %f", d.thresh))
|
2020-01-22 04:36:14 +03:00
|
|
|
|
|
|
|
// Don't write to destination if there is no motion.
|
|
|
|
if mean < d.thresh {
|
2020-01-27 03:53:56 +03:00
|
|
|
return len(f), nil
|
2020-01-22 04:36:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write to destination.
|
|
|
|
return d.dst.Write(f)
|
|
|
|
}
|