Consider this Python code snippet using OpenCV to access a camera stream. What will it print when run on a system with a working camera?
import cv2 cap = cv2.VideoCapture(0) ret, frame = cap.read() print(ret) cap.release()
The ret variable tells if the frame was captured successfully.
If the camera is accessible, cap.read() returns True and the frame. Otherwise, it returns False.
When you finish using a camera stream in OpenCV, which method should you call to free the camera for other programs?
Think about the method that frees hardware resources.
cap.release() frees the camera device so other programs can use it.
Look at this code snippet. It does not display the camera stream window as expected. What is the reason?
import cv2 cap = cv2.VideoCapture(0) ret, frame = cap.read() if ret: cv2.imshow('Camera', frame) cap.release()
OpenCV windows need event processing to appear and refresh.
Without cv2.waitKey(), the window does not process events and closes immediately.
Choose the code snippet that correctly opens the default camera and reads one frame.
Remember the order of returned values from cap.read().
cap.read() returns a tuple: (ret, frame). Option B uses correct syntax.
Analyze this code that captures frames from a camera and stops after 5 frames. How many frames does it actually capture?
import cv2 cap = cv2.VideoCapture(0) count = 0 while count < 5: ret, frame = cap.read() if not ret: break count += 1 cap.release() print(count)
The loop increments count only when a frame is successfully read.
The loop runs until count reaches 5, so it captures exactly 5 frames if the camera works.