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

[YOLOv3] Object Detection๊ณผ Bounding Box

H J 2022. 10. 2. 01:30

์ผ๋‹จ ํ•„์š”ํ•œ ๋ชจ๋“ˆ๋“ค import ํ•ด์ฃผ๊ธฐ !

import cv2
import numpy as np
from matplotlib import pyplot as plt

 

YOLO ๋กœ๋“œ

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

์—ฌ๊ธฐ์„œ ํ•„์š”ํ•œ ํŒŒ์ผ๋“ค์€ ์•„๋ž˜ ๊ธ€์— ๋‚˜์™€์žˆ๋‹ค !

 

[DARKNET] ๋‹คํฌ๋„ท์œผ๋กœ YOLOV3 ๋Œ๋ฆฌ๋Š” ๋ฒ•

1. ํ•„์š”ํ•œ ํŒŒ์ผ ๋‹ค์šด name cfg weight https://pjreddie.com/darknet/yolo/ YOLO: Real-Time Object Detection YOLO: Real-Time Object Detection You only look once (YOLO) is a state-of-the-art, real-time ob..

hjkim5004.tistory.com

 

 

์ด๋ฏธ์ง€ ๋กœ๋“œ

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

 

detect ( + ์ด๋ฏธ์ง€ blob์œผ๋กœ )

blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=True)

net.setInput(blob)
outs = net.forward(output_layers)

class_ids = []
confidences = []

boxes = []
for out in outs:
    for detection in out:
        scores = detection[5:]
        class_id = np.argmax(scores)
        if scores[class_id] != 0:
            print(class_id)
            print(scores[class_id])
        confidence = scores[class_id]
        if confidence > 0.6:
            center_x = int(detection[0] * width)
            center_y = int(detection[1] * height)
            #print(center_x,center_y)
            w = abs(int(detection[2] * width))
            h = abs(int(detection[3] * height))

            x = abs(int(center_x - w / 2))
            y = abs(int(center_y - h / 2))
            boxes.append([x, y, w, h])
            confidences.append(float(confidence))
            class_ids.append(class_id)

 

non maximum suppression

class_list = list(set(class_ids))
idxx = []
indexes=[]
for i in range(len(class_list)):
    max_v=0
    for j in range(len(class_ids)):
        if class_ids[j] == class_list[i]:
            if max_v < confidences[j]:
                max_v = confidences[j]
                idxx.append(j)
    indexes.append(idxx[len(idxx)-1])            

print(class_ids)

ใ„ด ํ•œ object์— ๋Œ€ํ•ด ์—ฌ๋Ÿฌ ๋ฐ”์šด๋”ฉ ๋ฐ•์Šค๊ฐ€ ์ƒ๊ธฐ๋ฉด ๊ทธ ์ค‘ ์ •ํ™•๋„๊ฐ€ ์ œ์ผ ๋†’์€ ๊ฒƒ์„ ์ œ์™ธํ•œ ๋ชจ๋“  ์ค‘๋ณต ๋ฐ”์šด๋”ฉ ๋ฐ•์Šค ์—†์• ๊ธฐ !

 

 

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

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

 

class name, bounding box, ์ขŒํ‘œ ์ถœ๋ ฅ

font = cv2.FONT_HERSHEY_PLAIN
det_foods = []
for i in range(len(boxes)):
    if i in indexes:
        x, y, w, h = boxes[i]
        class_name = classes[class_ids[i]]
        print(class_name)
        print(confidences[i])
        label = f"{class_name} {boxes[i]}"
        det_foods.append(label)
        color = colors[i]
        print(x,y,x+w,y+h)

        nx = (x + w) / width
        ny = (y + h) / height

        if nx > 1:
            nx = 1
        if ny > 1:
            ny =1
        cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
        print(x/width,y/height,nx,ny)
        cv2.rectangle(img, (x - 1, y), (x + len(class_name)*13, y-12), color, -1)
        cv2.putText(img, class_name, (x, y - 4), cv2.FONT_HERSHEY_COMPLEX_SMALL, 0.5, (0, 0, 0), 1,cv2.LINE_AA)

b,g,r = cv2.split(img)
image2 = cv2.merge([r,g,b])
plt.imshow(image2)
#imShow(img)
plt.show()
print(det_foods)

 

tiny๊ฐ€ ์•„๋‹Œ yolov3๋กœ ๋Œ๋ ธ์„ ๋•Œ !

 

tiny๋กœ ๋Œ๋ ธ์„ ๋•Œ !

 

tiny๊ฐ€ ์ผ๋ฐ˜ yolov3๋ณด๋‹ค ์ •ํ™•๋„๋„ ๊ฝค ๋‚ฎ๊ณ  ์ธ์‹์„ ๋ชป ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์—ˆ๋‹ค