36 lines
826 B
Python
36 lines
826 B
Python
import cv2
|
|
|
|
# Open a connection to the webcam (default camera index is 0)
|
|
cap = cv2.VideoCapture(0)
|
|
|
|
# Check if the camera is opened successfully
|
|
if not cap.isOpened():
|
|
print("Error: Could not open the camera.")
|
|
exit()
|
|
|
|
# Create a window to display the webcam feed
|
|
window_name = 'Webcam Feed'
|
|
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
|
|
|
|
while True:
|
|
# Read a frame from the webcam
|
|
ret, frame = cap.read()
|
|
|
|
# Check if the frame is read successfully
|
|
if not ret:
|
|
print("Error: Could not read frame.")
|
|
break
|
|
|
|
# Display the frame in the window
|
|
cv2.imshow(window_name, frame)
|
|
|
|
# Break the loop if 'q' key is pressed
|
|
if cv2.waitKey(1) & 0xFF == ord('q'):
|
|
break
|
|
|
|
# Release the camera capture object
|
|
cap.release()
|
|
|
|
# Destroy the OpenCV window
|
|
cv2.destroyAllWindows()
|