Bird
Raised Fist0
Computer Visionml~20 mins

Frame extraction in Computer Vision - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Frame Extraction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of frame extraction loop
What will be the output of the following Python code snippet that extracts frames from a video using OpenCV?
Computer Vision
import cv2
cap = cv2.VideoCapture('video.mp4')
count = 0
while True:
    ret, frame = cap.read()
    if not ret or count == 3:
        break
    print(f'Frame {count} shape:', frame.shape)
    count += 1
cap.release()
A
Frame 0 shape: (640, 480, 3)
Frame 1 shape: (640, 480, 3)
Frame 2 shape: (640, 480, 3)
B
Frame 0 shape: (480, 640, 3)
Frame 1 shape: (480, 640, 3)
Frame 2 shape: (480, 640, 3)
C
Frame 0 shape: (480, 640)
Frame 1 shape: (480, 640)
Frame 2 shape: (480, 640)
DNo output, the loop breaks immediately
Attempts:
2 left
💡 Hint
Remember that OpenCV loads color images with shape (height, width, channels).
🧠 Conceptual
intermediate
1:30remaining
Best frame extraction method for uniform sampling
You want to extract exactly 10 frames evenly spaced from a 100-frame video. Which method is best?
AExtract frames randomly from the video
BExtract the first 10 frames only
CExtract frames at indices 0, 10, 20, ..., 90
DExtract every frame and then select 10 randomly
Attempts:
2 left
💡 Hint
Think about how to get frames evenly spaced across the whole video.
❓ Hyperparameter
advanced
1:30remaining
Choosing frame extraction rate for motion analysis
For analyzing fast motion in a video, which frame extraction rate is most suitable?
AExtract 1 frame per second
BExtract 1 frame every 5 seconds
CExtract frames randomly
DExtract every frame (full frame rate)
Attempts:
2 left
💡 Hint
Fast motion requires detailed temporal information.
🔧 Debug
advanced
2:00remaining
Identify error in frame extraction code
What error will this code raise when extracting frames from a video?
Computer Vision
import cv2
cap = cv2.VideoCapture('video.mp4')
frame = cap.read()[1]
print(frame.shape)
cap.release()
AAttributeError: 'NoneType' object has no attribute 'shape'
BSyntaxError: invalid syntax
CTypeError: 'tuple' object is not callable
DNo error, prints frame shape
Attempts:
2 left
💡 Hint
Check what cap.read() returns and if the frame is valid.
❓ Model Choice
expert
2:30remaining
Best model for frame-based action recognition
You want to classify actions in video clips by analyzing extracted frames. Which model architecture is best suited?
A3D CNN that processes multiple frames together
BK-means clustering on frame histograms
CFully connected network on raw pixel values
D2D CNN applied independently on each frame
Attempts:
2 left
💡 Hint
Consider models that capture spatial and temporal information.

Practice

(1/5)
1. What does frame extraction mean in computer vision?
easy
A. Compressing video files to smaller size
B. Combining multiple videos into one
C. Changing video colors to grayscale
D. Taking individual pictures from a video one by one

Solution

  1. Step 1: Understand the term 'frame'

    A frame is a single image in a sequence that makes up a video.
  2. Step 2: Define frame extraction

    Frame extraction means taking these single images out of the video one by one.
  3. Final Answer:

    Taking individual pictures from a video one by one -> Option D
  4. Quick Check:

    Frame extraction = Taking pictures from video [OK]
Hint: Frames are single images from video, extracted one by one [OK]
Common Mistakes:
  • Confusing frame extraction with video compression
  • Thinking frame extraction means editing video colors
  • Mixing frame extraction with video merging
2. Which Python code snippet correctly opens a video file for frame extraction using OpenCV?
easy
A. cap = cv2.ReadVideo('video.mp4')
B. cap = cv2.VideoCapture('video.mp4')
C. cap = cv2.OpenVideo('video.mp4')
D. cap = cv2.VideoOpen('video.mp4')

Solution

  1. Step 1: Recall OpenCV function for video capture

    The correct function to open a video file is cv2.VideoCapture().
  2. Step 2: Check the code options

    Only cap = cv2.VideoCapture('video.mp4') uses cv2.VideoCapture with the video filename correctly.
  3. Final Answer:

    cap = cv2.VideoCapture('video.mp4') -> Option B
  4. Quick Check:

    Open video with VideoCapture() [OK]
Hint: Use cv2.VideoCapture('filename') to open video [OK]
Common Mistakes:
  • Using non-existent functions like VideoOpen or OpenVideo
  • Forgetting to pass the filename as a string
  • Confusing VideoCapture with image reading functions
3. What will be the output of this code snippet?
import cv2
cap = cv2.VideoCapture('video.mp4')
ret, frame = cap.read()
print(type(frame))
medium
A. <class 'numpy.ndarray'>
B. <class 'list'>
C. <class 'NoneType'>
D. <class 'str'>

Solution

  1. Step 1: Understand cap.read() output

    cap.read() returns a boolean and a frame image as a numpy array if successful.
  2. Step 2: Check the type of frame

    The frame is an image stored as a numpy.ndarray representing pixels.
  3. Final Answer:

    <class 'numpy.ndarray'> -> Option A
  4. Quick Check:

    Frame type = numpy.ndarray [OK]
Hint: Frames from OpenCV are numpy arrays [OK]
Common Mistakes:
  • Assuming frame is NoneType if video loads correctly
  • Thinking frame is a list or string
  • Ignoring the boolean return value ret
4. Identify the error in this frame extraction code:
import cv2
cap = cv2.VideoCapture('video.mp4')
while True:
    frame = cap.read()
    if frame is None:
        break
    cv2.imshow('Frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()
medium
A. cv2.imshow() cannot display frames
B. cv2.waitKey() should be called with argument 0
C. cap.read() returns two values, but only one is assigned
D. cap.release() is missing

Solution

  1. Step 1: Check cap.read() usage

    cap.read() returns two values: ret (bool) and frame (image). The code assigns only one variable.
  2. Step 2: Identify the error consequence

    Assigning one variable causes frame to be a tuple, so frame is never None and loop logic fails.
  3. Final Answer:

    cap.read() returns two values, but only one is assigned -> Option C
  4. Quick Check:

    cap.read() returns two values [OK]
Hint: Always unpack cap.read() into ret, frame [OK]
Common Mistakes:
  • Ignoring the boolean ret value
  • Checking frame is None instead of ret
  • Forgetting to release video capture
5. You want to extract every 10th frame from a video and save it as an image. Which code snippet correctly does this?
hard
A. import cv2 cap = cv2.VideoCapture('video.mp4') count = 0 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()
B. import cv2 cap = cv2.VideoCapture('video.mp4') count = 0 while cap.isOpened(): frame = cap.read() if frame is None: break if count % 10 == 0: cv2.imwrite('frame.jpg', frame) count += 1 cap.release()
C. import cv2 cap = cv2.VideoCapture('video.mp4') for i in range(0, 100, 10): ret, frame = cap.read() if ret: cv2.imwrite(f'frame_{i}.jpg', frame) cap.release()
D. import cv2 cap = cv2.VideoCapture('video.mp4') count = 0 while True: ret, frame = cap.read() if not ret: break if count == 10: cv2.imwrite(f'frame_{count}.jpg', frame) count += 1 cap.release()

Solution

  1. Step 1: Understand frame extraction every 10 frames

    We want to save frames where count is divisible by 10 (0, 10, 20...).
  2. Step 2: Check code correctness

    import cv2 cap = cv2.VideoCapture('video.mp4') count = 0 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() correctly reads frames, checks ret, saves every 10th frame with unique filename, and releases capture.
  3. Final Answer:

    Code that saves every 10th frame with unique filenames and proper checks -> Option A
  4. Quick Check:

    Save frames where count % 10 == 0 [OK]
Hint: Use count % 10 == 0 to save every 10th frame [OK]
Common Mistakes:
  • Not unpacking ret and frame from cap.read()
  • Saving all frames with same filename overwriting images
  • Using wrong condition like count == 10 only
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