λ€μμ μ¬μ©ν λΆμ 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 |
μ¬λ¬ μ μ κ²½μ°, μ΄κΈ° μ μ μμ νκΈ° μ μ νλ μμ κ·Έλ €μ§ μ΄ μ μ λλ€. |
points_width |
int |
15 |
μ ν¬μΈνΈ νκ΄νμ λλΉ. |
μΈμ model.track
μΈμ | μ ν | κΈ°λ³Έκ° | μ€λͺ |
---|---|---|---|
source |
str |
None |
Specifies the source directory for images or videos. Supports file paths and URLs. |
persist |
bool |
False |
Enables persistent tracking of objects between frames, maintaining IDs across video sequences. |
tracker |
str |
botsort.yaml |
Specifies the tracking algorithm to use, e.g., bytetrack.yaml λλ botsort.yaml . |
conf |
float |
0.3 |
Sets the confidence threshold for detections; lower values allow more objects to be tracked but may include false positives. |
iou |
float |
0.5 |
Sets the Intersection over Union (IoU) threshold for filtering overlapping detections. |
classes |
list |
None |
Filters results by class index. For example, classes=[0, 2, 3] only tracks the specified classes. |
verbose |
bool |
True |
Controls the display of tracking results, providing a visual output of tracked objects. |
κ²°λ‘
ν¨κ³Όμ μΈ λ°μ΄ν° λΆμμ μν΄μλ λ€μν μ νμ μκ°νλ₯Ό μΈμ , μ΄λ»κ² μ¬μ©ν΄μΌ νλμ§ μ΄ν΄νλ κ²μ΄ μ€μν©λλ€. μ κ·Έλν, λ§λν μ°¨νΈ, νμ΄ μ°¨νΈλ λ°μ΄ν°μ μ€ν 리λ₯Ό λ³΄λ€ λͺ ννκ³ ν¨κ³Όμ μΌλ‘ μ λ¬νλ λ° λμμ΄ λλ κΈ°λ³Έμ μΈ λꡬμ λλ€.
μμ£Ό 묻λ μ§λ¬Έ
Ultralytics YOLOv8 μ λ리ν±μ€λ₯Ό μ¬μ©νμ¬ λΌμΈ κ·Έλνλ₯Ό λ§λ€λ €λ©΄ μ΄λ»κ² ν΄μΌ νλμ?
Ultralytics YOLOv8 μ λ리ν±μ€λ₯Ό μ¬μ©νμ¬ λΌμΈ κ·Έλνλ₯Ό λ§λ€λ €λ©΄ λ€μ λ¨κ³λ₯Ό λ°λ₯΄μΈμ:
- YOLOv8 λͺ¨λΈμ λ‘λνκ³ λμμ νμΌμ μ½λλ€.
- μ΄κΈ°ν
Analytics
ν΄λμ€μ μ νμ "line"μΌλ‘ μ€μ ν©λλ€. - λΉλμ€ νλ μμ λ°λ³΅νμ¬ νλ μλΉ κ°μ²΄ μμ κ°μ κ΄λ ¨ λ°μ΄ν°λ‘ μ κ·Έλνλ₯Ό μ λ°μ΄νΈν©λλ€.
- μ κ·Έλνκ° νμλ μΆλ ₯ λμμμ μ μ₯ν©λλ€.
μμ:
import cv2
from ultralytics import YOLO, solutions
model = YOLO("yolov8s.pt")
cap = cv2.VideoCapture("Path/to/video/file.mp4")
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)
while cap.isOpened():
success, frame = cap.read()
if success:
results = model.track(frame, persist=True)
total_counts = sum([1 for box in results[0].boxes.xyxy])
analytics.update_line(frame_count, total_counts)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
cap.release()
out.release()
cv2.destroyAllWindows()
ꡬμ±μ λν μμΈν λ΄μ©μ Analytics
ν΄λμ€λ₯Ό λ°©λ¬Ένμμμ€. Ultralytics YOLOv8 πμ μ¬μ©ν λΆμ μΉμ
μΌλ‘ μ΄λν©λλ€.
λ§λν μ°¨νΈ μμ±μ Ultralytics YOLOv8 μ μ¬μ©νλ©΄ μ΄λ€ μ΄μ μ΄ μλμ?
λ§λν μ°¨νΈ μμ±μ Ultralytics YOLOv8 μ μ¬μ©νλ©΄ λͺ κ°μ§ μ΄μ μ΄ μμ΅λλ€:
- μ€μκ° λ°μ΄ν° μκ°ν: κ°μ²΄ κ°μ§ κ²°κ³Όλ₯Ό λ§λν μ°¨νΈμ μννκ² ν΅ν©νμ¬ λμ μΌλ‘ μ λ°μ΄νΈν μ μμ΅λλ€.
- μ¬μ© νΈμμ±: κ°λ¨ν APIμ ν¨μλ‘ λ°μ΄ν°λ₯Ό μ½κ² ꡬννκ³ μκ°νν μ μμ΅λλ€.
- μ¬μ©μ μ§μ : μ λͺ©, λΌλ²¨, μμ λ±μ νΉμ μꡬμ¬νμ λ§κ² μ¬μ©μ μ§μ νμΈμ.
- ν¨μ¨μ±: λμ©λ λ°μ΄ν°λ₯Ό ν¨μ¨μ μΌλ‘ μ²λ¦¬νκ³ λμμ μ²λ¦¬ μ€μ μ€μκ°μΌλ‘ νλ‘―μ μ λ°μ΄νΈν μ μμ΅λλ€.
λ€μ μμ λ₯Ό μ¬μ©νμ¬ λ§λν μ°¨νΈλ₯Ό μμ±ν©λλ€:
import cv2
from ultralytics import YOLO, solutions
model = YOLO("yolov8s.pt")
cap = cv2.VideoCapture("Path/to/video/file.mp4")
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)
while cap.isOpened():
success, frame = cap.read()
if success:
results = model.track(frame, persist=True)
clswise_count = {
model.names[int(cls)]: boxes.size(0)
for cls, boxes in zip(results[0].boxes.cls.tolist(), results[0].boxes.xyxy)
}
analytics.update_bar(clswise_count)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
cap.release()
out.release()
cv2.destroyAllWindows()
μμΈν μμλ³΄λ €λ©΄ κ°μ΄λμ λ§λν μ°¨νΈ μΉμ μ μ°Έμ‘°νμΈμ.
λ°μ΄ν° μκ°ν νλ‘μ νΈμμ νμ΄ μ°¨νΈλ₯Ό λ§λ€ λ Ultralytics YOLOv8 μ μ¬μ©ν΄μΌ νλ μ΄μ λ 무μμΈκ°μ?
Ultralytics YOLOv8 λ νμ΄ μ°¨νΈλ₯Ό λ§λλ λ° νμν μ νμ λλ€:
- κ°μ²΄ κ°μ§μ ν΅ν©: κ°μ²΄ κ°μ§ κ²°κ³Όλ₯Ό νμ΄ μ°¨νΈμ μ§μ ν΅ν©νμ¬ μ¦κ°μ μΈ μΈμ¬μ΄νΈλ₯Ό μ»μ μ μμ΅λλ€.
- μ¬μ©μ μΉνμ μΈ API: μ΅μνμ μ½λλ‘ κ°νΈνκ² μ€μ νκ³ μ¬μ©ν μ μμ΅λλ€.
- μ¬μ©μ μ§μ κ°λ₯: μμ, λΌλ²¨ λ±μ λν λ€μν μ¬μ©μ μ§μ μ΅μ μ΄ μμ΅λλ€.
- μ€μκ° μ λ°μ΄νΈ: μ€μκ°μΌλ‘ λ°μ΄ν°λ₯Ό μ²λ¦¬νκ³ μκ°νν μ μμ΄ λμμ λΆμ νλ‘μ νΈμ μ΄μμ μ λλ€.
λ€μμ κ°λ¨ν μμ λλ€:
import cv2
from ultralytics import YOLO, solutions
model = YOLO("yolov8s.pt")
cap = cv2.VideoCapture("Path/to/video/file.mp4")
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)
while cap.isOpened():
success, frame = cap.read()
if success:
results = model.track(frame, persist=True)
clswise_count = {
model.names[int(cls)]: boxes.size(0)
for cls, boxes in zip(results[0].boxes.cls.tolist(), results[0].boxes.xyxy)
}
analytics.update_pie(clswise_count)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
cap.release()
out.release()
cv2.destroyAllWindows()
μμΈν λ΄μ©μ κ°μ΄λμ νμ΄ μ°¨νΈ μΉμ μ μ°Έμ‘°νμΈμ.
Ultralytics YOLOv8 μ μ¬μ©νμ¬ κ°μ²΄λ₯Ό μΆμ νκ³ λΉμ£ΌμΌλ¦¬μ μ΄μ μ λμ μΌλ‘ μ λ°μ΄νΈν μ μλμ?
μ, Ultralytics YOLOv8 μ μ¬μ©νμ¬ κ°μ²΄λ₯Ό μΆμ νκ³ μκ°νλ₯Ό λμ μΌλ‘ μ λ°μ΄νΈν μ μμ΅λλ€. μ¬λ¬ κ°μ²΄λ₯Ό μ€μκ°μΌλ‘ μΆμ ν μ μμΌλ©° μΆμ λ κ°μ²΄μ λ°μ΄ν°λ₯Ό κΈ°λ°μΌλ‘ μ κ·Έλν, λ§λν μ°¨νΈ, μν μ°¨νΈ λ± λ€μν μκ°νλ₯Ό μ λ°μ΄νΈν μ μμ΅λλ€.
λΌμΈ κ·Έλν μΆμ λ° μ λ°μ΄νΈ μμ:
import cv2
from ultralytics import YOLO, solutions
model = YOLO("yolov8s.pt")
cap = cv2.VideoCapture("Path/to/video/file.mp4")
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)
while cap.isOpened():
success, frame = cap.read()
if success:
results = model.track(frame, persist=True)
total_counts = sum([1 for box in results[0].boxes.xyxy])
analytics.update_line(frame_count, total_counts)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
cap.release()
out.release()
cv2.destroyAllWindows()
μ 체 κΈ°λ₯μ λν΄ μμΈν μμλ³΄λ €λ©΄ μΆμ μΉμ μ μ°Έμ‘°νμΈμ.
Ultralytics YOLOv8 λ€λ₯Έ κ°μ²΄ κ°μ§ μ루μ (μ: OpenCV λ° TensorFlow)κ³Ό λ€λ₯Έ μ μ 무μμΈκ°μ?
Ultralytics YOLOv8 λ μ¬λ¬ κ°μ§ μ΄μ λ‘ OpenCVλ TensorFlow κ°μ λ€λ₯Έ κ°μ²΄ κ°μ§ μ루μ κ³Ό μ°¨λ³νλ©λλ€:
- μ΅μ²¨λ¨ μ νλ: YOLOv8 λ 물체 κ°μ§, μΈλΆν λ° λΆλ₯ μμ μμ λ°μ΄λ μ νλλ₯Ό μ 곡ν©λλ€.
- μ¬μ© νΈμμ±: μ¬μ©μ μΉνμ μΈ APIλ₯Ό ν΅ν΄ κ΄λ²μν μ½λ© μμ΄λ λΉ λ₯΄κ² ꡬννκ³ ν΅ν©ν μ μμ΅λλ€.
- μ€μκ° μ±λ₯: μ€μκ° μ ν리μΌμ΄μ μ μ ν©ν κ³ μ μΆλ‘ μ μ΅μ νλμ΄ μμ΅λλ€.
- λ€μν μ ν리μΌμ΄μ : λ€μ€ κ°μ²΄ μΆμ , μ¬μ©μ μ§μ λͺ¨λΈ νλ ¨, ONNX, TensorRT, CoreML κ³Ό κ°μ λ€μν νμμΌλ‘ λ΄λ³΄λ΄κΈ° λ± λ€μν μμ μ μ§μν©λλ€.
- μ’ ν©μ μΈ λ¬Έμ: λͺ¨λ λ¨κ³λ₯Ό μλ΄νλ λ°©λν λ¬Έμμ λΈλ‘κ·Έ 리μμ€.
μμΈν λΉκ΅ λ° μ¬μ© μ¬λ‘λ Ultralytics λΈλ‘κ·Έμμ νμΈν μ μμ΅λλ€.