본문 바로가기
전체보기/머신러닝

[Python] YOLO Style로 변환

by 피망우유 2019. 11. 22.
# Img_000_0001=45,76,538,404
# 0.490625 0.579167 0.840625 0.841667

w = 640    # 이미지 가로
h = 480    # 이미지 세로
x1 = 45    # 좌측 최상단 x좌표
y1 = 76    # 좌측 최상단 y좌표
x2 = 538   # Box 가로
y2 = 404   # Box 세로

dw = 1./w
dh = 1./h
x = (float(x1) + float(x1) + float(x2))/2.0
y = (float(y1) + float(y1) + float(y2))/2.0
w = float(x2)
h = float(y2)

x = round(x*dw, 6)
w = round(w*dw, 6)
y = round(y*dh, 6) # 6자리 표시
h = round(h*dh, 6)

print(str(x) + ' ' + str(y) + ' ' + str(w) + ' ' + str(h))

그럼 반대로 다시 변환할려면 어떻게 해야할까

 

x = 0.490625
y = 0.579167
w = 0.840625
h = 0.841667

img_w = 640            # 이미지 가로
img_h = 480            # 이미지 세로

dw = 1./img_w
dh = 1./img_h

x = x/dw
y = y/dh
w = round(w/dw)                 # Box 가로
h = round(h/dh)                 # Box 세로

x1 = round((2*x - w)/2)         # 좌측 최상단 x좌표
y1 = round((2*y - h)/2)         # 좌측 최상단 y좌표

print(str(x1) + ' ' + str(y1) + ' ' + str(w) + ' ' + str(h))
# 45 76 538 404

후... 머리 아팠다

'전체보기 > 머신러닝' 카테고리의 다른 글

Maritime MaskRCNN  (0) 2021.02.07
Image Segmentation Keras  (0) 2020.11.07
Semantic Segmentation 참고 자료  (0) 2020.11.07
Ubuntu 16.04 RTX2080Ti Development Environment  (0) 2020.07.04
Ubuntu 16.04 GTX960 Development Environment  (0) 2020.05.24

댓글