跳至内容

高级数据可视化:使用Ultralytics YOLOv8 绘制热图 🚀

热图简介

生成的热图 Ultralytics YOLOv8将复杂的数据转换成生动的彩色编码矩阵。这种可视化工具采用色谱来表示不同的数据值,暖色调表示较高的强度,冷色调表示较低的值。热图在可视化复杂的数据模式、相关性和异常情况方面表现出色,为不同领域的数据解读提供了一种易于理解且引人入胜的方法。



观看: 使用热图Ultralytics YOLOv8

为什么选择热力图进行数据分析?

  • 直观的数据分布可视化:热图可简化对数据集中和分布的理解,将复杂的数据集转换为易于理解的可视化格式。
  • 高效模式检测:通过热图格式的可视化数据,可以更容易地发现趋势、群组和异常值,从而促进更快地分析和洞察。
  • 增强空间分析和决策:热图有助于说明空间关系,帮助商业智能、环境研究和城市规划等领域的决策过程。

真实世界的应用

交通运输 零售
Ultralytics YOLOv8 交通热图 Ultralytics YOLOv8 零售业热图
Ultralytics YOLOv8 交通热图 Ultralytics YOLOv8 零售业热图

热图配置

  • heatmap_alpha:确保该值在 (0.0 - 1.0) 范围内。
  • decay_factor:用于在帧中不再存在对象后删除热图,其值也应在 (0.0 - 1.0) 范围内。

热图使用Ultralytics YOLOv8 示例

from ultralytics import YOLO
from ultralytics.solutions import heatmap
import cv2

model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))

# Video writer
video_writer = cv2.VideoWriter("heatmap_output.avi",
                               cv2.VideoWriter_fourcc(*'mp4v'),
                               fps,
                               (w, h))

# Init heatmap
heatmap_obj = heatmap.Heatmap()
heatmap_obj.set_args(colormap=cv2.COLORMAP_PARULA,
                     imw=w,
                     imh=h,
                     view_img=True,
                     shape="circle")

while cap.isOpened():
    success, im0 = cap.read()
    if not success:
        print("Video frame is empty or video processing has been successfully completed.")
        break
    tracks = model.track(im0, persist=True, show=False)

    im0 = heatmap_obj.generate_heatmap(im0, tracks)
    video_writer.write(im0)

cap.release()
video_writer.release()
cv2.destroyAllWindows()
from ultralytics import YOLO
from ultralytics.solutions import heatmap
import cv2

model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))

# Video writer
video_writer = cv2.VideoWriter("heatmap_output.avi",
                               cv2.VideoWriter_fourcc(*'mp4v'),
                               fps,
                               (w, h))

line_points = [(20, 400), (1080, 404)]  # line for object counting

# Init heatmap
heatmap_obj = heatmap.Heatmap()
heatmap_obj.set_args(colormap=cv2.COLORMAP_PARULA,
                     imw=w,
                     imh=h,
                     view_img=True,
                     shape="circle",
                     count_reg_pts=line_points)

while cap.isOpened():
    success, im0 = cap.read()
    if not success:
        print("Video frame is empty or video processing has been successfully completed.")
        break
    tracks = model.track(im0, persist=True, show=False)

    im0 = heatmap_obj.generate_heatmap(im0, tracks)
    video_writer.write(im0)

cap.release()
video_writer.release()
cv2.destroyAllWindows()
from ultralytics import YOLO
from ultralytics.solutions import heatmap
import cv2

model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))

# Video writer
video_writer = cv2.VideoWriter("heatmap_output.avi",
                               cv2.VideoWriter_fourcc(*'mp4v'),
                               fps,
                               (w, h))

# Define region points
region_points = [(20, 400), (1080, 404), (1080, 360), (20, 360)]

# Init heatmap
heatmap_obj = heatmap.Heatmap()
heatmap_obj.set_args(colormap=cv2.COLORMAP_PARULA,
                     imw=w,
                     imh=h,
                     view_img=True,
                     shape="circle",
                     count_reg_pts=region_points)

while cap.isOpened():
    success, im0 = cap.read()
    if not success:
        print("Video frame is empty or video processing has been successfully completed.")
        break
    tracks = model.track(im0, persist=True, show=False)

    im0 = heatmap_obj.generate_heatmap(im0, tracks)
    video_writer.write(im0)

cap.release()
video_writer.release()
cv2.destroyAllWindows()
from ultralytics import YOLO
from ultralytics.solutions import heatmap
import cv2

model = YOLO("yolov8s.pt")   # YOLOv8 custom/pretrained model

im0 = cv2.imread("path/to/image.png")  # path to image file
h, w = im0.shape[:2]  # image height and width

# Heatmap Init
heatmap_obj = heatmap.Heatmap()
heatmap_obj.set_args(colormap=cv2.COLORMAP_PARULA,
                     imw=w,
                     imh=h,
                     view_img=True,
                     shape="circle")

results = model.track(im0, persist=True)
im0 = heatmap_obj.generate_heatmap(im0, tracks=results)
cv2.imwrite("ultralytics_output.png", im0)
from ultralytics import YOLO
from ultralytics.solutions import heatmap
import cv2

model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))

# Video writer
video_writer = cv2.VideoWriter("heatmap_output.avi",
                               cv2.VideoWriter_fourcc(*'mp4v'),
                               fps,
                               (w, h))

classes_for_heatmap = [0, 2]  # classes for heatmap

# Init heatmap
heatmap_obj = heatmap.Heatmap()
heatmap_obj.set_args(colormap=cv2.COLORMAP_PARULA,
                     imw=w,
                     imh=h,
                     view_img=True,
                     shape="circle")

while cap.isOpened():
    success, im0 = cap.read()
    if not success:
        print("Video frame is empty or video processing has been successfully completed.")
        break
    tracks = model.track(im0, persist=True, show=False,
                         classes=classes_for_heatmap)

    im0 = heatmap_obj.generate_heatmap(im0, tracks)
    video_writer.write(im0)

cap.release()
video_writer.release()
cv2.destroyAllWindows()

论据 set_args

名称 类型 默认值 说明
view_img bool False 用热图显示框架
colormap cv2.COLORMAP None cv2.COLORMAP 用于热图
imw int None 热图宽度
imh int None 热图高度
heatmap_alpha float 0.5 热图阿尔法值
count_reg_pts list None 物体计数区域点
count_txt_thickness int 2 计数值文字大小
count_txt_color RGB Color (0, 0, 0) 对象计数文本的前景色
count_color RGB Color (255, 255, 255) 对象计数文本的背景颜色
count_reg_color RGB Color (255, 0, 255) 计数区域颜色
region_thickness int 5 计数区厚度值
decay_factor float 0.99 特定时间后移除热图区域的衰减系数
shape str circle 支持 "矩形 "或 "圆形 "热图显示形状
line_dist_thresh int 15 直线计数器的欧氏距离阈值

论据 model.track

名称 类型 默认值 说明
source im0 None 图像或视频的源目录
persist bool False 帧与帧之间的持久轨迹
tracker str botsort.yaml 跟踪方法 "bytetrack "或 "botsort
conf float 0.3 置信度阈值
iou float 0.5 借据阈值
classes list None 按类别筛选结果,即 classes=0,或 classes=[0,2,3]

热图 COLORMAPs

彩色地图名称 说明
cv::COLORMAP_AUTUMN 秋色地图
cv::COLORMAP_BONE 骨骼颜色图
cv::COLORMAP_JET 喷气彩图
cv::COLORMAP_WINTER 冬季彩色地图
cv::COLORMAP_RAINBOW 彩虹颜色地图
cv::COLORMAP_OCEAN 海洋颜色地图
cv::COLORMAP_SUMMER 夏季彩色地图
cv::COLORMAP_SPRING 春色地图
cv::COLORMAP_COOL 酷炫的彩色地图
cv::COLORMAP_HSV HSV(色调、饱和度、值)色彩图
cv::COLORMAP_PINK 粉色地图
cv::COLORMAP_HOT 热门彩色地图
cv::COLORMAP_PARULA 帕鲁拉彩图
cv::COLORMAP_MAGMA 岩浆颜色图
cv::COLORMAP_INFERNO 地狱彩色地图
cv::COLORMAP_PLASMA 等离子彩图
cv::COLORMAP_VIRIDIS Viridis 彩色地图
cv::COLORMAP_CIVIDIS Cividis 彩色地图
cv::COLORMAP_TWILIGHT 黄昏彩色地图
cv::COLORMAP_TWILIGHT_SHIFTED 偏移的曙光色彩图
cv::COLORMAP_TURBO 涡轮颜色图
cv::COLORMAP_DEEPGREEN 深绿色彩图

这些色图通常用于用不同的颜色表示可视化数据。



创建于 2023-12-07,更新于 2024-02-03
作者:glenn-jocher(7),AyushExel(1),chr043416@gmail.com(6),1579093407@qq.com(1)

评论