Skip to content

Commit 0d3bcfa

Browse files
Merge pull request EdjeElectronics#86 from moiseslodeiro/master
Bounding box size checker
2 parents 02b0664 + 1b7dad7 commit 0d3bcfa

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ Download and install LabelImg, point it to your \images\train directory, and the
196196

197197
LabelImg saves a .xml file containing the label data for each image. These .xml files will be used to generate TFRecords, which are one of the inputs to the TensorFlow trainer. Once you have labeled and saved each image, there will be one .xml file for each image in the \test and \train directories.
198198

199+
Also, you can check if the size of each bounding box is correct by running sizeChecker.py
200+
201+
```
202+
(tensorflow1) C:\tensorflow1\models\research\object_detection> python sizeChecker.py --move
203+
```
204+
199205
### 4. Generate Training Data
200206
With the images labeled, it’s time to generate the TFRecords that serve as input data to the TensorFlow training model. This tutorial uses the xml_to_csv.py and generate_tfrecord.py scripts from [Dat Tran’s Raccoon Detector dataset](https://github.com/datitran/raccoon_dataset), with some slight modifications to work with our directory structure.
201207

sizeChecker.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

Comments
 (0)