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.")
相关推荐
王哈哈^_^6 分钟前
PyTorch vs TensorFlow:从入门到落地的全方位对比
人工智能·pytorch·python·深度学习·计算机视觉·tensorflow·1024程序员节
刘逸潇200523 分钟前
Python并发编程
python
Victory_orsh1 小时前
“自然搞懂”深度学习系列(基于Pytorch架构)——02小试牛刀
人工智能·python·深度学习·神经网络·机器学习
tzc_fly1 小时前
DeepSeek-OCR:上下文光学压缩
人工智能·计算机视觉·ocr
Bruce-li__1 小时前
CI/CD流水线全解析:从概念到实践,结合Python项目实战
开发语言·python·ci/cd
2401_841495642 小时前
自然语言处理实战——英法机器翻译
人工智能·pytorch·python·深度学习·自然语言处理·transformer·机器翻译
gAlAxy...2 小时前
面试JAVASE基础(五)——Java 集合体系
java·python·面试·1024程序员节
夏玉林的学习之路3 小时前
Anaconda的常用指令
开发语言·windows·python
张可爱3 小时前
20251026-从网页 Console 到 Python 爬虫:一次 B 站字幕自动抓取的实践与复盘
前端·python
B站计算机毕业设计之家3 小时前
计算机视觉python口罩实时检测识别系统 YOLOv8模型 PyTorch 和PySide6界面 opencv (建议收藏)✅
python·深度学习·opencv·计算机视觉·cnn·1024程序员节