Merged in filter-debug-tag (pull request #370)

filter: use build tags to separate debug code from release code

Approved-by: Saxon Milton <saxon.milton@gmail.com>
This commit is contained in:
Scott Barnard 2020-02-07 03:20:46 +00:00 committed by Saxon Milton
commit bb5618f3c7
9 changed files with 252 additions and 219 deletions

View File

@ -33,59 +33,39 @@ import (
"image/color"
"image/jpeg"
"io"
"os"
"strconv"
"sync"
"golang.org/x/image/font"
"golang.org/x/image/font/basicfont"
"golang.org/x/image/math/fixed"
)
const debugfile = "motion.mjpeg"
type pixel struct{ r, g, b uint32 }
// Basic is a filter that provides basic motion detection via a difference
// method.
type Basic struct {
debugging debugWindows
dst io.WriteCloser
img image.Image
bg [][]pixel
bwImg *image.RGBA
bwImg *image.Gray
thresh int
pix int
w int
h int
file io.WriteCloser
motion int
debug bool
}
// NewBasic returns a pointer to a new Basic filter struct.
func NewBasic(dst io.WriteCloser, debug bool, t, p int) *Basic {
bwImg := image.NewRGBA(image.Rect(0, 0, 0, 0))
var file io.WriteCloser
var err error
file = nil
if debug {
file, err = os.Create(debugfile)
if err != nil {
panic(fmt.Sprintf("could not create debug file: %v", err))
func NewBasic(dst io.WriteCloser, t, p int) *Basic {
return &Basic{
dst: dst,
thresh: t,
pix: p,
debugging: newWindows("BASIC"),
}
}
return &Basic{dst, nil, nil, bwImg, t, p, 0, 0, file, 0, debug}
}
// Implements io.Closer.
func (bf *Basic) Close() error {
if bf.debug {
err := bf.file.Close()
if err != nil {
return fmt.Errorf("file cannot be closed: %w", err)
}
}
return nil
return bf.debugging.close()
}
// Implements io.Writer.
@ -105,7 +85,7 @@ func (bf *Basic) Write(f []byte) (int, error) {
bf.w = bounds.Max.X
bf.h = bounds.Max.Y
bf.bwImg = image.NewRGBA(image.Rect(0, 0, bf.w, bf.h))
bf.bwImg = image.NewGray(image.Rect(0, 0, bf.w, bf.h))
bf.bg = make([][]pixel, bf.h)
for j, _ := range bf.bg {
@ -136,13 +116,8 @@ func (bf *Basic) Write(f []byte) (int, error) {
wg.Wait()
}
// Will save a video of where motion is detected in motion.mjpeg (in the current folder).
if bf.debug {
err := bf.saveFrame()
if err != nil {
return len(f), fmt.Errorf("image can't be encoded for debug video: %w", err)
}
}
// Draw debug information.
bf.debugging.show(bf.img, bf.bwImg, bf.motion > bf.pix, nil, fmt.Sprintf("Motion: %d", bf.motion), fmt.Sprintf("Pix: %d", bf.pix))
// If there are not enough motion pixels then discard the frame.
if bf.motion < bf.pix {
@ -168,14 +143,9 @@ func (bf *Basic) process(j int, wg *sync.WaitGroup) {
if diff > bf.thresh {
bf.motion++
}
if bf.debug {
if diff > bf.thresh {
bf.bwImg.SetRGBA(i, j, color.RGBA{0xff, 0xff, 0xff, 0xff})
bf.bwImg.SetGray(i, j, color.Gray{0xff})
} else {
bf.bwImg.SetRGBA(i, j, color.RGBA{0x00, 0x00, 0x00, 0xff})
}
bf.bwImg.SetGray(i, j, color.Gray{0x00})
}
// Update backgound image.
@ -186,25 +156,6 @@ func (bf *Basic) process(j int, wg *sync.WaitGroup) {
wg.Done()
}
// Writes a visualisation of the motion being detected to file.
func (bf *Basic) saveFrame() error {
col := color.RGBA{200, 100, 0, 255} // Red text.
d := &font.Drawer{
Dst: bf.bwImg,
Src: image.NewUniform(col),
Face: basicfont.Face7x13,
Dot: fixed.P(20, 30),
}
var s string
if bf.motion > bf.pix {
s = strconv.Itoa(bf.motion) + " Motion"
} else {
s = strconv.Itoa(bf.motion)
}
d.DrawString(s)
return jpeg.Encode(bf.file, bf.bwImg, nil)
}
// Returns the absolute value of the difference of two uint32 numbers.
func absDiff(a, b uint32) int {
c := int(a) - int(b)
@ -213,5 +164,4 @@ func absDiff(a, b uint32) int {
} else {
return c
}
}

108
filter/debug.go Normal file
View File

@ -0,0 +1,108 @@
// +build debug
// +build !circleci
/*
DESCRIPTION
Displays debug information for the motion filters.
AUTHORS
Scott Barnard <scott@ausocean.org>
LICENSE
This file 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 (
"image"
"image/color"
"gocv.io/x/gocv"
)
// debugWindows is used for displaying debug information for the motion filters.
type debugWindows struct {
windows []*gocv.Window
}
// close frees resources used by gocv.
func (d *debugWindows) close() error {
for _, window := range d.windows {
err := window.Close()
if err != nil {
return err
}
}
return nil
}
// newWindows creates debugging windows for the motion filter.
func newWindows(name string) debugWindows {
return debugWindows{
windows: []*gocv.Window{
gocv.NewWindow(name + ": Video"),
gocv.NewWindow(name + ": Motion Detection"),
},
}
}
// show displays debug information for the motion filters.
func (d *debugWindows) show(img, imgDelta interface{}, motion bool, contours *[][]image.Point, text ...string) {
var im, imD gocv.Mat
const errMsg = "cannot show frame in window: wrong type"
var drkRed = color.RGBA{191, 0, 0, 0}
var lhtRed = color.RGBA{191, 31, 31, 0}
// Type conversions.
switch img.(type) {
case image.Image:
im, _ = gocv.ImageToMatRGB(img.(image.Image))
case gocv.Mat:
im = img.(gocv.Mat)
default:
panic(errMsg)
}
switch imgDelta.(type) {
case image.Image:
imD, _ = gocv.ImageToMatRGB(imgDelta.(image.Image))
case gocv.Mat:
imD = imgDelta.(gocv.Mat)
default:
panic(errMsg)
}
// Draw contours.
if contours != nil {
for _, c := range *contours {
rect := gocv.BoundingRect(c)
gocv.Rectangle(&im, rect, lhtRed, 1)
}
}
// Draw debugging text.
if motion {
text = append(text, "Motion Detected")
}
for i, str := range text {
gocv.PutText(&im, str, image.Pt(32, 32*(i+1)), gocv.FontHersheyPlain, 2.0, drkRed, 2)
}
// Display windows.
d.windows[0].IMShow(im)
d.windows[1].IMShow(imD)
d.windows[0].WaitKey(1)
}

View File

@ -31,8 +31,6 @@ package filter
import (
"fmt"
"image"
"image/color"
"io"
"gocv.io/x/gocv"
@ -42,20 +40,20 @@ import (
// 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 Difference struct {
debugging debugWindows
dst io.WriteCloser
thresh float64
prev gocv.Mat
debug bool
windows []*gocv.Window
}
// 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")}
func NewDifference(dst io.WriteCloser, threshold float64) *Difference {
return &Difference{
dst: dst,
thresh: threshold,
prev: gocv.NewMat(),
debugging: newWindows("DIFF"),
}
return &Difference{dst, threshold, gocv.NewMat(), debug, windows}
}
// Implements io.Closer.
@ -63,9 +61,7 @@ func NewDifference(dst io.WriteCloser, debug bool, threshold float64) *Differenc
// it using c-go.
func (d *Difference) Close() error {
d.prev.Close()
for _, window := range d.windows {
window.Close()
}
d.debugging.close()
return nil
}
@ -101,23 +97,7 @@ func (d *Difference) Write(f []byte) (int, error) {
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)
}
d.debugging.show(img, imgDelta, mean > d.thresh, nil, fmt.Sprintf("Mean: %f", mean), fmt.Sprintf("Threshold: %f", d.thresh))
// Don't write to destination if there is no motion.
if mean < d.thresh {

View File

@ -28,20 +28,34 @@ LICENSE
package filter
import (
"image"
"io"
)
// NewMOG returns a pointer to a new NoOp struct for testing purposes only.
func NewMOG(dst io.WriteCloser, area, threshold float64, history int, debug bool, hf int, scaleFactor int) *NoOp {
func NewMOG(dst io.WriteCloser, area, threshold float64, history int, hf int, scaleFactor int) *NoOp {
return &NoOp{dst: dst}
}
// NewKNN returns a pointer to a new NoOp struct for testing purposes only.
func NewKNN(dst io.WriteCloser, area, threshold float64, history, kernelSize int, debug bool, hf int, scaleFactor int) *NoOp {
func NewKNN(dst io.WriteCloser, area, threshold float64, history, kernelSize int, hf int, scaleFactor int) *NoOp {
return &NoOp{dst: dst}
}
// NewDiffference returns a pointer to a new NoOp struct for testing purposes only.
func NewDifference(dst io.WriteCloser, debug bool, threshold float64) *NoOp {
func NewDifference(dst io.WriteCloser, threshold float64) *NoOp {
return &NoOp{dst: dst}
}
// debugWindows is used for displaying debug information for the motion filters.
type debugWindows struct{}
// close frees resources used by gocv.
func (d *debugWindows) close() error { return nil }
// newWindows creates debugging windows for the motion filter.
func newWindows(name string) debugWindows { return debugWindows{} }
// show displays debug information for the motion filters.
func (d *debugWindows) show(img, imgDelta interface{}, motion bool, contours *[][]image.Point, text ...string) {
}

View File

@ -31,7 +31,6 @@ package filter
import (
"fmt"
"image"
"image/color"
"io"
"gocv.io/x/gocv"
@ -40,12 +39,11 @@ import (
// KNN is a filter that provides basic motion detection. KNN is short for
// K-Nearest Neighbours method.
type KNN struct {
debugging debugWindows
dst io.WriteCloser // Destination to which motion containing frames go.
area float64 // The minimum area that a contour can be found in.
bs *gocv.BackgroundSubtractorKNN // Uses the KNN algorithm to find the difference between the current and background frame.
knl gocv.Mat // Matrix that is used for calculations.
debug bool // If true then debug windows with the bounding boxes and difference will be shown on the screen.
windows []*gocv.Window // Holds debug windows.
hold [][]byte // Will hold all frames up to hf (so only every hf frame is motion detected).
hf int // The number of frames to be held.
hfCount int // Counter for the hold array.
@ -53,14 +51,19 @@ type KNN struct {
}
// NewKNN returns a pointer to a new KNN filter struct.
func NewKNN(dst io.WriteCloser, area, threshold float64, history, kernelSize int, debug bool, hf int, scaleFactor int) *KNN {
func NewKNN(dst io.WriteCloser, area, threshold float64, history, kernelSize int, hf int, scaleFactor int) *KNN {
bs := gocv.NewBackgroundSubtractorKNNWithParams(history, threshold, false)
k := gocv.GetStructuringElement(gocv.MorphRect, image.Pt(kernelSize, kernelSize))
var windows []*gocv.Window
if debug {
windows = []*gocv.Window{gocv.NewWindow("KNN: Bounding boxes"), gocv.NewWindow("KNN: Motion")}
return &KNN{
dst: dst,
area: area,
bs: &bs,
knl: k,
hold: make([][]byte, hf-1),
hf: hf,
scale: 1 / float64(scaleFactor),
debugging: newWindows("KNN"),
}
return &KNN{dst, area, &bs, k, debug, windows, make([][]byte, hf-1), hf, 0, 1 / float64(scaleFactor)}
}
// Implements io.Closer.
@ -69,9 +72,7 @@ func NewKNN(dst io.WriteCloser, area, threshold float64, history, kernelSize int
func (m *KNN) Close() error {
m.bs.Close()
m.knl.Close()
for _, window := range m.windows {
window.Close()
}
m.debugging.close()
return nil
}
@ -122,20 +123,7 @@ func (m *KNN) Write(f []byte) (int, error) {
}
// Draw debug information.
if m.debug {
for _, c := range contours {
rect := gocv.BoundingRect(c)
gocv.Rectangle(&img, rect, color.RGBA{0, 0, 255, 0}, 1)
}
if len(contours) > 0 {
gocv.PutText(&img, "Motion", image.Pt(32, 32), gocv.FontHersheyPlain, 2.0, color.RGBA{255, 0, 0, 0}, 2)
}
m.windows[0].IMShow(img)
m.windows[1].IMShow(imgDelta)
m.windows[0].WaitKey(1)
}
m.debugging.show(img, imgDelta, len(contours) > 0, &contours)
// Don't write to destination if there is no motion.
if len(contours) == 0 {

View File

@ -31,7 +31,6 @@ package filter
import (
"fmt"
"image"
"image/color"
"io"
"gocv.io/x/gocv"
@ -40,12 +39,11 @@ import (
// MOG is a filter that provides basic motion detection. MoG is short for
// Mixture of Gaussians method.
type MOG struct {
debugging debugWindows
dst io.WriteCloser // Destination to which motion containing frames go.
area float64 // The minimum area that a contour can be found in.
bs *gocv.BackgroundSubtractorMOG2 // Uses the MOG algorithm to find the difference between the current and background frame.
knl gocv.Mat // Matrix that is used for calculations.
debug bool // If true then debug windows with the bounding boxes and difference will be shown on the screen.
windows []*gocv.Window // Holds debug windows.
hold [][]byte // Will hold all frames up to hf (so only every hf frame is motion detected).
hf int // The number of frames to be held.
hfCount int // Counter for the hold array.
@ -53,14 +51,19 @@ type MOG struct {
}
// NewMOG returns a pointer to a new MOG filter struct.
func NewMOG(dst io.WriteCloser, area, threshold float64, history int, debug bool, hf int, scaleFactor int) *MOG {
func NewMOG(dst io.WriteCloser, area, threshold float64, history int, hf int, scaleFactor int) *MOG {
bs := gocv.NewBackgroundSubtractorMOG2WithParams(history, threshold, false)
k := gocv.GetStructuringElement(gocv.MorphRect, image.Pt(3, 3))
var windows []*gocv.Window
if debug {
windows = []*gocv.Window{gocv.NewWindow("MOG: Bounding boxes"), gocv.NewWindow("MOG: Motion")}
return &MOG{
dst: dst,
area: area,
bs: &bs,
knl: k,
hold: make([][]byte, hf-1),
hf: hf,
scale: 1 / float64(scaleFactor),
debugging: newWindows("MOG"),
}
return &MOG{dst, area, &bs, k, debug, windows, make([][]byte, hf-1), hf, 0, 1 / float64(scaleFactor)}
}
// Implements io.Closer.
@ -69,9 +72,7 @@ func NewMOG(dst io.WriteCloser, area, threshold float64, history int, debug bool
func (m *MOG) Close() error {
m.bs.Close()
m.knl.Close()
for _, window := range m.windows {
window.Close()
}
m.debugging.close()
return nil
}
@ -122,20 +123,7 @@ func (m *MOG) Write(f []byte) (int, error) {
}
// Draw debug information.
if m.debug {
for _, c := range contours {
rect := gocv.BoundingRect(c)
gocv.Rectangle(&img, rect, color.RGBA{0, 0, 255, 0}, 1)
}
if len(contours) > 0 {
gocv.PutText(&img, "Motion", image.Pt(32, 32), gocv.FontHersheyPlain, 2.0, color.RGBA{255, 0, 0, 0}, 2)
}
m.windows[0].IMShow(img)
m.windows[1].IMShow(imgDelta)
m.windows[0].WaitKey(1)
}
m.debugging.show(img, imgDelta, len(contours) > 0, &contours)
// Don't write to destination if there is no motion.
if len(contours) == 0 {

45
filter/release.go Normal file
View File

@ -0,0 +1,45 @@
// +build !debug
// +build !circleci
/*
DESCRIPTION
Displays debug information for the motion filters.
AUTHORS
Scott Barnard <scott@ausocean.org>
LICENSE
This file 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 (
"image"
)
// debugWindows is used for displaying debug information for the motion filters.
type debugWindows struct{}
// close frees resources used by gocv.
func (d *debugWindows) close() error { return nil }
// newWindows creates debugging windows for the motion filter.
func newWindows(name string) debugWindows { return debugWindows{} }
// show displays debug information for the motion filters.
func (d *debugWindows) show(img, imgDelta interface{}, motion bool, contours *[][]image.Point, text ...string) {
}

View File

@ -27,8 +27,6 @@ package config
import (
"errors"
"fmt"
"os/exec"
"time"
"bitbucket.org/ausocean/av/codec/codecutil"
@ -136,9 +134,6 @@ const (
FilterBasic
)
// OS names
const raspian = "Raspbian GNU/Linux"
// Config provides parameters relevant to a revid instance. A new config must
// be passed to the constructor. Default values for these fields are defined
// as consts above.
@ -259,10 +254,6 @@ type Config struct {
// of the file.
Exposure string
// ShowWindows enables or disables the display of windows used for debugging
// motion filters.
ShowWindows bool
// AutoWhiteBalance defines the auto white balance mode used by Raspivid input.
// Valid modes are defined in the exported []string AutoWhiteBalanceModes
// defined at the start of the file.
@ -370,7 +361,6 @@ var TypeData = map[string]string{
"RTMPURL": "string",
"RTPAddress": "string",
"Saturation": "int",
"ShowWindows": "bool",
"VBRBitrate": "int",
"VBRQuality": "enum:standard,fair,good,great,excellent",
"VerticalFlip": "bool",
@ -554,17 +544,6 @@ func (c *Config) Validate() error {
c.MotionDownscaling = defaultMotionDownscaling
}
if c.ShowWindows {
os, err := osName()
if err != nil {
return err
}
if os == raspian {
c.Logger.Log(logger.Info, pkg+"ShowWindows disabled on Raspbian GNU/Linux", "ShowWindows", false)
c.ShowWindows = false
}
}
if c.FileFPS <= 0 || (c.FileFPS > 0 && c.Input != InputFile) {
c.logInvalidField("FileFPS", defaultFileFPS)
c.FileFPS = defaultFileFPS
@ -573,15 +552,6 @@ func (c *Config) Validate() error {
return nil
}
func osName() (string, error) {
out, err := exec.Command("grep", "^NAME=", "/etc/os-release").Output()
if err != nil {
return "", fmt.Errorf("could not get OS name: %w", err)
}
return string(out)[6 : len(out)-2], nil
}
func (c *Config) logInvalidField(name string, def interface{}) {
c.Logger.Log(logger.Info, pkg+name+" bad or unset, defaulting", name, def)
}

View File

@ -334,15 +334,15 @@ func (r *Revid) setupPipeline(mtsEnc func(dst io.WriteCloser, rate float64) (io.
case config.FilterNoOp:
r.filters[i] = filter.NewNoOp(dst)
case config.FilterMOG:
r.filters[i] = filter.NewMOG(dst, r.cfg.MOGMinArea, r.cfg.MOGThreshold, int(r.cfg.MOGHistory), r.cfg.ShowWindows, r.cfg.MotionInterval, r.cfg.MotionDownscaling)
r.filters[i] = filter.NewMOG(dst, r.cfg.MOGMinArea, r.cfg.MOGThreshold, int(r.cfg.MOGHistory), r.cfg.MotionInterval, r.cfg.MotionDownscaling)
case config.FilterVariableFPS:
r.filters[i] = filter.NewVariableFPS(dst, r.cfg.MinFPS, filter.NewMOG(dst, r.cfg.MOGMinArea, r.cfg.MOGThreshold, int(r.cfg.MOGHistory), r.cfg.ShowWindows, r.cfg.MotionInterval, r.cfg.MotionDownscaling))
r.filters[i] = filter.NewVariableFPS(dst, r.cfg.MinFPS, filter.NewMOG(dst, r.cfg.MOGMinArea, r.cfg.MOGThreshold, int(r.cfg.MOGHistory), r.cfg.MotionInterval, r.cfg.MotionDownscaling))
case config.FilterKNN:
r.filters[i] = filter.NewKNN(dst, r.cfg.KNNMinArea, r.cfg.KNNThreshold, int(r.cfg.KNNHistory), int(r.cfg.KNNKernel), r.cfg.ShowWindows, r.cfg.MotionInterval, r.cfg.MotionDownscaling)
r.filters[i] = filter.NewKNN(dst, r.cfg.KNNMinArea, r.cfg.KNNThreshold, int(r.cfg.KNNHistory), int(r.cfg.KNNKernel), r.cfg.MotionInterval, r.cfg.MotionDownscaling)
case config.FilterDifference:
r.filters[i] = filter.NewDifference(dst, r.cfg.ShowWindows, r.cfg.DiffThreshold)
r.filters[i] = filter.NewDifference(dst, r.cfg.DiffThreshold)
case config.FilterBasic:
r.filters[i] = filter.NewBasic(dst, r.cfg.ShowWindows, r.cfg.BasicThreshold, r.cfg.BasicPixels)
r.filters[i] = filter.NewBasic(dst, r.cfg.BasicThreshold, r.cfg.BasicPixels)
default:
panic("Undefined Filter")
}
@ -793,16 +793,6 @@ func (r *Revid) Update(vars map[string]string) error {
break
}
r.cfg.CameraChan = v
case "ShowWindows":
switch strings.ToLower(value) {
case "true":
r.cfg.ShowWindows = true
case "false":
r.cfg.ShowWindows = false
default:
r.cfg.Logger.Log(logger.Warning, pkg+"invalid ShowWindows var", "value", value)
break
}
case "MinFPS":
v, err := strconv.ParseFloat(value, 64)
if err != nil {