首页 python 正文
  • 本文约4537字,阅读需23分钟
  • 59
  • 0

Supervision 计算机视觉工具

摘要

简介 Supervision库是Python计算机视觉低代码工具,旨在为用户提供便捷高效的接口,以便处理数据集并直观地展示检测结果。绘制检测结果,统计指定区域内检测目标数量Supervision都提供了相应的接口 安装库 要求Python版本 =3.8 1.安装无图像版本,轻量级,更适合服务器端应用程序: 命令:pip install supervision...

简介

Supervision库是Python计算机视觉低代码工具,旨在为用户提供便捷高效的接口,以便处理数据集并直观地展示检测结果。绘制检测结果,统计指定区域内检测目标数量Supervision都提供了相应的接口

安装库

要求Python版本>=3.8

1.安装无图像版本,轻量级,更适合服务器端应用程序:
命令:pip install supervision
2.安装有图像版本,此版本包括OpenCV的GUI组件,允许您在屏幕上显示图像和视频:
命令:pip install "supervision[desktop]"

Supervision提供了Inferen‍ce、Ultralytics、Transformers 推理案例,本文以为Ultralytics为主

1.目标检测

import cv2
import supervision as sv
from ultralytics import YOLO

model = YOLO("yolov8n.pt")
image = cv2.imread('1.jpeg')
results = model(image)[0]
detections = sv.Detections.from_ultralytics(results)

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

labels = [
    f"{class_name} {confidence:.2f}"
    for class_name, confidence
    in zip(detections['class_name'], detections.confidence)
]

# 标注识别框
annotated_image = box_annotator.annotate(scene=image, detections=detections)
# 标注识别标签和置信度
annotated_image = label_annotator.annotate(scene=annotated_image, detections=detections, labels=labels)
cv2.imwrite('2.jpeg',annotated_image)

(效果图)
Supervision 计算机视觉工具

supervision提供了多种函数来对识别结果进行可视化,以下举例部分案例

(角点边界框绘制)

corner_length-每个角线的长度
corner_annotator = sv.BoxCornerAnnotator(corner_length=15,thickness=2, color=sv.Color(r=0, g=255, b=255))
annotated_frame = corner_annotator.annotate(scene=image.copy(),detections=detections)
cv2.imwrite('2.jpeg',annotated_frame)

Supervision 计算机视觉工具

(三角形边界框绘制)

base/height-三角形的宽高,position-位置
triangle_annotator = sv.TriangleAnnotator(base = 30, height = 30, position = sv.Position['TOP_CENTER'])
annotated_frame = triangle_annotator.annotate(scene=image.copy(),detections=detections)
cv2.imwrite('2.jpeg',annotated_frame)

Supervision 计算机视觉工具

2.语义分割

‌语义分割是一种计算机视觉技术,旨在将图像中的每个像素分配到对应的语义类别,例如,在一张包含汽车、‌行人和道路的图像中,语义分割的目标是将图像中的每个像素标记为汽车、行人或道路。这种分割不区分不同的实例,即所有被标记为同一类别的对象都被视为一个整体。

‌语义分割的应用广泛,包括但不限于‌自动驾驶汽车、‌地理信息系统、‌医疗影像分析等。例如,在自动驾驶中,语义分割可以帮助车载系统识别道路、车辆和行人,从而做出相应的驾驶决策。在地理信息系统中,语义分割可以用于自动识别和标注卫星遥感影像中的道路、河流和建筑物等。

import cv2
import supervision as sv
from ultralytics import YOLO

model = YOLO("yolov8n-seg.pt")
image = cv2.imread('1.jpeg')
results = model(image)[0]
detections = sv.Detections.from_ultralytics(results)

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

annotated_image = mask_annotator.annotate(scene=image, detections=detections)
annotated_image = label_annotator.annotate(scene=annotated_image, detections=detections)

cv2.imwrite('2.jpeg', annotated_image)

(效果图)
Supervision 计算机视觉工具

3.目标追踪

目标追踪是一种通过分析图像序列,连续地跟踪目标在场景中运动和变化的技术。广泛应用在视频监控、无人驾驶等。

import cv2
import supervision as sv
from ultralytics import YOLO

model = YOLO("yolov8n.pt")
tracker = sv.ByteTrack()
box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()
trace_annotator = sv.TraceAnnotator()

cap = cv2.VideoCapture('people-walking.mp4')
while True:
    success,frame = cap.read()
    if success:
        results = model(frame)[0]
        detections = sv.Detections.from_ultralytics(results)
        detections = tracker.update_with_detections(detections)

        labels = [f"{tracker_id} {results.names[class_id]}" for class_id, tracker_id in zip(detections.class_id, detections.tracker_id)]

        annotated_frame = box_annotator.annotate(frame.copy(), detections=detections)
        annotated_frame = label_annotator.annotate(annotated_frame, detections=detections, labels=labels)
        trace_annotator.annotate(annotated_frame, detections=detections)

        cv2.imshow('1', annotated_frame)
        cv2.waitKey(1)
    else:
        break

4.越线数量统计

通过分析车辆轨迹和状态(是否跨过检测线)来统计车流量等,其他目标同样适用。

import cv2
import supervision as sv
from ultralytics import YOLO

model = YOLO("yolov8n.pt")
tracker = sv.ByteTrack()

cap = cv2.VideoCapture("vehicles.mp4")
success, frame = cap.read()
h, w = frame.shape[:2]
# 设置预设线(从左至右)
start, end = sv.Point(x=0, y=int(h / 2)), sv.Point(x=w, y=int(h / 2))
# 初始预线检测器
line_zone = sv.LineZone(start=start, end=end)
# 初始化可视化对象
trace_annotator = sv.TraceAnnotator()
label_annotator = sv.LabelAnnotator(text_scale=1)
line_zone_annotator = sv.LineZoneAnnotator(thickness=1, text_thickness=1, text_scale=1)

while True:
    ret, frame = cap.read()
    if ret:
        result = model(frame)[0]
        detections = sv.Detections.from_ultralytics(result)
        # 更新目标跟踪器
        detections = tracker.update_with_detections(detections)
        # 更新预线检测器,crossed_in是否进入结果,crossed_out是否出去结果
        crossed_in, crossed_out = line_zone.trigger(detections)

        # 获得各边界框的标签
        labels = [
            f"{tracker_id} {result.names[class_id]}"
            for class_id, tracker_id
            in zip(detections.class_id, detections.tracker_id)
        ]

        # 绘制轨迹
        annotated_frame = trace_annotator.annotate(scene=frame.copy(), detections=detections)
        # 绘制标签
        annotated_frame = label_annotator.annotate(scene=annotated_frame, detections=detections, labels=labels)
        # 绘制预制线
        annotated_frame = line_zone_annotator.annotate(annotated_frame, line_counter=line_zone)
        annotated_frame = cv2.resize(annotated_frame,(1280,720))
        cv2.imshow('1', annotated_frame)
        cv2.waitKey(1)
        print(f'in:{line_zone.in_count}', f'out:{line_zone.out_count}')
    else:
        break

结尾

以上是对Supervision部分功能的介绍,更多内容通过链接查看官方文档
官方地址:https://supervision.roboflow.com/latest

标签:Supervision

扫描二维码,在手机上阅读
    评论