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.")
相关推荐
GG向前冲14 小时前
【Python 金融量化】线性模型在AAPL股票数据的分析研究
大数据·python·机器学习·ai·金融
deep_drink14 小时前
【论文精读(二十五)】PCM:Mamba 首次杀入 3D 点云,线性复杂度吊打 PTv3(ArXiv 2024)
深度学习·神经网络·计算机视觉·3d·pcm·point cloud
程序猿202314 小时前
Java Thread
java·开发语言·python
喵手14 小时前
Python爬虫零基础入门【第九章:实战项目教学·第8节】可观测性:日志规范 + trace_id + 可复现错误包!
爬虫·python·日志规范·python爬虫实战·python爬虫工程化实战·python爬虫零基础入门·可复性错误包
嫂子开门我是_我哥14 小时前
第五节:字符串处理大全:文本操作的“万能工具箱”
开发语言·python
独行soc14 小时前
2026年渗透测试面试题总结-10(题目+回答)
android·网络·python·安全·web安全·渗透测试·安全狮
aiguangyuan14 小时前
词向量的艺术:从Word2Vec到GloVe的完整实践指南
人工智能·python·nlp
嫂子的姐夫14 小时前
24-MD5:红人点集登录+凡客网登录
爬虫·python·逆向·小白逆向练手
不绝19114 小时前
MonoBehavior/GameObject/Time/Transform/位移/角度旋转/缩放看向/坐标转换
开发语言·python
一招定胜负14 小时前
opencv视频处理
人工智能·opencv·音视频