This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Example to introduce how to read a video file and get all properties | |
""" | |
# Import the required packages: | |
import cv2 | |
import argparse | |
def decode_fourcc(fourcc): | |
"""Decodes the fourcc value to get the four chars identifying it""" | |
# Convert to int: | |
fourcc_int = int(fourcc) | |
# We print the int value of fourccprint("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 | |
# We first create the ArgumentParser object# The created object 'parser' will have the necessary information | |
# to parse the command-line arguments into data types. | |
parser = argparse.ArgumentParser() | |
# We add 'video_path' argument using add_argument() including a help. | |
parser.add_argument("video_path", help="path to the video file") | |
args = parser.parse_args() | |
# Create a VideoCapture object and read from input file# If the input is the camera, pass 0 instead of the video file name | |
capture = cv2.VideoCapture(args.video_path) | |
# 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))) | |
# Check if camera opened successfully | |
if capture.isOpened() is False: | |
print("Error opening video stream or file") | |
# Read until video is completed | |
while capture.isOpened(): | |
# Capture frame-by-frame-by-f | |
ret, frame = capture.read() | |
if ret is True: | |
# Print current frame number per iteration | |
print("CAP_PROP_POS_FRAMES : '{}'".format(capture.get(cv2.CAP_PROP_POS_FRAMES))) | |
# Get the timestamp of the current frame in milliseconds | |
print("CAP_PROP_POS_MSEC : '{}'".format(capture.get(cv2.CAP_PROP_POS_MSEC))) | |
# Display the resulting frame | |
cv2.imshow('Original frame', frame) | |
# Convert the frame to grayscale: | |
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
# Display the grayscale frame | |
cv2.imshow('Grayscale frame', gray_frame) | |
# Press q on keyboard to exit | |
if cv2.waitKey(25) & 0xFF == ord('q'): | |
break# Break the loop | |
else: | |
break | |
# Release everything: | |
capture.release() | |
cv2.destroyAllWindows() |
0 Comments