Python – 在子线程中使用OpenCV异步读取摄像头视频帧传递到主线程中进行处理
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:Python – 在子线程中使用OpenCV异步读取摄像头视频帧传递到主线程中进行处理
原文链接:https://www.stubbornhuang.com/2318/
发布于:2022年08月19日 8:36:20
修改于:2022年08月19日 8:36:20

一般情况下,我们会在主线程中直接使用opencv打开摄像头然后读取的视频帧进行操作,这种方式是阻塞的,如果我们想要在主线程读取摄像头视频帧的时候还进行其他的操作会非常不方便。这个时候有必要将读取摄像头视频帧的这一部分代码移动到子线程中,然后通过队列使得主线程可以获取摄像头的视频帧,同时也不会阻塞主线程。
1 子线程使用OpenCV读取摄像头视频帧
import cv2
import threading
from queue import Queue
class ReadCameraThread(threading.Thread):
def __init__(self, video_queue):
super(ReadCameraThread).__init__()
threading.Thread.__init__(self)
self.read_capture = cv2.VideoCapture(0)
self.video_queue = video_queue
self.running = True
def run(self):
if not self.read_capture.isOpened():
print('未检测到摄像头')
exit(0)
while self.read_capture.isOpened() and self.running:
ret, frame = self.read_capture.read()
if not ret:
break
self.video_queue.put(frame)
print('put the frame')
self.read_capture.release()
cv2.destroyAllWindows()
def stop_read_camera(self):
self.running = False
2 主线程中从缓存队列读取视频帧
class StreamingOnlineRecognize:
def __init__(self):
self.video_queue = Queue()
self.video_stream_in = ReadCameraThread(self.video_queue)
self.video_stream_in.start()
def start(self):
self._running = True
while self._running:
if not self.video_queue.empty():
frame = self.video_queue.get()
print('get the frame')
if frame is not None:
cv2.imshow('test', frame)
cv2.waitKey(1)
else:
print('the size of self.video_queue is <= 0')
cv2.destroyAllWindows()
self.video_stream_in.join()
3 完整示例
# -*- coding: utf-8 -*-
import cv2
import threading
from queue import Queue
class ReadCameraThread(threading.Thread):
def __init__(self, video_queue):
super(ReadCameraThread).__init__()
threading.Thread.__init__(self)
self.read_capture = cv2.VideoCapture(0)
self.video_queue = video_queue
self.running = True
def run(self):
if not self.read_capture.isOpened():
print('未检测到摄像头')
exit(0)
while self.read_capture.isOpened() and self.running:
ret, frame = self.read_capture.read()
if not ret:
break
self.video_queue.put(frame)
print('put the frame')
self.read_capture.release()
cv2.destroyAllWindows()
def stop_read_camera(self):
self.running = False
class StreamingOnlineRecognize:
def __init__(self):
self.video_queue = Queue()
self.video_stream_in = ReadCameraThread(self.video_queue)
self.video_stream_in.start()
def start(self):
self._running = True
while self._running:
if not self.video_queue.empty():
frame = self.video_queue.get()
print('get the frame')
if frame is not None:
cv2.imshow('test', frame)
cv2.waitKey(1)
else:
print('the size of self.video_queue is <= 0')
cv2.destroyAllWindows()
self.video_stream_in.join()
if __name__ == '__main__':
streaming_online_recognize = StreamingOnlineRecognize()
streaming_online_recognize.start()
当前分类随机文章推荐
- Python3爬虫 - 下载反盗链图片的方式 阅读2711次,点赞1次
- Python - 语音识别文本相似性度量库jiwer,可计算文字错误率WER、匹配错误率MER等相似性度量指标 阅读1225次,点赞0次
- Python - opencv-python统计一个文件夹以及所有子文件夹下所有视频的帧率和帧数 阅读231次,点赞0次
- Python - ModuleNotFoundError: No module named 'absl' 阅读2108次,点赞0次
- Python - 使用websockets库构建websocket服务器 阅读1535次,点赞0次
- Python - BeautifulSoup的find()和findAll() 阅读2729次,点赞0次
- Python - 使用jsonpickle库对Python类对象进行json序列化和json反序列化操作 阅读3003次,点赞0次
- Python - 类对象/列表/元祖/字典判空的方法 阅读2163次,点赞0次
- Python - 普通函数/lambda匿名函数/类成员函数作为回调函数的用法 阅读1925次,点赞0次
- Python - 使用命令行调用ffmpeg修改视频帧率,将60FPS的视频修改为30FPS的视频,视频时间保持不变 阅读303次,点赞0次
全站随机文章推荐
- human3.6m : Download(数据集下载) 阅读24645次,点赞39次
- WordPress - 获取某个用户发表的评论数量 阅读1828次,点赞0次
- C++11/std::shared_ptr - 循环引用问题 阅读4000次,点赞0次
- WordPress - 升级WordPress5.8后切换回旧版的小工具管理页面 阅读1473次,点赞0次
- WordPress - 禁用XML-RPC接口,禁止访问xmlrpc.php,避免DDOS攻击,防止暴力破解 阅读2690次,点赞0次
- Windows - OneDrive登录出现0x8019001错误 阅读206次,点赞0次
- VTK - 冠脉重建点匹配坐标数据下载 阅读3699次,点赞5次
- 资源分享 - Curves and Surfaces for Computer Graphics 英文高清PDF下载 阅读1007次,点赞0次
- Unity - Color32[]转IntPtr 阅读2877次,点赞1次
- NCNN - Windows编译NCNN项目出现无法打开包括文件vulkan.h的错误 阅读24次,点赞0次
评论
167