-
Notifications
You must be signed in to change notification settings - Fork 1
/
mturk_csv_to_label_images.py
executable file
·38 lines (35 loc) · 1.29 KB
/
mturk_csv_to_label_images.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
import csv
import re
import json
import numpy as np
import os
from PIL import Image
#import matplotlib.pyplot as plt
image_size = [362,543] #Height, width
re_file_name = re.compile('.*/(.*)$')
folder = 'image_labels'
if not os.path.exists(folder):
os.makedirs(folder)
with open('Batch_2939578_batch_results.csv') as csvfile:
reader = csv.reader(csvfile)
next(reader, None) # skip the headers
for row in reader:
re_filename_search = re.search(re_file_name, row[27]) #Find filename in URL
if re_filename_search:
filename = re_filename_search.group(1)
else:
print "Could not find filename in ", row[27]
continue
label_set = json.loads(row[29])
image_arr = np.zeros(image_size, dtype='uint8')
for label in label_set:
image_arr[label['top']:(label['top'] + label['height']),label['left']:(label['left'] + label['width'])] = 1
img = Image.fromarray(image_arr, mode='L')
img.save(os.path.join(folder, 'label_' + filename))
# if label_set:
# print label_set
# print filename
# im = plt.imread(filename)
# implot = plt.imshow(im)
# plt.imshow(image_arr, cmap='Greys', alpha=0.3)
# plt.show()