pyqt5+yolov5的AI功能实现……

Posted qq_41319718

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pyqt5+yolov5的AI功能实现……相关的知识,希望对你有一定的参考价值。

项目场景:

基于yolov5封装在UI上的功能实现。此次项目目标是实现监测手机为label的功能实现UI


配置:

windows 7 64位
Jetson NX (不做处理的话默认达到10帧。勉强能跑)
gtx 1080Ti
yolov5 4.0 + pytorch1.6 + opencv4.5.x +cuda 10.2
Anaconda -------pyqt5
海康网络监控摄像头——rtsp
Jetson Xavier NX ——英伟达家的开发板

实现效果:

1.图档侦测
2.视频侦测
3.USB物体侦测
4.rtsp物体侦测
5.截图、存图、LOG档
6.最大化、最小化、固定窗
7.实时滚动图——侦测Label 显示最近

后续补充优化:
1、GPIO输出语音提示模块
2、工业PLC 网络模块交握
3、侦测计数统计图 total

# 移植yolov5 detect.py 模型初始化:
import os, threading
import sys
import cv2
import argparse
import random
import torch
import numpy as np
import torch.backends.cudnn as cudnn
import time

from PyQt5 import QtCore, QtGui, QtWidgets

from utils.torch_utils import select_device
from models.experimental import attempt_load
from utils.general import check_img_size, non_max_suppression, scale_coords
from utils.datasets import letterbox
from utils.plots import plot_one_box
from PyQt5.QtWidgets import *

flag = False
flag2= False

class Ui_MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(Ui_MainWindow, self).__init__(parent)
        self.timer_video = QtCore.QTimer()
        self.timer_video_1 = QtCore.QTimer()
        self.setupUi(self)
        self.init_slots()
        self.cap = cv2.VideoCapture()



####---模型初始化---
        parser = argparse.ArgumentParser()
        parser.add_argument('--weights', nargs='+', type=str, default='weights/yolov5s.pt', help='model.pt path(s)')
        parser.add_argument('--source', type=str, default='data/images', help='source')  # file/folder, 0 for webcam
        parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)')
        parser.add_argument('--conf-thres', type=float, default=0.25, help='object confidence threshold')
        parser.add_argument('--iou-thres', type=float, default=0.45, help='IOU threshold for NMS')
        parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
        parser.add_argument('--view-img', action='store_true', help='display results')
        parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
        parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
        parser.add_argument('--nosave', action='store_true', help='do not save images/videos')
        parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --class 0, or --class 0 2 3')
        parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')
        parser.add_argument('--augment', action='store_true', help='augmented inference')
        parser.add_argument('--update', action='store_true', help='update all models')
        parser.add_argument('--project', default='runs/detect', help='save results to project/name')
        parser.add_argument('--name', default='exp', help='save results to project/name')
        parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
        self.opt = parser.parse_args()
        print(self.opt)

        source, weights, view_img, save_txt, imgsz = self.opt.source, self.opt.weights, self.opt.view_img, self.opt.save_txt, self.opt.img_size

        self.device = select_device(self.opt.device)
        self.half = self.device.type != 'cpu'  # half precision only supported on CUDA

        cudnn.benchmark = True

        # Load model
        self.model = attempt_load(weights, map_location=self.device)  # load FP32 model
        stride = int(self.model.stride.max())  # model stride
        self.imgsz = check_img_size(imgsz, s=stride)  # check img_size
        if self.half:
            self.model.half()  # to FP16

        # Get names and colors
        self.names = self.model.module.names if hasattr(self.model, 'module') else self.model.names
        self.colors = [[random.randint(0, 255) for _ in range(3)] for _ in self.names]


# 基于Anaconda 底下 designer.exe   配置pyqt5 封装UI

#######---UI界面---
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1270, 855)
        MainWindow.setBaseSize(QtCore.QSize(5, 0))
        MainWindow.setFixedSize(self.width(), self.height())
        MainWindow.setStyleSheet("\\n" "#MainWindow {background-image: url(C:/Users/Administrator/Anaconda3/envs/pytorch1.6/yolov5/img/pyqt5 backup.png);}")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.frame = QtWidgets.QFrame(self.centralwidget)
        self.frame.setGeometry(QtCore.QRect(9, 10, 1251, 741))
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.frame.setObjectName("frame")
        self.label = QtWidgets.QLabel(self.frame)
        self.label.setGeometry(QtCore.QRect(7, 14, 1231, 721))
       # self.label.setStyleSheet("background-color: rgb(15, 15, 15);")
        self.label.setTextFormat(QtCore.Qt.AutoText)
        self.label.setObjectName("label")
        self.pushButton = QtWidgets.QPushButton(self.frame)
        self.pushButton.setGeometry(QtCore.QRect(1220, 717, 21, 21))
        self.pushButton.setStyleSheet("")
        self.pushButton.setText("")
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("C:/Users/Administrator/Anaconda3/envs/pytorch1.6/yolov5/img/mm.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton.setIcon(icon)
        self.pushButton.setIconSize(QtCore.QSize(18, 18))
        self.pushButton.setObjectName("pushButton")
        self.train = QtWidgets.QPushButton(self.frame)
        self.train.setGeometry(QtCore.QRect(1190, 717, 21, 21))
        self.train.setStyleSheet("")
        self.train.setText("")
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap("C:/Users/Administrator/Anaconda3/envs/pytorch1.6/yolov5/img/ppn.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.train.setIcon(icon1)
        self.train.setIconSize(QtCore.QSize(20, 20))
        self.train.setObjectName("train")
        self.pushButton_3 = QtWidgets.QPushButton(self.frame)
        self.pushButton_3.setGeometry(QtCore.QRect(1160, 717, 21, 21))
        self.pushButton_3.setStyleSheet("")
        self.pushButton_3.setText("")
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap("C:/Users/Administrator/Anaconda3/envs/pytorch1.6/yolov5/img/min.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_3.setIcon(icon2)
        self.pushButton_3.setIconSize(QtCore.QSize(18, 18))
        self.pushButton_3.setObjectName("pushButton_3")
        self.label_2 = QtWidgets.QLabel(self.frame)
        self.label_2.setGeometry(QtCore.QRect(430, 720, 511, 411))
        self.label_2.setObjectName("label_2")
        self.btn_opencam_img = QtWidgets.QPushButton(self.centralwidget)
        self.btn_opencam_img.setGeometry(QtCore.QRect(100, 760, 131, 61))
        self.btn_opencam_img.setStyleSheet("color: rgb(255, 255, 255);\\n"
"\\n"
"font: 75 9pt \\"Aharoni\\";\\n"
"color: rgb(0, 0, 0);\\n"
"font: 75 9pt \\"微软雅黑\\";")
        self.btn_opencam_img.setObjectName("btn_opencam_img")
        self.btn_opencam_video = QtWidgets.QPushButton(self.centralwidget)
        self.btn_opencam_video.setGeometry(QtCore.QRect(310, 760, 131, 61))
        self.btn_opencam_video.setStyleSheet("color: rgb(255, 255, 255);\\n"
"font: 75 9pt \\"微软雅黑\\";\\n"
"color: rgb(2, 2, 2);")
        self.btn_opencam_video.setObjectName("btn_opencam_video")
        self.btn_opencam = QtWidgets.QPushButton(self.centralwidget)
        self.btn_opencam.setGeometry(QtCore.QRect(540, 760, 131, 61))
        self.btn_opencam.setStyleSheet("color: rgb(255, 255, 255);\\n"
"font: 75 9pt \\"微软雅黑\\";\\n"
"color: rgb(0, 0, 0);")
        self.btn_opencam.setObjectName("btn_opencam")
        self.frame_2 = QtWidgets.QFrame(self.centralwidget)
        self.frame_2.setGeometry(QtCore.QRect(9, 750, 1251, 81))
        self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame_2.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.frame_2.setObjectName("frame_2")
        self.btn_opencam_2 = QtWidgets.QPushButton(self.frame_2)
        self.btn_opencam_2.setGeometry(QtCore.QRect(760, 10, 131, 61))
        self.btn_opencam_2.setStyleSheet("color: rgb(255, 255, 255);\\n"
"font: 75 9pt \\"微软雅黑\\";\\n"
"color: rgb(0, 0, 0);")
        self.btn_opencam_2.setObjectName("btn_opencam_2")
        self.iccon4 = QtWidgets.QLabel(self.frame_2)
        self.iccon4.setGeometry(QtCore.QRect(850, 40, 41, 31))
        self.iccon4.setStyleSheet("\\n"
"image: url(C:/Users/Administrator/Anaconda3/envs/pytorch1.6/yolov5/img/0.png);")
        self.iccon4.setText("")
        self.iccon4.setObjectName("iccon4")
        self.btn_opencam_3 = QtWidgets.QPushButton(self.frame_2)
        self.btn_opencam_3.setGeometry(QtCore.QRect(990, 10, 141, 61))
        self.btn_opencam_3.setStyleSheet("color: rgb(255, 255, 255);\\n"
"background-color: rgb(127, 127, 127);\\n"
"font: 75 14pt \\"Aharoni\\";\\n"
"color: rgb(255, 255, 255);")
        self.btn_opencam_3.setObjectName("btn_opencam_3")
        self.iccon5 = QtWidgets.QLabel(self.frame_2)
        self.iccon5.setGeometry(QtCore.QRect(1090, 40, 41, 31))
       # self.iccon5.setStyleSheet("\\n"
#"image: url(C:/Users/Administrator/Anaconda3/envs/pytorch1.6/yolov5/img/0.png);")
        self.iccon5.setText("")
        self.iccon5.setObjectName("iccon5")
        self.down = QtWidgets.QLabel(self.frame_2)
        self.down.setGeometry(QtCore.QRect(1150, -10, 101, 101))
        self.down.setStyleSheet("font: 75 18pt \\"Aharoni\\";\\n"
"border-image: url(C:/Users/Administrator/Anaconda3/envs/pytorch1.6/yolov5/img/down.png);")
        self.down.setText("")
        self.down.setObjectName("down")
        self.iccon = QtWidgets.QLabel(self.centralwidget)
        self.iccon.setGeometry(QtCore.QRect(200, 790, 41, 31))
        self.iccon.setStyleSheet("\\n"
"image: url(C:/Users/Administrator/Anaconda3/envs/pytorch1.6/yolov5/img/0.png);")
        self.iccon.setText("")
        self.iccon.setObjectName("iccon")
        self.iccon2 = QtWidgets.QLabel(self.centralwidget)
        self.iccon2.setGeometry(QtCore.QRect(400, 790, 41, 31))
        self.iccon2.setStyleSheet("\\n"
"image: url(C:/Users/Administrator/Anaconda3/envs/pytorch1.6/yolov5/img/0.png);")
        self.iccon2.setText("")
        self.iccon2.setObjectName("iccon2")
        self.iccon3 = QtWidgets.QLabel(self.centralwidget)
        self.iccon3.setGeometry(QtCore.QRect(630, 790, 41, 31))
        self.iccon3.setStyleSheet("\\n"
"image: url(C:/Users/Administrator/Anaconda3/envs/pytorch1.6/yolov5/img/0.png);")
        self.iccon3.setText("")
        self.iccon3.setObjectName("iccon3")
        self.frame_2.raise_()
        self.frame.raise_()
        self.btn_opencam_img.raise_()
        self.btn_opencam_video.raise_()
        self.btn_opencam.raise_()
        self.iccon.raise_()
        self.iccon2.raise_()
        self.iccon3.raise_()
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1270, 23))
       

以上是关于pyqt5+yolov5的AI功能实现……的主要内容,如果未能解决你的问题,请参考以下文章

手把手教你使用 YOLOV5 训练目标检测模型

Ai实现FPS游戏自动瞄准 yolov5fps自瞄

使用PyQt5搭建yoloV5目标检测平台

目标检测利用PyQT5搭建YOLOv5可视化界面

Yolov5(v5.0) + pyqt5界面设计

YOLOv5/v7 进阶实战 | 目录 | 安卓 | PyQt5| 剪枝✂️ | 蒸馏⚗️ | Flask Web | 改进教程