Skip to content

Commit

Permalink
test_cameras.py: add a python script for simple camera devices test
Browse files Browse the repository at this point in the history
The script will show the camera video in a window. It supports multiple
cameras. Also it find available camera devices automatically.

Usage:
  Press 'q' to quit.
  press 'n' to change to next camera device.
  Press 's' to save current picture in current folder.

Signed-off-by: JoshWu <rainyfeeling@outlook.com>
  • Loading branch information
JoshWu committed Jan 13, 2021
1 parent 7add3f3 commit cfcbea6
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions test_cameras.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/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()

0 comments on commit cfcbea6

Please sign in to comment.