Bỏ để qua phần nội dung

Trực quan hóa dữ liệu nâng cao: Bản đồ nhiệt sử dụng Ultralytics YOLOv8 🚀

Giới thiệu về Heatmaps

Bản đồ nhiệt được tạo bằng Ultralytics YOLOv8 Chuyển đổi dữ liệu phức tạp thành một ma trận được mã hóa màu sắc sống động. Công cụ trực quan này sử dụng một phổ màu sắc để đại diện cho các giá trị dữ liệu khác nhau, trong đó màu sắc ấm hơn cho thấy cường độ cao hơn và tông màu lạnh hơn biểu thị giá trị thấp hơn. Bản đồ nhiệt vượt trội trong việc trực quan hóa các mẫu dữ liệu, mối tương quan và sự bất thường phức tạp, cung cấp một cách tiếp cận dễ tiếp cận và hấp dẫn để giải thích dữ liệu trên các lĩnh vực khác nhau.



Xem: Bản đồ nhiệt sử dụng Ultralytics YOLOv8

Tại sao chọn bản đồ nhiệt để phân tích dữ liệu?

  • Trực quan hóa phân phối dữ liệu trực quan: Bản đồ nhiệt đơn giản hóa việc hiểu tập trung và phân phối dữ liệu, chuyển đổi các bộ dữ liệu phức tạp thành các định dạng trực quan dễ hiểu.
  • Phát hiện mẫu hiệu quả: Bằng cách trực quan hóa dữ liệu ở định dạng bản đồ nhiệt, việc phát hiện xu hướng, cụm và ngoại lệ trở nên dễ dàng hơn, tạo điều kiện phân tích và hiểu biết nhanh hơn.
  • Tăng cường phân tích không gian và ra quyết định: Bản đồ nhiệt là công cụ minh họa các mối quan hệ không gian, hỗ trợ quá trình ra quyết định trong các lĩnh vực như kinh doanh thông minh, nghiên cứu môi trường và quy hoạch đô thị.

Ứng dụng trong thế giới thực

Giao thông vận tải Bán lẻ
Ultralytics YOLOv8 Bản đồ nhiệt giao thông Ultralytics YOLOv8 Bản đồ nhiệt bán lẻ
Ultralytics YOLOv8 Bản đồ nhiệt giao thông Ultralytics YOLOv8 Bản đồ nhiệt bán lẻ

Cấu hình bản đồ nhiệt

  • heatmap_alpha: Đảm bảo giá trị này nằm trong phạm vi (0.0 - 1.0).
  • decay_factor: Được sử dụng để loại bỏ heatmap sau khi một đối tượng không còn trong khung, giá trị của nó cũng phải nằm trong phạm vi (0.0 - 1.0).

Bản đồ nhiệt sử dụng Ultralytics YOLOv8 Ví dụ

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",
                     classes_names=model.names)

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,
                     classes_names=model.names)

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
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 polygon points
region_points = [(20, 400), (1080, 404), (1080, 360), (20, 360), (20, 400)]

# 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,
                     classes_names=model.names)

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,
                     classes_names=model.names)

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",
                     classes_names=model.names)

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",
                     classes_names=model.names)

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()

Lập luận set_args

Tên Kiểu Mặc định Sự miêu tả
view_img bool False Hiển thị khung bằng bản đồ nhiệt
colormap cv2.COLORMAP None CV2. COLORMAP cho bản đồ nhiệt
imw int None Chiều rộng của bản đồ nhiệt
imh int None Chiều cao của bản đồ nhiệt
line_thickness int 2 Tăng hộp giới hạn và đếm độ dày văn bản
view_in_counts bool True Chỉ hiển thị số lượng trên khung video
view_out_counts bool True Chỉ hiển thị số lượng vượt trội trên khung hình video
classes_names dict model.model.names Từ điển tên lớp
heatmap_alpha float 0.5 Giá trị alpha của bản đồ nhiệt
count_reg_pts list None Điểm vùng đếm đối tượng
count_txt_color RGB Color (0, 0, 0) Màu nền trước cho đối tượng đếm văn bản
count_reg_color RGB Color (255, 0, 255) Đếm màu vùng
region_thickness int 5 Đếm giá trị độ dày vùng
decay_factor float 0.99 Hệ số phân rã để loại bỏ khu vực bản đồ nhiệt sau thời gian cụ thể
shape str circle Hình dạng bản đồ nhiệt để hiển thị "rect" hoặc "circle" được hỗ trợ
line_dist_thresh int 15 Ngưỡng khoảng cách Euclide cho bộ đếm dòng
count_bg_color RGB Color (255, 255, 255) Đếm màu tô sáng
cls_txtdisplay_gap int 50 Hiển thị khoảng cách giữa mỗi lớp học

Lập luận model.track

Tên Kiểu Mặc định Sự miêu tả
source im0 None Thư mục nguồn cho hình ảnh hoặc video
persist bool False Các rãnh liên tục giữa các khung hình
tracker str botsort.yaml Phương pháp theo dõi 'bytetrack' hoặc 'botsort'
conf float 0.3 Ngưỡng tin cậy
iou float 0.5 Ngưỡng IOU
classes list None Lọc kết quả theo lớp, tức là lớp = 0 hoặc lớp = [0,2,3]

Bản đồ nhiệt COLORMAPs

Tên bản đồ màu Sự miêu tả
cv::COLORMAP_AUTUMN Bản đồ màu mùa thu
cv::COLORMAP_BONE Bản đồ màu xương
cv::COLORMAP_JET Bản đồ màu máy bay phản lực
cv::COLORMAP_WINTER Bản đồ màu mùa đông
cv::COLORMAP_RAINBOW Bản đồ màu cầu vồng
cv::COLORMAP_OCEAN Bản đồ màu đại dương
cv::COLORMAP_SUMMER Bản đồ màu mùa hè
cv::COLORMAP_SPRING Bản đồ màu mùa xuân
cv::COLORMAP_COOL Bản đồ màu sắc mát mẻ
cv::COLORMAP_HSV Bản đồ màu HSV (Hue, Saturation, Value)
cv::COLORMAP_PINK Bản đồ màu hồng
cv::COLORMAP_HOT Bản đồ màu nóng
cv::COLORMAP_PARULA Bản đồ màu Parula
cv::COLORMAP_MAGMA Bản đồ màu magma
cv::COLORMAP_INFERNO Bản đồ màu địa ngục
cv::COLORMAP_PLASMA Bản đồ màu plasma
cv::COLORMAP_VIRIDIS Bản đồ màu Viridis
cv::COLORMAP_CIVIDIS Bản đồ màu Cividis
cv::COLORMAP_TWILIGHT Bản đồ màu hoàng hôn
cv::COLORMAP_TWILIGHT_SHIFTED Bản đồ màu Chạng vạng thay đổi
cv::COLORMAP_TURBO Bản đồ màu Turbo
cv::COLORMAP_DEEPGREEN Bản đồ màu xanh lá cây đậm

Các bản đồ màu này thường được sử dụng để trực quan hóa dữ liệu với các biểu diễn màu khác nhau.



Đã tạo 2023-12-07, Cập nhật 2024-04-05
Tác giả: RizwanMunawar (8), glenn-jocher (7), AyushExel (1), 1579093407@qq.com (1)

Ý kiến