手把手教你用YOLO-v8.3:从零搭建零售货架识别系统
手把手教你用YOLO-v8.3从零搭建零售货架识别系统1. 项目背景与价值零售行业每天面临着一个共同的挑战如何高效准确地管理货架商品。传统的人工盘点方式不仅耗时费力而且容易出错。想象一下一家中型超市每天需要花费3-4小时进行货架盘点而使用基于YOLO-v8.3的自动识别系统这个时间可以缩短到几分钟。YOLO-v8.3作为当前最先进的目标检测模型之一具有以下特点使其特别适合零售场景实时性能在标准GPU上能达到100 FPS的处理速度高精度相比前代模型mAP(平均精度)提升15-20%多任务支持除了检测还支持分割、分类等任务易部署提供Python接口和预训练模型降低使用门槛2. 环境准备与快速部署2.1 获取YOLO-v8.3镜像我们使用预配置的YOLO-v8.3镜像它已经包含了所有必要的依赖Python 3.8PyTorch 1.12Ultralytics YOLO库OpenCV等计算机视觉工具包部署步骤拉取镜像并启动容器docker pull csdn/yolo-v8.3:latest docker run -it --gpus all -p 8888:8888 -p 6006:6006 csdn/yolo-v8.3选择使用方式Jupyter Lab访问http://localhost:8888SSH连接使用端口22连接2.2 验证环境运行以下代码验证环境是否正常import torch from ultralytics import YOLO # 检查GPU是否可用 print(fGPU可用: {torch.cuda.is_available()}) print(fGPU型号: {torch.cuda.get_device_name(0)}) # 加载预训练模型 model YOLO(yolov8n.pt) # 测试推理 results model(https://ultralytics.com/images/bus.jpg) # 打印检测结果 for result in results: print(\n检测结果:) for box in result.boxes: print(f{result.names[int(box.cls[0].item())]}: 置信度 {box.conf[0].item():.2f})预期输出应显示检测到的物体类别和置信度同时确认GPU可用。3. 数据准备与标注3.1 构建零售商品数据集一个高质量的零售商品数据集应包含每种商品至少100-150张图像不同角度、光照条件和遮挡情况同类商品的不同品牌和包装推荐数据收集方法使用智能手机在真实零售环境拍摄从公开数据集中补充如COCO、OpenImages使用3D渲染工具生成合成数据3.2 数据标注实践使用LabelImg或CVAT工具进行标注保存为YOLO格式类别索引 x_center y_center width height目录结构示例retail_dataset/ ├── images/ │ ├── train/ │ └── val/ └── labels/ ├── train/ └── val/创建数据集配置文件retail.yaml:path: /data/retail_dataset train: images/train val: images/val nc: 5 # 商品类别数 names: [cola, chips, chocolate, water, cookies]4. 模型训练与优化4.1 基础训练流程from ultralytics import YOLO # 加载预训练模型 model YOLO(yolov8s.pt) # 使用small版本平衡速度与精度 # 训练配置 results model.train( dataretail.yaml, epochs100, batch32, imgsz640, patience15, device0, nameretail_v1, optimizerAdamW, lr00.001, warmup_epochs3 )关键参数说明patience: 早停机制验证集指标不再提升时停止训练optimizer: 推荐使用AdamW或SGDlr0: 初始学习率可根据batch size调整4.2 高级训练技巧数据增强策略results model.train( # ...其他参数... augmentTrue, hsv_h0.015, hsv_s0.7, hsv_v0.4, degrees10, translate0.1, scale0.5, shear2.0, perspective0.0005, flipud0.0, fliplr0.5, mosaic1.0, mixup0.1 )小目标检测优化results model.train( # ...其他参数... imgsz1280, # 增大输入尺寸 anchor_t4.0, # 调整anchor阈值 box7.5, # 增加box loss权重 fl_gamma1.5 # Focal loss gamma参数 )5. 系统实现与部署5.1 实时检测实现import cv2 from ultralytics import YOLO import numpy as np class ShelfDetector: def __init__(self, model_path): self.model YOLO(model_path) self.class_colors { cola: (0, 0, 255), chips: (0, 255, 0), chocolate: (255, 0, 0) } def process_frame(self, frame): # 预处理 frame cv2.resize(frame, (1280, 720)) # 推理 results self.model(frame, imgsz1280, conf0.5) # 后处理 annotated_frame results[0].plot() # 商品计数 counts {name: 0 for name in self.class_colors.keys()} for box in results[0].boxes: cls_name results[0].names[int(box.cls[0].item())] counts[cls_name] 1 # 显示计数 y_offset 30 for name, count in counts.items(): cv2.putText(annotated_frame, f{name}: {count}, (10, y_offset), cv2.FONT_HERSHEY_SIMPLEX, 1, self.class_colors[name], 2) y_offset 40 return annotated_frame, counts # 使用示例 detector ShelfDetector(best.pt) cap cv2.VideoCapture(0) while True: ret, frame cap.read() if not ret: break result_frame, counts detector.process_frame(frame) cv2.imshow(Shelf Monitoring, result_frame) if cv2.waitKey(1) 0xFF ord(q): break cap.release() cv2.destroyAllWindows()5.2 库存管理集成import sqlite3 from datetime import datetime class InventoryManager: def __init__(self, db_pathinventory.db): self.conn sqlite3.connect(db_path) self._init_db() def _init_db(self): cursor self.conn.cursor() cursor.execute( CREATE TABLE IF NOT EXISTS inventory ( id INTEGER PRIMARY KEY AUTOINCREMENT, product_name TEXT NOT NULL, count INTEGER NOT NULL, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP ) ) self.conn.commit() def log_inventory(self, counts): timestamp datetime.now() cursor self.conn.cursor() for product, count in counts.items(): cursor.execute( INSERT INTO inventory (product_name, count) VALUES (?, ?), (product, count) ) self.conn.commit() def get_inventory_trend(self, product_name, hours24): cursor self.conn.cursor() cursor.execute( SELECT strftime(%Y-%m-%d %H:00, timestamp) as hour, AVG(count) as avg_count FROM inventory WHERE product_name ? AND timestamp datetime(now, ?) GROUP BY hour ORDER BY hour , (product_name, f-{hours} hours)) return cursor.fetchall()6. 性能优化与生产部署6.1 模型优化技术模型量化from ultralytics import YOLO # 加载训练好的模型 model YOLO(best.pt) # 导出为INT8量化模型 model.export(formatonnx, imgsz640, halfTrue, int8True, device0)TensorRT加速# 转换为TensorRT引擎 trtexec --onnxbest.onnx --saveEnginebest.engine --fp166.2 生产部署方案方案一边缘设备部署# 树莓派优化版检测代码 import cv2 from tflite_runtime.interpreter import Interpreter class EdgeDetector: def __init__(self, model_path): self.interpreter Interpreter(model_path) self.interpreter.allocate_tensors() def detect(self, frame): # 预处理 input_data cv2.resize(frame, (320, 320)) input_data input_data.astype(np.float32) / 255.0 # 设置输入 input_details self.interpreter.get_input_details() self.interpreter.set_tensor(input_details[0][index], [input_data]) # 推理 self.interpreter.invoke() # 获取输出 output_details self.interpreter.get_output_details() boxes self.interpreter.get_tensor(output_details[0][index]) scores self.interpreter.get_tensor(output_details[1][index]) return boxes, scores方案二云端微服务# Flask API示例 from flask import Flask, request, jsonify import cv2 import numpy as np from ultralytics import YOLO app Flask(__name__) model YOLO(best.pt) app.route(/detect, methods[POST]) def detect(): file request.files[image] img cv2.imdecode(np.frombuffer(file.read(), np.uint8), cv2.IMREAD_COLOR) results model(img) detections [] for box in results[0].boxes: detections.append({ class: results[0].names[int(box.cls[0].item())], confidence: float(box.conf[0].item()), bbox: box.xyxy[0].tolist() }) return jsonify({results: detections}) if __name__ __main__: app.run(host0.0.0.0, port5000)7. 总结与展望通过本教程我们完成了从零搭建零售货架识别系统的全过程。关键要点包括数据是关键高质量、多样化的训练数据是模型性能的基础模型选择YOLO-v8.3在速度和精度间取得了良好平衡系统思维从单一检测扩展到完整的库存管理系统部署优化根据场景需求选择边缘计算或云端方案未来扩展方向集成货架陈列合规性检测结合RFID技术提高识别精度开发基于检测结果的自动补货系统分析顾客拿取行为优化商品摆放获取更多AI镜像想探索更多AI镜像和应用场景访问 CSDN星图镜像广场提供丰富的预置镜像覆盖大模型推理、图像生成、视频生成、模型微调等多个领域支持一键部署。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2442950.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!