经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 大数据/云/AI » 人工智能基础 » 查看文章
[深度学习] 计算机视觉低代码工具Supervision库使用指北
来源:cnblogs  作者:落痕的寒假  时间:2024/3/18 15:04:13  对本文有异议

Supervision库是一款出色的Python计算机视觉低代码工具,其设计初衷在于为用户提供一个便捷且高效的接口,用以处理数据集以及直观地展示检测结果。Supervision库的官方开源仓库地址为:supervision,官方文档地址为:supervision-doc

Supervision库需要在Python3.8及以上版本的环境下运行。如果需要支持包含OpenCV的GUI组件以支持显示图像和视频,supervision安装方式如下:

pip install supervision[desktop]

如果仅仅想部署应用,而不需要GUI界面,supervision安装方式如下:

pip install supervision

注意,由于supervision版本经常变动,所提供的接口函数会相应发生变化。

  1. import supervision as sv
  2. # 打印supervision的版本
  3. sv.__version__
  1. '0.19.0'

1 不同任务的处理

1.1 目标检测与语义分割

1.1.1 结果分析

supervision提供了多种接口来支持对目标检测或语义分割结果的分析。supervision.Detections为主流目标检测或语义分割模型的输出结果分析提供了多种接口,常用的几个接口如下:

  • from_ultralytics(Ultralytics, YOLOv8)
  • from_detectron2(Detectron2)
  • from_mmdetection(MMDetection)
  • from_yolov5(YOLOv5)
  • from_sam(Segment Anything Model)
  • from_transformers(HuggingFace Transformers)
  • from_paddledet(PaddleDetecticon)

以上接口的具体使用见:supervision-doc-detection。下面以YOLOv8的结果分析为例,来说明相关代码的使用,以下代码输入图片如下:

img/cat.png

  1. import cv2
  2. import supervision as sv
  3. from ultralytics import YOLO
  4. model = YOLO("yolov8n.pt")
  5. # model = YOLO("yolov8n-seg.pt")
  6. image = cv2.imread("img/dog.png")
  7. results = model(image, verbose=False)[0]
  8. # 从YOLOv8中加载数据结果
  9. detections = sv.Detections.from_ultralytics(results)
  10. # 查看输出结果
  11. detections
  1. Detections(xyxy=array([[ 255.78, 432.86, 612.01, 1078.5],
  2. [ 255.98, 263.57, 1131.1, 837.2],
  3. [ 927.72, 143.31, 1375.6, 338.81],
  4. [ 926.96, 142.04, 1376.7, 339.12]], dtype=float32), mask=None, confidence=array([ 0.91621, 0.85567, 0.56325, 0.52481], dtype=float32), class_id=array([16, 1, 2, 7]), tracker_id=None, data={'class_name': array(['dog', 'bicycle', 'car', 'truck'], dtype='<U7')})
  1. # 查看输出结果预测框个数
  2. len(detections)
  1. 4
  1. # 查看第一个框输出结果
  2. detections[0]
  1. Detections(xyxy=array([[ 255.78, 432.86, 612.01, 1078.5]], dtype=float32), mask=None, confidence=array([ 0.91621], dtype=float32), class_id=array([16]), tracker_id=None, data={'class_name': array(['dog'], dtype='<U7')})
  1. # 查看每一个目标边界框的面积
  2. detections.box_area
  1. array([ 2.3001e+05, 5.0201e+05, 87569, 88643], dtype=float32)
  1. # 可视化识别结果
  2. # 确定可视化参数
  3. bounding_box_annotator = sv.BoundingBoxAnnotator()
  4. label_annotator = sv.LabelAnnotator()
  5. labels = [
  6. model.model.names[class_id]
  7. for class_id
  8. in detections.class_id
  9. ]
  10. annotated_image = bounding_box_annotator.annotate(
  11. scene=image, detections=detections)
  12. annotated_image = label_annotator.annotate(
  13. scene=annotated_image, detections=detections, labels=labels)
  14. sv.plot_image(annotated_image)

png

在上图标注了三个检测框,然而实际的检测结果中却包含了四个框。这是由于图中的汽车同时被识别为卡车(truck)和轿车(car)。

  1. len(detections)
  2. detections
  1. Detections(xyxy=array([[ 255.78, 432.86, 612.01, 1078.5],
  2. [ 255.98, 263.57, 1131.1, 837.2],
  3. [ 927.72, 143.31, 1375.6, 338.81],
  4. [ 926.96, 142.04, 1376.7, 339.12]], dtype=float32), mask=None, confidence=array([ 0.91621, 0.85567, 0.56325, 0.52481], dtype=float32), class_id=array([16, 1, 2, 7]), tracker_id=None, data={'class_name': array(['dog', 'bicycle', 'car', 'truck'], dtype='<U7')})
  1. labels
  1. ['dog', 'bicycle', 'car', 'truck']

解决的办法是,对目标检测结果执行执行类无关的非最大抑制NMS,代码如下:

  1. detections = detections.with_nms(threshold=0.5, class_agnostic=True)
  2. # 打印输出结果
  3. for class_id in detections.class_id:
  4. print(model.model.names[class_id])
  1. dog
  2. bicycle
  3. car

1.1.2 辅助函数

计算Intersection over Union(IOU)

  1. import supervision as sv
  2. import numpy as np
  3. box1 = np.array([[50, 50, 150, 150]]) # (x_min, y_min, x_max, y_max)
  4. box2 = np.array([[100, 100, 200, 200]])
  5. print(sv.box_iou_batch(box1,box2))
  1. [[ 0.14286]]

计算Non-Maximum Suppression (NMS)

  1. import supervision as sv
  2. box = np.array([[50, 50, 150, 150, 0.2],[100, 100, 200, 200, 0.5]]) # (x_min, y_min, x_max, y_max, score)
  3. # 返回哪些边界框需要保存
  4. # 参数:输入框数组和阈值
  5. print(sv.box_non_max_suppression(box,0.1))
  1. [False True]

从多边形生成mask

  1. import cv2
  2. import supervision as sv
  3. import numpy as np
  4. # 多边形
  5. vertices = np.array([(50, 50), (30, 50), (60,20), (70, 50), (90, 10)])
  6. # 创建遮罩mask
  7. # 参数:输入框数组和输出mask的宽高
  8. mask = sv.polygon_to_mask(vertices, (100,60))
  9. # mask中白色(像素值为1)表示多边形,其他区域像素值为0
  10. sv.plot_image(mask)
  11. # 从mask生成多边形
  12. # vertices = sv.mask_to_polygons(mask)

png

根据面积过滤多边形

  1. import supervision as sv
  2. import numpy as np
  3. # 创建包含多个多边形的示例列表
  4. polygon1 = np.array([[0, 0], [0, 1], [1, 1], [1, 0]])
  5. polygon2 = np.array([[0, 0], [0, 2], [2, 2], [2, 0]])
  6. polygon3 = np.array([[0, 0], [0, 3], [3, 3], [3, 0]])
  7. polygons = [polygon1, polygon2, polygon3]
  8. # 参数:输入多边形列表,面积最小值,面积最大值(为None表示无最大值限制)
  9. filtered_polygons = sv.filter_polygons_by_area(polygons, 2.5, None)
  10. print("原始多边形数组个数:", len(polygons))
  11. print("筛选后的多边形数组:", len(filtered_polygons))
  1. 原始多边形数组个数: 3
  2. 筛选后的多边形数组: 2

缩放边界框

  1. import numpy as np
  2. import supervision as sv
  3. boxes = np.array([[10, 10, 20, 20], [30, 30, 40, 40]])
  4. # 表示按比例缩放长方体尺寸的因子。大于1的因子将放大长方体,而小于1的因子将缩小长方体
  5. factor = 1.2
  6. scaled_bb = sv.scale_boxes(boxes, factor)
  7. print(scaled_bb)
  1. [[ 9 9 21 21]
  2. [ 29 29 41 41]]

1.2 目标跟踪

supervision中内置了ByteTrack目标跟踪器,ByteTrack与基于ReID特征进行匹配的目标跟踪方法不同,ByteTrack主要依赖目标检测器提供的目标框信息进行跟踪。因此,目标检测器的准确性和稳定性会直接影响到ByteTrack的跟踪效果。

通过supervision.ByteTrack类即可初始化ByteTrack追踪器,supervision.ByteTrack类的初始化参数如下:

  • track_thresh(float, 可选,默认0.25): 检测置信度阈值
  • track_buffer(int,可选,默认30): 轨道丢失时要缓冲的帧数。
  • match_thresh(float,可选,默认0.8): 将轨道与检测相匹配的阈值。
  • frame_rate(int,可选,默认30): 视频的帧速率。

supervision.ByteTrack类的主要类函数如下:

  • reset():重置ByteTrack跟踪器的内部状态。
  • update_with_detections(detections):使用提供的检测更新跟踪器并返回更新的检测结果,detections为supervision的目标检测结果。

示例代码如下:

  1. import supervision as sv
  2. from ultralytics import YOLO
  3. import numpy as np
  4. model = YOLO("yolov8n.pt")
  5. # 初始化目标跟踪器
  6. tracker = sv.ByteTrack()
  7. bounding_box_annotator = sv.BoundingBoxAnnotator()
  8. label_annotator = sv.LabelAnnotator()
  9. def callback(frame: np.ndarray, index: int) -> np.ndarray:
  10. results = model(frame)[0]
  11. # 获得Detections结果
  12. detections = sv.Detections.from_ultralytics(results)
  13. # 轨迹跟踪
  14. detections = tracker.update_with_detections(detections)
  15. labels = [f"#{tracker_id}" for tracker_id in detections.tracker_id]
  16. annotated_frame = bounding_box_annotator.annotate(scene=frame.copy(), detections=detections)
  17. annotated_frame = label_annotator.annotate( scene=annotated_frame, detections=detections, labels=labels)
  18. return annotated_frame
  19. sv.process_video(
  20. source_path="https://media.roboflow.com/supervision/video-examples/people-walking.mp4",
  21. # 输出结果参考:https://media.roboflow.com/supervision/video-examples/how-to/track-objects/annotate-video-with-traces.mp4
  22. target_path="output.mp4",
  23. callback=callback
  24. )

由于示例代码用的是yolov8n.pt,跟踪效果会很不稳定,可以考虑使用性能更强的目标跟踪器。

1.3 图像分类

supervision支持分析clip,timm,YOLOv8分类模型结果的输出,但是功能很弱,仅支持输出top-k及概率。

以下代码输入图片如下:

img/cat.png

  1. import cv2
  2. from ultralytics import YOLO
  3. import supervision as sv
  4. # 加载图片和模型
  5. image = cv2.imread("img/cat.png")
  6. # 加载分类模型
  7. model = YOLO('yolov8n-cls.pt')
  8. output = model(image)[0]
  9. # 将YOLO的分类输出导入supervision
  10. classifications = sv.Classifications.from_ultralytics(output)
  11. # 除此之外还支持from_clip和from_timm两类模型
  12. # 打印top2,输出类别和概率
  13. print(classifications.get_top_k(2))
  1. 0: 224x224 tiger_cat 0.29, tabby 0.23, Egyptian_cat 0.15, Siamese_cat 0.05, Pembroke 0.03, 36.7ms
  2. Speed: 6.3ms preprocess, 36.7ms inference, 0.0ms postprocess per image at shape (1, 3, 224, 224)
  3. (array([282, 281]), array([ 0.29406, 0.22982], dtype=float32))

2 数据展示与辅助处理

2.1 颜色设置

supervision提供Color类和ColorPalette类来设置颜色(调色板)和转换颜色。具体如下:

  1. # 获得默认颜色
  2. import supervision as sv
  3. # WHITE BLACK RED GREEN BLUE YELLOW ROBOFLOW
  4. sv.Color.ROBOFLOW
  1. Color(r=163, g=81, b=251)
  1. # 获得颜色的bgr值
  2. sv.Color(r=255, g=255, b=0).as_bgr()
  3. # 获得rgb值
  4. # sv.Color(r=255, g=255, b=0).as_rgb()
  1. (0, 255, 255)
  1. # 获得颜色的16进制值
  2. sv.Color(r=255, g=255, b=0).as_hex()
  1. '#ffff00'
  1. # 基于16进制色Color对象
  2. sv.Color.from_hex('#ff00ff')
  1. Color(r=255, g=0, b=255)
  1. # 返回默认调色板
  2. sv.ColorPalette.DEFAULT
  3. # sv.ColorPalette.ROBOFLOW
  4. # sv.ColorPalette.LEGACY
  1. ColorPalette(colors=[Color(r=163, g=81, b=251), Color(r=255, g=64, b=64), Color(r=255, g=161, b=160), Color(r=255, g=118, b=51), Color(r=255, g=182, b=51), Color(r=209, g=212, b=53), Color(r=76, g=251, b=18), Color(r=148, g=207, b=26), Color(r=64, g=222, b=138), Color(r=27, g=150, b=64), Color(r=0, g=214, b=193), Color(r=46, g=156, b=170), Color(r=0, g=196, b=255), Color(r=54, g=71, b=151), Color(r=102, g=117, b=255), Color(r=0, g=25, b=239), Color(r=134, g=58, b=255), Color(r=83, g=0, b=135), Color(r=205, g=58, b=255), Color(r=255, g=151, b=202), Color(r=255, g=57, b=201)])
  1. # 返回调试第i个颜色
  2. color_palette = sv.ColorPalette.from_hex(['#ff0000', '#00ff00', '#0000ff'])
  3. color_palette.by_idx(1)
  1. Color(r=0, g=255, b=0)
  1. # 从matpotlib导入调色板
  2. sv.ColorPalette.from_matplotlib('tab20', 5)
  1. ColorPalette(colors=[Color(r=31, g=119, b=180), Color(r=152, g=223, b=138), Color(r=140, g=86, b=75), Color(r=199, g=199, b=199), Color(r=158, g=218, b=229)])

2.2 识别结果可视化示例

supervision提供了多种函数来对识别结果进行可视化(主要针对目标检测和目标跟踪任务)。本文主要介绍目标检测边界框的各种展示效果。关于supervision所有数据注释可视化示例函数见:supervision-doc-annotators

以下是主要示例:

  1. # 获得数据结果
  2. import cv2
  3. import supervision as sv
  4. from ultralytics import YOLO
  5. model = YOLO("yolov8n.pt")
  6. image = cv2.imread("img/person.png")
  7. results = model(image, verbose=False)[0]
  8. # 从YOLOv8中加载数据结果
  9. detections = sv.Detections.from_ultralytics(results)
  10. # 查看输出结果维度
  11. len(detections)
  1. 32

目标框绘制

  1. import supervision as sv
  2. # 设置边界框绘制器
  3. # 参数:color-设置颜色,thickness-线条粗细,color_lookup-颜色映射策略/选项有INDEX、CLASS、TRACK。
  4. bounding_box_annotator = sv.BoundingBoxAnnotator(color= sv.ColorPalette.DEFAULT, thickness = 2, color_lookup = sv.ColorLookup.CLASS)
  5. annotated_frame = bounding_box_annotator.annotate(
  6. scene=image.copy(),
  7. detections=detections
  8. )
  9. sv.plot_image(annotated_frame)

png

圆角目标框绘制

  1. # roundness-边界框边缘的圆度百分比
  2. round_box_annotator = sv.RoundBoxAnnotator(color_lookup = sv.ColorLookup.INDEX, roundness=0.6)
  3. annotated_frame = round_box_annotator.annotate(
  4. scene=image.copy(),
  5. detections=detections
  6. )
  7. sv.plot_image(annotated_frame)

png

角点边界框绘制

  1. import supervision as sv
  2. # corner_length-每个角线的长度,
  3. corner_annotator = sv.BoxCornerAnnotator(corner_length=12, color=sv.Color(r=255, g=255, b=0))
  4. annotated_frame = corner_annotator.annotate(
  5. scene=image.copy(),
  6. detections=detections
  7. )
  8. sv.plot_image(annotated_frame)

png

遮罩边界框绘制

  1. # 颜色遮罩的不透明度
  2. color_annotator = sv.ColorAnnotator(opacity=0.4)
  3. annotated_frame = color_annotator.annotate(
  4. scene=image.copy(),
  5. detections=detections
  6. )
  7. sv.plot_image(annotated_frame)

png

圆形边界框绘制

  1. circle_annotator = sv.CircleAnnotator(color=sv.Color(r=255, g=255, b=128))
  2. annotated_frame = circle_annotator.annotate(
  3. scene=image.copy(),
  4. detections=detections
  5. )
  6. sv.plot_image(annotated_frame)

png

点形边界框绘制

Supervision提供DotAnnotator绘制类以在图像上的目标检测框特定位置绘制关键点,该绘制类有两个独有参数:radius(点的半径),position(点在边界框上的绘制)。position可选参数如下:

  • CENTER = "CENTER"
  • CENTER_LEFT = "CENTER_LEFT"
  • CENTER_RIGHT = "CENTER_RIGHT"
  • TOP_CENTER = "TOP_CENTER"
  • TOP_LEFT = "TOP_LEFT"
  • TOP_RIGHT = "TOP_RIGHT"
  • BOTTOM_LEFT = "BOTTOM_LEFT"
  • BOTTOM_CENTER = "BOTTOM_CENTER"
  • BOTTOM_RIGHT = "BOTTOM_RIGHT"
  • CENTER_OF_MASS = "CENTER_OF_MASS"

通过代码查看position可选参数实现如下:

  1. for i in sv.Position:
  2. print(i)
  1. dot_annotator = sv.DotAnnotator(radius=4)
  2. annotated_frame = dot_annotator.annotate(
  3. scene=image.copy(),
  4. detections=detections
  5. )
  6. sv.plot_image(annotated_frame)

png

三角形边界框绘制

  1. # base/height-三角形的宽高,position-位置
  2. triangle_annotator = sv.TriangleAnnotator(base = 30, height = 30, position = sv.Position['TOP_CENTER'])
  3. annotated_frame = triangle_annotator.annotate(
  4. scene=image.copy(),
  5. detections=detections
  6. )
  7. sv.plot_image(annotated_frame)

png

椭圆形边界框绘制

  1. # start_angle/end_angle-椭圆开始/结束角度
  2. ellipse_annotator = sv.EllipseAnnotator(start_angle=-45, end_angle=215)
  3. annotated_frame = ellipse_annotator.annotate(
  4. scene=image.copy(),
  5. detections=detections
  6. )
  7. sv.plot_image(annotated_frame)

png

置信度边界框绘制

  1. # 用于展示置信度百分比
  2. # border_color-百分比条颜色
  3. # position-位置
  4. # width/height-百分比条宽/高
  5. percentage_bar_annotator = sv.PercentageBarAnnotator(border_color = sv.Color(r=128, g=0, b=0), position=sv.Position['BOTTOM_CENTER'],
  6. width = 100, height = 20)
  7. annotated_frame = percentage_bar_annotator.annotate(
  8. scene=image.copy(),
  9. detections=detections
  10. )
  11. sv.plot_image(annotated_frame)

png

文字描述框绘制

  1. # color-文字背景色,text_color-文字颜色,text_scale-文字大小
  2. # text_position-文字位置,text_thickness-文字粗细,text_padding-文字填充距离
  3. label_annotator = sv.LabelAnnotator(color=sv.Color(r=255, g=255, b=255),text_color=sv.Color(r=128, g=0, b=128), text_scale=2,
  4. text_position=sv.Position.TOP_CENTER, text_thickness=2,text_padding=10)
  5. # 获得各边界框的标签
  6. labels = [
  7. model.model.names[class_id]
  8. for class_id
  9. in detections.class_id
  10. ]
  11. annotated_frame = label_annotator.annotate(
  12. scene=image.copy(),
  13. detections=detections,
  14. labels=labels
  15. )
  16. sv.plot_image(annotated_frame)

png

像素化目标

  1. # pixel_size-像素化的大小。
  2. pixelate_annotator = sv.PixelateAnnotator(pixel_size=12)
  3. annotated_frame = pixelate_annotator.annotate(
  4. scene=image.copy(),
  5. detections=detections
  6. )
  7. # 叠加其他边界框展示效果
  8. annotated_frame = label_annotator.annotate(
  9. scene=annotated_frame.copy(),
  10. detections=detections,
  11. labels=labels
  12. )
  13. sv.plot_image(annotated_frame)

png

2.3 辅助函数

2.3.1 视频相关

读取视频信息

  1. import supervision as sv
  2. # 读取视频文件的宽度、高度、fps和总帧数。
  3. video_info = sv.VideoInfo.from_video_path(video_path="https://media.roboflow.com/supervision/video-examples/people-walking.mp4")
  4. video_info
  1. VideoInfo(width=1920, height=1080, fps=25, total_frames=341)

视频读写

  1. import supervision as sv
  2. from tqdm import tqdm
  3. video_path="https://media.roboflow.com/supervision/video-examples/people-walking.mp4"
  4. video_info = sv.VideoInfo.from_video_path(video_path)
  5. # 获取一个生成视频帧的生成器
  6. # stride: 指示返回帧的时间间隔,默认为1
  7. # start: 开始帧编号,默认为0
  8. # end:结束帧编号,默认为None(一直到视频结束)
  9. frames_generator = sv.get_video_frames_generator(source_path=video_path, stride=10, start=0, end=100)
  10. TARGET_VIDEO_PATH = "out.avi"
  11. # target_path保存路径
  12. with sv.VideoSink(target_path=TARGET_VIDEO_PATH, video_info=video_info) as sink:
  13. for frame in tqdm(frames_generator):
  14. sink.write_frame(frame=frame)
  1. 10it [00:24, 2.47s/it]

fps计算

  1. import supervision as sv
  2. frames_generator = sv.get_video_frames_generator(source_path="https://media.roboflow.com/supervision/video-examples/people-walking.mp4")
  3. # 初始化fps监视器
  4. fps_monitor = sv.FPSMonitor()
  5. for frame in frames_generator:
  6. # 添加时间戳
  7. fps_monitor.tick()
  8. # 根据存储的时间戳计算并返回平均 FPS。
  9. fps = fps_monitor.fps
  10. fps
  1. 174.4186046525204

2.3.2 图像相关

  1. # 保存图片
  2. import supervision as sv
  3. # 创建图像保存类
  4. # target_dir_path-保存路径
  5. # overwrite-是否是否覆盖保存路径,默认False
  6. # image_name_pattern-图像文件名模式。 默认为“image_{:05d}.png”。
  7. with sv.ImageSink(target_dir_path='output', overwrite=True, image_name_pattern= "img_{:05d}.png") as sink:
  8. for image in sv.get_video_frames_generator( source_path='out.avi', stride=2):
  9. sink.save_image(image=image)
  1. # 根据给定的边界框裁剪图像。
  2. import supervision as sv
  3. import cv2
  4. import supervision as sv
  5. from ultralytics import YOLO
  6. model = YOLO("yolov8n.pt")
  7. image = cv2.imread("img/person.png")
  8. results = model(image)[0]
  9. # 从YOLOv8中加载数据结果
  10. detections = sv.Detections.from_ultralytics(results)
  11. with sv.ImageSink(target_dir_path='output') as sink:
  12. for xyxy in detections.xyxy:
  13. # 获得边界框裁剪图像
  14. cropped_image = sv.crop_image(image=image, xyxy=xyxy)
  15. sink.save_image(image=cropped_image)
  1. 0: 384x640 31 persons, 1 bird, 76.8ms
  2. Speed: 2.5ms preprocess, 76.8ms inference, 2.7ms postprocess per image at shape (1, 3, 384, 640)

2.4 其他函数

supervision中还有其他常用类,本文将不对其进行详细介绍,具体情况如下:

3 面向实际任务的工具

3.1 越线数量统计

supversion提供了LineZone类来实现越线数量统计功能,原理很简单就是目标检测+目标跟踪,然后根据车辆的边界框中心点来判断是否穿过预设线,从而实现越线数量统计。代码如下:

  1. import supervision as sv
  2. from ultralytics import YOLO
  3. model = YOLO("yolov8n.pt")
  4. tracker = sv.ByteTrack()
  5. frames_generator = sv.get_video_frames_generator("https://media.roboflow.com/supervision/video-examples/vehicles.mp4",start=0,end=500)
  6. video_info = sv.VideoInfo.from_video_path("https://media.roboflow.com/supervision/video-examples/vehicles.mp4")
  7. w = video_info.width
  8. h = video_info.height
  9. # 设置预设线(从左至右)
  10. start, end = sv.Point(x=0, y=int(h/2)), sv.Point(x=w, y=int(h/2))
  11. # 初始预线检测器
  12. line_zone = sv.LineZone(start=start, end=end)
  13. # 初始化可视化对象
  14. trace_annotator = sv.TraceAnnotator()
  15. label_annotator = sv.LabelAnnotator(text_scale=2,text_color= sv.Color.BLACK)
  16. line_zone_annotator = sv.LineZoneAnnotator(thickness=4, text_thickness=4, text_scale=1)
  17. with sv.ImageSink(target_dir_path='output', overwrite=False, image_name_pattern= "img_{:05d}.png") as sink:
  18. for frame in frames_generator:
  19. result = model(frame)[0]
  20. detections = sv.Detections.from_ultralytics(result)
  21. # 更新目标跟踪器
  22. detections = tracker.update_with_detections(detections)
  23. # 更新预线检测器,crossed_in是否进入结果,crossed_out是否出去结果
  24. crossed_in, crossed_out = line_zone.trigger(detections)
  25. # 获得各边界框的标签
  26. labels = [
  27. f"#{tracker_id} {model.model.names[class_id]}"
  28. for class_id, tracker_id
  29. in zip(detections.class_id, detections.tracker_id)
  30. ]
  31. # 绘制轨迹
  32. annotated_frame = trace_annotator.annotate(scene=frame.copy(), detections=detections)
  33. # 绘制标签
  34. annotated_frame = label_annotator.annotate(scene=annotated_frame, detections=detections, labels=labels)
  35. # 绘制预制线
  36. annotated_frame = line_zone_annotator.annotate(annotated_frame, line_counter=line_zone)
  37. # 数据展示
  38. # sv.plot_image(annotated_frame)
  39. # 保存可视化结果
  40. # sink.save_image(image=annotated_frame)
  41. # 从外到内越线的对象数量,从内到外越线的对象数量。
  42. print(line_zone.in_count, line_zone.out_count)
  43. # 代码输出结果见:https://media.roboflow.com/supervision/cookbooks/count-objects-crossing-the-line-result-1280x720.mp4

3.2 对特定区域进行检测跟踪

supversion提供了PolygonZone类来对特定区域进行检测跟踪,原理很简单就是目标检测或加上目标跟踪,然后选取特定区域来判断目标是否在此区域以及统计当前区域的目标个数。代码如下:

  1. import numpy as np
  2. import supervision as sv
  3. from ultralytics import YOLO
  4. model = YOLO('yolov8n.pt')
  5. # 视频路径
  6. video_path = "https://media.roboflow.com/supervision/video-examples/vehicles-2.mp4"
  7. # 查看视频信息
  8. video_info = sv.VideoInfo.from_video_path(video_path)
  9. print(video_info)
  10. # 读取视频
  11. generator = sv.get_video_frames_generator(video_path)
  12. # 设置要监控的区域
  13. polygons = [
  14. np.array([
  15. [718, 595],[927, 592],[851, 1062],[42, 1059]
  16. ]),
  17. np.array([
  18. [987, 595],[1199, 595],[1893, 1056],[1015, 1062]
  19. ])
  20. ]
  21. # 设置调色盘
  22. colors = sv.ColorPalette.DEFAULT
  23. zones = [
  24. # 定义多边形区域以检测对象。
  25. sv.PolygonZone(
  26. polygon=polygon, # 输入多边形
  27. frame_resolution_wh=video_info.resolution_wh # 全图尺寸
  28. )
  29. for polygon in polygons
  30. ]
  31. # 初始化可视化对象
  32. zone_annotators = [
  33. # 对不同监控区域分开进行可视化
  34. sv.PolygonZoneAnnotator(
  35. zone=zone,
  36. color=colors.by_idx(index), # 颜色
  37. thickness=4, # 线宽
  38. text_thickness=8, # 文本粗细
  39. text_scale=4, # 文本比例
  40. display_in_zone_count=False # 是否展示目标统计个数
  41. )
  42. for index, zone in enumerate(zones)
  43. ]
  44. # 分开为检测区域定义不同的边界框展示
  45. box_annotators = [
  46. sv.BoxAnnotator(
  47. color=colors.by_idx(index),
  48. thickness=4,
  49. text_thickness=4,
  50. text_scale=2
  51. )
  52. for index in range(len(polygons))
  53. ]
  54. with sv.ImageSink(target_dir_path='output', overwrite=False, image_name_pattern= "img_{:05d}.png") as sink:
  55. for frame in generator:
  56. # 为提高识别精度,需要设置模型各大的输入尺寸
  57. results = model(frame, imgsz=1280, verbose=False)[0]
  58. detections = sv.Detections.from_ultralytics(results)
  59. for zone, zone_annotator, box_annotator in zip(zones, zone_annotators, box_annotators):
  60. # 确定哪些目标检测结果位于多边形区域
  61. mask = zone.trigger(detections=detections)
  62. detections_filtered = detections[mask]
  63. frame = box_annotator.annotate(scene=frame, detections=detections_filtered)
  64. frame = zone_annotator.annotate(scene=frame)
  65. # 数据展示
  66. sv.plot_image(frame, (16, 16))
  67. # 保存可视化结果
  68. # sink.save_image(image=annotated_frame)
  69. # 代码输出结果见:https://blog.roboflow.com/content/media/2023/03/trim-counting.mp4

3.3 切片推理

supervision支持对图片进行切片推理以优化小目标识别,即基于SAHI(Slicing Aided Hyper Inference,切片辅助超推理)通过图像切片的方式来检测小目标。SAHI检测过程可以描述为:通过滑动窗口将图像切分成若干区域,各个区域分别进行预测,同时也对整张图片进行推理。然后将各个区域的预测结果和整张图片的预测结果合并,最后用NMS(非极大值抑制)进行过滤。SAHI的具体使用见:基于切片辅助超推理库SAHI优化小目标识别

supervision通过SAHI进行切片推理的示例代码如下所示:

  1. import cv2
  2. import supervision as sv
  3. from ultralytics import YOLO
  4. import numpy as np
  5. model = YOLO("yolov8n.pt")
  6. image = cv2.imread("img/person.png")
  7. results = model(image,verbose=False)[0]
  8. # 从YOLOv8中加载数据结果
  9. detections = sv.Detections.from_ultralytics(results)
  10. # 查看输出结果维度
  11. print("before slicer",len(detections))
  12. # 切片回调函数
  13. def callback(image_slice: np.ndarray) -> sv.Detections:
  14. result = model(image_slice,verbose=False)[0]
  15. return sv.Detections.from_ultralytics(result)
  16. # 设置 Slicing Adaptive Inference(SAHI)处理对象
  17. # callback-对于切片后每张子图进行处理的回调函数
  18. # slice_wh-切片后子图的大小
  19. # overlap_ratio_wh-连续切片之间的重叠率
  20. # iou_threshold-子图合并时用于nms的iou阈值
  21. # thread_workers-处理线程数
  22. slicer = sv.InferenceSlicer(callback = callback, slice_wh=(320,320),
  23. overlap_ratio_wh=(0.3,0.3), iou_threshold=0.4, thread_workers=4)
  24. detections = slicer(image)
  25. # 查看输出结果维度
  26. print("after slicer",len(detections))
  1. before slicer 32
  2. after slicer 53

3.4 轨迹平滑

supervision提供了用于平滑视频跟踪轨迹的实用类DetectionsSmoother。 DetectionsSmoother维护每个轨迹的检测历史记录,并根据这些历史记录提供平滑的预测。具体代码如下:

  1. import supervision as sv
  2. from ultralytics import YOLO
  3. video_path = "https://media.roboflow.com/supervision/video-examples/grocery-store.mp4"
  4. video_info = sv.VideoInfo.from_video_path(video_path=video_path)
  5. frame_generator = sv.get_video_frames_generator(source_path=video_path)
  6. model = YOLO("yolov8n.pt")
  7. tracker = sv.ByteTrack(frame_rate=video_info.fps)
  8. # 跟踪结果平滑器,length-平滑检测时要考虑的最大帧数
  9. smoother = sv.DetectionsSmoother(length=4)
  10. annotator = sv.BoundingBoxAnnotator()
  11. with sv.VideoSink("output.mp4", video_info=video_info) as sink:
  12. for frame in frame_generator:
  13. result = model(frame)[0]
  14. detections = sv.Detections.from_ultralytics(result)
  15. detections = tracker.update_with_detections(detections)
  16. # 平滑目标跟踪轨迹
  17. detections = smoother.update_with_detections(detections)
  18. annotated_frame = annotator.annotate(frame.copy(), detections)
  19. # 数据展示
  20. sv.plot_image(annotated_frame, (16, 16))
  21. # sink.write_frame(annotated_frame)
  22. # 代码输出结果见:https://media.roboflow.com/supervision-detection-smoothing.mp4

4 参考

原文链接:https://www.cnblogs.com/luohenyueji/p/18079658

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号