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.")
相关推荐
CodeCraft Studio1 小时前
CAD文件处理控件Aspose.CAD教程:使用 Python 将绘图转换为 Photoshop
python·photoshop·cad·aspose·aspose.cad
Python×CATIA工业智造3 小时前
Frida RPC高级应用:动态模拟执行Android so文件实战指南
开发语言·python·pycharm
千宇宙航3 小时前
闲庭信步使用SV搭建图像测试平台:第三十一课——基于神经网络的手写数字识别
图像处理·人工智能·深度学习·神经网络·计算机视觉·fpga开发
onceco3 小时前
领域LLM九讲——第5讲 为什么选择OpenManus而不是QwenAgent(附LLM免费api邀请码)
人工智能·python·深度学习·语言模型·自然语言处理·自动化
狐凄4 小时前
Python实例题:基于 Python 的简单聊天机器人
开发语言·python
悦悦子a啊5 小时前
Python之--基本知识
开发语言·前端·python
whoarethenext6 小时前
使用 C++/OpenCV 和 MFCC 构建双重认证智能门禁系统
开发语言·c++·opencv·mfcc
jndingxin6 小时前
OpenCV CUDA模块设备层-----高效地计算两个 uint 类型值的带权重平均值
人工智能·opencv·计算机视觉
笑稀了的野生俊7 小时前
在服务器中下载 HuggingFace 模型:终极指南
linux·服务器·python·bash·gpu算力
Naiva7 小时前
【小技巧】Python+PyCharm IDE 配置解释器出错,环境配置不完整或不兼容。(小智AI、MCP、聚合数据、实时新闻查询、NBA赛事查询)
ide·python·pycharm