- ๋น ๋ฅธ ์ถ๋ก ์๋์ ์๋นํ ์ ํ๋๋ฅผ ์๋ํ๋ ์ค๋ธ์ ํธ ๋ํ ์ [๋ฌผ์ฒด ํ์ง] ๋ชจ๋ธ
๋ฌผ์ฒด ํ์ง๋ ๊ฐ์ฒด์ ์์น์ ํฌ๊ธฐ๋ฅผ ์์ธกํ๋ ๋ฌธ์ ์ ํด๋น ๊ฐ์ฒด์ ํด๋์ค๋ฅผ ๋ถ๋ฅํ๋ ๋ฌธ์ ๋ก ์ธ๋ถํ ํ ์ ์๋ค.
๋ช ๊ฐ์ง๋ฅผ ์ ๋ฆฌํ๊ฒ ์ต๋๋ค. YOLO๋ ๋ฅ ๋ฌ๋ ์๊ณ ๋ฆฌ์ฆ์ด๋ฏ๋ก ์์ฒด ์ค์น๊ฐ ํ์ํ์ง ์์ผ๋ฉฐ ๋์ ์๊ณ ๋ฆฌ์ฆ์ ์คํํ
๋ฅ ๋ฌ๋ ํ๋ ์์ํฌ๊ฐ ํ์ํฉ๋๋ค.
YOLO๋ฅผ ์ค์นํ๋ ๋ฐฉ๋ฒ?
YOLO์ ํธํ๋๋ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ๋๊ณ ์๋ ค์ง 3๊ฐ์ง ํ๋ ์์ํฌ
- Darknet : YOLO ๊ฐ๋ฐ์๊ฐ ๋ง๋ ํ๋ ์์ํฌ๋ก yolo๋ฅผ ์ํด ํน๋ณํ ์ ์๋์์ต๋๋ค.
์ฅ์ : ๋น ๋ฅด๊ณ , GPU ๋๋ CPU์ ํจ๊ป ์๋ํ ์ ์์ต๋๋ค
. ๋จ์ : Linux os์ ํจ๊ป ์๋ํฉ๋๋ค.
- Darkflow: Tensorflow (๋ ๋ค๋ฅธ ๋ฅ ๋ฌ๋ ํ๋ ์์ํฌ)์ ๋ํ darknet์ ์ ์์
๋๋ค.
์ฅ์ : ๋น ๋ฅด๊ณ , GPU ๋๋ CPU์ ํจ๊ป ์๋ํ ์ ์์ผ๋ฉฐ, Linux, Windows ๋ฐ Mac๊ณผ๋ ํธํ๋ฉ๋๋ค.
๋จ์ : ํนํ Windows์์ ์ค์น๊ฐ ๋งค์ฐ ๋ณต์กํฉ๋๋ค.
- Opencv: ๋ํ opencv์๋ YOLO์ ํจ๊ป ์๋ํ๋ ๋ฅ ๋ฌ๋ ํ๋ ์์ํฌ๊ฐ ์์ต๋๋ค. ์ต์ํ opencv 3.4.2๊ฐ ์๋์ง ํ์ธํ์ญ์์ค.
์ฅ์ : opencv๋ฅผ ์ ์ธํ ์ด๋ค ๊ฒ๋ ์ค์นํ ํ์ ์์ด ์๋ํฉ๋๋ค.
๋จ์ : CPU์์๋ง ์๋ํ๋ฏ๋ก ์ค์๊ฐ์ผ๋ก ๋น๋์ค๋ฅผ ์ฒ๋ฆฌํ๋ ๋ฐ ์ ๋ง ๋น ๋ฅธ ์๋๋ฅผ ์ป์ ์ ์์ต๋๋ค.
Opencv์์ YOLO๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
Opencv์ ํจ๊ป YOLO๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ์ค์ ์ ๋ ๊ฒ์ ๋๋ค. ์ด๊ฒ์ ๋ณต์กํ ์ค์น๋ฅผ ์ํํ์ง ์๊ณ ์๋ํ๋ ์๊ณ ๋ฆฌ์ฆ์ ๋น ๋ฅด๊ฒ ์ป์ ์ ์๋ ์ด๋ณด์์๊ฒ ๊ฐ์ฅ ์ข์ ๋ฐฉ๋ฒ์ ๋๋ค.
๋จผ์ Opencv ๋ฐ numpy ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๊ฐ์ ธ์จ ๋ค์ ์๊ณ ๋ฆฌ์ฆ์ ๋ก๋ํฉ๋๋ค.
ํด๋์ค๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
import cv2
import numpy as np
3๊ฐ์ ํ์ผ์ด ํ์ํ ์๊ณ ๋ฆฌ์ฆ์ ์คํํฉ๋๋ค.
- ๊ฐ์ค์น ํ์ผ: ํ์ต๋ ๋ชจ๋ธ, ๊ฐ์ฒด๋ฅผ ๊ฐ์งํ๋ ์๊ณ ๋ฆฌ์ฆ์ ํต์ฌ์ ๋๋ค.
- Cfg ํ์ผ : ์๊ณ ๋ฆฌ์ฆ์ ๋ชจ๋ ์ค์ ์ด ์๋ ๊ตฌ์ฑ ํ์ผ์ ๋๋ค.
- ํ์ผ ์ด๋ฆ: ์๊ณ ๋ฆฌ์ฆ์ด ๊ฐ์งํ ์ ์๋ ๊ฐ์ฒด์ ์ด๋ฆ์ ํฌํจํฉ๋๋ค.
# Load 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))
๊ทธ๋ฐ ๋ค์ ๊ฐ์ฒด ๊ฐ์ง๋ฅผ ์ํํ๋ ค๋ ์ด๋ฏธ์ง๋ฅผ ๋ก๋ํ๊ณ ๋๋น์ ๋์ด๋ ์ป์ต๋๋ค.
# Loading image
img = cv2.imread("room_ser.jpg")
img = cv2.resize(img, None, fx=0.4, fy=0.4)
height, width, channels = img.shape
๋คํธ์ํฌ์์ ์ ์ฒด ์ด๋ฏธ์ง๋ฅผ ๋ฐ๋ก ์ฌ์ฉํ ์๋ ์์ง๋ง ๋จผ์ Blob์ผ๋ก ๋ณํํ๋ ๋ฐ ํ์ํฉ๋๋ค. Blob ์ด๋ฏธ์ง์์ ๊ธฐ๋ฅ์ ์ถ์ถํ๊ณ ํฌ๊ธฐ๋ฅผ ์กฐ์ ํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. YOLO๋ ์ธ ๊ฐ์ง ํฌ๊ธฐ๋ฅผ ํ์ฉํฉ๋๋ค.
- 320×320 ํฌ๊ธฐ๊ฐ ์์์ ์ ํ๋๋ ๋จ์ด์ง์ง๋ง ์๋๋ ๋ ์ข์ต๋๋ค.
- 609×609 ํฌ๊ธฐ๊ฐ ์ปค์ ์ ํ๋๊ฐ ๋๊ณ ์๋๊ฐ ๋๋ฆผ
- 416×416 ์ค๊ฐ์ ์๊ณ ๋ ๋ค ์กฐ๊ธ ์ป์ ์ ์์ต๋๋ค.
์ฌ๊ธฐ์ Blob ์ด๋?
Blob(Binary Large Object, ๋ธ๋)์ ์ด๋ฏธ์ง, ์ฌ์ด๋, ๋น๋์ค์ ๊ฐ์ ๋ฉํฐ๋ฏธ๋์ด ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃฐ ๋ ์ฌ์ฉํ ์ ์์ต๋๋ค.
์๋ ์ฝ๋ ๋งจ๋ง์ง๋ง ํ ์์ ์ ํ์ง ๊ฒฐ๊ณผ์ ๋๋ค. Outs๋ ํ์ง๋ ๊ฐ์ฒด, ํด๋น ์์น ๋ฐ ํ์ง์ ๋ํ ์ ๋ขฐ๋์ ๋ํ ๋ชจ๋ ์ ๋ณด๋ฅผ ํฌํจํ๋ ๋ฐฐ์ด์ ๋๋ค.
# Detecting objects
blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
net.setInput(blob)
outs = net.forward(output_layers)
์ด ์์ ์์ ๊ฐ์ง๊ฐ ์๋ฃ๋๊ณ ๊ฒฐ๊ณผ๋ฅผ ํ๋ฉด์ ํ์ํ๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค.
๊ทธ๋ฐ ๋ค์ ์์ ๋ฐฐ์ด์ ๋ฐ๋ณตํ๊ณ ์ ๋ขฐ๋๋ฅผ ๊ณ์ฐํ๊ณ ์ ๋ขฐ๋ ์๊ณ๊ฐ์ ์ ํํฉ๋๋ค.
์๋ ์ฝ๋ 32ํ์์ ์๊ณ๊ฐ ์ ๋ขฐ๋๋ฅผ 0.5๋ก ์ค์ ํ์ต๋๋ค . ๋ ํฌ๋ฉด ๊ฐ์ฒด๊ฐ ์ฌ๋ฐ๋ฅด๊ฒ ๊ฐ์ง๋ ๊ฒ์ผ๋ก ๊ฐ์ฃผํ๊ณ ๊ทธ๋ ์ง ์์ผ๋ฉด ๊ฑด๋๋๋๋ค.
์๊ณ๊ฐ์ 0์์ 1๊น์ง์
๋๋ค. 1์ ๊ฐ๊น์ธ์๋ก ๊ฐ์ง ์ ํ๋๊ฐ ๋๊ณ 0์ ๊ฐ๊น์ธ์๋ก ์ ํ๋๋ ๋ฎ์ง๋ง ๊ฐ์ง๋ ๊ฐ์ฒด์ ์๋ ๋ง์ต๋๋ค.
# Showing informations on the screen
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: # 32ํ์ ์ด ๋ผ์ธ์ ์๋ฏธํฉ๋๋ค!!!!!!!!!!!!!!!!!!!!!!!!!
# Object detected
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
# Rectangle coordinates
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)
๊ฐ์ง๋ฅผ ์ํํ ๋ ๋์ผํ ๊ฐ์ฒด์ ๋ํด ๋ ๋ง์ ์์๊ฐ ์์ผ๋ฏ๋ก ์ด "๋ ธ์ด์ฆ"๋ฅผ ์ ๊ฑฐํ๊ธฐ ์ํด ๋ค๋ฅธ ๊ธฐ๋ฅ์ ์ฌ์ฉํด์ผ ํฉ๋๋ค. ๋น์ต๋ ์ต์ ๋ผ๊ณ ํฉ๋๋ค.
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
๋ง์ง๋ง์ผ๋ก ๋ชจ๋ ์ ๋ณด๋ฅผ ์ถ์ถํ์ฌ ํ๋ฉด์ ํ์ํฉ๋๋ค.
- Box : ๊ฐ์ง๋ ๋ฌผ์ฒด๋ฅผ ๋๋ฌ์ธ๊ณ ์๋ ์ฌ๊ฐํ์ ์ขํ๋ฅผ ํฌํจํฉ๋๋ค.
- ๋ ์ด๋ธ : ๊ฐ์ง๋ ๊ฐ์ฒด์ ์ด๋ฆ์ ๋๋ค.
- Confidence : 0์์ 1๊น์ง์ ํ์ง์ ๋ํ ์ ๋ขฐ๋์ ๋๋ค.
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()
์ฐธ๊ณ : https://pysource.com/2019/06/27/yolo-object-detection-using-opencv-with-python/
'๋ฅ๋ฌ๋๐ค > YOLO, Opencv ๐ข' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[YOLO]#1 YOLO๋ฅผ ํ์ฉํ ๊ฐ์ฒด ํ์ง (darknet install)&๊ตฌ๊ธ์ฝ๋ฉ (2) | 2022.11.06 |
---|---|
๋ฌด๋ฃ๋ก Yolo , Opencv ์ ๋ํด ๊ณต๋ถํ ์ ์๋ ์ฌ์ดํธ (2) | 2022.04.01 |
webcam์์ ๊ฐ์ ธ์จ ์์์ mp4๋ก ์ ์ฅํ๋ OpenCV Python (0) | 2022.04.01 |
openCV๋ฅผ ์ฌ์ฉํ YOLO ๊ฐ์ฒด ๊ฐ์ง (0) | 2022.04.01 |
darknet_YOLOv4 (0) | 2022.03.23 |