ํฌํธํ์ ํ์งํ ๋ ์ด๋ป๊ฒ ์ฝ๋๋ฅผ ์งค์ง ๊ณต๋ถ๋ฅผ ํ๋ ์ค์ด๋ค
๋ด๊ฐ ์ฐธ๊ณ ํ ๋งํฌ !
YOLO + OpenCV
you only look once : object detection์ ์ํ CNN ๊ธฐ๋ฐ์ ๋ฌผ์ฒด์ธ์ ์๊ณ ๋ฆฌ์ฆ
YOLO ์ค์น ํ์ผ : YOLO: Real-Time Object Detection
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()
's t u d y . . ๐ง > AI ์ค ML ์ค DL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[CNN] Custom Dataset์ผ๋ก Pothole detection (1) | 2022.10.05 |
---|---|
[YOLOv3] Object Detection๊ณผ Bounding Box (0) | 2022.10.02 |
[DARKNET] ๋คํฌ๋ท์ผ๋ก YOLOV3 ๋๋ฆฌ๋ ๋ฒ (0) | 2022.10.02 |
[OpenCV] ์ค์๊ฐ ์์์ฒ๋ฆฌ (0) | 2022.09.28 |
[OpenCV] ์ค์๊ฐ ์์ ์ฒ๋ฆฌ : ๊ธฐ๋ณธ ํจ์ ์ ๋ฆฌ (0) | 2022.09.14 |