代码拉取完成,页面将自动刷新
#!/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(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()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。