Bird
0
0

What will be the output of this Python snippet when no motion is detected?

medium📝 Predict Output Q4 of 15
Raspberry Pi - Camera Module
What will be the output of this Python snippet when no motion is detected?
import cv2
cap = cv2.VideoCapture(0)
ret, frame1 = cap.read()
ret, frame2 = cap.read()
diff = cv2.absdiff(frame1, frame2)
print(diff.sum() == 0)
AError
BFalse
CNone
DTrue
Step-by-Step Solution
Solution:
  1. Step 1: Understand cv2.absdiff

    This function calculates the absolute difference between two frames pixel-wise.
  2. Step 2: When no motion occurs

    Both frames are nearly identical, so the difference matrix will be all zeros.
  3. Step 3: diff.sum() == 0

    If the sum of all pixel differences is zero, it means no motion.
  4. Final Answer:

    True -> Option D
  5. Quick Check:

    Sum zero means no difference [OK]
Quick Trick: Sum of diff zero means no motion detected [OK]
Common Mistakes:
MISTAKES
  • Assuming diff.any() returns False when no motion
  • Confusing sum with any() function
  • Expecting an error due to frame reading

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes