-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp.py
More file actions
39 lines (30 loc) · 975 Bytes
/
Copy pathtemp.py
File metadata and controls
39 lines (30 loc) · 975 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import cv2
import time
def preview_webcam(duration=10):
# Initialize webcam
cap = cv2.VideoCapture(0) # Try 0, 1, 2 if needed
if not cap.isOpened():
print("Error: Could not open webcam.")
return
print("Webcam opened successfully!")
# Get the start time
start_time = time.time()
# Keep camera open for specified duration
while (time.time() - start_time) < duration:
# Capture frame-by-frame
ret, frame = cap.read()
if ret:
# Display the frame
cv2.imshow('Webcam Preview', frame)
# Break loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
print("Error: Couldn't read frame.")
break
# Clean up
cap.release()
cv2.destroyAllWindows()
print(f"Preview ended after {duration} seconds.")
# Run the preview
preview_webcam(10)