-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_cams.py
More file actions
51 lines (40 loc) · 1.45 KB
/
Copy pathlist_cams.py
File metadata and controls
51 lines (40 loc) · 1.45 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
"""List available camera devices by probing indices.
On Windows this uses the DirectShow backend which gives more reliable results
for local webcams vs virtual cameras.
Usage:
python list_cams.py --max 8
Then run an exercise with the chosen index, e.g.:
python PushUp.py --time 30 --cam 1
"""
import argparse
import cv2
def probe(index, backend=cv2.CAP_DSHOW):
cap = cv2.VideoCapture(index, backend)
ok, frame = False, None
if cap is None or not cap.isOpened():
return False, None
try:
ok, frame = cap.read()
finally:
cap.release()
return ok, frame
def main():
parser = argparse.ArgumentParser(description='Probe camera indices')
parser.add_argument('--max', type=int, default=8, help='Maximum index to probe')
args = parser.parse_args()
found = []
print(f"Probing camera indices 0..{args.max - 1} (DirectShow backend)...")
for i in range(args.max):
ok, frame = probe(i)
if ok and frame is not None:
h, w = frame.shape[:2]
print(f"Index {i}: OK - resolution {w}x{h}")
found.append(i)
else:
print(f"Index {i}: no capture")
if not found:
print("No camera devices detected. If you expect a webcam, try unplugging virtual cameras or check OS privacy settings.")
else:
print("\nTry running e.g. `python PushUp.py --cam <index>` with the correct index from above.")
if __name__ == '__main__':
main()