一种在 Python 中实现更快 OpenCV 视频流的多线程方法

news2024/10/4 23:16:14

e65c17077560cdad5e035db137dad9ba.png

概述

在本文中,我们将看到两个没有多线程的 Python 代码示例,用于从摄像头读取视频帧。我们将看到使用/不使用多线程获得的 FPS 的差异。

什么是多线程?

线程是进程中的一个执行单元。多线程是指通过在线程之间快速切换对 CPU 的控制(称为上下文切换)来并发执行多个线程。在我们的示例中,我们将看到多线程通过提高 FPS(每秒帧数)实现更快的实时视频处理。

Python中的线程基础

以下代码片段显示了如何使用python 中的threading模块创建线程:

# importing the threading module 
import threading 

# importing the time module 
import time

# Function to print "Hello", however, the function sleeps
# for 2 seconds at the 11th iteration
def print_hello(): 
  for i in range(20):
    if i == 10:
      time.sleep(2)
    print("Hello")

# Function to print numbers till a given number
def print_numbers(num): 
  for i in range(num+1):
    print(i)

# Creating the threads. Target is set to the name of the
# function that neeeds to be executed inside the thread and
# args are the arguments to be supplied to the function that

# needs to be executed.
print("Greetings from the main thread.")
thread1 = threading.Thread(target = print_hello, args = ())
thread2 = threading.Thread(target = print_numbers, args = (10,))  

# Starting the two threads
thread1.start() 
thread2.start() 
print("It's the main thread again!")

让我们通过跟踪代码的执行来尝试理解输出:

  1. 主线程执行。打印“Greetings from the main thread”,创建thread1thread2并启动线程。

  2. 发生上下文切换,开始执行thread1

  3. 在前十次迭代之后,thread1进入睡眠状态,thread2开始执行,在下一次上下文切换之前完成。

  4. 现在,主线程获得了 CPU 的控制权并打印出“It’s the main thread again!”

  5. 另一个上下文切换发生,thread2恢复执行并完成。

  6. 由于主线程没有更多指令要执行,因此程序终止。

使用thread.join()

如果需要阻塞主线程,直到thread1和thread2完成执行,该怎么办?

thread.join()会派上用场,因为它会阻塞调用线程,直到调用其 join() 方法的线程终止:

# importing the threading module 
import threading 

# importing the time module 
import time
# Function to print "Hello", however, the function sleeps

# for 2 seconds at the 11th iteration
def print_hello(): 
  for i in range(20):
    if i == 10:
      time.sleep(2)
    print("Hello")

# Function to print numbers till a given number
def print_numbers(num): 
  for i in range(num+1):
    print(i)

# Creating the threads. Target is set to the name of the
# function that neeeds to be executed inside the thread and
# args are the arguments to be supplied to the function that
# needs to be executed.
print("Greetings from the main thread.")
thread1 = threading.Thread(target = print_hello, args = ())
thread2 = threading.Thread(target = print_numbers, args = (10,))  

# Starting the two threads
thread1.start() 
thread2.start() 
thread1.join()
thread2.join()
print("It's the main thread again!")
print("Threads 1 and 2 have finished executing.")

原因多线程有助于更快的处理

视频处理代码分为两部分:从摄像头读取下一个可用帧并对帧进行视频处理,例如运行深度学习模型进行人脸识别等。

读取下一帧并在没有多线程的程序中按顺序进行处理。程序等待下一帧可用,然后再对其进行必要的处理。读取帧所需的时间主要与请求、等待和将下一个视频帧从相机传输到内存所需的时间有关。对视频帧进行计算所花费的时间,无论是在 CPU 还是 GPU 上,占据了视频处理所花费的大部分时间。

在具有多线程的程序中,读取下一帧并处理它不需要是顺序的。当一个线程执行读取下一帧的任务时,主线程可以使用 CPU 或 GPU 来处理最后读取的帧。这样,通过重叠两个任务,可以减少读取和处理帧的总时间。

OpenCV 代码——没有多线程

# importing required libraries 
import cv2 
import time

# opening video capture stream
vcap = cv2.VideoCapture(0)
if vcap.isOpened() is False :
    print("[Exiting]: Error accessing webcam stream.")
    exit(0)
fps_input_stream = int(vcap.get(5))
print("FPS of webcam hardware/input stream: {}".format(fps_input_stream))
grabbed, frame = vcap.read() # reading single frame for initialization/ hardware warm-up

# processing frames in input stream
num_frames_processed = 0 
start = time.time()
while True :
    grabbed, frame = vcap.read()
    if grabbed is False :
        print('[Exiting] No more frames to read')
        break

# adding a delay for simulating time taken for processing a frame 
    delay = 0.03 # delay value in seconds. so, delay=1 is equivalent to 1 second 
    time.sleep(delay) 
    num_frames_processed += 1
cv2.imshow('frame' , frame)
    key = cv2.waitKey(1)
    if key == ord('q'):
        break
end = time.time()

# printing time elapsed and fps 
elapsed = end-start
fps = num_frames_processed/elapsed 
print("FPS: {} , Elapsed Time: {} , Frames Processed: {}".format(fps, elapsed, num_frames_processed))

# releasing input stream , closing all windows 
vcap.release()
cv2.destroyAllWindows()

OpenCV 代码——多线程

# importing required libraries 
import cv2 
import time 
from threading import Thread # library for implementing multi-threaded processing

# defining a helper class for implementing multi-threaded processing 
class WebcamStream :
    def __init__(self, stream_id=0):
        self.stream_id = stream_id   # default is 0 for primary camera 
        
        # opening video capture stream 
        self.vcap      = cv2.VideoCapture(self.stream_id)
        if self.vcap.isOpened() is False :
            print("[Exiting]: Error accessing webcam stream.")
            exit(0)
        fps_input_stream = int(self.vcap.get(5))
        print("FPS of webcam hardware/input stream: {}".format(fps_input_stream))
            
        # reading a single frame from vcap stream for initializing 
        self.grabbed , self.frame = self.vcap.read()
        if self.grabbed is False :
            print('[Exiting] No more frames to read')
            exit(0)

# self.stopped is set to False when frames are being read from self.vcap stream 
        self.stopped = True

# reference to the thread for reading next available frame from input stream 
        self.t = Thread(target=self.update, args=())
        self.t.daemon = True # daemon threads keep running in the background while the program is executing 
        
    # method for starting the thread for grabbing next available frame in input stream 
    def start(self):
        self.stopped = False
        self.t.start()

# method for reading next frame 
    def update(self):
        while True :
            if self.stopped is True :
                break
            self.grabbed , self.frame = self.vcap.read()
            if self.grabbed is False :
                print('[Exiting] No more frames to read')
                self.stopped = True
                break 
        self.vcap.release()

# method for returning latest read frame 
    def read(self):
        return self.frame

# method called to stop reading frames 
    def stop(self):
        self.stopped = True

# initializing and starting multi-threaded webcam capture input stream 
webcam_stream = WebcamStream(stream_id=0) #  stream_id = 0 is for primary camera 
webcam_stream.start()

# processing frames in input stream
num_frames_processed = 0 
start = time.time()
while True :
    if webcam_stream.stopped is True :
        break
    else :
        frame = webcam_stream.read()

# adding a delay for simulating time taken for processing a frame 
    delay = 0.03 # delay value in seconds. so, delay=1 is equivalent to 1 second 
    time.sleep(delay) 
    num_frames_processed += 1
cv2.imshow('frame' , frame)
    key = cv2.waitKey(1)
    if key == ord('q'):
        break
end = time.time()
webcam_stream.stop() # stop the webcam stream

# printing time elapsed and fps 
elapsed = end-start
fps = num_frames_processed/elapsed 
print("FPS: {} , Elapsed Time: {} , Frames Processed: {}".format(fps, elapsed, num_frames_processed))

# closing all windows 
cv2.destroyAllWindows()

下载所有代码

https://github.com/SihabSahariar/Multi-threading-OpenCV-

资源

  1. https://stackoverflow.com/questions/55099413/python-opencv-streaming-from-camera-multithreading-timestamps

  2. https://stackoverflow.com/questions/55828451/video-streaming-from-ip-camera-in-python-using-opencv-cv2-videocapture

  3. https://stackoverflow.com/questions/58592291/how-to-capture-multiple-camera-streams-with-opencv

  4. https://stackoverflow.com/questions/58293187/opencv-real-time-streaming-video-capture-is-slow-how-to-drop-frames-or-get-sync

  5. https://stackoverflow.com/questions/55141315/storing-rtsp-stream-as-video-file-with-opencv-videowriter

  6. https://stackoverflow.com/questions/29317262/opencv-video-saving-in-python/71624807#71624807

  7. https://stackoverflow.com/questions/72120491/python-opencv-multiprocessing-cv2-videocapture-mp4

参考

  1. https://github.com/PyImageSearch/imutils/tree/master/imutils/video

  2. https://www.pyimagesearch.com/2015/12/21/increasing-webcam-fps-with-python-and-opencv/

  3. https://forum.opencv.org/t/videocapture-opens-video-sources-by-multi-thread/8045

☆ END ☆

如果看到这里,说明你喜欢这篇文章,请转发、点赞。微信搜索「uncle_pn」,欢迎添加小编微信「 woshicver」,每日朋友圈更新一篇高质量博文。

扫描二维码添加小编↓

890f36c879949f453b110b6885e2b9a2.jpeg

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/60877.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

257.二叉树的所有路径

257.二叉树的所有路径 文章目录257.二叉树的所有路径题目题解技巧 - 参数的作用域巧妙的题解方法题目 给你一个二叉树的根节点 root ,按 任意顺序 ,返回所有从根节点到叶子节点的路径。 叶子节点 是指没有子节点的节点。 示例 1: 输入&a…

【Redis实战】GeoHash实现原理与基本使用

文章目录GeoHash可以解决什么问题?基于网格划分的最近邻查询GeoHash划分规则GeoHash的使用方式Redis GEOADD 命令语法示例Redis GEORADIUS 命令语法半径单位:可选性参数:示例Redis GEORADIUSBYMEMBER 命令语法示例Redis GEODIST 命令语法示例…

Android 面试中Binder必问,是否了解过?

1.简单介绍下binder binder是一种进程间通讯的机制 进程间通讯需要了解用户空间和内核空间 每个进程拥有自己的独立虚拟机,系统为他们分配的地址空间都是互相隔离的。 如两个进程需要进行通讯,则需要使用到内核空间做载体,内核空间是所有进…

GPS卫星位置解算

本文介绍了基于C语言的GPS卫星位置解算原理与程序设计。针对每个原理、公式、代码设计进行了详细讲解,希望能够给测绘学子们带来帮助。 参考书籍: 李征航 黄劲松:GPS测量与数据处理(第三版) 目录 基础原理 1&#xf…

Spring框架(十一):手动实现一个@Component,讲一讲Spring的工厂后处理器

手动实现一个Component,讲一讲Spring的工厂后处理器引子需求代码分析Spring工厂后处理器引子 痛定思痛,主要问题出现在自己雀氏不熟悉框架底层、一些面试题,以及sql的一些情况淡忘了。 本章节的开始是对于过去的重新回顾,当然&am…

【AI】Python 实现八数码问题

实验一 八数码问题 1. 题目介绍 八数码问题描述为:在 33 组成的九宫格棋盘上,摆有 8 张牌,每张牌都刻有 1-8 中的某一个数码。棋盘中留有一个空格,允许其周围的某张牌向空格移动,这样通过移动牌就可以不断改变棋盘布…

PDF或PPT中的某个图或表无损、高清的插入word里的方法,再转成pdf后放大6400%倍仍是高清图片...

本人使用Microsoft Office LTSC 专业增强版 2021版本的,其他版本不确定可不可以 可通过office tool plus下载安装相应版本的office,通过安装与激活安装与激活 0. 参考方法网址: PDF 转成 SVG 格式的方法(无损保留笔记痕迹) 1. pdf可能很多页&…

Dubbo SPI扩展机制源码详解(基于2.7.10)

Dubbo SPI 一. 概述 本文主要分享 Dubbo 的拓展机制 SPI。 想要理解 Dubbo ,理解 Dubbo SPI 是非常必须的。在 Dubbo 中,提供了大量的拓展点,基于 Dubbo SPI 机制加载 Dubbo SPI官方文档:Dubbo SPI 概述 | Apache Dubbo 本文基…

Spring-IOC控制反转

Spring 1.简介 1.1简介 1.常见的框架&#xff1a; 2.常见的依赖&#xff1a; <dependencies><!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --><dependency><groupId>org.springframework</groupId><artif…

SpringBoot基础之声明式事务和切面事务和编程式事务

文章目录前言一、事务特性开启事务二、事务的隔离级别三、事务的传播行为四、 Springboot事务1.Springboot声明式事务优点&#xff1a;缺点&#xff1a;实现方式&#xff1a;Transactional的参数声明式事务的约定流程&#xff1a;2. Springboot编程式事务SpringBoo切面编程式事…

[附源码]Python计算机毕业设计SSM教学团队管理系统(程序+LW)

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybatis Maven Vue 等等组成&#xff0c;B/S模式 M…

微服务框架 SpringCloud微服务架构 11 自定义镜像 11.2 Dockerfile

微服务框架 【SpringCloudRabbitMQDockerRedis搜索分布式&#xff0c;系统详解springcloud微服务技术栈课程|黑马程序员Java微服务】 SpringCloud微服务架构 文章目录微服务框架SpringCloud微服务架构11 自定义镜像11.2 Dockerfile11.2.1 什么是Dcokerfile11.2.2 直接开干11.…

【机器学习】支持向量机【上】硬间隔

有任何的书写错误、排版错误、概念错误等&#xff0c;希望大家包含指正。 在阅读本篇之前建议先学习&#xff1a; 【机器学习】拉格朗日对偶性 【机器学习】核函数 由于字数限制&#xff0c;分成两篇博客。 【机器学习】支持向量机【上】硬间隔 【机器学习】支持向量机【下】…

绿色荧光染料FITC-PEG-FA,Folic acid-PEG-Fluorescein,荧光素-聚乙二醇-叶酸

​ 1、名称 英文&#xff1a;FITC-PEG-FA&#xff0c;Folic acid-PEG-Fluorescein 中文&#xff1a;荧光素-聚乙二醇-叶酸 2、CAS编号&#xff1a;N/A 3、所属分类&#xff1a; Fluorescent PEG Folic acid&#xff08;FA&#xff09; PEG 4、分子量&#xff1a;可定制&a…

[附源码]计算机毕业设计网上书城网站Springboot程序

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybatis Maven Vue 等等组成&#xff0c;B/S模式 M…

Java实现猜数游戏

1 问题 编写一个Java程序&#xff0c;实现以下功能&#xff1a; 2 方法 首先导入java.util包下的Random&#xff0c;让程序随便分配给用户一个数。 再导入java.util包下的Scanner类&#xff0c;构建Scanner对象&#xff0c;以便输入。 利用Random().nextInt()生成一个随机的i…

统计学中关于自由度的详细解释以及求平均值为什么消耗自由度1

首先我们要了解自由度是什么 自由度在本科教学中一般都不会过度涉及&#xff0c;因为它给老师和学生都带来了很大的困扰&#xff0c;它本身也有一些历史遗留问题&#xff0c;有很多人给出了很多不同的出发点和解释&#xff0c;比如1946年“自由度就是二次型的秩”&#xff0c;…

Android入门第42天-Android中的Service(IntentService)

开篇 在前一篇中我们讲了bindService的使用。并且我们留下了一个念想&#xff0c;即在bindService取值时故意阻塞30秒&#xff0c;引起了一次ANR并引出了今天的章节-IntentService。 IntentService的生命周期中有一个非常好的方法-onHandleIntent方法&#xff0c;它是一个abs…

创建你的第⼀个XXL-Job分布式调度任务

文章目录一、程序讲解1. 注解介绍2. 编写代码二、执⾏器管理2.1. Appname2.2. 名称2.3. 注册⽅式2.4. 机器地址2.5. 效果图三、任务管理一、程序讲解 1. 注解介绍 在 Spring Bean 实例中&#xff0c;开发 Job ⽅法⽅式格式要求为 public ReturnT<String> execute(Stri…

ARM 重定位引入和链接脚本

一、一个事实&#xff1a;大部分指令是位置有关编码 位置无关编码(PIC&#xff0c;position independent code)&#xff1a;汇编源文件被编码成二进制可执行程序时&#xff0c;编码方式与位置&#xff08;内存地址&#xff09;无关。 位置有关编码&#xff1a;汇编源码编码成…