Raspberry Pi python opencv视频捕获在读卡器线程中不返回帧

Raspberry Pi python opencv视频捕获在读卡器线程中不返回帧,python,opencv,raspberry-pi,python-multithreading,video-capture,Python,Opencv,Raspberry Pi,Python Multithreading,Video Capture,我正在尝试将OpenCv视频捕获转移到一个单独的线程,以获得更好的性能,并删除帧缓冲区。我的一段代码基于的解决方案。 问题是,读取缓冲区不会给出任何结果,线程之间使用的QUE保持为空。这是我的密码: import numpy as np import time import cv2 import queue import threading class VideoCapture: def __init__(self, src = 0): self.cap =

我正在尝试将OpenCv视频捕获转移到一个单独的线程,以获得更好的性能,并删除帧缓冲区。我的一段代码基于的解决方案。 问题是,读取缓冲区不会给出任何结果,线程之间使用的QUE保持为空。这是我的密码:

import numpy as np
import time
import cv2
import queue
import threading

class VideoCapture:
    
    def __init__(self, src = 0):
        self.cap = cv2.VideoCapture(src)
        self.q = queue.SimpleQueue()
        t = threading.Thread(target=self._update)
        t.daemon = True
        t.start()
        
    def _update(self):
        while True:
            ret, frame = self.cap.read()
            if not ret:
                break
            if not self.q.empty():
                try:
                    self.q.get_nowait()
                except queue.Empty:
                    pass
            self.q.put(frame)
            time.sleep(.01)
        
    def read(self):
        if self.q.empty:
            return None
        return self.q.get()
    
    def release(self):
        self.cap.release()

print("Go!")

cap = VideoCapture(0)

while True:
    
    img = cap.read()
    
    print(img)
    
    #cv2.imshow('img', img)
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    
cap.release()
cv2.destroyAllWindows()
它返回:

Go!
None
None
None
...
我的问题是:为什么它不起作用,以及如何使它起作用

此外,摄像机工作正常。我很确定这个问题与线程有关