Pyside6 visual interface
Install Pyside6
Activate the previous virtual environmentyolov5
Enter the following command in the terminal of this environment
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyside6
Enter where python
the path to find the Python you are currently using
Find the designer.exe file in this path (/Lib/site-packages/PySide6/designer.exe), and then send it to the desktop to create a shortcut
UI design
- Open designer
- Select Main Window
- Remove menu bar
- Drag two labels and a line in
- Drag two buttons into them and name them
- Adjust the interface, fill in the files in the two labels, and make the interface smaller
- Align to the center and check scaledContents
- Element name changes
- Ctrl+S save, save to the file of the previous training data set
Set up the Pyside6-uic tool
- In the Python path of the current virtual environment, open
Scripts
the folder, find itpyside6-uic.exe
, and remember the current path
- Create tools
名称:PyUic(可自己定义)
程序:E:\kaifa\Anaconda3\envs\yolov5\Scripts\pyside6-uic.exe
实参:$FileName$ -o $FileNameWithoutExtension$.py
工作目录: $FileDir$
- use this tool
First open the ui file just generated in pycharm
If it cannot be opened by double-clicking it, drag the file to the right to open it.
Then under [external tools] under the [Tools] tab, click [PyUic] (this name was designed by you just now)
If a .py file with the same file name is generated, the setting is successful.
Write a running program
Create template file base_ui.py
import sys
import torch
from PySide6.QtGui import QPixmap, QImage
from PySide6.QtWidgets import QMainWindow, QApplication, QFileDialog
from main_window import Ui_MainWindow #main_windows是刚才生成的ui对用的Python文件名
def convert2QImage(img):
height, width, channel = img.shape
return QImage(img, width, height, width * channel, QImage.Format_RGB888)
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
Compile and run, and a pop-up window will indicate success.
Write code for internal specific functions
Explanation video: YOLOv5 Pyside6 visual interface
import sys
import cv2
import torch
from PySide6.QtGui import QPixmap, QImage
from PySide6.QtWidgets import QMainWindow, QApplication, QFileDialog
from PySide6.QtCore import QTimer
from main_window import Ui_MainWindow # main_windows是刚才生成的ui对用的Python文件名
def convert2QImage(img):
height, width, channel = img.shape
return QImage(img, width, height, width * channel, QImage.Format_RGB888)
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self)
self.model = torch.hub.load("./", "custom", path="runs/train/exp/weights/best.pt", source="local")
self.video = None
self.timer = QTimer()
self.timer.setInterval(1)
self.bind_slots() # 绑定槽函数
def image_pred(self, file_path): # 图片检测
results = self.model(file_path)
image = results.render()[0]
return convert2QImage(image)
def open_image(self): # 打开图片
print("点击了检测图片按钮")
self.timer.stop() # 停止视频检测
file_path = QFileDialog.getOpenFileName(self, dir="./data02/images/train", filter="*.jpg;*.png;*.jpeg")
if file_path[0]:
file_path = file_path[0]
qimage = self.image_pred(file_path)
self.input.setPixmap(QPixmap(file_path))
self.output.setPixmap(QPixmap.fromImage(qimage))
def video_pred(self): # 视频检测
ret, frame = self.video.read()
if not ret:
self.timer.stop()
else:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
self.input.setPixmap(QPixmap.fromImage(convert2QImage(frame)))
results = self.model(frame)
image = results.render()[0]
self.output.setPixmap(QPixmap.fromImage(convert2QImage(image)))
def open_video(self): # 打开视频
print("点击了检测视频!")
file_path = QFileDialog.getOpenFileName(self, dir="./data02", filter="*.mp4")
if file_path[0]:
file_path = file_path[0]
self.video = cv2.VideoCapture(file_path)
self.timer.start()
def bind_slots(self): # 绑定槽函数
self.det_image.clicked.connect(self.open_image)
self.det_video.clicked.connect(self.open_video)
self.timer.timeout.connect(self.video_pred)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
Gradio builds Web GUI
InstallGradio
Activate the previous virtual environmentyolov5
Enter the following command in the terminal of this environment
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple gradio
accomplish
Create new gradio_demo.py
file
Input picture, output picture, prediction function
Gradio does not support real-time detection
import torch
import gradio as gr
model = torch.hub.load("./", "custom", path="runs/train/exp/weights/best.pt", source="local")
title = "基于Gradio的YOLOv5演示项目"
desc = "这是一个基于Gradio的YOLOv5演示项目,非常简洁,非常方便!"
base_conf, base_iou = 0.25, 0.45
def det_image(img, conf_thres, iou_thres):
model.conf = conf_thres
model.iou = iou_thres
return model(img).render()[0]
gr.Interface(
inputs=["image", gr.Slider(minimum=0, maximum=1, value=base_conf), gr.Slider(minimum=0, maximum=1, value=base_iou)],
outputs=["image"],
fn=det_image,
title=title,
description=desc,
live=True,
examples=[["./data02/images/train/30.jpg", base_conf, base_iou],
["./data02/images/train/60.jpg", 0.3, base_iou]]
).launch(share=True)
If you want to publish to the Internet, set the share=True parameter, but when running, the following error will be reported due to network reasons. You can download the corresponding file through the prompted URL, rename it, put it in the corresponding directory, and run it again.