forked from Cartucho/mAP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_dr_yolo.py
45 lines (40 loc) · 1.94 KB
/
convert_dr_yolo.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import os
import re
# make sure that the cwd() in the beginning is the location of the python script (so that every path makes sense)
os.chdir(os.path.dirname(os.path.abspath(__file__)))
IN_FILE = 'result.txt'
# change directory to the one with the files to be changed
parent_path = os.path.abspath(os.path.join(os.getcwd(), os.pardir))
parent_path = os.path.abspath(os.path.join(parent_path, os.pardir))
DR_PATH = os.path.join(parent_path, 'input','detection-results')
#print(DR_PATH)
os.chdir(DR_PATH)
SEPARATOR_KEY = 'Enter Image Path:'
IMG_FORMAT = '.jpg'
outfile = None
with open(IN_FILE) as infile:
for line in infile:
if SEPARATOR_KEY in line:
if IMG_FORMAT not in line:
break
# get text between two substrings (SEPARATOR_KEY and IMG_FORMAT)
image_path = re.search(SEPARATOR_KEY + '(.*)' + IMG_FORMAT, line)
# get the image name (the final component of a image_path)
# e.g., from 'data/horses_1' to 'horses_1'
image_name = os.path.basename(image_path.group(1))
# close the previous file
if outfile is not None:
outfile.close()
# open a new file
outfile = open(os.path.join(DR_PATH, image_name + '.txt'), 'w')
elif outfile is not None:
# split line on first occurrence of the character ':' and '%'
class_name, info = line.split(':', 1)
confidence, bbox = info.split('%', 1)
# get all the coordinates of the bounding box
bbox = bbox.replace(')','') # remove the character ')'
# go through each of the parts of the string and check if it is a digit
left, top, width, height = [int(s) for s in bbox.split() if s.lstrip('-').isdigit()]
right = left + width
bottom = top + height
outfile.write("{} {} {} {} {} {}\n".format(class_name, float(confidence)/100, left, top, right, bottom))