Bird
Raised Fist0
Computer Visionml~20 mins

Reading video with OpenCV in Computer Vision - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
OpenCV Video Reading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What does this OpenCV video reading code output?
Consider this Python code that reads a video file frame by frame using OpenCV. What will be the printed output after running this code snippet?
Computer Vision
import cv2
cap = cv2.VideoCapture('video.mp4')
frame_count = 0
while True:
    ret, frame = cap.read()
    if not ret:
        break
    frame_count += 1
print(frame_count)
cap.release()
AThe first frame's pixel values printed as an array
B0
CThe total number of frames in the video
DRaises an error because 'cap.release()' is missing
Attempts:
2 left
💡 Hint
Think about what 'cap.read()' returns and when the loop stops.
❓ Model Choice
intermediate
1:30remaining
Which OpenCV method correctly opens a video file for reading?
You want to read a video file named 'clip.avi' frame by frame. Which OpenCV method call correctly opens the video for reading?
Acv2.VideoCapture('clip.avi')
Bcv2.VideoOpen('clip.avi')
Ccv2.readVideo('clip.avi')
Dcv2.VideoReader('clip.avi')
Attempts:
2 left
💡 Hint
Check the OpenCV documentation for video capture classes.
❓ Hyperparameter
advanced
2:00remaining
What does setting CAP_PROP_POS_FRAMES do in OpenCV?
In OpenCV, you can set a property of a video capture object using cap.set(cv2.CAP_PROP_POS_FRAMES, frame_number). What effect does this have?
AIt moves the video reading position to the specified frame number
BIt changes the frame rate of the video playback
CIt sets the brightness of the video frames
DIt resets the video capture object to the first frame
Attempts:
2 left
💡 Hint
Think about how you would jump to a specific frame in a video.
❓ Metrics
advanced
1:30remaining
How to calculate video duration using OpenCV properties?
You have opened a video with OpenCV. Which formula correctly calculates the video duration in seconds using OpenCV properties?
Aduration = frames_per_second / total_frames
Bduration = total_frames / frames_per_second
Cduration = total_frames * frames_per_second
Dduration = frames_per_second - total_frames
Attempts:
2 left
💡 Hint
Duration is how many seconds the video lasts, so think about frames and frame rate.
🔧 Debug
expert
2:30remaining
Why does this OpenCV video reading loop freeze?
This code snippet freezes and never ends when reading a video. What is the most likely cause? import cv2 cap = cv2.VideoCapture('video.mp4') while True: ret, frame = cap.read() if ret: cv2.imshow('Frame', frame) if cv2.waitKey(25) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
Acap.release() is called before the loop
Bcv2.imshow is called without a window name
Ccv2.waitKey is missing a delay argument
DThe loop never breaks because it does not check for ret == False
Attempts:
2 left
💡 Hint
What happens if the video ends and ret is False?

Practice

(1/5)
1. What does the cv2.VideoCapture function do in OpenCV when reading a video?
easy
A. It opens the video file or camera stream for reading frames.
B. It displays the video frames on the screen.
C. It saves the video to a new file.
D. It converts video frames to grayscale automatically.

Solution

  1. Step 1: Understand the purpose of cv2.VideoCapture

    This function is used to open a video file or a camera stream so that frames can be read one by one.
  2. Step 2: Differentiate from other functions

    Displaying frames or saving videos are done by other functions like cv2.imshow or cv2.VideoWriter. Conversion to grayscale is a separate processing step.
  3. Final Answer:

    It opens the video file or camera stream for reading frames. -> Option A
  4. Quick Check:

    cv2.VideoCapture opens video source [OK]
Hint: Remember: VideoCapture opens video, does not display or save [OK]
Common Mistakes:
  • Confusing opening video with displaying frames
  • Thinking it saves or processes frames automatically
  • Assuming it converts frames to grayscale
2. Which of the following is the correct way to read a frame from a video capture object cap in OpenCV?
easy
A. cap.read() -> frame, ret
B. frame, ret = cap.read()
C. cap.read(frame, ret)
D. ret, frame = cap.read()

Solution

  1. Step 1: Recall the return order of cap.read()

    The method returns two values: a boolean ret indicating success, and the frame image.
  2. Step 2: Match the correct variable order

    The correct order is ret, frame = cap.read(). Assigning in reverse order causes errors or wrong values.
  3. Final Answer:

    ret, frame = cap.read() -> Option D
  4. Quick Check:

    ret, frame = cap.read() [OK]
Hint: Remember: ret (bool) comes first, then frame image [OK]
Common Mistakes:
  • Swapping ret and frame variables
  • Trying to pass variables as arguments to read()
  • Using incorrect assignment syntax
3. What will be the output of the following code snippet if the video file path is incorrect or missing?
import cv2
cap = cv2.VideoCapture('wrong_path.mp4')
ret, frame = cap.read()
print(ret)
medium
A. True
B. None
C. False
D. Raises an exception

Solution

  1. Step 1: Understand behavior when video path is invalid

    If the video file cannot be opened, cap.read() returns ret = False and frame = None.
  2. Step 2: Check the printed output

    The code prints the value of ret, which will be False indicating failure to read a frame.
  3. Final Answer:

    False -> Option C
  4. Quick Check:

    Invalid path makes ret False [OK]
Hint: ret is False if frame read fails (e.g., bad path) [OK]
Common Mistakes:
  • Expecting True even if file missing
  • Assuming exception is raised automatically
  • Confusing None with False for ret
4. Identify the error in this code snippet for reading and displaying video frames:
import cv2
cap = cv2.VideoCapture('video.mp4')
while True:
    ret, frame = cap.read()
    if ret:
        cv2.imshow('Video', frame)
    else:
        break
cv2.destroyAllWindows()
medium
A. Missing cap.release() to free video resource
B. Incorrect use of cv2.imshow function
C. Loop should use while ret: instead of while True:
D. No error, code runs fine

Solution

  1. Step 1: Check resource management in video reading

    After finishing reading frames, the video capture object should be released with cap.release() to free resources.
  2. Step 2: Verify other parts of the code

    The cv2.imshow usage and loop logic are correct. The missing cap.release() is the main issue.
  3. Final Answer:

    Missing cap.release() to free video resource -> Option A
  4. Quick Check:

    Always call cap.release() after reading video [OK]
Hint: Always release video capture after use [OK]
Common Mistakes:
  • Forgetting cap.release() causes resource leaks
  • Thinking cv2.destroyAllWindows() frees video capture
  • Changing loop condition unnecessarily
5. You want to read a video file and save only every 10th frame as an image using OpenCV. Which code snippet correctly implements this?
hard
A. import cv2 cap = cv2.VideoCapture('video.mp4') count = 0 while True: ret, frame = cap.read() if not ret: break count += 1 if count % 10 == 0: cv2.imwrite(f'frame_{count}.jpg', frame) cap.release()
B. import cv2 cap = cv2.VideoCapture('video.mp4') count = 0 while True: ret, frame = cap.read() if ret: if count % 10 == 0: cv2.imwrite(f'frame_{count}.jpg', frame) count += 1 else: break cap.release()
C. import cv2 cap = cv2.VideoCapture('video.mp4') count = 0 while cap.isOpened(): ret, frame = cap.read() if ret: if count % 10 == 0: cv2.imwrite('frame_{count}.jpg', frame) count += 1 else: break cap.release()
D. import cv2 cap = cv2.VideoCapture('video.mp4') count = 1 while True: ret, frame = cap.read() if not ret: break if count % 10 == 0: cv2.imwrite(f'frame_{count}.jpg', frame) count += 1 cap.release()

Solution

  1. Step 1: Understand frame reading loop and counting

    We need a loop that reads frames until no more frames are available, counting frames starting from zero.
  2. Step 2: Check saving every 10th frame with correct filename formatting

    Using f-strings like f'frame_{count}.jpg' correctly formats the filename. The count increments only after reading a frame.
  3. Step 3: Compare options for correctness

    import cv2 cap = cv2.VideoCapture('video.mp4') count = 0 while True: ret, frame = cap.read() if not ret: break count += 1 if count % 10 == 0: cv2.imwrite(f'frame_{count}.jpg', frame) cap.release() increments count before modulo check, causing off-by-one (saves frame 9 as frame_10.jpg). import cv2 cap = cv2.VideoCapture('video.mp4') count = 0 while cap.isOpened(): ret, frame = cap.read() if ret: if count % 10 == 0: cv2.imwrite('frame_{count}.jpg', frame) count += 1 else: break cap.release() uses wrong string formatting in cv2.imwrite. import cv2 cap = cv2.VideoCapture('video.mp4') count = 1 while True: ret, frame = cap.read() if not ret: break if count % 10 == 0: cv2.imwrite(f'frame_{count}.jpg', frame) count += 1 cap.release() starts count at 1, causing off-by-one error (saves frame 9 as frame_10.jpg). import cv2 cap = cv2.VideoCapture('video.mp4') count = 0 while True: ret, frame = cap.read() if ret: if count % 10 == 0: cv2.imwrite(f'frame_{count}.jpg', frame) count += 1 else: break cap.release() correctly handles all requirements.
  4. Final Answer:

    while True with if ret: (modulo check, save, increment) else: break, using f-string -> Option B
  5. Quick Check:

    Use f-strings and check ret before saving frames [OK]
Hint: Use f-strings and check ret before saving frames [OK]
Common Mistakes:
  • Incrementing count before modulo check (off-by-one error)
  • Using wrong string formatting in filename
  • Starting count at 1 without adjusting logic (off-by-one)
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard