forked from EdjeElectronics/TensorFlow-Object-Detection-API-Tutorial-Train-Multiple-Objects-Windows-10
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pbtxt.py
63 lines (54 loc) · 2.24 KB
/
pbtxt.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
import argparse
import os.path
DEFAULT_IMAGE_ROOT = 'images'
def read_tags(source_root):
with open(os.path.join(source_root,'predefined_classes.txt')) as f:
lines = f.readlines()
tag_num = 0;
tags = []
for tag in lines:
tag = tag.strip()
if tag:
tag_num += 1
print (str(tag_num) + " " + tag)
tags.append(tag)
return tags;
def read_temp1(tags, source_root):
a = " if row_label == '{0}':\n"
b = " return {0}\n"
result = ""
for i in range(len(tags)):
result += a.format(tags[i])
result += b.format(i+1)
with open('template/generate_tfrecord.template') as f:
template = f.read();
with open(os.path.join(source_root,'generate_tfrecord.py'), 'w') as x_file:
x_file.write(template.replace("<replace with tags>", result))
def read_temp2(tags, source_root):
pbtxt_path = os.path.join(source_root,"label_map.pbtxt")
trainrecord_path = os.path.join(source_root,"train.record")
testrecordpath = os.path.join(source_root,"test.record")
with open('template/pipeline.template') as f:
template = f.read();
template = template.replace("<tag_number>", str(len(tags)) )
template = template.replace("<pbtxt_path>", pbtxt_path)
template = template.replace("<train_record_path>", trainrecord_path)
template = template.replace("<test_record_path>", testrecordpath)
with open(os.path.join(source_root,'pipeline.config'), 'w') as x_file:
x_file.write(template)
def read_temp3(tags, source_root):
a = "item {{\n id: {0}\n name: '{1}'\n}}\n"
result=""
for i in range(len(tags)):
result += a.format(i+1, tags[i])
with open(os.path.join(source_root,'label_map.pbtxt'), 'w') as x_file:
x_file.write(result)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--source_root", help="image source folder root", required=True, default=DEFAULT_IMAGE_ROOT )
args = parser.parse_args()
tags = read_tags(args.source_root)
read_temp1(tags, args.source_root)
read_temp2(tags, args.source_root)
read_temp3(tags, args.source_root)
main()