implement frameskip for motion detection

reduce power consumption
This commit is contained in:
Steven Polley 2020-10-29 04:32:45 +00:00
parent 300367c00b
commit d22a376146
1 changed files with 17 additions and 12 deletions

29
main.go
View File

@ -14,7 +14,7 @@ import (
const (
minimumMotionArea = 3000 // Motion detection minimum area needed to move
recordLengthAfterMotion = 30 // Number of seconds to keep recording going after motion was last detected
motionDetectInterval = 1 //number of seconds between motion detection attempts // TBD: Implement this, currently does nothing
motionDetectInterval = 30 //number of frames between motion detection attempts // TBD: Implement this, currently does nothing
)
var (
@ -65,6 +65,8 @@ func main() {
detectMotion(img)
}
frameCount := 0
// main loop
for {
if ok := webcam.Read(&img); !ok {
@ -75,19 +77,22 @@ func main() {
continue
}
if detectMotion(img) {
// Determine if a new recording needs to start
if time.Now().After(lastMotionDetectedTime.Add(time.Second * recordLengthAfterMotion)) {
fileName := fmt.Sprintf("storage-%s.avi", time.Now().Format(time.RFC3339))
log.Printf("motion detected, started recording to file named %s", fileName)
currentRecording, err = gocv.VideoWriterFile(fileName, "MJPG", 25, img.Cols(), img.Rows(), true)
if err != nil {
fmt.Printf("error opening video writer device: %v\n", err)
return
if frameCount >= motionDetectInterval {
if detectMotion(img) {
// Determine if a new recording needs to start
if time.Now().After(lastMotionDetectedTime.Add(time.Second * recordLengthAfterMotion)) {
fileName := fmt.Sprintf("storage-%s.avi", time.Now().Format(time.RFC3339))
log.Printf("motion detected, started recording to file named %s", fileName)
currentRecording, err = gocv.VideoWriterFile(fileName, "MJPG", 25, img.Cols(), img.Rows(), true)
if err != nil {
fmt.Printf("error opening video writer device: %v\n", err)
return
}
}
// And always update the timestamp
lastMotionDetectedTime = time.Now()
}
// And always update the timestamp
lastMotionDetectedTime = time.Now()
frameCount = 0
}
// Determine if we are currently recording and if so, then save the frame to the video