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. 推理的效果显示

相关推荐
lljss202029 分钟前
Python11中创建虚拟环境、安装 TensorFlow
开发语言·python·tensorflow
空中湖31 分钟前
tensorflow武林志第二卷第九章:玄功九转
人工智能·python·tensorflow
CodeCraft Studio2 小时前
CAD文件处理控件Aspose.CAD教程:使用 Python 将绘图转换为 Photoshop
python·photoshop·cad·aspose·aspose.cad
Python×CATIA工业智造4 小时前
Frida RPC高级应用:动态模拟执行Android so文件实战指南
开发语言·python·pycharm
onceco4 小时前
领域LLM九讲——第5讲 为什么选择OpenManus而不是QwenAgent(附LLM免费api邀请码)
人工智能·python·深度学习·语言模型·自然语言处理·自动化
狐凄5 小时前
Python实例题:基于 Python 的简单聊天机器人
开发语言·python
悦悦子a啊6 小时前
Python之--基本知识
开发语言·前端·python
笑稀了的野生俊7 小时前
在服务器中下载 HuggingFace 模型:终极指南
linux·服务器·python·bash·gpu算力
Naiva7 小时前
【小技巧】Python+PyCharm IDE 配置解释器出错,环境配置不完整或不兼容。(小智AI、MCP、聚合数据、实时新闻查询、NBA赛事查询)
ide·python·pycharm
路来了8 小时前
Python小工具之PDF合并
开发语言·windows·python