diff --git a/main.go b/main.go index 70dd722..3a41487 100644 --- a/main.go +++ b/main.go @@ -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