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

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.

主站蜘蛛池模板: 瑞昌市| 成武县| 湟中县| 静安区| 苍山县| 迁西县| 南充市| 通江县| 广饶县| 莒南县| 乌拉特中旗| 绵阳市| 镇江市| 双柏县| 常德市| 涞源县| 南丹县| 塔河县| 临桂县| 铁岭县| 南木林县| 绥中县| 芒康县| 仁化县| 长子县| 都匀市| 佛坪县| 集安市| 托克托县| 从化市| 韶山市| 十堰市| 永平县| 海安县| 公主岭市| 巴青县| 永丰县| 昌宁县| 乌兰浩特市| 南安市| 桐庐县|