Opencv人脸检测在直播视频中使用QtGui执行缓慢



我有一个项目,需要在qt中设计一个gui。此设计包含一个小部件,其中live video feed将使用opencv从usb网络摄像头显示。该项目将检测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的大小持续增加,因此总体上变得非常慢。有没有什么好的方法可以让我毫不拖延地检测人脸并显示出来。请帮忙。感谢

Queue累积线程无法处理的帧。所以,根本没有办法处理它们。这就是为什么这里的队伍没用。这里的工作时钟由到达的帧定义,每个帧都会生成事件,这些事件可能在帧处理完成后在它自己的线程中工作(比如在处理线程中(,处理线程会生成另一个事件,并在另一个线程中处理,比如在GUI线程中,它会向用户显示结果。

如果您必须需要一些缓冲区,请检查环形缓冲区的长度是否有限。

您有一个生产者/消费者序列。。。

  1. 抓取帧并推送队列1
  2. 将帧从队列1中出队,在队列2上处理和排队结果
  3. 将队列2的结果出列并显示

根据您所述的第2阶段。是瓶颈。在这种情况下,您可以尝试为该阶段分配更多的资源(即线程(,因此2。有多个线程从队列1读取,处理结果并将结果推送到队列2。您只需要确保从队列2中弹出的已处理数据正确排序——大概是通过为每个初始帧分配序列号或id。

最新更新