[PYTHON] Get a list of camera parameters that can be set with cv2.VideoCapture and make it a dictionary type

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

Get a list of camera parameters that can be set with cv2.VideoCapture and make it a dictionary type
Color list that can be set with tkinter (memorial)
Get the value of a specific key in a list from the dictionary type in the list with Python
Let's make a diagram that can be clicked with IPython
[Python] Make a graph that can be moved around with Plotly
Make a Spinbox that can be displayed in Binary with Tkinter
Make a Spinbox that can be displayed in HEX with Tkinter
Make a currency chart that can be moved around with Plotly (1)
Draw a graph that can be moved around with HoloViews and Bokeh
Article that can be a human resource who understands and masters the mechanism of API (with Python code)
Get a list of IAM users with Boto3
List packages that can be updated with pip
Get the stock price of a Japanese company with Python and make a graph
How to write a list / dictionary type of Python3
A server that returns the number of people in front of the camera with bottle.py and OpenCV
Easily make a TweetBot that notifies you of temperature and humidity with Raspberry Pi + DHT11.
Get a list of CloudWatch Metrics and a correspondence table for Unit units with Python boto
[Python] A program to find the number of apples and oranges that can be harvested
Format summary of formats that can be serialized with gensim
It seems that Skeleton Tracking can be done with RealSense
NumPy zeros can be defined even with a size of 0
Get a list of purchased DMM eBooks with Python + Selenium
Python: Create a dictionary from a list of keys and values
[Python] Draw elevation data on a sphere with Plotly and draw a globe that can be rotated round and round
Understand the probabilities and statistics that can be used for progress management with a python program
A list of functions that I came across with 100 Numpy knocks and thought "This is convenient!"
It is surprisingly troublesome to get a list of the last login date and time of Workspaces
[Python] A program that finds the maximum number of toys that can be purchased with your money
A story and its implementation that arbitrary a1 * a2 data can be represented by a 3-layer ReLU neural network with a1 and a2 intermediate neurons with an error of 0.
How to make a surveillance camera (Security Camera) with Opencv and Python
Make a thermometer with Raspberry Pi and make it viewable with a browser Part 4
I made a shuffle that can be reset (reverted) with Python
What is a dog? Volume of GET request and query parameters
Comparison of 4 styles that can be passed to seaborn with set_context
Get a list of files in a folder with python without a path
I wrote a tri-tree that can be used for high-speed dictionary implementation in D language and Python.
It seems that some RHEL will be free with a big boo for the end of CentOS
[Python] How to create a dictionary type list, add / change / delete elements, and extract with a for statement
How to make a rock-paper-scissors bot that can be easily moved (commentary)
Get a list of packages installed in your current environment with python
Until you get a snapshot of Amazon Elasticsearch service and restore it
Make a Discord Bot that you can search for and paste images
Set up a Lambda function and let it work with S3 events!
Maximum number of function parameters that can be defined in each language
[Personal memo] Get data on the Web and make it a DataFrame
The story of making a sound camera with Touch Designer and ReSpeaker
[Python] Creating a tool that can list, select, and execute python files with tkinter & about the part that got caught