-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautocamera.py
48 lines (37 loc) · 1.22 KB
/
autocamera.py
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
import cv2
import matplotlib.pyplot as plt
def get_available_camera_indices(max_check=10):
available_indices = []
for index in range(max_check):
cap = cv2.VideoCapture(index)
ret, _ = cap.read()
if ret:
available_indices.append(index)
cap.release()
return available_indices
camera_indices = get_available_camera_indices()
camera_indices = camera_indices[:5]
caps = [cv2.VideoCapture(index) for index in camera_indices]
rows = len(camera_indices) // 5 + (len(camera_indices) % 5 > 0)
fig, axs = plt.subplots(rows, min(len(camera_indices), 5))
if rows == 1 or len(camera_indices) == 1:
axs = [axs] # Ensure axs is always a list of lists.
try:
while True:
for idx, cap in enumerate(caps):
ret, frame = cap.read()
if ret:
row, col = divmod(idx, 5)
axs[row][col].imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
axs[row][col].axis('off')
plt.pause(0.01)
for ax_row in axs:
for ax in ax_row:
ax.cla()
if cv2.waitKey(1) & 0xFF == ord('q'):
break
finally:
for cap in caps:
cap.release()
cv2.destroyAllWindows()
plt.close()