微信公众号:OpenCV学堂
关注获取更多计算机视觉与深度学习知识
YOLOv8 OBB介绍
class_index, x1, y1, x2, y2, x3, y3, x4, y4
导出与预测
## 导出
yolo export model=yolov8s-obb.pt format=onnx
## 推理
yolo obb predict model=yolov8n-obb.pt source=plane_03.jpg
ONNX推理代码演示
class_list = load_classes()
colors = [(255, 255, 0), (0, 255, 0), (0, 255, 255), (255, 0, 0)]
ie = Core()
for device in ie.available_devices:
print(device)
# Read IR
model = ie.read_model(model="yolov8s-obb.onnx")
compiled_model = ie.compile_model(model=model, device_name="CPU")
output_layer = compiled_model.output(0)
## xywhr
frame = cv.imread("D:/wh860.jpg")
# frame = cv.imread("wh300.jpg")
# frame = cv.imread("obb_01.jpeg")
bgr = format_yolov8(frame)
img_h, img_w, img_c = bgr.shape
start = time.time()
image = cv.dnn.blobFromImage(bgr, 1 / 255.0, (1024, 1024), swapRB=True, crop=False)
res = compiled_model([image])[output_layer] # 1x25x8400
rows = np.squeeze(res, 0).T
boxes, confidences, angles, class_ids = post_process(rows)
indexes = cv.dnn.NMSBoxes(boxes, confidences, 0.25, 0.45)
M = np.zeros((2, 3), dtype=np.float32)
for index in indexes:
box = boxes[index]
d1 = -angles[index]
color = colors[int(class_ids[index]) % len(colors)]
pts = [(box[0], box[1]), (box[0]+box[2], box[1]), (box[0]+box[2], box[1]+box[3]), (box[0], box[1]+box[3])]
rrt_pts = get_rotate_point(pts, M, d1, box)
cv.drawContours(frame, [np.asarray(rrt_pts).astype(np.int32)], 0, (255, 0, 255), 2)
cv.rectangle(frame, (box[0], box[1] - 20), (box[0] + box[2], box[1]), color, -1)
cv.putText(frame, class_list[class_ids[index]], (box[0], box[1]-8), cv.FONT_HERSHEY_SIMPLEX, .5, (255, 255, 255))
end = time.time()
inf_end = end - start
fps = 1 / inf_end
fps_label = "FPS: %.2f" % fps
cv.putText(frame, fps_label, (20, 45), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
cv.imshow("YOLOv8-obb+OpenVINO2023.x Object Detection", frame)
cv.imwrite("D:/wk_result.jpg", frame)
cv.waitKey(0)
cv.destroyAllWindows()
运行结果如下:
扫码学习YOLOv8视频课程
推荐阅读
OpenCV4.8+YOLOv8对象检测C++推理演示
三行代码实现 TensorRT8.6 C++ 深度学习模型部署
实战 | YOLOv8+OpenCV 实现DM码定位检测与解析
好书推荐
https://item.jd.com/10092255924058.html
学习课程有专属答疑群
负责贴身答疑解惑
读者专属QQ群 :657875553
进群暗号:OpenCV4读者