python读取mp4视频,读取摄像头代码

python读取mp4视频

python 复制代码
import cv2

# 读取视频文件
video_path = 'path_to_your_video.mp4'  # 将此处替换为你的MP4文件路径
cap = cv2.VideoCapture(video_path)

# 检查视频是否成功打开
if not cap.isOpened():
    print("Error: Could not open video.")
    exit()

# 播放视频
while True:
    ret, frame = cap.read()
    if not ret:
        break

    # 显示视频帧
    cv2.imshow('Video', frame)

    # 按 'q' 键退出播放
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# 释放视频捕获对象并关闭所有显示窗口
cap.release()
cv2.destroyAllWindows()

python读取摄像头

python 复制代码
import cv2

def capture_video(camera_index=0):
    """
    Capture video from the specified camera index (default is 0 for the built-in camera).
    Press 'q' to exit the video stream.
    """
    # 打开摄像头
    cap = cv2.VideoCapture(camera_index)

    if not cap.isOpened():
        print(f"Cannot open camera {camera_index}")
        return

    while True:
        # 读取帧
        ret, frame = cap.read()
        if not ret:
            print("Cannot receive frame (stream end?). Exiting ...")
            break

        # 显示帧
        cv2.imshow('Camera', frame)

        # 按下 'q' 键退出
        if cv2.waitKey(1) == ord('q'):
            break

    # 释放摄像头并关闭所有窗口
    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    capture_video(camera_index=0)  # 0 表示默认的笔记本自带摄像头

检查有几个可用摄像头

python 复制代码
import cv2

def list_cameras(max_cameras=10):
    """
    List available cameras.
    """
    available_cameras = []
    for index in range(max_cameras):
        cap = cv2.VideoCapture(index)
        if cap.isOpened():
            available_cameras.append(index)
            cap.release()
    return available_cameras

def select_camera(camera_index):
    """
    Open the specified camera and start capturing video.
    """
    cap = cv2.VideoCapture(camera_index)
    if not cap.isOpened():
        print(f"Cannot open camera {camera_index}")
        return

    while True:
        ret, frame = cap.read()
        if not ret:
            print("Cannot receive frame (stream end?). Exiting ...")
            break

        cv2.imshow(f'Camera {camera_index}', frame)
        if cv2.waitKey(1) == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    cameras = list_cameras()
    print(f"Available cameras: {cameras}")

    if cameras:
        selected_camera = cameras[0]  # You can change the index to select a different camera
        print(f"Using camera {selected_camera}")
        select_camera(selected_camera)
    else:
        print("No cameras found.")
相关推荐
阿_旭4 分钟前
基于YOLO11深度学习的运动品牌LOGO检测与识别系统【python源码+Pyqt5界面+数据集+训练代码】
人工智能·python·深度学习·毕业设计·logo检测
SomeB1oody5 分钟前
【Python机器学习】1.9. 逻辑回归实战(进阶):建立二阶边界模型
人工智能·python·机器学习·ai·逻辑回归
go546315846511 分钟前
简单的 Python 示例,用于生成电影解说视频的第一人称独白解说文案
开发语言·python
YueiL14 分钟前
OpenCV 颜色空间:原理与操作指南
python·opencv
Dmatteratall22 分钟前
目标检测热力图的生成代码(基于GridCam)生成的
人工智能·目标检测·计算机视觉
WenGyyyL1 小时前
使用OpenCV和MediaPipe库——增强现实特效(在手腕添加虚拟手表)
人工智能·opencv·计算机视觉·ar·cv·mediapipe
CoovallyAIHub1 小时前
一码难求的Manus,又对计算机视觉产生冲击?复刻开源版已在路上!
人工智能·深度学习·计算机视觉
databook2 小时前
manim边学边做--线性变换的场景类
python·动效
AmazingKO2 小时前
【够用就好008】开新坑自学esb32烧录进军物联网和嵌入式
人工智能·python·物联网·chatgpt·github·方方上土·aigc创意人竹相左边
rocksun2 小时前
UNITTEST: PYTHON开发者内置的安全网
python