跳至内容

Analytics using Ultralytics YOLO11

导言

This guide provides a comprehensive overview of three fundamental types of data visualizations: line graphs, bar plots, and pie charts. Each section includes step-by-step instructions and code snippets on how to create these visualizations using Python.



观看: How to generate Analytical Graphs using Ultralytics | Line Graphs, Bar Plots, Area and Pie Charts

视觉样本

折线图条形图饼图
折线图条形图饼图

图表为何重要

  • 折线图是跟踪短期和长期变化以及比较同一时期多组变化的理想工具。
  • 条形图则适用于比较不同类别的数量,并显示类别与其数值之间的关系。
  • 最后,饼图可以有效地说明不同类别之间的比例,并显示整体的各个部分。

分析实例

import cv2

from ultralytics import solutions

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

out = cv2.VideoWriter(
    "ultralytics_analytics.avi",
    cv2.VideoWriter_fourcc(*"MJPG"),
    fps,
    (1920, 1080),  # This is fixed
)

analytics = solutions.Analytics(
    analytics_type="line",
    show=True,
)

frame_count = 0
while cap.isOpened():
    success, im0 = cap.read()
    if success:
        frame_count += 1
        im0 = analytics.process_data(im0, frame_count)  # update analytics graph every frame
        out.write(im0)  # write the video file
    else:
        break

cap.release()
out.release()
cv2.destroyAllWindows()
import cv2

from ultralytics import solutions

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

out = cv2.VideoWriter(
    "ultralytics_analytics.avi",
    cv2.VideoWriter_fourcc(*"MJPG"),
    fps,
    (1920, 1080),  # This is fixed
)

analytics = solutions.Analytics(
    analytics_type="pie",
    show=True,
)

frame_count = 0
while cap.isOpened():
    success, im0 = cap.read()
    if success:
        frame_count += 1
        im0 = analytics.process_data(im0, frame_count)  # update analytics graph every frame
        out.write(im0)  # write the video file
    else:
        break

cap.release()
out.release()
cv2.destroyAllWindows()
import cv2

from ultralytics import solutions

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

out = cv2.VideoWriter(
    "ultralytics_analytics.avi",
    cv2.VideoWriter_fourcc(*"MJPG"),
    fps,
    (1920, 1080),  # This is fixed
)

analytics = solutions.Analytics(
    analytics_type="bar",
    show=True,
)

frame_count = 0
while cap.isOpened():
    success, im0 = cap.read()
    if success:
        frame_count += 1
        im0 = analytics.process_data(im0, frame_count)  # update analytics graph every frame
        out.write(im0)  # write the video file
    else:
        break

cap.release()
out.release()
cv2.destroyAllWindows()
import cv2

from ultralytics import solutions

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

out = cv2.VideoWriter(
    "ultralytics_analytics.avi",
    cv2.VideoWriter_fourcc(*"MJPG"),
    fps,
    (1920, 1080),  # This is fixed
)

analytics = solutions.Analytics(
    analytics_type="area",
    show=True,
)

frame_count = 0
while cap.isOpened():
    success, im0 = cap.read()
    if success:
        frame_count += 1
        im0 = analytics.process_data(im0, frame_count)  # update analytics graph every frame
        out.write(im0)  # write the video file
    else:
        break

cap.release()
out.release()
cv2.destroyAllWindows()

论据 Analytics

下面的表格显示了 Analytics 争论:

名称类型默认值说明
analytics_typestrlineType of graph i.e "line", "bar", "area", "pie"
modelstrNonePath to Ultralytics YOLO Model File
line_widthint2边界框的线条粗细
showboolFalse用于控制是否显示视频流的标志。

论据 model.track

论据类型默认值说明
sourcestrNoneSpecifies the source directory for images or videos. Supports file paths and URLs.
persistboolFalseEnables persistent tracking of objects between frames, maintaining IDs across video sequences.
trackerstrbotsort.yamlSpecifies the tracking algorithm to use, e.g., bytetrack.yamlbotsort.yaml.
conffloat0.3Sets the confidence threshold for detections; lower values allow more objects to be tracked but may include false positives.
ioufloat0.5Sets the Intersection over Union (IoU) threshold for filtering overlapping detections.
classeslistNoneFilters results by class index. For example, classes=[0, 2, 3] only tracks the specified classes.
verboseboolTrueControls the display of tracking results, providing a visual output of tracked objects.

结论

了解何时以及如何使用不同类型的可视化数据对于有效的数据分析至关重要。折线图、柱状图和饼图是基本工具,可以帮助您更清晰、更有效地传达数据信息。

常见问题

How do I create a line graph using Ultralytics YOLO11 Analytics?

To create a line graph using Ultralytics YOLO11 Analytics, follow these steps:

  1. Load a YOLO11 model and open your video file.
  2. 初始化 Analytics 类,类型设置为 "行"。
  3. 迭代视频帧,用相关数据更新折线图,如每帧的对象计数。
  4. 保存显示折线图的输出视频。

例如

import cv2

from ultralytics import solutions

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

out = cv2.VideoWriter(
    "ultralytics_analytics.avi",
    cv2.VideoWriter_fourcc(*"MJPG"),
    fps,
    (1920, 1080),  # This is fixed
)

analytics = solutions.Analytics(
    analytics_type="line",
    show=True,
)

frame_count = 0
while cap.isOpened():
    success, im0 = cap.read()
    if success:
        frame_count += 1
        im0 = analytics.process_data(im0, frame_count)  # update analytics graph every frame
        out.write(im0)  # write the video file
    else:
        break

cap.release()
out.release()
cv2.destroyAllWindows()

有关配置 Analytics 课程,请访问 Analytics using Ultralytics YOLO11 📊 节。

What are the benefits of using Ultralytics YOLO11 for creating bar plots?

Using Ultralytics YOLO11 for creating bar plots offers several benefits:

  1. Real-time Data Visualization: Seamlessly integrate object detection results into bar plots for dynamic updates.
  2. 易用性:简单的应用程序接口和功能使其能够直接实施和可视化数据。
  3. 自定义:自定义标题、标签、颜色等,以满足您的特定要求。
  4. 效率:有效处理大量数据,并在视频处理过程中实时更新绘图。

使用下面的示例生成条形图:

import cv2

from ultralytics import solutions

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

out = cv2.VideoWriter(
    "ultralytics_analytics.avi",
    cv2.VideoWriter_fourcc(*"MJPG"),
    fps,
    (1920, 1080),  # This is fixed
)

analytics = solutions.Analytics(
    analytics_type="bar",
    show=True,
)

frame_count = 0
while cap.isOpened():
    success, im0 = cap.read()
    if success:
        frame_count += 1
        im0 = analytics.process_data(im0, frame_count)  # update analytics graph every frame
        out.write(im0)  # write the video file
    else:
        break

cap.release()
out.release()
cv2.destroyAllWindows()

要了解更多信息,请访问指南中的条形图部分。

Why should I use Ultralytics YOLO11 for creating pie charts in my data visualization projects?

Ultralytics YOLO11 is an excellent choice for creating pie charts because:

  1. 与对象检测集成:直接将对象检测结果整合到饼图中,以便立即获得洞察力。
  2. 用户友好的应用程序接口:只需最少的代码即可轻松设置和使用。
  3. 可定制:颜色、标签等多种定制选项。
  4. 实时更新:实时处理和可视化数据,是视频分析项目的理想选择。

这里有一个简单的例子:

import cv2

from ultralytics import solutions

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

out = cv2.VideoWriter(
    "ultralytics_analytics.avi",
    cv2.VideoWriter_fourcc(*"MJPG"),
    fps,
    (1920, 1080),  # This is fixed
)

analytics = solutions.Analytics(
    analytics_type="pie",
    show=True,
)

frame_count = 0
while cap.isOpened():
    success, im0 = cap.read()
    if success:
        frame_count += 1
        im0 = analytics.process_data(im0, frame_count)  # update analytics graph every frame
        out.write(im0)  # write the video file
    else:
        break

cap.release()
out.release()
cv2.destroyAllWindows()

有关详细信息,请参阅指南中的饼图部分。

Can Ultralytics YOLO11 be used to track objects and dynamically update visualizations?

Yes, Ultralytics YOLO11 can be used to track objects and dynamically update visualizations. It supports tracking multiple objects in real-time and can update various visualizations like line graphs, bar plots, and pie charts based on the tracked objects' data.

跟踪和更新折线图的示例:

import cv2

from ultralytics import solutions

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

out = cv2.VideoWriter(
    "ultralytics_analytics.avi",
    cv2.VideoWriter_fourcc(*"MJPG"),
    fps,
    (1920, 1080),  # This is fixed
)

analytics = solutions.Analytics(
    analytics_type="line",
    show=True,
)

frame_count = 0
while cap.isOpened():
    success, im0 = cap.read()
    if success:
        frame_count += 1
        im0 = analytics.process_data(im0, frame_count)  # update analytics graph every frame
        out.write(im0)  # write the video file
    else:
        break

cap.release()
out.release()
cv2.destroyAllWindows()

要了解完整的功能,请参阅 "跟踪"部分。

What makes Ultralytics YOLO11 different from other object detection solutions like OpenCV and TensorFlow?

Ultralytics YOLO11 stands out from other object detection solutions like OpenCV and TensorFlow for multiple reasons:

  1. State-of-the-art Accuracy: YOLO11 provides superior accuracy in object detection, segmentation, and classification tasks.
  2. 易于使用:用户友好的应用程序接口允许快速实施和集成,无需大量编码。
  3. 实时性能:针对高速推理进行了优化,适合实时应用。
  4. 应用多样:支持多种任务,包括多目标跟踪、自定义模型训练以及导出为不同格式,如ONNX 、TensorRT 和CoreML 。
  5. 全面的文档:广泛的文档博客资源可指导用户完成每个步骤。

有关更详细的比较和使用案例,请浏览我们的Ultralytics 博客

📅 Created 5 months ago ✏️ Updated 8 days ago

评论