s t u d y . . ๐Ÿง/AI ์•ค ML ์•ค DL

[YOLOv3] Object detection

H J 2022. 9. 30. 12:49

ํฌํŠธํ™€์„ ํƒ์ง€ํ•  ๋•Œ ์–ด๋–ป๊ฒŒ ์ฝ”๋“œ๋ฅผ ์งค์ง€ ๊ณต๋ถ€๋ฅผ ํ•˜๋Š” ์ค‘์ด๋‹ค

๋‚ด๊ฐ€ ์ฐธ๊ณ ํ•œ ๋งํฌ !

 

YOLO object detection using Opencv with Python - Pysource

We’re going to learn in this tutorial YOLO object detection. Yolo is a deep learning algorythm which came out on may 2016...

pysource.com


YOLO + OpenCV

you only look once : object detection์„ ์œ„ํ•œ CNN ๊ธฐ๋ฐ˜์˜ ๋ฌผ์ฒด์ธ์‹ ์•Œ๊ณ ๋ฆฌ์ฆ˜

 

YOLO ์„ค์น˜ ํŒŒ์ผ : YOLO: Real-Time Object Detection

 

YOLO: Real-Time Object Detection

YOLO: Real-Time Object Detection You only look once (YOLO) is a state-of-the-art, real-time object detection system. On a Pascal Titan X it processes images at 30 FPS and has a mAP of 57.9% on COCO test-dev. Comparison to Other Detectors YOLOv3 is extremel

pjreddie.com

 

1. OpenCV ์„ค์น˜

python3 -m pip install opencv-python

cv2.__version__: ์„ค์น˜ ํ™•์ธ

 

2. YOLO ์‚ฌ์šฉ

import cv2
import numpy as np

 

3. YOLO ๋กœ๋“œ

net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
classes = []
with open("coco.names", "r") as f:
    classes = [line.strip() for line in f.readlines()]
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
colors = np.random.uniform(0, 255, size=(len(classes), 3))

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋กœ๋“œ →

weight (ํ›ˆ๋ จ๋œ ๋ชจ๋ธ)

cfg (๊ตฌ์„ฑํŒŒ์ผ, ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•œ ๋ชจ๋“  ์„ค์ •)

name (์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๊ฐ์ง€ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด์˜ ์ด๋ฆ„ ํฌํ•จ)

 

4. ์ด๋ฏธ์ง€ ๋กœ๋“œ ( + ์ด๋ฏธ์ง€ ์ •๋ณด)

img = cv2.imread("sample.jpg")
img = cv2.resize(img, None, fx=0.4, fy=0.4)
height, width, channels = img.shape

 

 

object detect

blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
net.setInput(blob)
outs = net.forward(output_layers # ๊ฐ์ง€ ๊ฒฐ๊ณผ (ํƒ์ง€๋œ ๊ฐœ์ฒด์˜ ๋ชจ๋“  ์ •๋ณด, ์œ„์น˜ ์ œ๊ณต)

์ด๋ฏธ์ง€ Blob๋กœ ๋ณ€ํ™˜

Blob : ์ด๋ฏธ์ง€์—์„œ ํŠน์ง•์„ ์žก์•„๋‚ด๊ณ  ํฌ๊ธฐ ์กฐ์ •

YOLO๊ฐ€ ํ—ˆ์šฉํ•˜๋Š” ์„ธ๊ฐ€์ง€ ํฌ๊ธฐ

  • 320 × 320 : ์ž‘๊ณ  ์ •ํ™•๋„๋Š” ๋–จ์–ด์ง€์ง€ ๋งŒ ์†๋„ ๋น ๋ฆ„
  • 609 × 609 : ์ •ํ™•๋„๋Š” ๋” ๋†’์ง€๋งŒ ์†๋„ ๋Š๋ฆผ
  • 416 × 416 : ์ค‘๊ฐ„

 

6. ๊ฒฐ๊ณผ ํ™”๋ฉด์— ํ‘œ์‹œ + ์‹ ๋ขฐ๋„, ์‹ ๋ขฐ ์ž„๊ณ„๊ฐ’ ๊ณ„์‚ฐ

class_ids = []
confidences = []
boxes = []
for out in outs:
    for detection in out:
        scores = detection[5:]
        class_id = np.argmax(scores)
        confidence = scores[class_id]
        if confidence > 0.5:
            # Object detected
            center_x = int(detection[0] * width)
            center_y = int(detection[1] * height)
            w = int(detection[2] * width)
            h = int(detection[3] * height)
            # ์ขŒํ‘œ
            x = int(center_x - w / 2)
            y = int(center_y - h / 2)
            boxes.append([x, y, w, h])
            confidences.append(float(confidence))
            class_ids.append(class_id)

์‹ ๋ขฐ๋„๊ฐ€ 0.5 ์ด์ƒ์ด๋ผ๋ฉด ๋ฌผ์ฒด ๊ฐ์ง€๋กœ ํŒ๋‹จ

 

7. ๋…ธ์ด์ฆˆ ์ œ๊ฑฐ

indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)

non maximum suppresion : ๊ฐ™์€ ๋ฌผ์ฒด์— ๋Œ€ํ•œ ์ค‘๋ณต ๋ฐ•์Šค → ๊ฐ€์žฅ ์ •ํ™•ํ•œ bounding box ์„ ํƒ

box : ๊ฐ์ง€๋œ ๋ฌผ์ฒด๋ฅผ ํ‘œ์‹œํ•˜๋Š” ์‚ฌ๊ฐํ˜• , label : ๊ฐ์ง€๋œ ๋ฌผ์ฒด์˜ ์ด๋ฆ„, confidence : 0~1 ์‚ฌ์ด์˜ ์‹ ๋ขฐ๋„

 

8. ํ™”๋ฉด์— ํ‘œ์‹œํ•˜๊ธฐ

font = cv2.FONT_HERSHEY_PLAIN
for i in range(len(boxes)):
    if i in indexes:
        x, y, w, h = boxes[i]
        label = str(classes[class_ids[i]])
        color = colors[i]
        cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
        cv2.putText(img, label, (x, y + 30), font, 3, color, 3)
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

 


๊ท€์—ฌ๋ธ ํ–„ ~