Okay, before I ask, I have already seen the following answers:
Python OrderedDict not keeping element order
Converting dict to OrderedDict
OrderedDict does not preserve the order
but still I am unable to make it right.
Background: I have a groundtruth file, like, jp1_GT.txt (there are other files too, at different paths but currently I am concerned with just one file) at some path. In this file, each line represents one bounding box in the format . I am changing the with some calculation, and aim to write them in a new groundtruth file.
#Path containing old folders
src_path = r"C:\Users\username\Downloads\LITIV_dataset"
#dictionary creation
newgtdict ={}
for folderName in os.listdir(src_path):
folderPath = src_path + "\\" + folderName
if not os.path.isdir(folderPath):
continue
listOfFilePaths = glob.glob(folderPath + "\\"+ folderName +"_GT.txt")
for filePath in listOfFilePaths:
openedFile = open(filePath, 'r')
lines = openedFile.readlines()
linecount =0
for line in lines:
linecount+=1
linenumber = str(linecount)
my_list = str(line).split(",")
bboxes=[]
#checking for non-empty lines
if len(my_list)>0:
x_br = int(my_list[2])
y_br = int(my_list[3])
x_tl = int(my_list[0]) - (x_br/2)
y_tl = int(my_list[1]) - (y_br/2)
box = [x_tl,y_tl,x_br,y_br]
newgtdict[str(filePath)+str(linenumber)] = box
print newgtdict
The first four lines of my jp1_GT.txt looks like this:
189,163,72,72
190,162,72,72
188,160,72,72
189,163,72,72
However, first four lines of output looks like this:
{'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt1': [153, 127, 72, 72]}
{'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt1': [153, 127, 72, 72], 'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt2': [154, 126, 72, 72]}
{'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt1': [153, 127, 72, 72], 'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt3': [152, 124, 72, 72], 'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt2': [154, 126, 72, 72]}
{'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt4': [153, 127, 72, 72], 'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt1': [153, 127, 72, 72], 'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt3': [152, 124, 72, 72], 'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt2': [154, 126, 72, 72]}
Expected first 4 lines of output:
{'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt1': [153, 127, 72, 72]}
{'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt2': [154, 126, 72, 72]}
{'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt3': [152, 124, 72, 72]}
{'C:\\Users\\username\\Downloads\\LITIV_dataset\\jp1\\jp1_GT.txt4': [153, 127, 72, 72]}
Just a side question: Do you suggest any other better way to this.