-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera_detect.py
More file actions
104 lines (85 loc) · 3.21 KB
/
camera_detect.py
File metadata and controls
104 lines (85 loc) · 3.21 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
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python3
"""
Camera Detection Script - Find all available cameras
"""
import cv2
import sys
def find_cameras():
"""Test different camera indices to find available cameras"""
available_cameras = []
print("Searching for available cameras...")
# Test camera indices 0-10 (usually sufficient)
for i in range(11):
cap = cv2.VideoCapture(i)
if cap.isOpened():
ret, frame = cap.read()
if ret:
height, width = frame.shape[:2]
available_cameras.append({
'index': i,
'width': width,
'height': height
})
print(f"Camera {i}: Found - Resolution: {width}x{height}")
else:
print(f"Camera {i}: Opened but no frame received")
cap.release()
else:
print(f"Camera {i}: Not available")
return available_cameras
def test_camera(camera_index):
"""Test a specific camera with live preview"""
cap = cv2.VideoCapture(camera_index)
if not cap.isOpened():
print(f"Cannot open camera {camera_index}")
return False
print(f"Testing camera {camera_index}. Press 'q' to quit, 's' to save frame")
frame_count = 0
try:
while True:
ret, frame = cap.read()
if not ret:
print("Failed to grab frame")
break
frame_count += 1
# Add text overlay
cv2.putText(frame, f"Camera {camera_index} - Frame {frame_count}",
(10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
try:
cv2.imshow(f'Camera {camera_index} Test', frame)
except cv2.error:
# If display fails, save frame instead
if frame_count % 30 == 0: # Save every 30th frame
filename = f'camera_{camera_index}_test_frame_{frame_count}.jpg'
cv2.imwrite(filename, frame)
print(f"Saved {filename}")
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
elif key == ord('s'):
filename = f'camera_{camera_index}_saved_frame.jpg'
cv2.imwrite(filename, frame)
print(f"Frame saved as {filename}")
except KeyboardInterrupt:
print("\nInterrupted by user")
finally:
cap.release()
cv2.destroyAllWindows()
return True
if __name__ == "__main__":
if len(sys.argv) > 1:
try:
camera_index = int(sys.argv[1])
test_camera(camera_index)
except ValueError:
print("Please provide a valid camera index number")
else:
cameras = find_cameras()
if cameras:
print(f"\nFound {len(cameras)} available camera(s):")
for cam in cameras:
print(f" Camera {cam['index']}: {cam['width']}x{cam['height']}")
print(f"\nTo test a specific camera, run:")
print(f"python3 {sys.argv[0]} <camera_index>")
else:
print("No cameras found!")