Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to open the default camera stream using OpenCV.
Drone Programming
import cv2 cap = cv2.VideoCapture([1]) if not cap.isOpened(): print("Cannot open camera")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 for the default camera index.
Passing a string instead of an integer.
✗ Incorrect
The default camera is accessed with index 0 in OpenCV.
2fill in blank
mediumComplete the code to read a frame from the camera stream.
Drone Programming
ret, frame = cap.[1]() if not ret: print("Failed to grab frame")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
get() which only retrieves properties.Using
capture() or fetch() which do not exist.✗ Incorrect
The read() method captures a frame from the video stream.
3fill in blank
hardFix the error in the code to release the camera resource properly.
Drone Programming
cap.[1]()
cv2.destroyAllWindows() Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
close() which does not exist for VideoCapture.Using
stop() or end() which are invalid.✗ Incorrect
The release() method frees the camera resource.
4fill in blank
hardFill both blanks to convert the captured frame to grayscale and display it.
Drone Programming
gray = cv2.[1](frame, cv2.[2]) cv2.imshow('Gray Frame', gray)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
convertColor which is not a valid function.Using
COLOR_RGB2GRAY when the frame is in BGR format.✗ Incorrect
Use cvtColor with COLOR_BGR2GRAY to convert a BGR image to grayscale.
5fill in blank
hardFill all three blanks to create a loop that reads frames, shows them, and breaks on pressing 'q'.
Drone Programming
while True: ret, frame = cap.[1]() if not ret: break cv2.imshow('Frame', frame) if cv2.waitKey([2]) & 0xFF == ord([3]): break cap.release() cv2.destroyAllWindows()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
wait() instead of waitKey().Using a wrong key character or missing quotes around 'q'.
Not breaking the loop properly on key press.
✗ Incorrect
The loop reads frames with read(), waits 1 ms for a key press, and breaks if 'q' is pressed.