杰瑞科技汇

VideoCapture Python如何实现视频捕获?

Of course! Using VideoCapture in Python is a fundamental skill for any computer vision project. The primary library for this is OpenCV, which is the de facto standard.

VideoCapture Python如何实现视频捕获?-图1
(图片来源网络,侵删)

Here’s a comprehensive guide covering everything from basic setup to advanced use cases.

Installation

First, you need to install the OpenCV library. It's best to install it along with its high-level GUI module (contrib-python).

pip install opencv-contrib-python

Capturing Video from a Camera

The most common use case is to capture live video from your webcam. OpenCV provides the cv2.VideoCapture() class for this.

Basic Example: Displaying Live Video

VideoCapture Python如何实现视频捕获?-图2
(图片来源网络,侵删)

This code will open a window showing your webcam feed. Press any key to close the window.

import cv2
# Create a VideoCapture object to access the default camera (camera index 0)
cap = cv2.VideoCapture(0)
# Check if the camera opened successfully
if not cap.isOpened():
    print("Error: Could not open video device.")
    exit()
# Loop to continuously read frames from the camera
while True:
    # cap.read() returns a tuple (success, frame)
    # 'success' is a boolean indicating if the frame was read correctly
    # 'frame' is the actual image (a NumPy array)
    ret, frame = cap.read()
    # If the frame was not read, break the loop
    if not ret:
        print("Error: Failed to capture frame.")
        break
    # Display the frame in a window named 'Camera Feed'
    cv2.imshow('Camera Feed', frame)
    # Wait for a key press for 1 millisecond.
    # If the 'q' key is pressed, break the loop to exit
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
# Release the VideoCapture object to close the camera
cap.release()
# Close all OpenCV windows
cv2.destroyAllWindows()

Explanation:

  • cv2.VideoCapture(0): The 0 argument tells OpenCV to use the default webcam. If you have multiple cameras, you can try 1, 2, etc.
  • cap.read(): This method captures a single frame from the video source. It's crucial to call this inside a loop to get a continuous stream.
  • cv2.imshow(): Displays the captured frame in a window.
  • cv2.waitKey(1): This is essential for the window to be responsive. It waits for 1 millisecond for a key event. The & 0xFF is a bitwise AND operation to ensure we get the correct ASCII value of the key, which is good practice across different operating systems.
  • cap.release(): Very important! This releases the camera resource, allowing other applications to use it.
  • cv2.destroyAllWindows(): Closes all the windows created by OpenCV.

Capturing Video from a File

You can also use VideoCapture to process a pre-recorded video file (e.g., .mp4, .avi). The process is almost identical.

Example: Processing a Video File

VideoCapture Python如何实现视频捕获?-图3
(图片来源网络,侵删)
import cv2
# Create a VideoCapture object for a video file
# Make sure 'my_video.mp4' is in the same directory or provide the full path
cap = cv2.VideoCapture('my_video.mp4')
# Check if the video file opened successfully
if not cap.isOpened():
    print("Error: Could not open video file.")
    exit()
# Loop to read frames from the video
while cap.isOpened():
    ret, frame = cap.read()
    # If the frame was not read, it means we've reached the end of the video
    if not ret:
        print("Reached the end of the video.")
        break
    # Display the frame
    cv2.imshow('Video Playback', frame)
    # Wait for 25 milliseconds between frames (this approximates 40 fps)
    # Press 'q' to quit
    if cv2.waitKey(25) & 0xFF == ord('q'):
        break
# Release everything
cap.release()
cv2.destroyAllWindows()

Getting Video Properties

You can get useful information about the video source using the cap.get() method.

Common Properties:

  • cv2.CAP_PROP_FRAME_WIDTH: Width of the frames in the video stream.
  • cv2.CAP_PROP_FRAME_HEIGHT: Height of the frames.
  • cv2.CAP_PROP_FPS: Frames per second (fps).
  • cv2.CAP_PROP_FRAME_COUNT: Total number of frames in a video file.

Example: Printing Video Properties

import cv2
cap = cv2.VideoCapture(0) # Or a video file
if not cap.isOpened():
    print("Error: Could not open video source.")
    exit()
# Get and print properties
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
frame_count = cap.get(cv2.CAP_PROP_FRAME_COUNT)
print(f"Frame Width: {width}")
print(f"Frame Height: {height}")
print(f"FPS: {fps}")
print(f"Total Frames (for file): {frame_count}")
# ... rest of your capture loop ...
cap.release()
cv2.destroyAllWindows()

Writing/Saving Video (VideoWriter)

To save the video you are capturing, you need to use the cv2.VideoWriter class.

Example: Saving a Video File

import cv2
# --- Setup ---
cap = cv2.VideoCapture(0) # Capture from camera
# Get the width, height, and fps from the camera to use for the output video
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
# Define the codec and create a VideoWriter object
# 'XVID' is a popular codec for .avi files
# 'mp4v' is for .mp4 files
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, fps, (frame_width, frame_height))
# --- Capture and Write Loop ---
while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        print("Error: Failed to capture frame.")
        break
    # You can process the frame here, e.g., convert to grayscale
    # gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # out.write(gray_frame) # Write the processed frame
    # For this example, we just write the original frame
    out.write(frame)
    cv2.imshow('Recording', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
# --- Release ---
cap.release()
out.release() # Release the VideoWriter object
cv2.destroyAllWindows()
print("Video saved as output.avi")

Explanation of cv2.VideoWriter:

  • 'output.avi': The name of the output file.
  • cv2.VideoWriter_fourcc(*'XVID'): Specifies the codec. The unpacks the string into characters. Common codecs are XVID (for .avi), mp4v (for .mp4), and MJPG.
  • fps: The frames per second for the output video.
  • (frame_width, frame_height): The size of the frames. This must match the size of the frames you are writing!

Advanced: Drawing on Video Frames

A powerful feature is the ability to draw shapes and text directly onto the video frames in real-time.

Example: Drawing a Rectangle and Text

import cv2
cap = cv2.VideoCapture(0)
while True:
    ret, frame = cap.read()
    if not ret:
        break
    # Get frame dimensions
    height, width, _ = frame.shape
    # 1. Draw a rectangle (start_point, end_point, color, thickness)
    # Let's draw a box in the top-left corner
    cv2.rectangle(frame, (10, 10), (200, 100), (0, 255, 0), 2)
    # 2. Put text on the frame
    # (text, position, font, scale, color, thickness)
    cv2.putText(frame, 'Hello, OpenCV!', (50, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2)
    cv2.imshow('Frame with Drawings', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

Summary of Key Functions

Function Description
cv2.VideoCapture(index) Creates an object to capture video from a camera (index) or file (path).
cap.read() Reads a frame from the video source. Returns (success, frame).
cap.get(prop) Gets a property of the video source (e.g., width, height, fps).
cap.release() Releases the video resource.
cv2.imshow(name, frame) Displays a frame in a window.
cv2.waitKey(ms) Waits for a key press for ms milliseconds.
cv2.destroyAllWindows() Closes all OpenCV windows.
cv2.VideoWriter(...) Creates an object to write video frames to a file.
out.write(frame) Writes a frame to the output video file.
out.release() Releases the VideoWriter resource.
cv2.rectangle() Draws a rectangle on an image.
cv2.putText() Adds text to an image.
分享:
扫描分享到社交APP
上一篇
下一篇