Yolov5水果分类识别+pyqt交互式界面

Yolov5 Fruits Detector

  • Yolov5 是一种先进的目标检测算法,可以应用于水果分类识别任务。结合 PyQT
    框架,可以创建一个交互式界面,使用户能够方便地上传图片并获取水果分类结果。以下将详细阐述 Yolov5 水果分类识别和 PyQT
    交互式界面的实现。
  • Yolov5 是由 Ultralytics
    公司开发的一种基于深度学习的目标检测算法,它采用了一种称为单阶段目标检测的方法,具有高准确率和实时性的特点。在水果分类识别任务中,Yolov5
    可以检测图像中的水果,并将其分类为不同的类别,例如苹果、香蕉、橙子等。
  • 为了实现 Yolov5 水果分类识别的交互式界面,可以使用 PyQT 框架进行开发。PyQT 是一个功能强大且易于使用的 Python
    GUI 开发工具包,它提供了丰富的界面组件和布局选项,可以轻松创建用户友好的界面。
  • 在界面设计方面,可以使用 PyQT 创建一个包含上传图片按钮和显示分类结果的窗口。当用户点击上传图片按钮时,可以调用 Yolov5
    模型对上传的图片进行识别,并将分类结果显示在界面上。同时,还可以添加其他功能,如清除界面、保存结果等。

要求

  • 可以使用 Linux 或者 Windows。我们推荐使用 Linux 以获得更好的性能。
  • 需要安装 Python 3.6+ 和 PyTorch 1.7+。

安装

运行以下命令来安装依赖项:

复制代码
pip install -r requirements.txt
  • 下载模型,请使用此链接:https://drive.google.com/file/d/1W6qZeutnqnp3YX9w4iYgR44xsoi_64ff/view?usp=sharing
  • 将下载的文件放置在 weights 目录下

代码

运行此部分检测ui界面代码

复制代码
import sys
import os

from PySide6.QtWidgets import QApplication, QWidget, QFileDialog
from PySide6.QtCore import QFile
from PySide6.QtUiTools import QUiLoader
from PySide6.QtGui import QPixmap, QImage
from PySide6.QtCore import QThread, Signal, QDir
import cv2


def convertCVImage2QtImage(cv_img):
    cv_img = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB)
    height, width, channel = cv_img.shape
    bytesPerLine = 3 * width
    qimg = QImage(cv_img.data, width, height, bytesPerLine, QImage.Format_RGB888)
    return QPixmap.fromImage(qimg)


class ProcessImage(QThread):
    signal_show_frame = Signal(object)

    def __init__(self, fileName):
        QThread.__init__(self)
        self.fileName = fileName

        from detector import Detector
        self.detector = Detector()

    def run(self):
        self.video = cv2.VideoCapture(self.fileName)
        while True:
            valid, self.frame = self.video.read()
            if valid is not True:
                break
            self.frame = self.detector.detect(self.frame)
            self.signal_show_frame.emit(self.frame)
            cv2.waitKey(30)
        self.video.release()

    def stop(self):
        try:
            self.video.release()
        except:
            pass


class show(QThread):
    signal_show_image = Signal(object)

    def __init__(self, fileName):
        QThread.__init__(self)
        self.fileName = fileName
        self.video=cv2.VideoCapture(self.fileName)

    def run(self): 
        while True:
            valid, self.frame = self.video.read()
            if valid is not True:
                break
            self.signal_show_image.emit(self.frame)
            cv2.waitKey(30)
        self.video.release()

    def stop(self):
        try:
            self.video.release()
        except:
            pass


class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        loader = QUiLoader()
        self.ui = loader.load("ui/form.ui")
        
        self.ui.btn_browse.clicked.connect(self.getFile)
        self.ui.btn_start.clicked.connect(self.predict)

        self.ui.show()

    def getFile(self):
        self.fileName = QFileDialog.getOpenFileName(self,'Single File','C:\'','*.jpg *.mp4 *.jpeg *.png *.avi')[0]
        self.ui.txt_address.setText(str(self.fileName))
        self.show=show(self.fileName)
        self.show.signal_show_image.connect(self.show_input)
        self.show.start()
        
        
    def predict(self):
        self.process_image = ProcessImage(self.fileName)
        self.process_image.signal_show_frame.connect(self.show_output)
        self.process_image.start()

    def show_input(self, image):
        pixmap = convertCVImage2QtImage(image)
        self.ui.lbl_input.setPixmap(pixmap)

    def show_output(self, image):
        pixmap = convertCVImage2QtImage(image)
        self.ui.lbl_output.setPixmap(pixmap)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    widget = MainWindow()
    sys.exit(app.exec())

运行界面

要对图像或视频进行推断,请运行以下命令:

复制代码
python main.py 

数据集:

  • 数据集可以在此链接中找到https://t.ly/NZWj
  • 在 Yolov5 水果分类识别的实现过程中,需要使用训练好的 Yolov5 模型来进行目标检测和分类。可以使用已经预训练好的 Yolov5 模型,也可以自己训练一个适用于水果分类的模型。

总结

总结起来,Yolov5 水果分类识别结合 PyQT 交互式界面可以提供一个方便用户上传图片并获取水果分类结果的工具。Yolov5 算法具有高准确率和实时性,在水果分类任务中表现出色。PyQT 框架提供了丰富的界面组件和布局选项,使得界面开发更加简单。通过 Yolov5 水果分类识别和 PyQT 交互式界面的结合,用户可以轻松地进行水果分类识别,并获得准确的分类结果。

相关推荐
程序员羽痕1 天前
基于深度学习的眼疾识别系统
人工智能·pytorch·深度学习·分类·django
大学生技术野农1 天前
基于Yolo26和Grad-CAM可视化融合的深度学习的猕猴桃分级系统-Python源码
python·深度学习·yolo
YOLO数据集集合2 天前
番茄病害智能诊断系统:YOLO+DeepSeek农业AI落地实践
人工智能·yolo·目标检测·计算机视觉
Studying 开龙wu2 天前
YOLOv11s-Pose 模型导出 ONNX 并适配 Rockchip NPU 全流程记录
yolo
八解毒剂2 天前
计算机分类
分类
金色旭光2 天前
YOLO26 目标检测原理
算法·yolo
解局易否结局2 天前
鸿蒙 NEXT 端侧 AI 实战:用 MindSpore Lite 把图像分类跑在手机上
人工智能·华为·分类·harmonyos
YOLO数据集集合2 天前
多模态AI道路病害检测系统:YOLO+DeepSeek融合实战
人工智能·yolo·目标检测·计算机视觉·目标跟踪
YOLO数据集集合2 天前
吊装作业的“电子围栏”:YOLOV13如何用AI框住塔吊下的致命风险
人工智能·安全·yolo·目标检测
想会飞的蒲公英3 天前
逻辑回归为什么叫回归,却用来做分类
人工智能·python·分类·回归·逻辑回归