官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 浦江县| 望都县| 邓州市| 廊坊市| 莱芜市| 西平县| 黄浦区| 临沂市| 牙克石市| 西乌珠穆沁旗| 汝州市| 闸北区| 宜君县| 邵阳市| 汕头市| 游戏| 金堂县| 德清县| 瓮安县| 彰武县| 海原县| 海宁市| 弥渡县| 呼伦贝尔市| 万州区| 太原市| 舒兰市| 遂溪县| 金塔县| 台安县| 定南县| 扶沟县| 白玉县| 通海县| 丁青县| 凤庆县| 宁阳县| 南康市| 明溪县| 珲春市| 蓬莱市|