-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasiccam.py
More file actions
53 lines (40 loc) · 1.17 KB
/
basiccam.py
File metadata and controls
53 lines (40 loc) · 1.17 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
Webcam Viewer Script
--------------------
Displays video from the default webcam.
Press ESC to exit.
"""
import cv2
def show_webcam(mirror: bool = False) -> None:
"""
Display webcam feed in a window.
Parameters
----------
mirror : bool, optional
If True, the webcam feed will be mirrored horizontally.
"""
# cam = cv2.VideoCapture(0, cv2.CAP_DSHOW) # CAP_DSHOW for Windows, improves stability
# cam = cv2.VideoCapture(0, cv2.CAP_AVFOUNDATION)
cam = cv2.VideoCapture(0)
if not cam.isOpened():
raise RuntimeError("Could not open webcam.")
try:
while True:
ret_val, frame = cam.read()
if not ret_val or frame is None:
print("Failed to capture frame.")
break
if mirror:
frame = cv2.flip(frame, 1)
cv2.imshow("Webcam Feed", frame)
# ESC key to quit
if cv2.waitKey(1) & 0xFF == 27:
break
finally:
cam.release()
cv2.destroyAllWindows()
def main() -> None:
"""Main entry point of the script."""
show_webcam(mirror=True)
if __name__ == "__main__":
main()