Bird
0
0

What is wrong with this motion detection loop?

medium📝 Debug Q7 of 15
Raspberry Pi - Camera Module
What is wrong with this motion detection loop?
import cv2
cap = cv2.VideoCapture(0)
while True:
    ret, frame1 = cap.read()
    ret, frame2 = cap.read()
    diff = cv2.absdiff(frame1, frame2)
    if diff.any():
        print("Motion detected")
AReading two frames inside the loop causes missed frames
Bdiff.any() is not a valid method
CMissing break condition to exit loop
Dcap.read() returns only one value
Step-by-Step Solution
Solution:
  1. Step 1: Analyze frame reading inside loop

    Reading two frames each loop skips frames and reduces detection accuracy.
  2. Step 2: Correct approach

    Read one frame per loop and compare with previous frame stored outside loop.
  3. Final Answer:

    Reading two frames inside the loop causes missed frames -> Option A
  4. Quick Check:

    Frame reading logic affects motion detection accuracy [OK]
Quick Trick: Read one frame per loop, compare with previous [OK]
Common Mistakes:
MISTAKES
  • Reading multiple frames per loop causing skips
  • Misusing diff.any()
  • Ignoring loop exit conditions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes