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

概述

在本文中,我们将看到两个没有多线程的 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()  

【我之前自学Python的时候整理了很多Python学习资料,现在我也用不上了,我上传到CSDN官方了,有需要的朋友可以扫描下方二维码进行获取】

一、学习大纲

在这里插入图片描述

二、开发工具

在这里插入图片描述

三、Python基础材料

在这里插入图片描述

四、实战资料

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个示例代码,可以同时播放和处理视频流: ```python import cv2 import threading class VideoPlayer(threading.Thread): def __init__(self, video_path): threading.Thread.__init__(self) self.video_path = video_path self.frame = None self.stop_event = threading.Event() def run(self): cap = cv2.VideoCapture(self.video_path) while not self.stop_event.is_set(): ret, self.frame = cap.read() if not ret: break cv2.imshow('Video Player', self.frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows() cap.release() def stop(self): self.stop_event.set() class VideoProcessor(threading.Thread): def __init__(self, video_player): threading.Thread.__init__(self) self.video_player = video_player self.stop_event = threading.Event() def run(self): while not self.stop_event.is_set(): if self.video_player.frame is not None: # 在这里进行视频处理 processed_frame = self.video_player.frame.copy() # 显示处理后的视频帧 cv2.imshow('Video Processor', processed_frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows() def stop(self): self.stop_event.set() if __name__ == '__main__': video_path = 'test.mp4' video_player = VideoPlayer(video_path) video_processor = VideoProcessor(video_player) video_player.start() video_processor.start() while True: if cv2.waitKey(1) & 0xFF == ord('q'): break video_processor.stop() video_player.stop() video_processor.join() video_player.join() ``` 在这里,我们使用了两个线程,一个用于播放视频,一个用于处理视频流。在播放线程,我们使用OpenCV读取视频文件,并在窗口显示每一帧。在处理线程,我们在每一帧上进行处理,然后显示处理后的帧。两个线程都可以通过`stop()`方法来停止运行。 在主线程,我们启动了两个线程,并等待用户按下q键来停止程序。注意,在程序结束时,我们必须先停止处理线程,再停止播放线程,并等待两个线程都结束运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值