28 lines
619 B
Python
28 lines
619 B
Python
|
import cv2
|
||
|
|
||
|
def take_picture(camera_index=0, file_name='captured_image.jpg'):
|
||
|
# Open a connection to the camera
|
||
|
cap = cv2.VideoCapture(camera_index)
|
||
|
|
||
|
if not cap.isOpened():
|
||
|
print("Error: Could not open camera.")
|
||
|
return
|
||
|
|
||
|
# Capture a single frame
|
||
|
ret, frame = cap.read()
|
||
|
|
||
|
if not ret:
|
||
|
print("Error: Could not capture a frame.")
|
||
|
cap.release()
|
||
|
return
|
||
|
|
||
|
# Save the captured frame to a file
|
||
|
cv2.imwrite(file_name, frame)
|
||
|
|
||
|
# Release the camera
|
||
|
cap.release()
|
||
|
print(f"Picture saved as {file_name}")
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
take_picture()
|