-
Notifications
You must be signed in to change notification settings - Fork 322
/
test_cameras.py
executable file
·91 lines (75 loc) · 2.48 KB
/
test_cameras.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
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
#!/usr/bin/env python3
import cv2
import time
# get the installed camera list for initialization.
def get_cam_lst(cam_lst=range(0, 24)):
arr = []
for iCam in cam_lst:
cap = cv2.VideoCapture(iCam)
if not cap.read()[0]:
continue
else:
arr.append(iCam)
cap.release()
return arr
def show_cam_img(caps, cam_list):
print("INFO: Press 'q' to quit! Press 's' to save a picture, 'n' to change to next camera device!")
idx = 0
while True:
cap_device = caps[idx]
ret, frame = cap_device.read()
if ret:
cv2.imshow('video', frame)
else:
print("ERROR: failed read frame!")
# quit the test
c = cv2.waitKey(1)
if c == ord('q'):
break
# change to next camera device
if c == ord('n'):
idx += 1
if idx >= len(caps):
idx = 0
continue
# save the picture
if c == ord('s'):
if ret:
name = 'video{0}_{1}.png'.format(cam_list[idx],
time.strftime("%Y-%m-%d_%H:%M:%S", time.localtime()))
cv2.imwrite(name, frame)
print("saved file: %s!" %name)
cv2.destroyAllWindows()
def init_caps(cam_list, resolution=(1280,720)):
caps = []
for iCam in cam_list:
cap = cv2.VideoCapture(iCam)
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*"MJPG"))
cap.set(3, resolution[0])
cap.set(4, resolution[1])
caps.append(cap)
return caps
def deinit_caps(cap_list):
for cap in cap_list:
cap.release()
def show_cameras(video_list=None):
if video_list == None:
print("Start to search all available camera devices, please wait... ")
cam_list = get_cam_lst()
err_msg = "cannot find any video device!"
else:
cam_list = get_cam_lst(video_list)
err_msg = "cannot find available video device in list: {0}!".format(video_list) +\
"\nPlease check the video devices in /dev/v4l/by-path/ folder!"
if len(cam_list) < 1:
print("ERROR: " + err_msg)
return
print("Available video device list is {}".format(cam_list))
caps = init_caps(cam_list)
show_cam_img(caps, cam_list)
deinit_caps(caps)
if __name__ == "__main__":
# User can specify the video list here.
#show_cameras([2, 6, 10, 14])
# Or search all available video devices automatically.
show_cameras()