Analytics met Ultralytics YOLOv8 π
Inleiding
Deze handleiding biedt een uitgebreid overzicht van drie fundamentele soorten datavisualisaties: lijngrafieken, staafdiagrammen en taartdiagrammen. Elk hoofdstuk bevat stapsgewijze instructies en codefragmenten voor het maken van deze visualisaties met Python.
Visuele voorbeelden
Lijngrafiek | Balk | Cirkeldiagram |
---|---|---|
Waarom grafieken belangrijk zijn
- Lijngrafieken zijn ideaal voor het bijhouden van veranderingen over korte en lange perioden en voor het vergelijken van veranderingen voor meerdere groepen over dezelfde periode.
- Staafdiagrammen zijn daarentegen geschikt voor het vergelijken van hoeveelheden in verschillende categorieΓ«n en het tonen van relaties tussen een categorie en de numerieke waarde ervan.
- Tot slot zijn cirkeldiagrammen effectief voor het illustreren van verhoudingen tussen categorieΓ«n en het tonen van delen van een geheel.
Analytics-voorbeelden
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()
Argument Analytics
Hier is een tabel met de Analytics
argumenten:
Naam | Type | Standaard | Beschrijving |
---|---|---|---|
type |
str |
None |
Type gegevens of object. |
im0_shape |
tuple |
None |
Vorm van de oorspronkelijke afbeelding. |
writer |
cv2.VideoWriter |
None |
Object voor het schrijven van videobestanden. |
title |
str |
ultralytics |
Titel voor de visualisatie. |
x_label |
str |
x |
Label voor de x-as. |
y_label |
str |
y |
Label voor de y-as. |
bg_color |
str |
white |
Achtergrondkleur. |
fg_color |
str |
black |
Voorgrondkleur. |
line_color |
str |
yellow |
Kleur van de lijnen. |
line_width |
int |
2 |
Breedte van de lijnen. |
fontsize |
int |
13 |
Lettergrootte voor tekst. |
view_img |
bool |
False |
Vlag om de afbeelding of video weer te geven. |
save_img |
bool |
True |
Vlag om de afbeelding of video op te slaan. |
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. |
Argumenten model.track
Naam | Type | Standaard | Beschrijving |
---|---|---|---|
source |
im0 |
None |
bronmap voor afbeeldingen of video's |
persist |
bool |
False |
Blijvende sporen tussen frames |
tracker |
str |
botsort.yaml |
Volgmethode 'bytetrack' of 'botsort' |
conf |
float |
0.3 |
Vertrouwensdrempel |
iou |
float |
0.5 |
Drempel IOU |
classes |
list |
None |
filter resultaten op klasse, d.w.z. classes=0, of classes=[0,2,3] |
verbose |
bool |
True |
De resultaten van het volgen van objecten weergeven |
Conclusie
Begrijpen wanneer en hoe je verschillende soorten visualisaties moet gebruiken is cruciaal voor effectieve gegevensanalyse. Lijngrafieken, staafdiagrammen en taartdiagrammen zijn fundamentele hulpmiddelen die je kunnen helpen om het verhaal van je gegevens duidelijker en effectiever over te brengen.