์ผ๋จ ํ์ํ ๋ชจ๋๋ค 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))
์ฌ๊ธฐ์ ํ์ํ ํ์ผ๋ค์ ์๋ ๊ธ์ ๋์์๋ค !
์ด๋ฏธ์ง ๋ก๋
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๋ณด๋ค ์ ํ๋๋ ๊ฝค ๋ฎ๊ณ ์ธ์์ ๋ชป ํ๋ ๊ฒฝ์ฐ๋ ์์๋ค
๋๋ณด๊ธฐ
๋ด๊ฐ ์ฐธ๊ณ ํ ๋ธ๋ก๊ทธ !
's t u d y . . ๐ง > AI ์ค ML ์ค DL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[YOLOv5] Custom Dataset์ผ๋ก Pothole detection (0) | 2022.10.07 |
---|---|
[CNN] Custom Dataset์ผ๋ก Pothole detection (1) | 2022.10.05 |
[DARKNET] ๋คํฌ๋ท์ผ๋ก YOLOV3 ๋๋ฆฌ๋ ๋ฒ (0) | 2022.10.02 |
[YOLOv3] Object detection (1) | 2022.09.30 |
[OpenCV] ์ค์๊ฐ ์์์ฒ๋ฆฌ (0) | 2022.09.28 |