python配合yolo分类模型开发分类软件

  1. 上一篇文章写了yolo的分类模型的训练,写篇文章基于yolo分类模型开发分类软件。
  2. 开发环境:pycharm,PySide6 6.6.1 ,PySide6-Addons 6.6.1,PySide6-Essentials 6.6.1,torch 2.3.1+cu121,torchaudio 2.3.1+cu121,torchvision 0.18.1+cu121,onnx 1.16.1,onnxruntime 1.17.3,opencv-contrib-python 4.10.0.82,opencv-python 4.10.0.82,opencv-python-headless 4.7.0.72
  3. 分类使用的数据集,halcon的pill分类demo的数据集
  4. 软件界面


  5. 核心代码
    yolo推理代码
bash 复制代码
  results = self.Model.predict(image_mat_)
            if(len(results)>0):
                ###获取分类名
                names_=results[0].names
                probs_=results[0].probs
                class_id_=probs_.top1
                class_socre_=probs_.top1conf.item()
                self.Classification=names_[class_id_]
                self.Sorce=str(class_socre_)

onnx推理核心代码

bash 复制代码
 			target_image_height_ = input_onnx_.shape[2]###获取输入的目标维度
            target_image_width_ = input_onnx_.shape[3]

            # image_mat_=image_mat_.resize(target_image_width_,target_image_height_)###缩放图片
            scale_image_mat_=cv2.resize(image_mat_,(target_image_width_,target_image_height_))
            print("ImageMatShape:", scale_image_mat_.shape)

            image_np_ = np.array(scale_image_mat_)  ###图片转成np数组
            print("ImageNpShape:", image_np_.shape)

            image_np_ = image_np_.transpose(2, 0, 1)  ##转成通道在前面的维度
            print("ImageNpShape:", image_np_.shape)

            resized_width = image_np_.shape[2]  ##获取图片输入输出
            resized_height = image_np_.shape[1]

            image_np_ = image_np_.reshape(1, 3, target_image_height_, target_image_width_)  ##添加一个新维度
            print(image_np_[0, 0, 0, 0])
            print("ImageNpShape:", image_np_.shape)

            image_np_ = image_np_.astype(np.float32)

            image_np_ = image_np_ / 255.0  ##数据归一化
            print(image_np_[0, 0, 0, 0])

            ###获取输出点
            outputs = self.OnnxModel.get_outputs()
            output_onnx_ = outputs[0]
            print("Name:", output_onnx_.name)
            print("Type:", output_onnx_.type)
            print("Shape:", output_onnx_.shape)

            ###运行推理
            outputs = self.OnnxModel.run(["output0"], {"images": image_np_})

            if(len(outputs)>0):
                ng_sorce_=outputs[0][0][0]
                ok_sorce_=outputs[0][0][1]

                if(ng_sorce_>ok_sorce_):
                    self.Classification="ng"
                    self.Sorce=str(ng_sorce_)
                else:
                    self.Classification="ok"
                    self.Sorce = str(ok_sorce_)
  1. 推理的效果显示

相关推荐
luckys.one11 小时前
第9篇:Freqtrade量化交易之config.json 基础入门与初始化
javascript·数据库·python·mysql·算法·json·区块链
大翻哥哥12 小时前
Python 2025:量化金融与智能交易的新纪元
开发语言·python·金融
zhousenshan14 小时前
Python爬虫常用框架
开发语言·爬虫·python
IMER SIMPLE14 小时前
人工智能-python-深度学习-经典神经网络AlexNet
人工智能·python·深度学习
CodeCraft Studio14 小时前
国产化Word处理组件Spire.DOC教程:使用 Python 将 Markdown 转换为 HTML 的详细教程
python·html·word·markdown·国产化·spire.doc·文档格式转换
专注API从业者15 小时前
Python/Java 代码示例:手把手教程调用 1688 API 获取商品详情实时数据
java·linux·数据库·python
java1234_小锋15 小时前
[免费]基于Python的协同过滤电影推荐系统(Django+Vue+sqlite+爬虫)【论文+源码+SQL脚本】
python·django·电影推荐系统·协同过滤
看海天一色听风起雨落16 小时前
Python学习之装饰器
开发语言·python·学习
XiaoMu_00116 小时前
基于Python+Streamlit的旅游数据分析与预测系统:从数据可视化到机器学习预测的完整实现
python·信息可视化·旅游
THMAIL16 小时前
深度学习从入门到精通 - 生成对抗网络(GAN)实战:创造逼真图像的魔法艺术
人工智能·python·深度学习·神经网络·机器学习·生成对抗网络·cnn