Phân tích bằng cách sử dụng Ultralytics YOLOv8 📊
Giới thiệu
Hướng dẫn này cung cấp tổng quan toàn diện về ba loại trực quan hóa dữ liệu cơ bản: biểu đồ đường, biểu đồ thanh và biểu đồ tròn. Mỗi phần bao gồm hướng dẫn từng bước và đoạn mã về cách tạo các trực quan hóa này bằng cách sử dụng Python.
Mẫu hình ảnh
Biểu đồ đường | Cốt truyện quán bar | Biểu đồ hình tròn |
---|---|---|
Tại sao đồ thị lại quan trọng
- Biểu đồ đường lý tưởng để theo dõi các thay đổi trong thời gian ngắn và dài và để so sánh các thay đổi cho nhiều nhóm trong cùng một khoảng thời gian.
- Mặt khác, biểu đồ thanh phù hợp để so sánh số lượng trên các danh mục khác nhau và hiển thị mối quan hệ giữa một danh mục và giá trị số của nó.
- Cuối cùng, biểu đồ hình tròn có hiệu quả để minh họa tỷ lệ giữa các danh mục và hiển thị các phần của tổng thể.
Ví dụ về Analytics
import cv2
from ultralytics import YOLO, solutions
model = YOLO("yolov8s.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))
out = cv2.VideoWriter("line_plot.avi", cv2.VideoWriter_fourcc(*"MJPG"), fps, (w, h))
analytics = solutions.Analytics(
type="line",
writer=out,
im0_shape=(w, h),
view_img=True,
)
total_counts = 0
frame_count = 0
while cap.isOpened():
success, frame = cap.read()
if success:
frame_count += 1
results = model.track(frame, persist=True, verbose=True)
if results[0].boxes.id is not None:
boxes = results[0].boxes.xyxy.cpu()
for box in boxes:
total_counts += 1
analytics.update_line(frame_count, total_counts)
total_counts = 0
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
import cv2
from ultralytics import YOLO, solutions
model = YOLO("yolov8s.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))
out = cv2.VideoWriter("multiple_line_plot.avi", cv2.VideoWriter_fourcc(*"MJPG"), fps, (w, h))
analytics = solutions.Analytics(
type="line",
writer=out,
im0_shape=(w, h),
view_img=True,
max_points=200,
)
frame_count = 0
data = {}
labels = []
while cap.isOpened():
success, frame = cap.read()
if success:
frame_count += 1
results = model.track(frame, persist=True)
if results[0].boxes.id is not None:
boxes = results[0].boxes.xyxy.cpu()
track_ids = results[0].boxes.id.int().cpu().tolist()
clss = results[0].boxes.cls.cpu().tolist()
for box, track_id, cls in zip(boxes, track_ids, clss):
# Store each class label
if model.names[int(cls)] not in labels:
labels.append(model.names[int(cls)])
# Store each class count
if model.names[int(cls)] in data:
data[model.names[int(cls)]] += 1
else:
data[model.names[int(cls)]] = 0
# update lines every frame
analytics.update_multiple_lines(data, labels, frame_count)
data = {} # clear the data list for next frame
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
import cv2
from ultralytics import YOLO, solutions
model = YOLO("yolov8s.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))
out = cv2.VideoWriter("pie_chart.avi", cv2.VideoWriter_fourcc(*"MJPG"), fps, (w, h))
analytics = solutions.Analytics(
type="pie",
writer=out,
im0_shape=(w, h),
view_img=True,
)
clswise_count = {}
while cap.isOpened():
success, frame = cap.read()
if success:
results = model.track(frame, persist=True, verbose=True)
if results[0].boxes.id is not None:
boxes = results[0].boxes.xyxy.cpu()
clss = results[0].boxes.cls.cpu().tolist()
for box, cls in zip(boxes, clss):
if model.names[int(cls)] in clswise_count:
clswise_count[model.names[int(cls)]] += 1
else:
clswise_count[model.names[int(cls)]] = 1
analytics.update_pie(clswise_count)
clswise_count = {}
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
import cv2
from ultralytics import YOLO, solutions
model = YOLO("yolov8s.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))
out = cv2.VideoWriter("bar_plot.avi", cv2.VideoWriter_fourcc(*"MJPG"), fps, (w, h))
analytics = solutions.Analytics(
type="bar",
writer=out,
im0_shape=(w, h),
view_img=True,
)
clswise_count = {}
while cap.isOpened():
success, frame = cap.read()
if success:
results = model.track(frame, persist=True, verbose=True)
if results[0].boxes.id is not None:
boxes = results[0].boxes.xyxy.cpu()
clss = results[0].boxes.cls.cpu().tolist()
for box, cls in zip(boxes, clss):
if model.names[int(cls)] in clswise_count:
clswise_count[model.names[int(cls)]] += 1
else:
clswise_count[model.names[int(cls)]] = 1
analytics.update_bar(clswise_count)
clswise_count = {}
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
import cv2
from ultralytics import YOLO, solutions
model = YOLO("yolov8s.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))
out = cv2.VideoWriter("area_plot.avi", cv2.VideoWriter_fourcc(*"MJPG"), fps, (w, h))
analytics = solutions.Analytics(
type="area",
writer=out,
im0_shape=(w, h),
view_img=True,
)
clswise_count = {}
frame_count = 0
while cap.isOpened():
success, frame = cap.read()
if success:
frame_count += 1
results = model.track(frame, persist=True, verbose=True)
if results[0].boxes.id is not None:
boxes = results[0].boxes.xyxy.cpu()
clss = results[0].boxes.cls.cpu().tolist()
for box, cls in zip(boxes, clss):
if model.names[int(cls)] in clswise_count:
clswise_count[model.names[int(cls)]] += 1
else:
clswise_count[model.names[int(cls)]] = 1
analytics.update_area(frame_count, clswise_count)
clswise_count = {}
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
Lý lẽ Analytics
Đây là bảng với Analytics
Lập luận:
Tên | Kiểu | Mặc định | Sự miêu tả |
---|---|---|---|
type |
str |
None |
Loại dữ liệu hoặc đối tượng. |
im0_shape |
tuple |
None |
Hình dạng của hình ảnh ban đầu. |
writer |
cv2.VideoWriter |
None |
Đối tượng để ghi các tập tin video. |
title |
str |
ultralytics |
Tiêu đề cho trực quan hóa. |
x_label |
str |
x |
Nhãn cho trục x. |
y_label |
str |
y |
Nhãn cho trục y. |
bg_color |
str |
white |
Màu nền. |
fg_color |
str |
black |
Màu nền trước. |
line_color |
str |
yellow |
Màu sắc của các dòng. |
line_width |
int |
2 |
Chiều rộng của các dòng. |
fontsize |
int |
13 |
Cỡ chữ cho văn bản. |
view_img |
bool |
False |
Gắn cờ để hiển thị hình ảnh hoặc video. |
save_img |
bool |
True |
Gắn cờ để lưu hình ảnh hoặc video. |
max_points |
int |
50 |
For multiple lines, total points drawn on frame, before deleting initial points. |
points_width |
int |
15 |
Width of line points highlighter. |
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] |
verbose |
bool |
True |
Hiển thị kết quả theo dõi đối tượng |
Kết thúc
Hiểu khi nào và làm thế nào để sử dụng các loại trực quan hóa khác nhau là rất quan trọng để phân tích dữ liệu hiệu quả. Biểu đồ đường, biểu đồ thanh và biểu đồ hình tròn là những công cụ cơ bản có thể giúp bạn truyền tải câu chuyện dữ liệu của mình rõ ràng và hiệu quả hơn.