news-navigator/code/code-merge-no-pretrained/change_to_nonrelative.py
2022-08-02 13:14:34 +00:00

50 lines
1.7 KiB
Python

import sys
import cv2
from labels_list import labels_list
import subprocess
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 = []
# this is slower than code below
#dims = cv2.imread(im_path).shape
#bashCommand = f'file {im_path}'
#process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
#output, error = process.communicate()
#x_total, y_total = str(output).split(', ')[-2].split('x')
#y_total = int(y_total)
#x_total = int(x_total)
dims = cv2.imread(im_path).shape
bashCommand = f'file {im_path}'
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
x_total, y_total = str(output).split(', ')[-2].split('x')
y_total = int(y_total)
x_total = int(x_total)
for line_in in f_in:
cls, xcenter, ycenter, xwidth, ywidth, confidence = line_in.rstrip().split(' ')
cls = labels_list[int(cls)]
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))