Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to open a video file using OpenCV.
Computer Vision
import cv2 cap = cv2.VideoCapture([1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing 0 opens the webcam, not a video file.
Passing True or None causes errors.
✗ Incorrect
To open a video file, you pass the filename as a string to cv2.VideoCapture.
2fill in blank
mediumComplete the code to read a frame from the video capture object.
Computer Vision
ret, frame = cap.[1]() Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using grab() only grabs the frame but does not decode it.
capture() and fetch() are not OpenCV methods.
✗ Incorrect
The read() method reads the next frame from the video.
3fill in blank
hardFix the error in the loop that extracts frames until the video ends.
Computer Vision
while True: ret, frame = cap.read() if not [1]: break cv2.imshow('Frame', frame) if cv2.waitKey(25) & 0xFF == ord('q'): break
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking frame instead of ret can cause errors if frame is None.
Checking cap or cv2 is incorrect.
✗ Incorrect
ret is a boolean indicating if the frame was read successfully; we break if it's False.
4fill in blank
hardFill both blanks to release the video and close all windows.
Computer Vision
cap.[1]() cv2.[2]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using close() or stop() are not valid OpenCV methods.
Not releasing the video can cause resource leaks.
✗ Incorrect
release() frees the video resource; destroyAllWindows() closes all OpenCV windows.
5fill in blank
hardFill all three blanks to save the extracted frame as an image file.
Computer Vision
if ret: cv2.[1]('[2]', [3])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using imshow instead of imwrite displays the image but does not save it.
Swapping filename and image arguments causes errors.
✗ Incorrect
imwrite saves an image to a file; first argument is filename, second is image data.