news-navigator/code/code-merge/change_to_nonrelative.py
2022-08-02 10:09:06 +00:00

29 lines
1.0 KiB
Python

import sys
import cv2
from labels_list import labels_list
im_name = sys.argv[1].split('/')[-1].split('.')[0] + '.jpg'
if 'dev-0' in sys.argv[1]:
im_path = '../../dev-0-dataset/images/' + im_name
elif 'test-A' in sys.argv[1]:
im_path = '../../test-A-dataset/images/' + im_name
with open(sys.argv[1],'r') as f_in, open(sys.argv[2],'w') as f_out:
refs = []
for line_in in f_in:
cls, xcenter, ycenter, xwidth, ywidth, confidence = line_in.rstrip().split(' ')
cls = labels_list[int(cls)]
dims = cv2.imread(im_path).shape
y_total, x_total, _ = dims
xcenter = float(xcenter) * x_total
ycenter = float(ycenter) * y_total
xwidth = float(xwidth) * x_total
ywidth = float(ywidth) * y_total
xmin = xcenter - xwidth/2
xmax = xcenter + xwidth/2
ymin = ycenter - ywidth/2
ymax = ycenter + ywidth/2
refs.append(cls + ':' + str(round(xmin)) + ',' + str(round(ymin)) + ',' + str(round(xmax)) + ',' + str(round(ymax)))
f_out.write(' '.join(refs))