|
| 1 | +#!/bin/python3 |
| 2 | +# -*- encode: utf-8 -*- |
| 3 | + |
| 4 | +""" |
| 5 | +This mini program looks for xmin, ymin, xmax and ymax values in order to |
| 6 | +check if the figures have the proper size (min 33px) |
| 7 | +Version: 0.1 |
| 8 | +Author: Moisés Lodeiro-Santiago @ https://github.com/moiseslodeiro |
| 9 | +""" |
| 10 | + |
| 11 | +from xml.etree import ElementTree |
| 12 | +from termcolor import colored |
| 13 | +from os.path import isdir |
| 14 | +from os import makedirs, rmdir |
| 15 | +from sys import exit |
| 16 | +from glob import glob as xmlFiles |
| 17 | +import shutil |
| 18 | +import argparse |
| 19 | + |
| 20 | +# Directories |
| 21 | +train_directory = './images/train' # It should contain the xml files with bounding boxes |
| 22 | +test_directory = './images/train' # It should contain the xml files with bounding boxes |
| 23 | + |
| 24 | +# Keep calm and do not touch the rest of the code :3 |
| 25 | +parser = argparse.ArgumentParser() |
| 26 | +parser.add_argument("--move", help="Put all wrong xml and images to a wrong_data folder inside each folder", action="store_true") |
| 27 | +args = parser.parse_args() |
| 28 | + |
| 29 | +if not isdir(train_directory) or not isdir(test_directory): |
| 30 | + print(colored('[!]', 'yellow', attrs=['bold']), colored('The training or test directories do not exist')) |
| 31 | + exit(1) |
| 32 | +else: |
| 33 | + print(colored('[Ok]', 'green'), colored('Directories exists')) |
| 34 | + |
| 35 | +everythingWentAsExpected = True |
| 36 | + |
| 37 | +for tree in [train_directory, test_directory]: |
| 38 | + if args.move and not isdir(tree + '/wrong_data'): |
| 39 | + makedirs(tree + '/wrong_data') |
| 40 | + |
| 41 | + for file in xmlFiles(tree + '/*.xml'): |
| 42 | + xmlFile = ElementTree.parse(file) |
| 43 | + boxes = xmlFile.findall('object/bndbox') |
| 44 | + for box in boxes: |
| 45 | + xmin, ymin, xmax, ymax = box.getchildren() |
| 46 | + x_value = int(xmax.text) - int(xmin.text) |
| 47 | + y_value = int(ymax.text) - int(ymin.text) |
| 48 | + |
| 49 | + if x_value < 33 or y_value < 33: |
| 50 | + print(colored('[!]', 'red'), 'File {} contains a bounding box smaller than 32 in height or width'.format(file)) |
| 51 | + print(colored('xmax - xmin', 'yellow', attrs=['bold']), x_value) |
| 52 | + print(colored('ymax - ymin', 'yellow', attrs=['bold']), y_value) |
| 53 | + everythingWentAsExpected = False |
| 54 | + |
| 55 | + if args.move: |
| 56 | + wrongPicture = xmlFile.find('filename') |
| 57 | + try: |
| 58 | + shutil.move(file, tree + '/wrong_data/') |
| 59 | + shutil.move(tree + '/' + wrongPicture.text, tree + '/wrong_data/') |
| 60 | + print(colored('Files moved to' + tree + '/wrong_data', 'blue')) |
| 61 | + except Exception as e: |
| 62 | + print(colored(e, 'blue')) |
| 63 | + |
| 64 | +if everythingWentAsExpected: |
| 65 | + print(colored('[Ok]', 'green'), 'All bounding boxes are equal or larger than 32 :-)') |
| 66 | + try: |
| 67 | + rmdir(train_directory + '/wrong_data') |
| 68 | + rmdir(test_directory + '/wrong_data') |
| 69 | + except OSError: |
| 70 | + print(colored('[Info]', 'blue'), 'Directories wrong_data were not removed because they contain some files') |
| 71 | + |
| 72 | +else: |
| 73 | + print() |
| 74 | + print(colored('[Error]', 'red'), ' (╯°□°)╯ ┻━┻') |
0 commit comments