์ฝ˜ํ…์ธ ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

Ultralytics YOLOv8 ๐Ÿš€์„ ์‚ฌ์šฉํ•œ ๊ฐœ์ฒด ์ž๋ฅด๊ธฐ

๊ฐœ์ฒด ์ž๋ฅด๊ธฐ๋ž€ ๋ฌด์—‡์ธ๊ฐ€์š”?

๋ฅผ ์‚ฌ์šฉํ•œ ๊ฐœ์ฒด ์ž๋ฅด๊ธฐ Ultralytics YOLOv8 ๋ฅผ ์‚ฌ์šฉํ•œ ๊ฐ์ฒด ์ž๋ฅด๊ธฐ์—๋Š” ์ด๋ฏธ์ง€ ๋˜๋Š” ๋™์˜์ƒ์—์„œ ๊ฐ์ง€๋œ ํŠน์ • ๊ฐ์ฒด๋ฅผ ๋ถ„๋ฆฌํ•˜๊ณ  ์ถ”์ถœํ•˜๋Š” ์ž‘์—…์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. YOLOv8 ๋ชจ๋ธ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐœ์ฒด๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ์‹๋ณ„ํ•˜๊ณ  ๋ฌ˜์‚ฌํ•˜์—ฌ ์ถ”๊ฐ€ ๋ถ„์„ ๋˜๋Š” ์กฐ์ž‘์„ ์œ„ํ•œ ์ •๋ฐ€ํ•œ ์ž๋ฅด๊ธฐ๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.



Watch: Object Cropping using Ultralytics YOLOv8

์˜ค๋ธŒ์ ํŠธ ์ž๋ฅด๊ธฐ์˜ ์žฅ์ ์€?

  • ์ง‘์ค‘ ๋ถ„์„: YOLOv8 ๋Œ€์ƒ ๊ฐœ์ฒด ์ž๋ฅด๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์žฅ๋ฉด ๋‚ด์—์„œ ๊ฐœ๋ณ„ ํ•ญ๋ชฉ์„ ์‹ฌ์ธต์ ์œผ๋กœ ์กฐ์‚ฌํ•˜๊ฑฐ๋‚˜ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ฐ์ดํ„ฐ ์šฉ๋Ÿ‰ ๊ฐ์†Œ: ๊ฐœ์ฒด ์ž๋ฅด๊ธฐ๋Š” ๊ด€๋ จ ๊ฐœ์ฒด๋งŒ ์ถ”์ถœํ•จ์œผ๋กœ์จ ๋ฐ์ดํ„ฐ ํฌ๊ธฐ๋ฅผ ์ตœ์†Œํ™”ํ•˜์—ฌ ์ €์žฅ, ์ „์†ก ๋˜๋Š” ํ›„์† ๊ณ„์‚ฐ ์ž‘์—…์„ ํšจ์œจ์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค๋‹ˆ๋‹ค.
  • ํ–ฅ์ƒ๋œ ์ •๋ฐ€๋„: YOLOv8 ์˜ ๊ฐ์ฒด ๊ฐ์ง€ ์ •ํ™•๋„๋Š” ์ž˜๋ฆฐ ๊ฐ์ฒด๊ฐ€ ๊ณต๊ฐ„ ๊ด€๊ณ„๋ฅผ ์œ ์ง€ํ•˜๋„๋ก ๋ณด์žฅํ•˜์—ฌ ์ƒ์„ธํ•œ ๋ถ„์„์„ ์œ„ํ•ด ์‹œ๊ฐ์  ์ •๋ณด์˜ ๋ฌด๊ฒฐ์„ฑ์„ ๋ณด์กดํ•ฉ๋‹ˆ๋‹ค.

๋น„์ฃผ์–ผ

๊ณตํ•ญ ์ˆ˜ํ•˜๋ฌผ
๊ณตํ•ญ ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ๋ฅผ ์ด์šฉํ•œ ์—ฌํ–‰ ๊ฐ€๋ฐฉ ์ž๋ฅด๊ธฐ Ultralytics YOLOv8
๊ณตํ•ญ ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ๋ฅผ ์ด์šฉํ•œ ์—ฌํ–‰ ๊ฐ€๋ฐฉ ์ž๋ฅด๊ธฐ Ultralytics YOLOv8

YOLOv8 ์˜ˆ์ œ๋ฅผ ์‚ฌ์šฉํ•œ ๊ฐœ์ฒด ์ž๋ฅด๊ธฐ

import os

import cv2

from ultralytics import YOLO
from ultralytics.utils.plotting import Annotator, colors

model = YOLO("yolov8n.pt")
names = model.names

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

crop_dir_name = "ultralytics_crop"
if not os.path.exists(crop_dir_name):
    os.mkdir(crop_dir_name)

# Video writer
video_writer = cv2.VideoWriter("object_cropping_output.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))

idx = 0
while cap.isOpened():
    success, im0 = cap.read()
    if not success:
        print("Video frame is empty or video processing has been successfully completed.")
        break

    results = model.predict(im0, show=False)
    boxes = results[0].boxes.xyxy.cpu().tolist()
    clss = results[0].boxes.cls.cpu().tolist()
    annotator = Annotator(im0, line_width=2, example=names)

    if boxes is not None:
        for box, cls in zip(boxes, clss):
            idx += 1
            annotator.box_label(box, color=colors(int(cls), True), label=names[int(cls)])

            crop_obj = im0[int(box[1]) : int(box[3]), int(box[0]) : int(box[2])]

            cv2.imwrite(os.path.join(crop_dir_name, str(idx) + ".png"), crop_obj)

    cv2.imshow("ultralytics", im0)
    video_writer.write(im0)

    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

cap.release()
video_writer.release()
cv2.destroyAllWindows()

์ธ์ˆ˜ model.predict

์ธ์ˆ˜ ์œ ํ˜• ๊ธฐ๋ณธ๊ฐ’ ์„ค๋ช…
source str 'ultralytics/assets' ์ถ”๋ก ํ•  ๋ฐ์ดํ„ฐ ์†Œ์Šค๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ๊ฒฝ๋กœ, ๋™์˜์ƒ ํŒŒ์ผ, ๋””๋ ‰ํ† ๋ฆฌ, URL ๋˜๋Š” ์‹ค์‹œ๊ฐ„ ํ”ผ๋“œ์šฉ ๋””๋ฐ”์ด์Šค ID๊ฐ€ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์–‘ํ•œ ํ˜•์‹๊ณผ ์†Œ์Šค๋ฅผ ์ง€์›ํ•˜๋ฏ€๋กœ ๋‹ค์–‘ํ•œ ์œ ํ˜•์˜ ์ž…๋ ฅ์— ์œ ์—ฐํ•˜๊ฒŒ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
conf float 0.25 ํƒ์ง€์— ๋Œ€ํ•œ ์ตœ์†Œ ์‹ ๋ขฐ๋„ ์ž„๊ณ„๊ฐ’์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ž„๊ณ„๊ฐ’๋ณด๋‹ค ๋‚ฎ์€ ์‹ ๋ขฐ๋„๋กœ ํƒ์ง€๋œ ๊ฐœ์ฒด๋Š” ๋ฌด์‹œ๋ฉ๋‹ˆ๋‹ค. ์ด ๊ฐ’์„ ์กฐ์ •ํ•˜๋ฉด ์˜คํƒ์ง€๋ฅผ ์ค„์ด๋Š” ๋ฐ ๋„์›€์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
iou float 0.7 ๋น„์ตœ๋Œ€ ์–ต์ œ(NMS)์— ๋Œ€ํ•œ ๊ต์ฐจ์  ์ดˆ๊ณผ(IoU) ์ž„๊ณ„๊ฐ’์ž…๋‹ˆ๋‹ค. ๊ฐ’์ด ๋‚ฎ์„์ˆ˜๋ก ์ค‘๋ณต๋˜๋Š” ์ƒ์ž๋ฅผ ์ œ๊ฑฐํ•˜์—ฌ ํƒ์ง€ ํšŸ์ˆ˜๊ฐ€ ์ค„์–ด๋“ค์–ด ์ค‘๋ณต์„ ์ค„์ด๋Š” ๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.
imgsz int or tuple 640 ์ถ”๋ก ํ•  ์ด๋ฏธ์ง€ ํฌ๊ธฐ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์ผ ์ •์ˆ˜์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 640 ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •์‚ฌ๊ฐํ˜• ํฌ๊ธฐ ์กฐ์ • ๋˜๋Š” (๋†’์ด, ๋„ˆ๋น„) ํŠœํ”Œ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ ์ ˆํ•œ ํฌ๊ธฐ ์กฐ์ •์€ ๊ฐ์ง€ ์ •ํ™•๋„์™€ ์ฒ˜๋ฆฌ ์†๋„๋ฅผ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
half bool False ์ •ํ™•๋„์— ๋ฏธ์น˜๋Š” ์˜ํ–ฅ์„ ์ตœ์†Œํ™”ํ•˜๋ฉด์„œ ์ง€์›๋˜๋Š” GPU์—์„œ ๋ชจ๋ธ ์ถ”๋ก  ์†๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ๋Š” ๋ฐ˜์ •ํ™•๋„(FP16) ์ถ”๋ก ์„ ํ™œ์„ฑํ™”ํ•ฉ๋‹ˆ๋‹ค.
device str None ์ถ”๋ก ํ•  ์žฅ์น˜๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค(์˜ˆ, cpu, cuda:0 ๋˜๋Š” 0). ์‚ฌ์šฉ์ž๊ฐ€ ๋ชจ๋ธ ์‹คํ–‰์„ ์œ„ํ•ด CPU, ํŠน์ • GPU ๋˜๋Š” ๊ธฐํƒ€ ์ปดํ“จํŒ… ์žฅ์น˜ ์ค‘์—์„œ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
max_det int 300 ์ด๋ฏธ์ง€๋‹น ํ—ˆ์šฉ๋˜๋Š” ์ตœ๋Œ€ ๊ฐ์ง€ ํšŸ์ˆ˜. ๋ชจ๋ธ์ด ํ•œ ๋ฒˆ์˜ ์ถ”๋ก ์—์„œ ๊ฐ์ง€ํ•  ์ˆ˜ ์žˆ๋Š” ์ด ์˜ค๋ธŒ์ ํŠธ ์ˆ˜๋ฅผ ์ œํ•œํ•˜์—ฌ ๋ฐ€์ง‘๋œ ์žฅ๋ฉด์—์„œ ๊ณผ๋„ํ•œ ์ถœ๋ ฅ์„ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค.
vid_stride int 1 ๋น„๋””์˜ค ์ž…๋ ฅ์˜ ํ”„๋ ˆ์ž„ ๋ณดํญ. ๋™์˜์ƒ์—์„œ ํ”„๋ ˆ์ž„์„ ๊ฑด๋„ˆ๋›ฐ์–ด ์‹œ๊ฐ„ ํ•ด์ƒ๋„๋ฅผ ํฌ์ƒํ•˜๋ฉด์„œ ์ฒ˜๋ฆฌ ์†๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ’์ด 1์ด๋ฉด ๋ชจ๋“  ํ”„๋ ˆ์ž„์„ ์ฒ˜๋ฆฌํ•˜๊ณ , ๊ฐ’์ด ํด์ˆ˜๋ก ํ”„๋ ˆ์ž„์„ ๊ฑด๋„ˆ๋œ๋‹ˆ๋‹ค.
stream_buffer bool False ๋น„๋””์˜ค ์ŠคํŠธ๋ฆผ์„ ์ฒ˜๋ฆฌํ•  ๋•Œ ๋ชจ๋“  ํ”„๋ ˆ์ž„์„ ๋ฒ„ํผ๋งํ• ์ง€ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค(True), ๋˜๋Š” ๋ชจ๋ธ์ด ๊ฐ€์žฅ ์ตœ๊ทผ ํ”„๋ ˆ์ž„์„ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•˜๋Š”์ง€(False). ์‹ค์‹œ๊ฐ„ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.
visualize bool False ์ถ”๋ก  ์ค‘์— ๋ชจ๋ธ ๊ธฐ๋Šฅ์˜ ์‹œ๊ฐํ™”๋ฅผ ํ™œ์„ฑํ™”ํ•˜์—ฌ ๋ชจ๋ธ์ด '๋ณด๊ณ  ์žˆ๋Š” ๊ฒƒ'์— ๋Œ€ํ•œ ์ธ์‚ฌ์ดํŠธ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋””๋ฒ„๊น… ๋ฐ ๋ชจ๋ธ ํ•ด์„์— ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.
augment bool False ์˜ˆ์ธก์„ ์œ„ํ•œ ํ…Œ์ŠคํŠธ ์‹œ๊ฐ„ ์ฆ๊ฐ•(TTA)์„ ์ง€์›ํ•˜์—ฌ ์ถ”๋ก  ์†๋„๋ฅผ ํฌ์ƒํ•˜๋ฉด์„œ ํƒ์ง€ ๊ฒฌ๊ณ ์„ฑ์„ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
agnostic_nms bool False ์„œ๋กœ ๋‹ค๋ฅธ ํด๋ž˜์Šค์˜ ๊ฒน์น˜๋Š” ์ƒ์ž๋ฅผ ๋ณ‘ํ•ฉํ•˜๋Š” ํด๋ž˜์Šค ๋ฌด๊ด€ NMS(Non-Maximum Suppression)๋ฅผ ํ™œ์„ฑํ™”ํ•ฉ๋‹ˆ๋‹ค. ํด๋ž˜์Šค ์ค‘๋ณต์ด ์ผ๋ฐ˜์ ์ธ ๋‹ค์ค‘ ํด๋ž˜์Šค ํƒ์ง€ ์‹œ๋‚˜๋ฆฌ์˜ค์— ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.
classes list[int] None ํด๋ž˜์Šค ID ์ง‘ํ•ฉ์œผ๋กœ ์˜ˆ์ธก์„ ํ•„ํ„ฐ๋งํ•ฉ๋‹ˆ๋‹ค. ์ง€์ •๋œ ํด๋ž˜์Šค์— ์†ํ•˜๋Š” ํƒ์ง€๋งŒ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. ๋‹ค์ค‘ ํด๋ž˜์Šค ํƒ์ง€ ์ž‘์—…์—์„œ ๊ด€๋ จ ๊ฐœ์ฒด์— ์ง‘์ค‘ํ•˜๋Š” ๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.
retina_masks bool False ๋ชจ๋ธ์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ ๊ณ ํ•ด์ƒ๋„ ์„ธ๋ถ„ํ™” ๋งˆ์Šคํฌ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์„ธ๋ถ„ํ™” ์ž‘์—…์˜ ๋งˆ์Šคํฌ ํ’ˆ์งˆ์„ ํ–ฅ์ƒ์‹œ์ผœ ๋” ์„ธ๋ฐ€ํ•œ ๋””ํ…Œ์ผ์„ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
embed list[int] None ํŠน์ง• ๋ฒกํ„ฐ ๋˜๋Š” ์ž„๋ฒ ๋”ฉ์„ ์ถ”์ถœํ•  ๋ ˆ์ด์–ด๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ํด๋Ÿฌ์Šคํ„ฐ๋ง์ด๋‚˜ ์œ ์‚ฌ๋„ ๊ฒ€์ƒ‰๊ณผ ๊ฐ™์€ ๋‹ค์šด์ŠคํŠธ๋ฆผ ์ž‘์—…์— ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.


Created 2024-01-09, Updated 2024-06-13
Authors: RizwanMunawar (2), glenn-jocher (7), IvorZhu331 (1), AyushExel (1)

๋Œ“๊ธ€