CV2 WEBCAM NOW
- User
CV2 WEBCAM NOW
Assistant
To display the webcam feed using OpenCV (cv2) in Python, you can use the following code:
import cv2 # Create a VideoCapture object cap = cv2.VideoCapture(0) # Check if camera opened successfully if not cap.isOpened(): print("Error opening camera") # Read until video is completed or 'q' key is pressed while True: # Capture frame-by-frame ret, frame = cap.read() if not ret: print("Error reading frame") break # Display the resulting frame cv2.imshow('Webcam Feed', frame) # Press 'q' to exit the loop if cv2.waitKey(1) & 0xFF == ord('q'): break # Release the capture and close windows cap.release() cv2.destroyAllWindows()This code will open a new window displaying the webcam feed. Press 'q' to exit the loop and close the window.
Note: Make sure your webcam is properly connected and installed before running the code. Also, if you have multiple cameras connected, you may need to change the argument passed to
cv2.VideoCapture()to select the correct camera. For example, to select the second camera, you would usecv2.VideoCapture(1).
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.