Add simple script to take an image with webcam and python
All checks were successful
/ test (push) Successful in 1m53s
All checks were successful
/ test (push) Successful in 1m53s
This commit is contained in:
parent
ed3274fe93
commit
725feb5d9a
1 changed files with 27 additions and 0 deletions
27
PythonScripts/webcam.py
Normal file
27
PythonScripts/webcam.py
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
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()
|
Loading…
Reference in a new issue