Complete the code to open the default camera stream using OpenCV.
import cv2 cap = cv2.VideoCapture([1]) if not cap.isOpened(): print("Cannot open camera")
The default camera is accessed with index 0 in OpenCV.
Complete the code to read a frame from the camera stream.
ret, frame = cap.[1]() if not ret: print("Failed to grab frame")
get() which only retrieves properties.capture() or fetch() which do not exist.The read() method captures a frame from the video stream.
Fix the error in the code to release the camera resource properly.
cap.[1]()
cv2.destroyAllWindows()close() which does not exist for VideoCapture.stop() or end() which are invalid.The release() method frees the camera resource.
Fill both blanks to convert the captured frame to grayscale and display it.
gray = cv2.[1](frame, cv2.[2]) cv2.imshow('Gray Frame', gray)
convertColor which is not a valid function.COLOR_RGB2GRAY when the frame is in BGR format.Use cvtColor with COLOR_BGR2GRAY to convert a BGR image to grayscale.
Fill all three blanks to create a loop that reads frames, shows them, and breaks on pressing 'q'.
while True: ret, frame = cap.[1]() if not ret: break cv2.imshow('Frame', frame) if cv2.waitKey([2]) & 0xFF == ord([3]): break cap.release() cv2.destroyAllWindows()
wait() instead of waitKey().The loop reads frames with read(), waits 1 ms for a key press, and breaks if 'q' is pressed.
