微信公众号:OpenCV学堂
关注获取更多计算机视觉与深度学习知识
引言
图像分析SDK支持
YOLOv8推理SDK支持
from dlcore.dl_infer_settings import DLInferSettings
from dlcore.yolov8_vino_ort_infer import YOLOv8Detector
import cv2 as cv
settings = DLInferSettings()
settings.weight_file_path = self.weight_file_path.text()
settings.label_map_file_path = "D:/projects/classes.txt"
settings.target_deploy = 1
detector = YOLOv8Detector(settings)
image = cv.imread(image_file)
detector.infer_image(image)
cv.waitKey("result", image)
综合代码演示
灰度
YOLOv8推理
相关实现代码如下:
1from dlcore.yolov8_vino_ort_infer import YOLOv8Detector
2from dlcore.dl_infer_settings import DLInferSettings
3import cv2 as cv
4from PyQt5 import QtWidgets, QtCore, QtGui
5from vmcore.color_space_task import ColorSpaceTask
6import sys
7
8
9class RadioCheckBoxDemoPanel(QtWidgets.QWidget):
10 def __init__(self, parent=None):
11 super().__init__(parent)
12 # 文本标签
13 self.rbtn0 = QtWidgets.QRadioButton("原图")
14 self.rbtn1 = QtWidgets.QRadioButton("灰度")
15 self.rbtn3 = QtWidgets.QRadioButton("YOLOv8推理")
16 self.rbtn0.setChecked(True)
17
18 hbox_layout1 = QtWidgets.QHBoxLayout()
19 hbox_layout1.addWidget(self.rbtn0)
20 hbox_layout1.addWidget(self.rbtn1)
21 hbox_layout1.addWidget(self.rbtn3)
22
23 panel1 = QtWidgets.QGroupBox("SDK演示")
24 panel1.setLayout(hbox_layout1)
25
26 # 输入文本框
27 self.image_file_edit = QtWidgets.QLineEdit()
28 self.image_file_edit.setMinimumWidth(100)
29 self.image_file_edit.setEnabled(False)
30 fileBtn = QtWidgets.QPushButton("图像")
31 self.weight_file_path = QtWidgets.QLineEdit()
32 self.weight_file_path.setMinimumWidth(100)
33 self.weight_file_path.setEnabled(False)
34 modelBtn = QtWidgets.QPushButton("模型")
35
36 hbox_layout2 = QtWidgets.QHBoxLayout()
37 hbox_layout2.addWidget(fileBtn)
38 hbox_layout2.addWidget(self.image_file_edit)
39 hbox_layout2.addWidget(modelBtn)
40 hbox_layout2.addWidget(self.weight_file_path)
41
42 panel2 = QtWidgets.QGroupBox("参数文件")
43 panel2.setLayout(hbox_layout2)
44
45 # 输入文本框
46 self.label = QtWidgets.QLabel()
47 pixmap = QtGui.QPixmap("images/wp.jpg")
48 pix = pixmap.scaled(QtCore.QSize(620, 500), QtCore.Qt.KeepAspectRatio)
49 self.label.setPixmap(pix)
50 self.label.setAlignment(QtCore.Qt.AlignCenter)
51 self.label.setStyleSheet("background-color:black; color: green")
52
53 # 添加到布局管理器中
54 vbox_layout = QtWidgets.QVBoxLayout()
55 vbox_layout.addWidget(panel2)
56 vbox_layout.addWidget(panel1)
57 vbox_layout.addWidget(self.label)
58 vbox_layout.addStretch(1)
59
60 # 面板容器
61 self.setLayout(vbox_layout)
62
63 # setup listener
64 self.rbtn0.toggled.connect(self.on_update_original)
65 self.rbtn1.toggled.connect(self.on_update_gray)
66 self.rbtn3.toggled.connect(self.on_yolov8_infer)
67 modelBtn.clicked.connect(self.on_weight_select)
68 fileBtn.clicked.connect(self.on_update_image)
69
70 def on_update_original(self):
71 image_file = self.image_file_edit.text()
72 if len(image_file) == 0 or image_file is None:
73 QtWidgets.QMessageBox.warning(self, "警告", "图像文件未选择...")
74 return
75 pixmap = QtGui.QPixmap(image_file)
76 pix = pixmap.scaled(QtCore.QSize(620, 500), QtCore.Qt.KeepAspectRatio)
77 self.label.setPixmap(pix)
78
79 def on_update_gray(self):
80 image_file = self.image_file_edit.text()
81 if len(image_file) == 0 or image_file is None:
82 QtWidgets.QMessageBox.warning(self, "警告", "图像文件未选择...")
83 return
84 image = cv.imread(image_file)
85 cst = ColorSpaceTask()
86 cst.low_scalar = (0, 0, 0)
87 cst.high_scalar = (0, 0, 0)
88 # 0 - BGR, 1 - HSV, 2 - gray
89 cst.color_type = 2
90 output = cst.t_exec(image)
91 gray = output['result']
92 dst = cv.cvtColor(gray, cv.COLOR_GRAY2RGB)
93
94 height, width, channel = dst.shape
95 bytesPerLine = 3 * width
96 img = QtGui.QImage(dst.data, width, height, bytesPerLine, QtGui.QImage.Format_RGB888)
97 pixmap = QtGui.QPixmap(img)
98 pix = pixmap.scaled(QtCore.QSize(620, 500), QtCore.Qt.KeepAspectRatio)
99 self.label.setPixmap(pix)
100
101 def on_yolov8_infer(self):
102 image_file = self.image_file_edit.text()
103 if len(image_file) == 0 or image_file is None:
104 QtWidgets.QMessageBox.warning(self, "警告", "图像文件未选择...")
105 return
106
107 settings = DLInferSettings()
108 settings.weight_file_path = self.weight_file_path.text()
109 settings.label_map_file_path = "D:/projects/classes.txt"
110 settings.target_deploy = 1
111 detector = YOLOv8Detector(settings)
112 image = cv.imread(image_file)
113 detector.infer_image(image)
114
115 dst = cv.cvtColor(image, cv.COLOR_BGR2RGB)
116 height, width, channel = dst.shape
117 bytesPerLine = 3 * width
118 img = QtGui.QImage(dst.data, width, height, bytesPerLine, QtGui.QImage.Format_RGB888)
119 pixmap = QtGui.QPixmap(img)
120 pix = pixmap.scaled(QtCore.QSize(620, 500), QtCore.Qt.KeepAspectRatio)
121 self.label.setPixmap(pix)
扫码查看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推理