supervision CV视觉可视化辅助工具

2024-03-28 18:20

本文主要是介绍supervision CV视觉可视化辅助工具,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

参考:
https://supervision.roboflow.com/latest/
https://github.com/roboflow/supervision/tree/develop/examples

版本:

pip install -U supervision

ultralytics-8.1.35 (大于8.1才行,不然可能会有错误AttributeError: ‘Results’ object has no attribute ‘obb’ )
supervision 0.16.0

简单案例:

import cv2
import supervision as sv
from ultralytics import YOLOimage = cv2.imread(...)
model = YOLO('yolov8s.pt')
result = model(image)[0]
detections = sv.Detections.from_ultralytics(result)len(detections)

跟踪案例

https://github.com/roboflow/supervision/tree/develop/examples/heatmap_and_track

运行结果:
在这里插入图片描述

python D:\opencv2\supervision_cv\test.py --source_weights_path "C:\Users\loong\Downloads\yolov8m (1).pt" --source_video_path  "C:\Users\loong\Downloads\istockphoto-1047817112-640_adpp_is.mp4"  --confidence_threshold 0.3 --iou_threshold 0.5 --target_video_path  output_video.mp4

具体代码:

import argparseimport cv2
from ultralytics import YOLOimport supervision as sv
from supervision.assets import VideoAssets, download_assetsdef download_video() -> str:download_assets(VideoAssets.PEOPLE_WALKING)return VideoAssets.PEOPLE_WALKING.valuedef heatmap_and_track(source_weights_path: str,source_video_path: str,target_video_path: str,confidence_threshold: float = 0.35,iou_threshold: float = 0.5,heatmap_alpha: float = 0.5,radius: int = 25,track_threshold: float = 0.35,track_seconds: int = 5,match_threshold: float = 0.99,
) -> None:### instantiate modelmodel = YOLO(source_weights_path)### heatmap configheat_map_annotator = sv.HeatMapAnnotator(position=sv.Position.BOTTOM_CENTER,opacity=heatmap_alpha,radius=radius,kernel_size=25,top_hue=0,low_hue=125,)### annotation configlabel_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER)### get the video fpscap = cv2.VideoCapture(source_video_path)fps = int(cap.get(cv2.CAP_PROP_FPS))cap.release()### tracker configbyte_tracker = sv.ByteTrack(track_thresh=track_threshold,track_buffer=track_seconds * fps,match_thresh=match_threshold,frame_rate=fps,)### video configvideo_info = sv.VideoInfo.from_video_path(video_path=source_video_path)frames_generator = sv.get_video_frames_generator(source_path=source_video_path, stride=1)### Detect, track, annotate, savewith sv.VideoSink(target_path=target_video_path, video_info=video_info) as sink:for frame in frames_generator:result = model(source=frame,classes=[0],  # only person classconf=confidence_threshold,iou=iou_threshold,# show_conf = True,# save_txt = True,# save_conf = True,# save = True,device=None,  # use None = CPU, 0 = single GPU, or [0,1] = dual GPU# agnostic_nms=True)[0]detections = sv.Detections.from_ultralytics(result)  # get detectionsdetections = byte_tracker.update_with_detections(detections)  # update tracker### draw heatmapannotated_frame = heat_map_annotator.annotate(scene=frame.copy(), detections=detections)### draw other attributes from `detections` objectlabels = [f"#{tracker_id}"for class_id, tracker_id in zip(detections.class_id, detections.tracker_id)]label_annotator.annotate(scene=annotated_frame, detections=detections, labels=labels)sink.write_frame(frame=annotated_frame)if __name__ == "__main__":parser = argparse.ArgumentParser(description="Heatmap and Tracking with Supervision")parser.add_argument("--source_weights_path",required=True,help="Path to the source weights file",type=str,)parser.add_argument("--source_video_path",default=download_video(),help="Path to the source video file",type=str,)parser.add_argument("--target_video_path",default="output.mp4",help="Path to the target video file (output)",type=str,)parser.add_argument("--confidence_threshold",default=0.35,help="Confidence threshold for the model",type=float,)parser.add_argument("--iou_threshold",default=0.5,help="IOU threshold for the model",type=float,)parser.add_argument("--heatmap_alpha",default=0.5,help="Opacity of the overlay mask, between 0 and 1",type=float,)parser.add_argument("--radius",default=25,help="Radius of the heat circle",type=float,)parser.add_argument("--track_threshold",default=0.35,help="Detection confidence threshold for track activation",type=float,)parser.add_argument("--track_seconds",default=5,help="Number of seconds to buffer when a track is lost",type=int,)parser.add_argument("--match_threshold",default=0.99,help="Threshold for matching tracks with detections",type=float,)args = parser.parse_args()heatmap_and_track(source_weights_path=args.source_weights_path,source_video_path=args.source_video_path,target_video_path=args.target_video_path,confidence_threshold=args.confidence_threshold,iou_threshold=args.iou_threshold,heatmap_alpha=args.heatmap_alpha,radius=args.radius,track_threshold=args.track_threshold,track_seconds=args.track_seconds,match_threshold=args.match_threshold,)

这篇关于supervision CV视觉可视化辅助工具的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/856360

相关文章

Python数据分析与可视化的全面指南(从数据清洗到图表呈现)

《Python数据分析与可视化的全面指南(从数据清洗到图表呈现)》Python是数据分析与可视化领域中最受欢迎的编程语言之一,凭借其丰富的库和工具,Python能够帮助我们快速处理、分析数据并生成高质... 目录一、数据采集与初步探索二、数据清洗的七种武器1. 缺失值处理策略2. 异常值检测与修正3. 数据

使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)

《使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)》字体设计和矢量图形处理是编程中一个有趣且实用的领域,通过Python的matplotlib库,我们可以轻松将字体轮廓... 目录背景知识字体轮廓的表示实现步骤1. 安装依赖库2. 准备数据3. 解析路径指令4. 绘制图形关键

8种快速易用的Python Matplotlib数据可视化方法汇总(附源码)

《8种快速易用的PythonMatplotlib数据可视化方法汇总(附源码)》你是否曾经面对一堆复杂的数据,却不知道如何让它们变得直观易懂?别慌,Python的Matplotlib库是你数据可视化的... 目录引言1. 折线图(Line Plot)——趋势分析2. 柱状图(Bar Chart)——对比分析3

使用Vue-ECharts实现数据可视化图表功能

《使用Vue-ECharts实现数据可视化图表功能》在前端开发中,经常会遇到需要展示数据可视化的需求,比如柱状图、折线图、饼图等,这类需求不仅要求我们准确地将数据呈现出来,还需要兼顾美观与交互体验,所... 目录前言为什么选择 vue-ECharts?1. 基于 ECharts,功能强大2. 更符合 Vue

Git可视化管理工具(SourceTree)使用操作大全经典

《Git可视化管理工具(SourceTree)使用操作大全经典》本文详细介绍了SourceTree作为Git可视化管理工具的常用操作,包括连接远程仓库、添加SSH密钥、克隆仓库、设置默认项目目录、代码... 目录前言:连接Gitee or github,获取代码:在SourceTree中添加SSH密钥:Cl

Pandas中统计汇总可视化函数plot()的使用

《Pandas中统计汇总可视化函数plot()的使用》Pandas提供了许多强大的数据处理和分析功能,其中plot()函数就是其可视化功能的一个重要组成部分,本文主要介绍了Pandas中统计汇总可视化... 目录一、plot()函数简介二、plot()函数的基本用法三、plot()函数的参数详解四、使用pl

使用Python实现矢量路径的压缩、解压与可视化

《使用Python实现矢量路径的压缩、解压与可视化》在图形设计和Web开发中,矢量路径数据的高效存储与传输至关重要,本文将通过一个Python示例,展示如何将复杂的矢量路径命令序列压缩为JSON格式,... 目录引言核心功能概述1. 路径命令解析2. 路径数据压缩3. 路径数据解压4. 可视化代码实现详解1

Python 交互式可视化的利器Bokeh的使用

《Python交互式可视化的利器Bokeh的使用》Bokeh是一个专注于Web端交互式数据可视化的Python库,本文主要介绍了Python交互式可视化的利器Bokeh的使用,具有一定的参考价值,感... 目录1. Bokeh 简介1.1 为什么选择 Bokeh1.2 安装与环境配置2. Bokeh 基础2

基于Python打造一个可视化FTP服务器

《基于Python打造一个可视化FTP服务器》在日常办公和团队协作中,文件共享是一个不可或缺的需求,所以本文将使用Python+Tkinter+pyftpdlib开发一款可视化FTP服务器,有需要的小... 目录1. 概述2. 功能介绍3. 如何使用4. 代码解析5. 运行效果6.相关源码7. 总结与展望1

Python Dash框架在数据可视化仪表板中的应用与实践记录

《PythonDash框架在数据可视化仪表板中的应用与实践记录》Python的PlotlyDash库提供了一种简便且强大的方式来构建和展示互动式数据仪表板,本篇文章将深入探讨如何使用Dash设计一... 目录python Dash框架在数据可视化仪表板中的应用与实践1. 什么是Plotly Dash?1.1