If the camera opens and a frame is successfully read, ret will be True.
Step 2: Analyze the print statement
The code prints the value of ret, so it will print True if successful.
Final Answer:
True -> Option D
Quick Check:
Successful frame read returns True [OK]
Hint: ret is True if frame read succeeds, else False [OK]
Common Mistakes:
Assuming ret is the frame itself
Expecting printed frame data instead of boolean
Not releasing the camera after use
4. Identify the error in this code snippet that tries to display a live camera feed:
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read
if not ret:
break
cv2.imshow('Camera', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
medium
A. Missing parentheses after cap.read
B. Incorrect window name in imshow
C. waitKey argument should be 0 instead of 1
D. cap.release() should be inside the loop
Solution
Step 1: Check method call syntax
The line ret, frame = cap.read is missing parentheses. It should be cap.read() to call the method.
Step 2: Understand impact of missing parentheses
Without parentheses, cap.read is a method reference, not a call, so unpacking fails or returns wrong values.
Final Answer:
Missing parentheses after cap.read -> Option A
Quick Check:
Method calls need () after name [OK]
Hint: Always add () to call methods like cap.read() [OK]
Common Mistakes:
Forgetting parentheses on method calls
Misunderstanding waitKey timing
Releasing camera inside loop causing errors
5. You want to capture video from a drone's camera and save only frames where the video stream is successfully read. Which code snippet correctly implements this using OpenCV?
hard
A. cap = cv2.VideoCapture(0)
while True:
frame, ret = cap.read()
if ret:
cv2.imwrite('frame.jpg', frame)
cap.release()
B. cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if ret:
cv2.imwrite('frame.jpg', frame)
else:
break
cap.release()
C. cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read
if ret:
cv2.imwrite('frame.jpg', frame)
else:
break
cap.release()
D. cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imwrite('frame.jpg', frame)
if not ret:
break
cap.release()
Solution
Step 1: Verify correct method call and variable order
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if ret:
cv2.imwrite('frame.jpg', frame)
else:
break
cap.release() correctly calls cap.read() and assigns ret, frame in the right order.
Step 2: Check logic for saving frames
It saves the frame only if ret is True, otherwise breaks the loop, which is correct behavior.
Final Answer:
Correctly captures and saves frames only when read succeeds -> Option B
Quick Check:
Call cap.read() and check ret before saving [OK]
Hint: Save frames only if ret is True after cap.read() [OK]
Common Mistakes:
Swapping ret and frame variables
Missing parentheses on cap.read
Saving frames even when ret is False
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.