Trouble: When using cv2.VideoCapture (), you have to manually enter the camera output settings.
Example
WIDTH = 100
HEIGHT = 100
FPS = 10
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, WIDTH)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, HEIGHT)
cap.set(cv2.CAP_PROP_FPS, FPS)
ret, frame = cap.read() #It works or it doesn't
Depending on the output settings, it may be automatically set to the resolution closest to the value or FPS, or an error may be thrown with cap.read (). I couldn't access the corresponding configuration information of the usb camera from opencv, so I get it from the v412-ctl command.
Command to output a list
import subprocess
import re
cmd = 'v4l2-ctl --device /dev/video0 --list-formats-ext'
proc = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
outs_bytes = proc.communicate()[0]
outs_str = outs_bytes.decode('utf-8')
outs_str_lists = outs_str.split('\n')
d = {}
i = 0
for line in outs_str_lists:
if "Pixel Format" in line:
pixelformat = line.split(":")[-1].strip()
if "Size:" in line:
resolution = line.split()[-1]
if "Interval" in line:
fps = re.findall("(?<=\().+?(?=\))",line)[0].split()[0]
_d = {"format":pixelformat,"height":resolution.split("x")[1],"width":resolution.split("x")[0],"fps":fps}
d[i] = _d
i +=1
print d
result
{0: {'format': "'MJPG' (compressed)",
'height': '2880',
'width': '3840',
'fps': '15.000'},
1: {'format': "'MJPG' (compressed)",
'height': '2880',
'width': '3840',
'fps': '10.000'},
2: {'format': "'MJPG' (compressed)",
'height': '2880',
'width': '3840',
'fps': '5.000'},
Now that we have obtained the corresponding settings as a dictionary type, it is possible to use the camera information as a variable inside the program.
Recommended Posts