๊ณ ๊ธ ๋ฐ์ดํฐ ์๊ฐํ: Ultralytics YOLOv8 ๐์ ์ฌ์ฉํ ํํธ ๋งต
ํํธ๋งต ์๊ฐ
๋ก ์์ฑ๋ ํํธ๋งต์ Ultralytics YOLOv8 ๋ก ์์ฑ๋ ํํธ๋งต์ ๋ณต์กํ ๋ฐ์ดํฐ๋ฅผ ์์ํ ์์์ผ๋ก ๊ตฌ๋ถ๋ ๋งคํธ๋ฆญ์ค๋ก ๋ณํํฉ๋๋ค. ์ด ์๊ฐ์ ๋๊ตฌ๋ ๋ค์ํ ์์ ์คํํธ๋ผ์ ์ฌ์ฉํ์ฌ ๋ค์ํ ๋ฐ์ดํฐ ๊ฐ์ ๋ํ๋ด๋ฉฐ, ๋ฐ๋ปํ ์์กฐ๋ ๋ ๋์ ๊ฐ๋๋ฅผ, ์ฐจ๊ฐ์ด ์์กฐ๋ ๋ ๋ฎ์ ๊ฐ์ ๋ํ๋ ๋๋ค. ํํธ๋งต์ ๋ณต์กํ ๋ฐ์ดํฐ ํจํด, ์๊ด๊ด๊ณ, ์ด์ ์งํ๋ฅผ ์๊ฐํํ๋ ๋ฐ ํ์ํ๋ฉฐ, ๋ค์ํ ์์ญ์์ ๋ฐ์ดํฐ๋ฅผ ํด์ํ๋ ๋ฐ ์ ๊ทผํ๊ธฐ ์ฝ๊ณ ๋งค๋ ฅ์ ์ธ ์ ๊ทผ ๋ฐฉ์์ ์ ๊ณตํฉ๋๋ค.
Watch: ํํธ๋งต์ ์ฌ์ฉํ Ultralytics YOLOv8
๋ฐ์ดํฐ ๋ถ์์ ํํธ๋งต์ ์ ํํ๋ ์ด์ ๋ ๋ฌด์์ธ๊ฐ์?
- ์ง๊ด์ ์ธ ๋ฐ์ดํฐ ๋ถํฌ ์๊ฐํ: ํํธ๋งต์ ๋ณต์กํ ๋ฐ์ดํฐ ์งํฉ์ ์ดํดํ๊ธฐ ์ฌ์ด ์๊ฐ์ ํ์์ผ๋ก ๋ณํํ์ฌ ๋ฐ์ดํฐ ์ง์ค๋์ ๋ถํฌ์ ๋ํ ์ดํด๋ฅผ ๊ฐ์ํํฉ๋๋ค.
- ํจ์จ์ ์ธ ํจํด ํ์ง: ๋ฐ์ดํฐ๋ฅผ ํํธ๋งต ํ์์ผ๋ก ์๊ฐํํ๋ฉด ์ถ์ธ, ๊ตฐ์ง, ์ด์๊ฐ์ ๋ ์ฝ๊ฒ ๋ฐ๊ฒฌํ ์ ์์ด ๋ถ์๊ณผ ์ธ์ฌ์ดํธ๋ฅผ ๋ ๋น ๋ฅด๊ฒ ์ป์ ์ ์์ต๋๋ค.
- ํฅ์๋ ๊ณต๊ฐ ๋ถ์ ๋ฐ ์์ฌ ๊ฒฐ์ : ํํธ๋งต์ ๊ณต๊ฐ ๊ด๊ณ๋ฅผ ์ค๋ช ํ๋ ๋ฐ ์ ์ฉํ๋ฉฐ ๋น์ฆ๋์ค ์ธํ ๋ฆฌ์ ์ค, ํ๊ฒฝ ์ฐ๊ตฌ, ๋์ ๊ณํ๊ณผ ๊ฐ์ ๋ถ์ผ์ ์์ฌ ๊ฒฐ์ ํ๋ก์ธ์ค๋ฅผ ๋์ต๋๋ค.
์ค์ ์ ํ๋ฆฌ์ผ์ด์
๊ตํตํธ | ๋ฆฌํ ์ผ |
---|---|
Ultralytics YOLOv8 ์ด์ก ํํธ๋งต | Ultralytics YOLOv8 ๋ฆฌํ ์ผ ํํธ๋งต |
ํํธ๋งต ๊ตฌ์ฑ
heatmap_alpha
: ์ด ๊ฐ์ด ๋ฒ์(0.0 - 1.0) ๋ด์ ์๋์ง ํ์ธํฉ๋๋ค.decay_factor
: ๊ฐ์ฒด๊ฐ ๋ ์ด์ ํ๋ ์์ ์๋ ํ ํํธ๋งต์ ์ ๊ฑฐํ๋ ๋ฐ ์ฌ์ฉ๋๋ฉฐ, ๊ฐ๋ (0.0 - 1.0) ๋ฒ์์ฌ์ผ ํฉ๋๋ค.
Ultralytics YOLOv8 ์์ ๋ฅผ ์ฌ์ฉํ ํํธ๋งต
from ultralytics import YOLO
from ultralytics.solutions import heatmap
import cv2
model = YOLO("yolov8n.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))
# Video writer
video_writer = cv2.VideoWriter("heatmap_output.avi",
cv2.VideoWriter_fourcc(*'mp4v'),
fps,
(w, h))
# Init heatmap
heatmap_obj = heatmap.Heatmap()
heatmap_obj.set_args(colormap=cv2.COLORMAP_PARULA,
imw=w,
imh=h,
view_img=True,
shape="circle",
classes_names=model.names)
while cap.isOpened():
success, im0 = cap.read()
if not success:
print("Video frame is empty or video processing has been successfully completed.")
break
tracks = model.track(im0, persist=True, show=False)
im0 = heatmap_obj.generate_heatmap(im0, tracks)
video_writer.write(im0)
cap.release()
video_writer.release()
cv2.destroyAllWindows()
from ultralytics import YOLO
from ultralytics.solutions import heatmap
import cv2
model = YOLO("yolov8n.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))
# Video writer
video_writer = cv2.VideoWriter("heatmap_output.avi",
cv2.VideoWriter_fourcc(*'mp4v'),
fps,
(w, h))
line_points = [(20, 400), (1080, 404)] # line for object counting
# Init heatmap
heatmap_obj = heatmap.Heatmap()
heatmap_obj.set_args(colormap=cv2.COLORMAP_PARULA,
imw=w,
imh=h,
view_img=True,
shape="circle",
count_reg_pts=line_points,
classes_names=model.names)
while cap.isOpened():
success, im0 = cap.read()
if not success:
print("Video frame is empty or video processing has been successfully completed.")
break
tracks = model.track(im0, persist=True, show=False)
im0 = heatmap_obj.generate_heatmap(im0, tracks)
video_writer.write(im0)
cap.release()
video_writer.release()
cv2.destroyAllWindows()
from ultralytics import YOLO
import heatmap
import cv2
model = YOLO("yolov8n.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))
# Video writer
video_writer = cv2.VideoWriter("heatmap_output.avi",
cv2.VideoWriter_fourcc(*'mp4v'),
fps,
(w, h))
# Define polygon points
region_points = [(20, 400), (1080, 404), (1080, 360), (20, 360), (20, 400)]
# Init heatmap
heatmap_obj = heatmap.Heatmap()
heatmap_obj.set_args(colormap=cv2.COLORMAP_PARULA,
imw=w,
imh=h,
view_img=True,
shape="circle",
count_reg_pts=region_points,
classes_names=model.names)
while cap.isOpened():
success, im0 = cap.read()
if not success:
print("Video frame is empty or video processing has been successfully completed.")
break
tracks = model.track(im0, persist=True, show=False)
im0 = heatmap_obj.generate_heatmap(im0, tracks)
video_writer.write(im0)
cap.release()
video_writer.release()
cv2.destroyAllWindows()
from ultralytics import YOLO
from ultralytics.solutions import heatmap
import cv2
model = YOLO("yolov8n.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))
# Video writer
video_writer = cv2.VideoWriter("heatmap_output.avi",
cv2.VideoWriter_fourcc(*'mp4v'),
fps,
(w, h))
# Define region points
region_points = [(20, 400), (1080, 404), (1080, 360), (20, 360)]
# Init heatmap
heatmap_obj = heatmap.Heatmap()
heatmap_obj.set_args(colormap=cv2.COLORMAP_PARULA,
imw=w,
imh=h,
view_img=True,
shape="circle",
count_reg_pts=region_points,
classes_names=model.names)
while cap.isOpened():
success, im0 = cap.read()
if not success:
print("Video frame is empty or video processing has been successfully completed.")
break
tracks = model.track(im0, persist=True, show=False)
im0 = heatmap_obj.generate_heatmap(im0, tracks)
video_writer.write(im0)
cap.release()
video_writer.release()
cv2.destroyAllWindows()
from ultralytics import YOLO
from ultralytics.solutions import heatmap
import cv2
model = YOLO("yolov8s.pt") # YOLOv8 custom/pretrained model
im0 = cv2.imread("path/to/image.png") # path to image file
h, w = im0.shape[:2] # image height and width
# Heatmap Init
heatmap_obj = heatmap.Heatmap()
heatmap_obj.set_args(colormap=cv2.COLORMAP_PARULA,
imw=w,
imh=h,
view_img=True,
shape="circle",
classes_names=model.names)
results = model.track(im0, persist=True)
im0 = heatmap_obj.generate_heatmap(im0, tracks=results)
cv2.imwrite("ultralytics_output.png", im0)
from ultralytics import YOLO
from ultralytics.solutions import heatmap
import cv2
model = YOLO("yolov8n.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))
# Video writer
video_writer = cv2.VideoWriter("heatmap_output.avi",
cv2.VideoWriter_fourcc(*'mp4v'),
fps,
(w, h))
classes_for_heatmap = [0, 2] # classes for heatmap
# Init heatmap
heatmap_obj = heatmap.Heatmap()
heatmap_obj.set_args(colormap=cv2.COLORMAP_PARULA,
imw=w,
imh=h,
view_img=True,
shape="circle",
classes_names=model.names)
while cap.isOpened():
success, im0 = cap.read()
if not success:
print("Video frame is empty or video processing has been successfully completed.")
break
tracks = model.track(im0, persist=True, show=False,
classes=classes_for_heatmap)
im0 = heatmap_obj.generate_heatmap(im0, tracks)
video_writer.write(im0)
cap.release()
video_writer.release()
cv2.destroyAllWindows()
์ธ์ set_args
์ด๋ฆ | ์ ํ | ๊ธฐ๋ณธ๊ฐ | ์ค๋ช |
---|---|---|---|
view_img |
bool |
False |
ํํธ๋งต์ผ๋ก ํ๋ ์ ํ์ |
colormap |
cv2.COLORMAP |
None |
ํํธ๋งต์ฉ cv2.COLORMAP |
imw |
int |
None |
ํํธ๋งต ๋๋น |
imh |
int |
None |
ํํธ๋งต ๋์ด |
line_thickness |
int |
2 |
๊ฒฝ๊ณ ์์ ๋๋ฆฌ๊ธฐ ๋ฐ ํ ์คํธ ๋๊ป ๊ณ์ฐ |
view_in_counts |
bool |
True |
๋น๋์ค ํ๋ ์์๋ง ์ธ์นด์ดํธ ํ์ |
view_out_counts |
bool |
True |
๋น๋์ค ํ๋ ์์๋ง ์์ ์นด์ดํธ ํ์ |
classes_names |
dict |
model.model.names |
ํด๋์ค ์ด๋ฆ ์ฌ์ |
heatmap_alpha |
float |
0.5 |
ํํธ๋งต ์ํ ๊ฐ |
count_reg_pts |
list |
None |
๊ฐ์ฒด ๊ณ์ฐ ์์ญ ํฌ์ธํธ |
count_txt_color |
RGB Color |
(0, 0, 0) |
๊ฐ์ฒด ์ ํ ์คํธ์ ์ ๊ฒฝ์ |
count_reg_color |
RGB Color |
(255, 0, 255) |
๊ณ์ฐ ์์ญ ์์ |
region_thickness |
int |
5 |
์์ญ ๋๊ป ๊ฐ ๊ณ์ฐ |
decay_factor |
float |
0.99 |
ํน์ ์๊ฐ ํ ํํธ๋งต ์์ญ ์ ๊ฑฐ๋ฅผ ์ํ ๊ฐ์ ๊ณ์ |
shape |
str |
circle |
"์ง์ฌ๊ฐํ" ๋๋ "์" ํ์์ฉ ํํธ๋งต ๋ชจ์ ์ง์ |
line_dist_thresh |
int |
15 |
๋ผ์ธ ์นด์ดํฐ์ ์ ํด๋ฆฌ๋ ๊ฑฐ๋ฆฌ ์๊ณ๊ฐ |
count_bg_color |
RGB Color |
(255, 255, 255) |
ํ๊ดํ ์์ ๊ณ์ฐ |
cls_txtdisplay_gap |
int |
50 |
๊ฐ ํด๋์ค ์ ์ฌ์ด์ ๊ฐ๊ฒฉ ํ์ |
์ธ์ 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]). |
ํํธ๋งต ์ปฌ๋ฌ๋งต
์ปฌ๋ฌ๋งต ์ด๋ฆ | ์ค๋ช |
---|---|
cv::COLORMAP_AUTUMN |
๊ฐ์ ์ ์ง๋ |
cv::COLORMAP_BONE |
๋ผ๋ ์์ ๋งต |
cv::COLORMAP_JET |
์ ํธ ์ปฌ๋ฌ ๋งต |
cv::COLORMAP_WINTER |
๊ฒจ์ธ ์์ ์ง๋ |
cv::COLORMAP_RAINBOW |
๋ ์ธ๋ณด์ฐ ์ปฌ๋ฌ ๋งต |
cv::COLORMAP_OCEAN |
๋ฐ๋ค ์์ ์ง๋ |
cv::COLORMAP_SUMMER |
์ฌ๋ฆ ์์ ์ง๋ |
cv::COLORMAP_SPRING |
๋ด ์ปฌ๋ฌ ๋งต |
cv::COLORMAP_COOL |
๋ฉ์ง ์ปฌ๋ฌ ๋งต |
cv::COLORMAP_HSV |
HSV(์์กฐ, ์ฑ๋, ๊ฐ) ์์ ๋งต |
cv::COLORMAP_PINK |
ํํฌ ์ปฌ๋ฌ ๋งต |
cv::COLORMAP_HOT |
ํซ ์ปฌ๋ฌ ๋งต |
cv::COLORMAP_PARULA |
ํ๋ฃฐ๋ผ ์ปฌ๋ฌ ๋งต |
cv::COLORMAP_MAGMA |
๋ง๊ทธ๋ง ์ปฌ๋ฌ ๋งต |
cv::COLORMAP_INFERNO |
์ธํ๋ฅด๋ ธ ์ปฌ๋ฌ ๋งต |
cv::COLORMAP_PLASMA |
ํ๋ผ์ฆ๋ง ์์ ๋งต |
cv::COLORMAP_VIRIDIS |
๋น๋ฆฌ๋์ค ์ปฌ๋ฌ ๋งต |
cv::COLORMAP_CIVIDIS |
์๋น๋์ค ์ปฌ๋ฌ ๋งต |
cv::COLORMAP_TWILIGHT |
ํฉํผ ์ปฌ๋ฌ ๋งต |
cv::COLORMAP_TWILIGHT_SHIFTED |
์ํํธ๋ ํฉํผ ์ปฌ๋ฌ ๋งต |
cv::COLORMAP_TURBO |
ํฐ๋ณด ์ปฌ๋ฌ ๋งต |
cv::COLORMAP_DEEPGREEN |
๋ฅ ๊ทธ๋ฆฐ ์ปฌ๋ฌ ๋งต |
์ด๋ฌํ ์ปฌ๋ฌ๋งต์ ์ผ๋ฐ์ ์ผ๋ก ๋ค์ํ ์์ ํํ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์๊ฐํํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
2023-12-07 ์์ฑ, 2024-04-05 ์ ๋ฐ์ดํธ
์์ฑ์: ๋ฆฌ์ฆ์ ๋ฌด๋์๋ฅด (8), ๊ธ๋ ์กฐ์ฒ (7), ์์ ์ ์์ (1), 1579093407@qq.com (1)