- Mastering OpenCV 4 with Python
- Alberto Fernández Villán
- 426字
- 2021-07-02 12:07:18
Getting all the properties from the video capture object
First, we create the read_video_file_all_properties.py script to show all the properties. Some of these properties only work when we're working with cameras (not with video files). In these cases, a 0 value is returned. Additionally, we have created the decode_fourcc() function, which converts the value that's returned by capture.get(cv2.CAP_PROP_FOURCC) as a string value that contains the int representation of the codec. In this sense, this value should be converted into a four-byte char representation to output the codec properly. Therefore, the decode_fourcc() function copes with this.
The code of this function is given as follows:
def decode_fourcc(fourcc):
"""Decodes the fourcc value to get the four chars identifying it
"""
fourcc_int = int(fourcc)
# We print the int value of fourcc
print("int value of fourcc: '{}'".format(fourcc_int))
# We can also perform this in one line:
# return "".join([chr((fourcc_int >> 8 * i) & 0xFF) for i in range(4)])
fourcc_decode = ""
for i in range(4):
int_value = fourcc_int >> 8 * i & 0xFF
print("int_value: '{}'".format(int_value))
fourcc_decode += chr(int_value)
return fourcc_decode
To explain how it works, the following diagram summarizes the main steps:

As you can see, the first step is to obtain the int representation of the value that's returned by capture.get(cv2.CAP_PROP_FOURCC), which is a string. Then, we iterate four times to get every eight bits and convert these eight bits into int. Finally, these int values are converted into char using the chr() function. It should be noted that we can perform this function in only one line of code, as follows:
return "".join([chr((fourcc_int >> 8 * i) & 0xFF) for i in range(4)])
The CAP_PROP_POS_FRAMES property gives you the current frame of the video file and the CAP_PROP_POS_MSEC property gives you the timestamp of the current frame. We can also get the number of fps with the CAP_PROP_FPS property. The CAP_PROP_FRAME_COUNT property gives you the total number of frames of the video file.
To get and print all the properties, use the following code:
# Get and print these values:
print("CV_CAP_PROP_FRAME_WIDTH: '{}'".format(capture.get(cv2.CAP_PROP_FRAME_WIDTH)))
print("CV_CAP_PROP_FRAME_HEIGHT : '{}'".format(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
print("CAP_PROP_FPS : '{}'".format(capture.get(cv2.CAP_PROP_FPS)))
print("CAP_PROP_POS_MSEC : '{}'".format(capture.get(cv2.CAP_PROP_POS_MSEC)))
print("CAP_PROP_POS_FRAMES : '{}'".format(capture.get(cv2.CAP_PROP_POS_FRAMES)))
print("CAP_PROP_FOURCC : '{}'".format(decode_fourcc(capture.get(cv2.CAP_PROP_FOURCC))))
print("CAP_PROP_FRAME_COUNT : '{}'".format(capture.get(cv2.CAP_PROP_FRAME_COUNT)))
print("CAP_PROP_MODE : '{}'".format(capture.get(cv2.CAP_PROP_MODE)))
print("CAP_PROP_BRIGHTNESS : '{}'".format(capture.get(cv2.CAP_PROP_BRIGHTNESS)))
print("CAP_PROP_CONTRAST : '{}'".format(capture.get(cv2.CAP_PROP_CONTRAST)))
print("CAP_PROP_SATURATION : '{}'".format(capture.get(cv2.CAP_PROP_SATURATION)))
print("CAP_PROP_HUE : '{}'".format(capture.get(cv2.CAP_PROP_HUE)))
print("CAP_PROP_GAIN : '{}'".format(capture.get(cv2.CAP_PROP_GAIN)))
print("CAP_PROP_EXPOSURE : '{}'".format(capture.get(cv2.CAP_PROP_EXPOSURE)))
print("CAP_PROP_CONVERT_RGB : '{}'".format(capture.get(cv2.CAP_PROP_CONVERT_RGB)))
print("CAP_PROP_RECTIFICATION : '{}'".format(capture.get(cv2.CAP_PROP_RECTIFICATION)))
print("CAP_PROP_ISO_SPEED : '{}'".format(capture.get(cv2.CAP_PROP_ISO_SPEED)))
print("CAP_PROP_BUFFERSIZE : '{}'".format(capture.get(cv2.CAP_PROP_BUFFERSIZE)))
You can view the full code of this script in the read_video_file_all_properties.py file.
- Getting Started with Gulp(Second Edition)
- Visual Studio 2012 Cookbook
- 零基礎(chǔ)學C++程序設計
- Python入門很簡單
- 深入淺出Spring Boot 2.x
- Access 2010數(shù)據(jù)庫應用技術(shù)(第2版)
- Raspberry Pi Home Automation with Arduino(Second Edition)
- HTML5 APP開發(fā)從入門到精通(微課精編版)
- Android系統(tǒng)下Java編程詳解
- C# 7.0本質(zhì)論
- HTML5程序開發(fā)范例寶典
- Learning Dynamics NAV Patterns
- JavaScript高級程序設計(第4版)
- 熱處理常見缺陷分析與解決方案
- IPython Notebook Essentials