学习《OpenCV应用开发:入门、进阶与工程化实践》一书
做真正的OpenCV开发者,从入门到入职,一步到位!
YOLOv8旋转对象检测
YOLOv8框架在在支持分类、对象检测、实例分割、姿态评估的基础上更近一步,现已经支持旋转对象检测(OBB),基于DOTA数据集,支持航拍图像的15个类别对象检测,包括车辆、船只、典型各种场地等。包含2800多张图像、18W个实例对象。
YOLO OBB标注数据格式,主要是类别与四个角点归一化到0~1之间的坐标,格式表示如下:
class_index, x1, y1, x2, y2, x3, y3, x4, y4
训练以后的YOLOv8预测xyhwr + 类别数目,不同尺度的YOLOv8 OBB模型的精度与输入格式列表如下:
图片
导出与预测
基于YOLOv8命令行推理测试:
## 导出
yolo export model=yolov8s-obb.pt format=onnx
## 推理
yolo obb predict model=yolov8n-obb.pt source=plane_03.jpg
ONNX推理代码演示
基于OpenVINO2023与ONNX格式模型直接预测推理,首先看一下ONNX格式的YOLOv8-OBB输入与输出格式:
旋转对象检测-代码演示
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()
运行结果如下:
好书推荐
《OpenCV应用开发:入门、进阶与工程化实践》全书共计16个章节,重点聚焦OpenCV开发常用模块详解与工程化开发实践,提升OpenCV应用开发能力,助力读者成为OpenCV开发者,同时包含深度学习模型训练与部署加速等知识,帮助OpenCV开发者进一步拓展技能地图,满足工业项目落地所需技能提升。购买请点链接:
《OpenCV4 应用开发-入门、进阶与工程化实践》
学习课程有专属答疑群,负责贴身答疑解惑
读者专属QQ群 :657875553
进群暗号:OpenCV4读者