๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐˜ผ๐™„/๐˜พ๐™ค๐™™๐™š

YOLO V8 detection ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๊ธฐ

by beomcoder 2023. 7. 6.
728x90
๋ฐ˜์‘ํ˜•

https://ultralytics.com/yolov8

 

Ultralytics | Revolutionizing the World of Vision AI

Ultralytics' mission is to empower people and companies to unleash the positive potential of AI. Bring your models to life with our vision AI tools.

ultralytics.com

๊ทธ๋ƒฅ ์ด๋ฏธ์ง€ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ™•์ธํ•˜์—ฌ ๊ฒ€์ถœ๋œ ๋‚ด์šฉ์„ ํ™•์ธํ•˜๊ณ  ์‹ถ์—ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ฐพ์•„๋ณด๋‹ˆ๊นŒ ์›น์บ ์—์„œ๋งŒ ์‚ฌ์šฉํ•˜๋Š”๊ฑธ ๋งŽ์ด ํฌ์ŠคํŒ…ํ•˜๊ณ  ์žˆ์–ด์„œ ์ฐพ๊ธฐ ์–ด๋ ค์› ๋‹ค.

๊ทธ๋ž˜์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ด๋ฏธ์ง€ ํ•œ์žฅ ํ™•์ธํ•˜๋Š”๊ฑธ ํฌ์ŠคํŒ…ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

 

!pip install ultralytics
!pip install opencv-python
!pip install supervision

 

๋จผ์ € yolo v8์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ultralytics๋ฅผ install ํ•œ๋‹ค.

yolo v8์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” python 3.10 ๋ฒ„์ „ ์ด์ƒ์„ ์‚ฌ์šฉํ•ด์•ผ ์ข‹๋‹ค๊ณ  ํ•œ๋‹ค.

python 3.8 ๋ฒ„์ „์„ ์‚ฌ์šฉ์ค‘์ด์—ˆ๋Š”๋ฐ, ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ์•Œ์•„๋ณด๊ณ  ๋ฒ„์ „ ์—…๊ทธ๋ ˆ์ด๋“œ๋ฅผ ํ•ด์ฃผ์—ˆ๋‹ค.

 

import cv2
from ultralytics import YOLO

model = YOLO('yolov8n.pt')
# yolo v8n ๊ฐ€์ค‘์น˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
# git ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•ด๋ณด๋‹ˆ YOLO๋ผ๋Š” ํ•จ์ˆ˜์•ˆ์—์„œ yolov8n.pt๊ฐ€ default์ด๊ณ ,
# string์œผ๋กœ ๋„˜๊ฒจ์ค€ ๋ชจ๋ธ์ด ์—†์œผ๋ฉด git์—์„œ downloadํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.
# ์ž˜๋ชป๋œ ๊ธ€์ž๋ผ๋ฉด ์—๋Ÿฌ๊ฐ€ ์ถœ๋ ฅ๋จ

cap = cv2.VideoCapture(0)
# cv2์—์„œ ์›น์บ ์„ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค.

while cap.isOpened():
    success, frame = cap.read() 
    # cv2์—์„œ ์›น์บ ์„ readํ•œ๋‹ค.
    # cap.read()ํ•จ์ˆ˜๋Š” ํŠœํ”Œ๋กœ 2๊ฐœ์˜ ์ธ์ž๋ฅผ ๋ฆฌํ„ดํ•˜๋Š”๋ฐ,
    # ์ฒซ๋ฒˆ์งธ ์ธ์ž๋Š” frame์„ ๊ฐ€์ ธ์™”๋Š”์ง€์˜ bool ๋ณ€์ˆ˜ (true, false)์ด๊ณ 
    # ๋‘๋ฒˆ์งธ ์ธ์ž๋Š” ์›น์บ ํ”„๋ ˆ์ž„ 1์žฅ์˜ ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ง€๊ณ  ์˜จ๋‹ค.
    
    if success:
        results= model(frame) 
        # frame์ด๋ฏธ์ง€๋ฅผ ๋ชจ๋ธ์— ๋„ฃ์–ด ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ง€๊ณ  ์˜จ๋‹ค.
        # ๋ฆฌํ„ดํ•˜๋Š”๊ฐ’์€ ๋ฆฌ์ŠคํŠธ์ด๋‹ค.
        annotated_frame = results[0].plot()
        # ์ด๋ถ€๋ถ„์„ ์ž˜๋ชจ๋ฅด๊ฒ ๋Š”๋ฐ results[0]์˜ ํƒ€์ž…์€ ultralytics.yolo.engine.results.Results ์ด๋‹ค.
        # ์ด๊ฑธ plot()ํ•˜๊ฒŒ ๋˜๋ฉด numpy ์–ด๋ ˆ์ด ์ขŒํ‘œ๊ฐ€ ๋‚˜์˜จ๋‹ค.
        # ์ด ์ขŒํ‘œ๊ฐ’๋“ค์€ ๊ฒ€์ถœ๋œ ๋ฐ•์Šค๋ฅผ ๊ทธ๋ ค์ฃผ๋Š” ์ขŒํ‘œ๋‹ค.
        cv2.imshow("YOLOv8 Inference", annotated_frame)
        # ์ด ์ขŒํ‘œ๊ฐ’๋“ค์ด ํ‘œ์‹œ๋œ๊ฑธ inshow์— ๋„ฃ์œผ๋ฉด ๋ฐ•์Šค๊ฐ€ ๊ทธ๋ ค์ ธ์„œ ์ถœ๋ ฅ๋œ๋‹ค.
        
        if cv2.waitKey(1)&0xFF == ord("q"): # ํ‚ค๊ฐ’์ด ๋“ค์–ด์™”๋Š”๋ฐ q๋ผ๋ฉด ๊บผ์ง„๋‹ค.
            break
    else:
        print("error")
        break
    
cap.release() # ์œ•์บ ์„ ๋ˆ๋‹ค.
cv2.destroyAllWindows() # cv2๋กœ ์—ด์–ด์ง„ ๋ชจ๋“  ์ฐฝ์„ ์ข…๋ฃŒ์‹œํ‚จ๋‹ค.

 

์œ„ ์ฝ”๋“œ๋Š” ์›น์บ ์„ ์‚ฌ์šฉํ• ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

์ฃผ์„์€ ๋‚ด๊ฐ€ ์ž„์˜๋กœ ์ž‘์„ฑํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์•ˆ๋ด๋„ ๋œ๋‹ค.

 

๋‚˜๋Š” ๊ฒ€์ถœ๊ฒฐ๊ณผ๋ฅผ ๋ณ€์ˆ˜์— ๋„ฃ๊ณ  ์‹ถ์—ˆ๋Š”๋ฐ ๋ณ€์ˆ˜์— ๋„ฃ์–ด๋„ ๊ฐ’์ด ์•ˆ๋„ฃ์–ด์กŒ๋‹ค.

์•Œ์•„๋ณด๋‹ˆ๊นŒ ๊ฐ’์ด ์•ˆ๋‹ด๊ฒจ์ ธ ์˜จ๋‹ค๋Š”๊ฒƒ์ด๋‹ค.

 

๊ทธ๋ž˜์„œ stackoverflow์—์„œ ์—ด์‹ฌํžˆ ๊ฒ€์ƒ‰ํ•˜๊ณ , git์„ ์ฐพ์•„๋ณด๊ณ  ๋ฐฉ๋ฒ•์„ ์ฐพ์•˜๋‹ค.

import cv2
import supervision as sv
from ultralytics import YOLO

model = YOLO('yolov8n.pt')
result = model.predict(cv2.imread('temp.jpg'))[0]
detections = sv.Detections.from_yolov8(result)
labels = [[f"{model.model.names[class_id]}", confidence] for _, _, confidence, class_id, _ in detections]
labels
'''
0: 640x480 5 persons, 1 tv, 140.1ms
Speed: 5.0ms preprocess, 140.1ms inference, 10.9ms postprocess per image at shape (1, 3, 640, 480)
[['tv', 0.9154752],
 ['person', 0.860976],
 ['person', 0.84003013],
 ['person', 0.75352204],
 ['person', 0.40899122],
 ['person', 0.3967925]]
'''

 

supervision ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ yolov8์˜ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฆฌ์ŠคํŠธ์— ๋‹ด์•„์ค€๋‹ค.

๋‚ด๊ฐ€ ํ•„์š”ํ•œ๊ฐ’์€ ์ •ํ™•๋„์™€ ํด๋ž˜์Šค์•„์ด๋””์ด๋ฏ€๋กœ 2๊ฐœ๋งŒ ๋ฐ›์•„์„œ labels ๋ณ€์ˆ˜์— ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ฃผ์„์ฒ˜๋Ÿผ ๊ฐ’์ด ๋ฐ›์•„์ง„๋‹ค.

 

์ง€๊ธˆ ๋‹น์žฅ์€ ์‚ฌ๋žŒ๋งŒ ๊ฒ€์ถœํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ปค์Šคํ…€ํ•™์Šต์€ ํ•„์š”์—†์–ด์„œ ์ง„ํ–‰ํ•˜์ง€ ์•Š์•˜๋‹ค.

์กฐ๋งŒ๊ฐ„ ์ปค์Šคํ…€ํ•™์Šต๋„ ์ง„ํ–‰ํ•  ๊ณ„ํš์ด๋‹ค.

 

 

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€