当前位置: 欣欣网 > 码农

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

2023-12-20码农

点击上方 蓝字 关注我们

微信公众号: OpenCV学堂

关注获取更多计算机视觉与深度学习知识

前言

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

01

制作数据集

本人从网络上随便找到了个工业工件,然后写代码合成了一些数据,总计数据有360张图像、其中336张作为训练集、24张作为验证集。

其中YOLOv的数据格式如下:

解释一下:

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

02

模型训练

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

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

03

模型导出预测

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

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

导出模型为ONNX格式,使用下面命令行即可

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

04

部署推理

基于ONNX格式模型,采用ONNXRUNTIME推理结果如下:

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

defort_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, (640640), 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, (00255), 2)
cv.rectangle(frame, (box[0], box[1] - 20), (box[0] + box[2], box[1]), (0255255), -1)
cv.putText(frame, ("%.2f" % confidence), (box[0], box[1] - 10), cv.FONT_HERSHEY_SIMPLEX, .5, (000))
cx = kpts[0]
cy = kpts[1]
cv.circle(frame, (int(cx), int(cy)), 3, (2550255), 480)
cv.imshow("Circle Detection Demo", frame)
cv.waitKey(0)
cv.destroyAllWindows()

if __name__ == "__main__":
ort_circle_demo()







扫码学习YOLOv8视频课程

推荐阅读