/
motion.go
180 lines (170 loc) · 5.03 KB
/
motion.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package motion
import (
"image"
log "github.com/sirupsen/logrus"
"github.com/jonoton/scout/videosource"
"gocv.io/x/gocv"
)
// Motion detects motion within images
type Motion struct {
Name string
Skip bool
padding int
scaleWidth int
minimumPercentage int
maximumPercentage int
maxMotions int
overloadPercent int
thresholdPercent int
noiseReduction int
highlightColor string
highlightThickness int
}
// NewMotion creates a new Motion
func NewMotion(name string) *Motion {
m := &Motion{
Name: name,
padding: 0,
scaleWidth: 320,
minimumPercentage: 2,
maximumPercentage: 75,
maxMotions: 20,
overloadPercent: 90,
thresholdPercent: 40,
noiseReduction: 10,
highlightColor: "purple",
highlightThickness: 3,
}
return m
}
// SetConfig on motion
func (m *Motion) SetConfig(config *Config) {
if config != nil {
m.Skip = config.Skip
if config.Padding > 0 {
m.padding = config.Padding
}
if config.ScaleWidth < 0 || 0 < config.ScaleWidth {
m.scaleWidth = config.ScaleWidth
}
if config.MinimumPercentage >= 0 {
m.minimumPercentage = config.MinimumPercentage
}
if config.MaximumPercentage > 0 {
m.maximumPercentage = config.MaximumPercentage
}
if config.MaxMotions > 0 {
m.maxMotions = config.MaxMotions
}
if config.OverloadPercent > 0 {
m.overloadPercent = config.OverloadPercent
}
if config.ThresholdPercent > 0 {
m.thresholdPercent = config.ThresholdPercent
}
if config.NoiseReduction > 0 {
m.noiseReduction = config.NoiseReduction
}
if config.HighlightColor != "" {
m.highlightColor = config.HighlightColor
}
if config.HighlightThickness > 0 {
m.highlightThickness = config.HighlightThickness
}
}
}
// Run starts the motion detection process
func (m *Motion) Run(input <-chan videosource.Image) <-chan videosource.ProcessedImage {
r := make(chan videosource.ProcessedImage)
go func() {
defer func() {
// recover from panic if one occured
if recover() != nil {
log.Errorln("Recovered from panic in motion for", m.Name)
}
}()
mog2 := gocv.NewBackgroundSubtractorMOG2()
for cur := range input {
result := *videosource.NewProcessedImage(cur)
if m.Skip {
r <- result
continue
}
origWidth := cur.Width()
scaleWidth := m.scaleWidth
if m.scaleWidth <= 0 {
scaleWidth = origWidth
}
scaleRatio := float64(origWidth) / float64(scaleWidth)
scaledImg := cur.ScaleToWidth(scaleWidth)
blurMat := gocv.NewMat()
// reduce noise - must be odd number
if m.noiseReduction%2 == 0 {
m.noiseReduction++
}
gocv.GaussianBlur(scaledImg.SharedMat.Mat, &blurMat, image.Pt(m.noiseReduction, m.noiseReduction), 0, 0, gocv.BorderDefault)
matDelta := gocv.NewMat()
matThresh := gocv.NewMat()
// obtain foreground only
mog2.Apply(blurMat, &matDelta)
// threshold range is 0-255, lower is more sensitive
threshold := 255 * m.thresholdPercent / 100
gocv.Threshold(matDelta, &matThresh, float32(threshold), 255, gocv.ThresholdBinary)
matDelta.Close()
// dilate
kernel := gocv.GetStructuringElement(gocv.MorphRect, image.Pt(3, 3))
gocv.Dilate(matThresh, &matThresh, kernel)
kernel.Close()
// find contours
contours := gocv.FindContours(matThresh, gocv.RetrievalExternal, gocv.ChainApproxSimple)
matThresh.Close()
imgWidth := scaledImg.Width()
imgHeight := scaledImg.Height()
imageArea := imgHeight * imgWidth
overloadArea := float64(imageArea * m.overloadPercent / 100)
minimumArea := float64(imageArea * m.minimumPercentage / 100)
maximumArea := float64(imageArea * m.maximumPercentage / 100)
minimumWidth := imgWidth * m.minimumPercentage / 100
maximumWidth := imgWidth * m.maximumPercentage / 100
minimumHeight := imgHeight * m.minimumPercentage / 100
maximumHeight := imgHeight * m.maximumPercentage / 100
numMotions := 0
for index := 0; index < contours.Size(); index++ {
c := contours.At(index)
area := gocv.ContourArea(c)
if numMotions > m.maxMotions || area >= overloadArea {
numMotions = 0
for _, motion := range result.Motions {
motion.Cleanup()
}
result.Motions = make([]videosource.MotionInfo, 0)
break
}
if area < minimumArea || area > maximumArea {
continue
}
rect := videosource.CorrectRectangle(cur, gocv.BoundingRect(c))
rectWidth := rect.Dx()
rectHeight := rect.Dy()
if rectWidth < minimumWidth || rectWidth > maximumWidth {
continue
}
if rectHeight < minimumHeight || rectHeight > maximumHeight {
continue
}
scaledRect := videosource.RectScale(cur, rect, scaleRatio)
finalRect := videosource.RectPadded(cur, scaledRect, m.padding)
motionInfo := videosource.NewMotionInfo(finalRect, *videosource.NewColorThickness(m.highlightColor, m.highlightThickness))
result.Motions = append(result.Motions, *motionInfo)
numMotions++
}
contours.Close()
scaledImg.Cleanup()
blurMat.Close()
r <- result
}
mog2.Close()
close(r)
}()
return r
}