Challenge - 5 Problems
Motion Detection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this motion detection frame difference code?
Consider this Python snippet using OpenCV on Raspberry Pi to detect motion by frame difference. What will be printed when no motion is detected?
Raspberry Pi
import cv2 frame1 = cv2.imread('frame1.jpg', 0) frame2 = cv2.imread('frame2.jpg', 0) diff = cv2.absdiff(frame1, frame2) non_zero_count = cv2.countNonZero(diff) if non_zero_count > 1000: print('Motion detected') else: print('No motion')
Attempts:
2 left
💡 Hint
Think about what happens when the two frames are identical or very similar.
✗ Incorrect
If the two frames are almost the same, the difference image will have very few non-zero pixels, so the count will be less than 1000, printing 'No motion'.
🧠 Conceptual
intermediate1:30remaining
Which sensor setup is best for detecting motion with a Raspberry Pi camera?
You want to detect motion using a Raspberry Pi camera module. Which setup will give the most reliable motion detection?
Attempts:
2 left
💡 Hint
Motion means change over time in the camera view.
✗ Incorrect
Motion detection works best by comparing consecutive frames to find differences. A single frame or very slow captures won't detect motion well.
🔧 Debug
advanced2:30remaining
Why does this Raspberry Pi motion detection code raise an error?
This code snippet is intended to detect motion but raises an error. What is the cause?
Raspberry Pi
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) blur = cv2.GaussianBlur(gray, (5,5), 0) _, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY) contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) for contour in contours: if cv2.contourArea(contour) < 1000: continue cv2.drawContours(frame1, [contour], -1, (0,255,0), 2) cv2.imshow('Motion', frame1) cv2.waitKey(0) cv2.destroyAllWindows()
Attempts:
2 left
💡 Hint
Check the number of values returned by cv2.findContours in your OpenCV version.
✗ Incorrect
In OpenCV 3.x, cv2.findContours returns 3 values: image, contours, hierarchy. The code expects 2 values, causing a ValueError.
📝 Syntax
advanced2:00remaining
Which option fixes the syntax error in this Raspberry Pi motion detection snippet?
Fix the syntax error in this dictionary comprehension used to count motion pixels:
motion_pixels = {x: x*2 if x > 5 for x in range(10)}
Attempts:
2 left
💡 Hint
Remember the correct order of if conditions in dictionary comprehensions.
✗ Incorrect
Option D correctly places the if condition after the for loop to filter keys. Option D uses a ternary but includes else, which changes the output.
🚀 Application
expert3:00remaining
How many contours will be detected by this Raspberry Pi motion detection code?
Given this code snippet, how many contours will be detected if the camera captures a static scene with one moving object covering a large area?
Raspberry Pi
import cv2 # Assume frame1 and frame2 are consecutive frames # with one large moving object frame1 = cv2.imread('static_scene.jpg') frame2 = cv2.imread('moving_object.jpg') diff = cv2.absdiff(frame1, frame2) gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(gray, (5,5), 0) _, thresh = cv2.threshold(blur, 25, 255, cv2.THRESH_BINARY) contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) large_contours = [c for c in contours if cv2.contourArea(c) > 5000] print(len(large_contours))
Attempts:
2 left
💡 Hint
Think about how a large moving object appears in the thresholded image.
✗ Incorrect
A large moving object creates one big contour above the area threshold, so the list will contain one contour.
