OpenCV Python 版使用教程(三)摄像头读取延迟解决方法

文章目录


一、上篇回顾

在上一篇中,简单介绍了如何使用 OpenCV 操作摄像头,本期来讲在摄像头读取时,可能会出现画面延迟的解决方法。

二、产生原因

OpenCV 在读取的时候,会将视频流放在缓冲区中,然后每次调用的时候,会从缓冲区内读取视频帧。虽然说大多数情况难以出现写入缓冲区远大于读取缓冲区的速度,但是当设备出现性能瓶颈,尤其是在 树莓派 等嵌入式设备中,容易出现CPU 瓶颈,从而引发问题。

三、解决方法

1. cap.grab() 方法

cpp 复制代码
virtual bool cv::VideoCapture::grab()

官网对于这个函数的描述如下:

复制代码
The primary use of the function is in multi-camera environments, especially when the cameras do not have hardware synchronization. 
That is, you call VideoCapture::grab() for each camera and after that call the slower method VideoCapture::retrieve()
to decode and get frame from each camera. This way the overhead on demosaicing or motion jpeg
decompression etc. is eliminated and the retrieved frames from different cameras will be closer in time. 

从官网的描述来看,这个函数主要用在多个摄像机中,用于摄像头硬件同步,这个函数和 cap.retrieve() 配合使用。当然也可以使用 cap.read() 再次读取。

同时也可以进一步设置缓冲区的大小,代码如下:

python 复制代码
cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)

在这里设置 Capture 的缓冲区大小为 1 帧。

2. 多线程法

python3 复制代码
import cv2
import threading

class VideoCaptureThread:
    def __init__(self, index=0):
        self.cap = cv2.VideoCapture(index)
        self.frame = None
        self.running = True
        self.lock = threading.Lock()
        self.thread = threading.Thread(target=self.update)
        self.thread.start()

    def update(self):
        while self.running:
            ret, frame = self.cap.read()
            if ret:
                with self.lock:  # 确保线程安全
                    self.frame = frame

    def read(self):
        with self.lock:  # 确保线程安全
            return self.frame

    def stop(self):
        self.running = False
        self.thread.join()
        self.cap.release()


# 使用示例
video_capture = VideoCaptureThread()

while True:
    frame = video_capture.read()
    if frame is not None:
        cv2.imshow('Frame', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

video_capture.stop()
cv2.destroyAllWindows()

在本例中,读取视频帧放在了一个单独的线程,读取和处理分别单独运行,从而避免了两者速度不一致导致的读取延迟。


总结

本篇介绍了两种解决摄像头延迟的方法。在读取到图像后,接下来要做的就是图像处理了,下期将介绍图像的各种色彩空间。

相关推荐
前端若水6 小时前
会话管理:创建、切换、删除对话历史
前端·人工智能·python·react.js
Upsy-Daisy6 小时前
AI Agent 项目学习笔记(八):Tool Calling 工具调用机制总览
人工智能·笔记·学习
企学宝6 小时前
企学宝5月专题课程丨《OpenClaw AI 智能体实战营:从零基础部署到全场景自动化落地》
人工智能·ai·企业培训
冬奇Lab7 小时前
让 AI Agent 更可靠:Harness Engineering 与多 Agent 系统工程实践
人工智能·llm·agent
放下华子我只抽RuiKe57 小时前
React 从入门到生产(四):自定义 Hook
前端·javascript·人工智能·深度学习·react.js·自然语言处理·前端框架
想你依然心痛7 小时前
HarmonyOS 6(API 23)实战:基于悬浮导航、沉浸光感与HMAF的“文思智脑“——PC端AI智能体沉浸式智能写作工作台
人工智能·ar·harmonyos·ai写作
冬奇Lab7 小时前
一天一个开源项目(第108篇):Andrej Karpathy Skills - 用一个 CLAUDE.md 文件修复 LLM 编码的四个顽疾
人工智能·开源·资讯
涛声依旧-底层原理研究所7 小时前
残差连接与层归一化通俗易懂的详解
人工智能·python·神经网络·transformer
csdn_aspnet7 小时前
Python 算法快闪 LeetCode 编号 70 - 爬楼梯
python·算法·leetcode·职场和发展