-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerator.py
109 lines (87 loc) · 3.66 KB
/
generator.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""
Generates the correct text file to train the YOLOv3 model
Accepts via_region_data or flat images with the correct names
Also add the classes
"""
import os
import json
import cv2
# Directory containing training data, each subfolder is a class
DATA_DIR = "training_data"
# Create the train.txt file
training = open('train.txt', 'a+')
# Create the plate_classes.txt file
classes = open('model_data/plate_and_car_classes.txt', 'a+')
class_list = os.listdir(DATA_DIR)
def remove_ds_store(directory_list):
if ".DS_Store" in directory_list:
directory_list.remove(".DS_Store")
# Remove .DS_Store for macOS users
remove_ds_store(class_list)
# Write each class to the plate_classes.txt file
for index, one_class in enumerate(class_list):
# Get all images in the class directory
class_dir = os.path.join(DATA_DIR, one_class)
images = os.listdir(class_dir)
remove_ds_store(images)
# Special handling for region.json
if "region.json" in images:
print("region.json found!")
print("Loading images...")
json_path = os.path.join(class_dir, "region.json")
annotations = json.load(open(json_path))
for image in annotations:
image_exists = False
filename = annotations[image]['filename']
# Make sure that the name of the file has no spaces
image_path = os.path.join(class_dir, filename)
filename = filename.replace(" ", "_")
new_image_path = os.path.join(class_dir, filename)
try:
os.rename(image_path, new_image_path)
image_exists = True
except:
# Image doesn't exist
pass
if image_exists:
print("Loaded " + filename)
regions = annotations[image]['regions']
boxes = []
for region in regions:
x_coords = region['shape_attributes']['all_points_x']
y_coords = region['shape_attributes']['all_points_y']
x_min, x_max = min(x_coords), max(x_coords)
y_min, y_max = min(y_coords), max(y_coords)
# Box format: x_min,y_min,x_max,y_max,class_id
boxes.append('{},{},{},{},{}'.format(x_min, y_min, x_max, y_max, class_list.index(list(region['region_attributes'].keys())[0])))
# Row format: image_file_path box1 box2 ... boxN
row_string = new_image_path
for box in boxes:
row_string += " "
row_string += box
# Check if was populated with box at all, could be an image that exists without a bounding box
if row_string != new_image_path:
training.write(row_string + "\n")
else: # Get bounding boxes from file name
# Each box is formatted as such: x_min,y_min,x_max,y_max,NAME
# Row format: image_file_path box1 box2 ... boxN
for image in images:
row_string = image
boxes = image.split()
# Add class_id
for box_index, box in enumerate(boxes):
box_params = box.split(",")
class_name = box_params.pop()
box_params.append(class_list.index(class_name))
boxes[box_index] = ",".join(box_params)
for box in boxes:
row_string += " "
row_string += box
training.write(row_string + "\n")
# Write classes
if index == len(class_list): # Last line does not have a line escape
classes.write(one_class)
else:
classes.write(one_class + "\n")
training.close()
classes.close()