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

29
main.go
View File

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