Created
May 6, 2020 14:07
-
-
Save p3jitnath/dfa07b95efed90442e55f5f733f2f418 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
DIRECTORY = 'mox_voc/' | |
ANNOTATION_PATH = 'MOXA/Annotations/' | |
IMAGES_PATH = 'MOXA/JPEGImages/' | |
EXTENSION = 'xml' # 'txt | |
NAME = 'MOXA' | |
# file_names = os.listdir(DIRECTORY)[:10] | |
file_names = os.listdir(DIRECTORY) | |
counter = 0 | |
def write2dirs(file, name, extension): | |
"""Write the images and their corresponding annotation into respective directories""" | |
global counter | |
with open(DIRECTORY + file, 'rb') as input_file: | |
with open(IMAGES_PATH + NAME + '_' + str(counter) + '.' + extension, 'wb') as output_file: | |
output_file.write(input_file.read()) | |
annotation = name + '.' + EXTENSION | |
with open(DIRECTORY + annotation, 'rb') as input_file: | |
with open(ANNOTATION_PATH + NAME + '_' + str(counter) + '.' + EXTENSION, 'wb') as output_file: | |
output_file.write(input_file.read()) | |
counter = counter + 1 | |
with open(NAME + '/' + 'logs.txt', 'w') as logs: | |
for file in file_names: | |
name = '.' .join(str(f) for f in file.split('.')[:-1]) | |
extension = file.split('.')[-1] | |
if (extension == 'txt'): | |
continue | |
if (extension != EXTENSION): | |
log = file + ' - ' + NAME + '_' + str(counter) + '.' + extension | |
print(log) | |
logs.write(log + '\n') | |
write2dirs(file, name, extension) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment