-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera_helper.py
More file actions
93 lines (82 loc) · 3.57 KB
/
Copy pathcamera_helper.py
File metadata and controls
93 lines (82 loc) · 3.57 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
Camera Helper - Detect and Select Available Cameras
Copy functions ini ke script yang pakai webcam
"""
import cv2
def detect_available_cameras(max_cameras=5):
"""Detect available cameras and return list of camera info"""
available_cameras = []
print("\n🔍 Detecting available cameras...")
for camera_id in range(max_cameras):
cap = cv2.VideoCapture(camera_id)
if cap.isOpened():
ret, frame = cap.read()
if ret and frame is not None:
# Get camera properties
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
backend = cap.getBackendName()
# Common camera types
if camera_id == 0:
name = "Built-in Webcam / Default Camera"
elif camera_id == 1:
name = "External USB Camera / Secondary Camera"
else:
name = f"Camera Device {camera_id}"
camera_info = {
'id': camera_id,
'name': name,
'resolution': f"{width}x{height}",
'fps': fps,
'backend': backend
}
available_cameras.append(camera_info)
print(f" ✅ Camera {camera_id}: {name}")
print(f" Resolution: {width}x{height} | FPS: {fps} | Backend: {backend}")
cap.release()
else:
break
return available_cameras
def select_camera(available_cameras):
"""Let user select camera from available options"""
if len(available_cameras) == 0:
print("\n❌ No cameras detected!")
print("💡 Troubleshooting:")
print(" 1. Check if webcam is connected")
print(" 2. Close other apps using the camera (Zoom, Teams, etc.)")
print(" 3. Try reconnecting USB camera")
return None
if len(available_cameras) == 1:
cam = available_cameras[0]
print(f"\n✅ Found 1 camera:")
print(f" [{cam['id']}] {cam['name']}")
print(f" {cam['resolution']} @ {cam['fps']}fps")
while True:
confirm = input(f"\nUse this camera? (y/n): ").strip().lower()
if confirm == 'y':
print(f"✅ Selected: {cam['name']}")
return cam['id']
elif confirm == 'n':
print("❌ Camera selection cancelled")
return None
print(f"\n📹 Found {len(available_cameras)} cameras:")
for cam in available_cameras:
print(f" [{cam['id']}] {cam['name']}")
print(f" {cam['resolution']} @ {cam['fps']}fps | Backend: {cam['backend']}")
while True:
try:
choice = input(f"\nSelect camera number [0-{max([c['id'] for c in available_cameras])}]: ").strip()
camera_id = int(choice)
selected = next((c for c in available_cameras if c['id'] == camera_id), None)
if selected:
print(f"✅ Selected: {selected['name']}")
return camera_id
else:
available_ids = [c['id'] for c in available_cameras]
print(f"❌ Camera {camera_id} not available. Choose from: {available_ids}")
except ValueError:
print("❌ Please enter a valid number")
except KeyboardInterrupt:
print("\n❌ Camera selection cancelled")
return None