- Mastering OpenCV 4 with Python
- Alberto Fernández Villán
- 261字
- 2021-07-02 12:07:17
Accessing some properties of the capture object
Finally, you can access some properties of the capture object using capture.get(property_identifier). In this case, we get some properties, such as frame width, frame height, and frames per second (fps). If we call a property that is not supported, the returned value will be 0:
# Import the required packages
import cv2
import argparse
# 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 'index_camera' argument using add_argument() including a help.
parser.add_argument("index_camera", help="index of the camera to read from", type=int)
args = parser.parse_args()
# We create a VideoCapture object to read from the camera (pass 0):
capture = cv2.VideoCapture(args.index_camera)
# Get some properties of VideoCapture (frame width, frame height and frames per second (fps)):
frame_width = capture.get(cv2.CAP_PROP_FRAME_WIDTH)
frame_height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
fps = capture.get(cv2.CAP_PROP_FPS)
# Print these values:
print("CV_CAP_PROP_FRAME_WIDTH: '{}'".format(frame_width))
print("CV_CAP_PROP_FRAME_HEIGHT : '{}'".format(frame_height))
print("CAP_PROP_FPS : '{}'".format(fps))
# Check if camera opened successfully
if capture.isOpened()is False:
print("Error opening the camera")
# Read until video is completed
while capture.isOpened():
# Capture frame-by-frame from the camera
ret, frame = capture.read()
if ret is True:
# Display the captured frame:
cv2.imshow('Input frame from the camera', frame)
# Convert the frame captured from the camera to grayscale:
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the grayscale frame:
cv2.imshow('Grayscale input camera', gray_frame)
# Press q on keyboard to exit the program
if cv2.waitKey(20) & 0xFF == ord('q'):
break
# Break the loop
else:
break
# Release everything:
capture.release()
cv2.destroyAllWindows()
推薦閱讀
- UML和模式應(yīng)用(原書(shū)第3版)
- DevOps with Kubernetes
- C#程序設(shè)計(jì)教程
- 征服RIA
- Python Data Analysis(Second Edition)
- C#程序設(shè)計(jì)基礎(chǔ):教程、實(shí)驗(yàn)、習(xí)題
- Python算法詳解
- C語(yǔ)言程序設(shè)計(jì)
- C++寶典
- Visualforce Developer’s guide
- Microsoft 365 Certified Fundamentals MS-900 Exam Guide
- Spring+Spring MVC+MyBatis從零開(kāi)始學(xué)
- C語(yǔ)言程序設(shè)計(jì)與應(yīng)用(第2版)
- Mastering ASP.NET Core 2.0
- WCF編程(第2版)