Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- DB 데이터 저장
- VS Code
- 튜토리얼
- flask
- heroku
- #ifdef
- #ifndef
- jinja2
- Action
- href
- openweathermap
- 사이트 도메인
- 실시간 시계
- javascript
- bootstrap
- #if
- 환경변수 설정
- MySQL 세팅
- 콘솔 가상환경 # 콘솔 #가상환경
- Django
- #else
- #undef
- OpenCV + Flask
- 성능지표
- bootstrap4 패키지
- PyQt5
- DB 데이터
- OpenCV
- 명령어
- #endif
Archives
- Today
- Total
PROGRAMMING
YOLO.py 본문
1. 코드블럭
import cv2
import numpy as np
def draw_text(img, text,
font=cv2.FONT_HERSHEY_PLAIN,
pos=(0, 0),
font_scale=1,
font_thickness=1,
text_color=(0, 0, 0),
text_color_bg=(0, 0, 0)
):
x, y = pos
text_size, _ = cv2.getTextSize(text, font, font_scale, font_thickness)
text_w, text_h = text_size
cv2.rectangle(img, pos, (x + text_w, y + text_h), text_color_bg, -1)
cv2.putText(img, text, (x, y + text_h + font_scale - 1), font, font_scale, text_color, font_thickness)
return text_size
min_confidence = 0.5
# Load Yolo
net = cv2.dnn.readNet("machineVision/YOLO/yolov3.weights", "machineVision/YOLO/yolov3.cfg")
classes = []
with open("machineVision/YOLO/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("machineVision/YOLO/yolo_03.jpg")
img = cv2.resize(img, None, fx=0.4, fy=0.4)
height, width, channels = img.shape
cv2.imshow("Original Image", img)
# 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)
# 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 > min_confidence:
# 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, min_confidence, 0.4)
font = cv2.FONT_HERSHEY_PLAIN
font_scale = 1
font_thickness = 1
for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
label = str(classes[class_ids[i]])
print(i, label)
color = colors[i]
# 사각형 그리기
cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
# 사각형 위 채우기
# cv2.rectangle(img, (x-1, y-15 ), (x-1 + w+2, y), color, cv2.FILLED)
# cv2.putText(img, label, (x, y-2), font, 1, (0, 255, 0), 1)
# draw_text함수 사용하여 코딩(맨위에 있음)
# https://stackoverflow.com/questions/60674501/how-to-make-black-background-in-cv2-puttext-with-python-opencv
#draw_text(img, label, font=font, pos=(x-1, y-10),text_color_bg=color)
# draw_text함수에서 일부 가져와서 글자 쓰기
# text_size, _ = cv2.getTextSize(label, font, font_scale, font_thickness)
# text_w, text_h = text_size
# cv2.rectangle(img, (x-1, y-11), (x-1 + text_w, y-11 + text_h), color, -1)
cv2.putText(img, label, (x, y-2), font, 1, (0, 255, 0), 1)
cv2.imshow("YOLO Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
2. 원본 화면 - 결과 화면
※ 참고
i) Coco dataset https://cocodataset.org/#keypoints-2020
COCO - Common Objects in Context
cocodataset.org
- YOLO3 부터 coco dataset을 사용하기 시작했으며, coco에는 이미지를 분류하는 카테고리가 80개 정도 있음
- 그에 반해, PASCAL VOC dataset은 YOLO1 ~ 2에서 사용된 데이터셋으로 분류 카테고리가 20개임
- ★ YOLO에 대한 설명은 다른 글을 참고바람
'OpenCV > 머신비전 - 이미지 디텍팅' 카테고리의 다른 글
YOLO (gum, milk, chocolate) 추가 자료 (0) | 2020.12.24 |
---|---|
YOLO (gum, chocolate, milk / 실습) (0) | 2020.12.24 |
YOLO (apple, orange / 교육) (0) | 2020.12.24 |
YOLO에 관한 이론 (0) | 2020.12.24 |
labelImg (0) | 2020.12.24 |
Comments