-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtransform.py
62 lines (41 loc) · 1.74 KB
/
transform.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
import json
from os.path import join
import pickle
def read_all(directory, filename):
with open(join(directory, filename), 'r') as f:
lines = f.readlines()
return json.loads(lines[0])
directory = "/nfs/bigcornea/add_disk0/pathak/biodata2/BBBC041"
image_directory= join(directory, 'images')
data_train = read_all(directory, "training.json")
data_test = read_all(directory, "test.json")
voc_labels = ('schizont', 'gametocyte', 'trophozoite', 'red blood cell', 'difficult', 'ring', 'leukocyte')
voc_labels += ('bg',)
label_map = {k: v for v, k in enumerate(voc_labels)}
def process(data):
image_paths = []
labels = []
for e in data:
path = directory + e["image"]["pathname"]
image_paths.append(path)
boxes = {"boxes": [] , "labels" : []}
for elements in e["objects"]:
bounding_box , label = elements["bounding_box"] , elements["category"]
min_y = bounding_box['minimum']['r']
min_x = bounding_box['minimum']['c']
max_y = bounding_box['maximum']['r']
max_x = bounding_box['maximum']['c']
boxes["boxes"].append([min_x, min_y, max_x, max_y])
boxes["labels"].append(label_map[label])
labels.append(boxes)
return labels , image_paths
data_train = process(data_train)
data_test = process(data_test)
with open("TRAIN_images.json", 'w') as output:
output.write( str(data_train[1]).replace("'", "\"") )
with open("TRAIN_objects.json", 'w') as output:
output.write( str(data_train[0]).replace("'", "\"") )
with open("TEST_images.json", 'w') as output:
output.write( str(data_test[1]).replace("'", "\"") )
with open("TEST_objects.json", 'w') as output:
output.write( str(data_test[0]).replace("'", "\"") )