python - 使用 Qt Gui 执行慢速实时视频源的 Opencv 人脸检测
问题描述
我有一个项目需要设计一个gui
in qt
. 此设计包含一个小部件,该小部件live video feed
将使用opencv
. 该项目将检测faces
并识别它们,这意味着每帧都会发生大量处理。
为此,我所做的是创建了一个线程来初始化相机并使用 opencv 从中获取帧。然后它将所有帧放入队列中,然后由一个update_frame
基本上在 qt 小部件上显示帧的函数读取该队列。这工作正常,没有延迟。
在update_frame
函数内部,我添加了face detection
它执行速度非常慢的原因。所以我创建了另一个线程start_inferencing
,它基本上从检测到人脸后读取帧queue
,它将帧再次放入另一个队列q2
中,然后由它读取update_frame
并显示,但它的响应仍然非常慢。下面是代码:
q = queue.Queue()
q2 = queue.Queue()
def grab(cam, qu, width, height):
global running
capture = cv2.VideoCapture(cam)
capture.set(cv2.CAP_PROP_FRAME_WIDTH, width)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
while running:
frame = {}
capture.grab()
ret_val, img = capture.retrieve(0)
frame["img"] = img
if qu.qsize() < 100:
qu.put(frame)
else:
print(qu.qsize())
class Logic(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.setupUi(self)
set_initial_alert_temp()
self.window_width = self.ImgWidget.frameSize().width()
self.window_height = self.ImgWidget.frameSize().height()
self.ImgWidget = OwnImageWidget(self.ImgWidget)
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.update_frame)
self.timer.start(1)
self.outside_temp_text_box.setText(str(curr_temp_cel))
def update_frame(self):
if not q2.empty():
frame1 = q2.get()
img = frame1["img"]
img_height, img_width, img_colors = img.shape
scale_w = float(self.window_width) / float(img_width)
scale_h = float(self.window_height) / float(img_height)
scale = min([scale_w, scale_h])
if scale == 0:
scale = 1
img = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_CUBIC)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
height, width, bpc = img.shape
bpl = bpc * width
image = QtGui.QImage(img.data, width, height, bpl, QtGui.QImage.Format_RGB888)
self.ImgWidget.setImage(image)
def start_inferencing():
while True:
if not q.empty():
frame = q.get()
img = frame["img"]
face_bbox = face.detect_face(img)
if face_bbox is not None:
(f_startX, f_startY, f_endX, f_endY) = face_bbox.astype("int")
f_startX = f_startX + 10
f_startY = f_startY + 10
f_endX = f_endX - 10
f_endY = f_endY - 10
cv2.rectangle(img, (f_startX, f_startY), (f_endX, f_endY), (0, 255, 0), 2)
frame1 = {"img": img}
if q2.qsize() < 100:
q2.put(frame1)
else:
print(q2.qsize())
def main():
capture_thread = threading.Thread(target=grab, args=(0, q, 640, 480))
capture_thread.start()
infer_thread = threading.Thread(target=start_inferencing)
infer_thread.start()
app = QtWidgets.QApplication(sys.argv)
w = Logic(None)
w.setWindowTitle('Test')
w.show()
app.exec_()
main()
以下是代码中发生的事情的摘要:
camera -> frame -> queue.put # (reading frame from camera and putting it in queue)
queue.get -> frame -> detect face -> queue2.put # (getting frame from queue, detecting face in it and putting the updated frames in queue2)
queue2.get -> frame -> display it on qt widget # (getting frame from queue2 and display it on qt widget)
直播视频速度慢的主要原因是因为在grab
函数中读取的帧无法更快地处理,因此queue
尺寸不断增加,因此整体变得非常慢。我可以使用什么好的方法来检测面部并立即显示它。请帮忙。谢谢
解决方案
队列会累积线程无法处理的帧。所以,根本没有机会处理它们。这就是为什么队列在这里没用的原因。这里的工作时钟由到达的帧定义,每个帧都会产生事件,它可以在帧处理完成后在它自己的线程中工作(比如说在处理线程中),处理线程产生另一个事件并在另一个线程中处理,比如说在 GUI 线程中并将结果显示给用户。
如果您强制需要一些缓冲区,请检查其长度有限的环形缓冲区。
推荐阅读
- scheme - Racket 类中的动态函数调用结合应用
- r - 如何为某些 x 值的多元回归添加回归图?
- python - Astropy 抱怨 Numpy 版本
- css - How to put the icons on the left side of a nav bar?
- python - 如何使用 python 和 pyrebase 从 firebase 存储中获取文件的大小
- javascript - 未捕获的类型错误:$.ajax 不是函数 | 在使用最新的完整 jQuery 版本时
- java - Dagger 2 Android - 将()依赖项注入 ViewModel 与具有依赖项引用的应用程序
- javascript - 螺旋矩阵:内部函数有效,但在 While 循环中使用时抛出错误
- c# - json.net,如何从相对路径或实际 json 对象反序列化 json 对象
- angular - 你如何 TypeScript 一个承诺的结果?