当前位置:   article > 正文

【YOLOv8新玩法】姿态评估解锁找圆心位置_yolo能识别圆

yolo能识别圆

前言

Hello大家好,今天给大家分享一下如何基于深度学习模型训练实现圆检测与圆心位置预测,主要是通过对YOLOv8姿态评估模型在自定义的数据集上训练,生成一个自定义的圆检测与圆心定位预测模型

制作数据集

本人从网络上随便找到了个工业工件,然后写代码合成了一些数据,总计数据有360张图像、其中336张作为训练集、24张作为验证集。
在这里插入图片描述
其中YOLOv的数据格式如下:
在这里插入图片描述
解释一下:

Class-index 表示对象类型索引,
从0开始 后面的四个分别是对象的中心位置与宽高 xc、yc、width、height
px1,py1表示第一个关键点坐标、p1v表示师傅可见,默认填2即可

模型训练

跟训练YOLOv8对象检测模型类似,直接运行下面的命令行即可:

yolo train model=yolov8n-pose.pt data=circle_dataset.yaml epochs=15 imgsz=640 batch=1
  • 1

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

模型导出预测

训练完成以后模型预测推理测试 使用下面的命令行:

yolo predict model=D:\python\my_yolov8_train_demo\runs\pose\train3\weights\best.pt source=D:\bird_test\back1\2.png
  • 1

在这里插入图片描述
导出模型为ONNX格式,使用下面命令行即可

yolo export model=D:\python\my_yolov8_train_demo\runs\pose\train3\weights\best.pt format=onnx
  • 1

在这里插入图片描述

部署推理

基于ONNX格式模型,采用ONNXRUNTIME推理结果如下:
在这里插入图片描述
在这里插入图片描述

ORT相关的推理演示代码如下:

def ort_circle_demo():

    # initialize the onnxruntime session by loading model in CUDA support
    model_dir = "D:/python/my_yolov8_train_demo/circle_detect.onnx"
    session = onnxruntime.InferenceSession(model_dir, providers=['CUDAExecutionProvider'])

    # 就改这里, 把RTSP的地址配到这边就好啦,然后直接运行,其它任何地方都不准改!
    # 切记把 onnx文件放到跟这个python文件同一个文件夹中!
    frame = cv.imread("D:/bird_test/back1/3.png")
    bgr = format_yolov8(frame)
    fh, fw, fc = frame.shape

    start = time.time()
    image = cv.dnn.blobFromImage(bgr, 1 / 255.0, (640, 640), swapRB=True, crop=False)

    # onnxruntime inference
    ort_inputs = {session.get_inputs()[0].name: image}
    res = session.run(None, ort_inputs)[0]
    # matrix transpose from 1x8x8400 => 8400x8
    out_prob = np.squeeze(res, 0).T
    
    result_kypts, confidences, boxes = wrap_detection(bgr, out_prob)
    for (kpts, confidence, box) in zip(result_kypts, confidences, boxes):
        cv.rectangle(frame, box, (0, 0, 255), 2)
        cv.rectangle(frame, (box[0], box[1] - 20), (box[0] + box[2], box[1]), (0, 255, 255), -1)
        cv.putText(frame, ("%.2f" % confidence), (box[0], box[1] - 10), cv.FONT_HERSHEY_SIMPLEX, .5, (0, 0, 0))
        cx = kpts[0]
        cy = kpts[1]
        cv.circle(frame, (int(cx), int(cy)), 3, (255, 0, 255), 4, 8, 0)

    cv.imshow("Circle Detection Demo", frame)
    cv.waitKey(0)
    cv.destroyAllWindows()

if __name__ == "__main__":
    ort_circle_demo()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

从此,我又相信YOLOv8了。

特别推荐

学会使用最新版本OpenCV4.8部署YOLO系列模型,请看本人新书
《OpenCV应用开发:入门、进阶与工程化实践》

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/420848
推荐阅读
相关标签
  

闽ICP备14008679号