supervision 提供了无缝处理注释多种目标检测和语义分割模型生成的预测。

注释目标检测的图片

import cv2 as cv
import supervision as sv
from ultralytics import YOLO

model_path=r'F:\python\yolov8\yolov8n.pt'
# 加载模型
model=YOLO(model_path)
# 读取图片
img=cv.imread('820d83e3e2f8ffb308dd604acdcf2fc4.jpg')
# 预测
results=model(img)[0]
# 加载预测到supervision
detections=sv.Detections.from_ultralytics(results)

box_annotator=sv.BoundingBoxAnnotator()
label_annotator=sv.LabelAnnotator()

labels=[
    f"{class_name} {conference:.2f}"
    for class_name,conference in zip(detections['class_name'],detections.confidence)
]
# 标注边界框
annotated_frame=box_annotator.annotate(scene=img,detections=detections)
# 标注文本
annotated_frame=label_annotator.annotate(scene=annotated_frame,detections=detections,labels=labels)

cv.imshow('img',annotated_frame)
if cv.waitKey(0)&0XFF==ord('q'):
    cv.destroyAllWindows()

注释语义分割的图片

import supervision as sv
import cv2 as cv
from ultralytics import YOLO

model_path=r'F:\python\yolov8\yolov8n-seg.pt'
# 加载语义分割模型
model=YOLO(model_path)
# 读取图片
img=cv.imread('820d83e3e2f8ffb308dd604acdcf2fc4.jpg')
results=model(img)[0]
# 将预测加载到supervision
detections=sv.Detections.from_ultralytics(results)

mask_annotator=sv.MaskAnnotator()
label_annotator=sv.LabelAnnotator(text_position=sv.Position.CENTER_OF_MASS)

# 语义注释
annotated_frame=mask_annotator.annotate(scene=img,detections=detections)
# 文本注释
annotated_frame=label_annotator.annotate(scene=annotated_frame,detections=detections)

cv.imshow('img',annotated_frame)
if cv.waitKey(0)&0XFF==ord('q'):
    cv.destoryAllWindows()

 

 posted on 2024-07-21 15:07  会飞的金鱼  阅读(69)  评论(0)    收藏  举报