─░├žeri─če ge├ž

Analytics kullanarak Ultralytics YOLOv8 ­čôŐ

Giri┼č

Bu k─▒lavuz, ├╝├ž temel veri g├Ârselle┼čtirme t├╝r├╝ne kapsaml─▒ bir genel bak─▒┼č sunmaktad─▒r: ├žizgi grafikler, ├žubuk grafikler ve pasta grafikler. Her b├Âl├╝m, Python adresini kullanarak bu g├Ârselle┼čtirmelerin nas─▒l olu┼čturulaca─č─▒na dair ad─▒m ad─▒m talimatlar ve kod par├žac─▒klar─▒ i├žermektedir.

G├Ârsel ├ľrnekler

├çizgi Grafi─či ├çubuk Arsa Pasta Grafi─či
├çizgi Grafi─či ├çubuk Arsa Pasta Grafi─či

Grafikler Neden ├ľnemlidir?

  • ├çizgi grafikler, k─▒sa ve uzun d├Ânemlerdeki de─či┼čiklikleri izlemek ve ayn─▒ d├Ânemde birden fazla grup i├žin de─či┼čiklikleri kar┼č─▒la┼čt─▒rmak i├žin idealdir.
  • ├ľte yandan ├žubuk grafikler, farkl─▒ kategorilerdeki miktarlar─▒ kar┼č─▒la┼čt─▒rmak ve bir kategori ile say─▒sal de─čeri aras─▒ndaki ili┼čkileri g├Âstermek i├žin uygundur.
  • Son olarak, pasta grafikler kategoriler aras─▒ndaki oranlar─▒ g├Âstermek ve bir b├╝t├╝n├╝n par├žalar─▒n─▒ g├Âstermek i├žin etkilidir.

Analitik ├ľrnekler

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

Tart─▒┼čma Analytics

─░┼čte bir tablo Analytics Arg├╝manlar:

─░sim Tip Varsay─▒lan A├ž─▒klama
type str None Veri veya nesne t├╝r├╝.
im0_shape tuple None ─░lk g├Âr├╝nt├╝n├╝n ┼čekli.
writer cv2.VideoWriter None Video dosyalar─▒n─▒ yazmak i├žin nesne.
title str ultralytics G├Ârselle┼čtirme i├žin ba┼čl─▒k.
x_label str x X ekseni i├žin etiket.
y_label str y Y ekseni i├žin etiket.
bg_color str white Arka plan rengi.
fg_color str black ├ľn plan rengi.
line_color str yellow Çizgilerin rengi.
line_width int 2 ├çizgilerin geni┼čli─či.
fontsize int 13 Metin i├žin yaz─▒ tipi boyutu.
view_img bool False G├Âr├╝nt├╝y├╝ veya videoyu g├Âr├╝nt├╝lemek i├žin bayrak.
save_img bool True G├Âr├╝nt├╝y├╝ veya videoyu kaydetmek i├žin i┼čaretleyin.
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.

Arg├╝manlar model.track

─░sim Tip Varsay─▒lan A├ž─▒klama
source im0 None resimler veya videolar i├žin kaynak dizin
persist bool False kareler aras─▒nda kal─▒c─▒ izler
tracker str botsort.yaml ─░zleme y├Ântemi 'bytetrack' veya 'botsort'
conf float 0.3 G├╝ven E┼či─či
iou float 0.5 IOU E┼či─či
classes list None sonu├žlar─▒ s─▒n─▒fa g├Âre filtreleyin, yani classes=0 veya classes=[0,2,3]
verbose bool True Nesne izleme sonu├žlar─▒n─▒ g├Âr├╝nt├╝leme

Sonu├ž

Farkl─▒ g├Ârselle┼čtirme t├╝rlerinin ne zaman ve nas─▒l kullan─▒laca─č─▒n─▒ anlamak, etkili veri analizi i├žin ├žok ├Ânemlidir. ├çizgi grafikler, ├žubuk grafikler ve pasta grafikler, verilerinizin hikayesini daha net ve etkili bir ┼čekilde aktarman─▒za yard─▒mc─▒ olabilecek temel ara├žlard─▒r.



Created 2024-05-23, Updated 2024-06-10
Authors: glenn-jocher (3), IvorZhu331 (1), RizwanMunawar (3)

Yorumlar