-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabel_generator.py
61 lines (51 loc) · 2.28 KB
/
label_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
from matplotlib import pyplot as plt
import matplotlib.patches as mpatches
from skimage.io import imread
import numpy as np
def char_concatenate(source, dest):
fp_r = open(source, 'r')
fp_w = open(dest, 'w')
for aLine in fp_r:
coord_list = aLine.split(';')
raw_char = []
for coord in coord_list:
c, prob, minx, miny, maxx, maxy = coord.split(',')
c = int(c); prob = float(prob); minx = int(minx); miny = int(miny); maxx = int(maxx); maxy = int(maxy)
raw_char.append([c, prob, minx, miny, maxx, maxy])
sorted_char = sorted(raw_char, key=lambda raw_char: raw_char[2])
label = ""
min_prob = 1
max_prob = 0
for digit in sorted_char:
label += str(digit[0])
if min_prob > digit[1]:
min_prob = digit[1]
if max_prob < digit[1]:
max_prob = digit[1]
min_x = sorted_char[0][2]
min_y = sorted_char[0][3]
max_x = sorted_char[len(sorted_char)-1][4]
max_y = sorted_char[len(sorted_char)-1][5]
print("{}, {:.6f}, {:.6f}, {}, {}, {}, {}".format(label, min_prob, max_prob, min_x, min_y, max_x, max_y), file=fp_w)
fp_r.close()
fp_w.close()
def plot_labels(source, image_file):
fp = open(source, 'r')
label_list=[]
for aLine in fp:
label, min_p, max_p, minx, miny, maxx, maxy = aLine.split(',')
min_p = float(min_p); max_p = float(max_p); minx = int(minx); miny = int(miny); maxx = int(maxx); maxy = int(maxy)
label_list.append([label, min_p, max_p, minx, miny, maxx, maxy])
image = imread(image_file, as_grey=True)
fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(round(image.shape[1]/100), round(image.shape[0]/100)))
ax.imshow(np.flipud(image))
for label in label_list:
rect = mpatches.Rectangle((label[3], label[4]), abs(label[5] - label[3])+10, abs(label[6] - label[4])+10,
fill=False, edgecolor='green', linewidth=2)
ax.add_patch(rect)
ax.text(label[5]+10, label[6]+10, label[0], size=16, color='green')
ymax = image.shape[0]
xmax = image.shape[1]
ax.set_ylim(-10, ymax + 10)
ax.set_xlim(-10, xmax + 10)
plt.show()