Ultralytics YOLOv8 πμ μ¬μ©ν λΆμ
μκ°
μ΄ κ°μ΄λμμλ μ κ·Έλν, λ§λν μ°¨νΈ, μν μ°¨νΈμ μΈ κ°μ§ κΈ°λ³Έ λ°μ΄ν° μκ°ν μ νμ λν ν¬κ΄μ μΈ κ°μλ₯Ό μ 곡ν©λλ€. κ° μΉμ μλ Python μ μ¬μ©νμ¬ μ΄λ¬ν μκ°νλ₯Ό λ§λλ λ°©λ²μ λν λ¨κ³λ³ μ§μΉ¨κ³Ό μ½λ μ€λν«μ΄ ν¬ν¨λμ΄ μμ΅λλ€.
μκ°μ μν
μ κ·Έλν | λ§λ νλ‘― | νμ΄ μ°¨νΈ |
---|---|---|
κ·Έλνκ° μ€μν μ΄μ
- μ κ·Έλνλ μ₯λ¨κΈ° λ³νλ₯Ό μΆμ νκ³ κ°μ κΈ°κ° λμ μ¬λ¬ κ·Έλ£Ήμ λ³νλ₯Ό λΉκ΅νλ λ° μ΄μμ μ λλ€.
- λ°λ©΄ λ§λν λ§λκ·Έλνλ μ¬λ¬ μΉ΄ν κ³ λ¦¬μ μλμ λΉκ΅νκ³ μΉ΄ν κ³ λ¦¬μ ν΄λΉ μμΉ μ¬μ΄μ κ΄κ³λ₯Ό νμνλ λ° μ ν©ν©λλ€.
- λ§μ§λ§μΌλ‘ νμ΄ μ°¨νΈλ μΉ΄ν κ³ λ¦¬ κ°μ λΉμ¨μ μ€λͺ νκ³ μ 체μ μΌλΆλ₯Ό νμνλ λ° ν¨κ³Όμ μ λλ€.
λΆμ μμ
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()
μΈμ Analytics
λ€μμ νμ
λλ€. Analytics
μΈμλ₯Ό μ¬μ©ν©λλ€:
μ΄λ¦ | μ ν | κΈ°λ³Έκ° | μ€λͺ |
---|---|---|---|
type |
str |
None |
λ°μ΄ν° λλ κ°μ²΄μ μ νμ λλ€. |
im0_shape |
tuple |
None |
μ΄κΈ° μ΄λ―Έμ§μ λͺ¨μμ λλ€. |
writer |
cv2.VideoWriter |
None |
λΉλμ€ νμΌ μ°κΈ°μ© κ°μ²΄μ λλ€. |
title |
str |
ultralytics |
λΉμ£ΌμΌλ¦¬μ μ΄μ μ μ λͺ©μ λλ€. |
x_label |
str |
x |
XμΆμ λ μ΄λΈμ λλ€. |
y_label |
str |
y |
YμΆμ λ μ΄λΈμ λλ€. |
bg_color |
str |
white |
λ°°κ²½μ. |
fg_color |
str |
black |
μ κ²½μ. |
line_color |
str |
yellow |
μ μ μμ. |
line_width |
int |
2 |
μ μ λλΉμ λλ€. |
fontsize |
int |
13 |
ν μ€νΈμ κΈκΌ΄ ν¬κΈ°μ λλ€. |
view_img |
bool |
False |
μ΄λ―Έμ§ λλ λμμμ νμνλ €λ©΄ νλκ·Έλ₯Ό ν΄λ¦ν©λλ€. |
save_img |
bool |
True |
νλκ·Έλ₯Ό μ§μ νμ¬ μ΄λ―Έμ§ λλ λμμμ μ μ₯ν©λλ€. |
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. |
μΈμ model.track
μ΄λ¦ | μ ν | κΈ°λ³Έκ° | μ€λͺ |
---|---|---|---|
source |
im0 |
None |
μ΄λ―Έμ§ λλ λΉλμ€μ μμ€ λλ ν 리 |
persist |
bool |
False |
νλ μ κ° νΈλ μ§μ |
tracker |
str |
botsort.yaml |
μΆμ λ°©λ² 'λ°μ΄νΈνΈλ' λλ 'λ΄μνΈ' |
conf |
float |
0.3 |
μ λ’° μκ³κ° |
iou |
float |
0.5 |
IOU μκ³κ° |
classes |
list |
None |
ν΄λμ€λ³λ‘ κ²°κ³Όλ₯Ό νν°λ§ν©λλ€(μ: classes=0 λλ classes=[0,2,3]). |
verbose |
bool |
True |
κ°μ²΄ μΆμ κ²°κ³Ό νμ |
κ²°λ‘
ν¨κ³Όμ μΈ λ°μ΄ν° λΆμμ μν΄μλ λ€μν μ νμ μκ°νλ₯Ό μΈμ , μ΄λ»κ² μ¬μ©ν΄μΌ νλμ§ μ΄ν΄νλ κ²μ΄ μ€μν©λλ€. μ κ·Έλν, λ§λν μ°¨νΈ, νμ΄ μ°¨νΈλ λ°μ΄ν°μ μ€ν 리λ₯Ό λ³΄λ€ λͺ ννκ³ ν¨κ³Όμ μΌλ‘ μ λ¬νλ λ° λμμ΄ λλ κΈ°λ³Έμ μΈ λꡬμ λλ€.