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()

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


总结

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

相关推荐
2501_9333295521 分钟前
媒介宣发技术实践:Infoseek舆情系统的AI中台架构与应用解析
开发语言·人工智能·架构·数据库开发
热爱生活的五柒1 小时前
026主流三大模型(GPT / Gemini / Claude Code)总结
人工智能·gpt
DuHz1 小时前
论文精读:大语言模型 (Large Language Models, LLM) —— 一项调查
论文阅读·人工智能·深度学习·算法·机器学习·计算机视觉·语言模型
AI木马人1 小时前
9.【AI任务队列实战】如何在高并发下保证系统不崩?(Redis + Celery完整方案)
数据库·人工智能·redis·神经网络·缓存
陈天伟教授1 小时前
GPT Image 2-桂林山水
人工智能·神经网络·安全·架构
offer收割机小鹅1 小时前
大学生求职必备:AI面试、AI写作与设计工具助力职场发展
人工智能·ai·面试·aigc·ai写作
茅盾体1 小时前
汽车零件订单自动同步系统方案
python
2401_883600252 小时前
golang如何理解weak pointer弱引用_golang weak pointer弱引用总结
jvm·数据库·python
FreakStudio2 小时前
和做工厂系统的印尼老哥,复刻了一套属于 MicroPython 的包管理系统
python·单片机·嵌入式·大学生·面向对象·并行计算·电子diy·电子计算机
乔江seven2 小时前
【李沐 | 动手学深度学习】20 计算机视觉:数据增广(Data Augmentation)
人工智能·深度学习