微信公众号:OpenCV开发者联盟
关注获取更多计算机视觉与深度学习知识
引言
QThread线程介绍
开启一个QThread线程直接调用start()方法即可。QThread会发出 started() 与 finished()两种线程状态的信号,通过isFinished() 与isRunning() 两个方法可以直接查询线程的当前状态,调用exit()或者 quit()可以停止或者退出线程,线程执行完成之后信号finished()链接到deleteLater() 方法实现线程中对象回收与销毁。关于QThread基本知识汇总如下:完成与实现-QThread
重载run方法
start()
isRunning()
isFinished()
exit()
quit()
finished().connect(deleteLater())
代码演示部分
YOLOv8推理线程代码如下:
1class InferenceThread(QtCore.QThread):
2 fire_stats_signal = QtCore.pyqtSignal(dict)
3
4 def __init__(self, settings):
5 super(InferenceThread, self).__init__()
6 self.settings = settings
7 self.detector = YOLOv8Detector(settings)
8 self.input_image = settings.input_image
9
10 def run(self):
11 if self.detector == None:
12 return
13 if self.input_image.endswith(".mp4"):
14 cap = cv.VideoCapture(self.input_image)
15 while True:
16 ret, frame = cap.read()
17 if ret is True:
18 self.detector.infer_image(frame)
19 self.fire_stats_signal.emit({"result": frame})
20 else:
21 break
22 else:
23 frame = cv.imread(self.input_image)
24 self.detector.infer_image(frame)
25 self.fire_stats_signal.emit({"result": frame})
26 self.fire_stats_signal.emit({"done": "done"})
27 return
1def on_update_result_image(self, outs):
2 image = outs.get("result")
3 done = outs.get("done")
4 if image is not None:
5 dst = cv.cvtColor(image, cv.COLOR_BGR2RGB)
6 height, width, channel = dst.shape
7 bytesPerLine = 3 * width
8 img = QtGui.QImage(dst.data, width, height, bytesPerLine, QtGui.QImage.Format_RGB888)
9 pixmap = QtGui.QPixmap(img)
10 pix = pixmap.scaled(QtCore.QSize(800, 600), QtCore.Qt.KeepAspectRatio)
11 self.label.setPixmap(pix)
12 self.show_text("OpenCV开发者联盟-YOLOv8推理演示")
13 if done is not None:
14 self.stopBtn.setStyleSheet("background-color:gray; color: white")
15 self.stopBtn.setEnabled(False)
16 self.startBtn.setStyleSheet("background-color:cyan; color: black")
17 self.startBtn.setEnabled(True)
18
19def show_text(self, text):
20 painter = QtGui.QPainter(self.label.pixmap())
21 pen = QtGui.QPen(QtCore.Qt.green)
22 painter.setPen(pen)
23 font = QtGui.QFont()
24 font.setBold(True)
25 font.setPointSizeF(16)
26 painter.setFont(font)
27 painter.drawText(QtCore.QPoint(10, 200), text)
28 painter.end()
视频处理演示截图如下:
扫码关注
OpenCV开发者联盟,
专注各种语言的OpenCV开发教程分享
OpenCV周边开发技术应用!
扫码查看OpenCV+OpenVIO+Pytorch系统化学习路线图
推荐阅读
CV全栈开发者说 - 从传统算法到深度学习怎么修炼
2022入坑深度学习,我选择Pytorch框架!
Pytorch轻松实现经典视觉任务
教程推荐 | Pytorch框架CV开发-从入门到实战
OpenCV4 C++学习 必备基础语法知识三
OpenCV4 C++学习 必备基础语法知识二
OpenCV4.5.4 人脸检测+五点landmark新功能测试
OpenCV4.5.4人脸识别详解与代码演示
OpenCV二值图象分析之Blob分析找圆
OpenCV4.5.x DNN + YOLOv5 C++推理
OpenCV4.5.4 直接支持YOLOv5 6.1版本模型推理
OpenVINO2021.4+YOLOX目标检测模型部署测试
比YOLOv5还厉害的YOLOX来了,官方支持OpenVINO推理