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

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.

主站蜘蛛池模板: 岳西县| 含山县| 若尔盖县| 治多县| 永春县| 云安县| 曲周县| 莱西市| 远安县| 临高县| 淳化县| 兴仁县| 梧州市| 库伦旗| 兴和县| 海晏县| 阿巴嘎旗| 得荣县| 喀喇| 灵丘县| 塘沽区| 恭城| 什邡市| 昌宁县| 会昌县| 南靖县| 海盐县| 博客| 林西县| 贵州省| 邹平县| 文化| 屏东市| 平江县| 大城县| 边坝县| 上犹县| 新沂市| 北辰区| 荆门市| 台湾省|