Bird
0
0

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

medium📝 Predict Output Q13 of 15
Raspberry Pi - Camera Module
What will be the output of this code snippet when motion is detected?
import cv2
cap = cv2.VideoCapture(0)
ret, frame1 = cap.read()
ret, frame2 = cap.read()
diff = cv2.absdiff(frame1, frame2)
gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 30, 255, cv2.THRESH_BINARY)
print(thresh.sum() > 10000)
ATrue if motion detected, False otherwise
BAlways prints True
CAlways prints False
DRaises a syntax error
Step-by-Step Solution
Solution:
  1. Step 1: Understand the code logic for motion detection

    The code compares two frames, converts difference to grayscale, thresholds it, then sums pixels to detect motion.
  2. Step 2: Interpret the print statement

    If the sum of thresholded pixels is greater than 10000, it prints True (motion detected), else False.
  3. Final Answer:

    True if motion detected, False otherwise -> Option A
  4. Quick Check:

    Threshold sum > 10000 means motion True [OK]
Quick Trick: Sum of thresholded diff > limit means motion detected [OK]
Common Mistakes:
MISTAKES
  • Assuming it always prints True or False
  • Confusing thresholding with color conversion
  • Expecting syntax errors from correct code

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes