-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
executable file
·160 lines (108 loc) · 3.29 KB
/
utils.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import math
import numpy as np
from PIL import ImageDraw, Image
import cv2
import matplotlib.pyplot as plt
import random
def determine_max(rect):
x_min = math.inf
x_max = 0
y_min = math.inf
y_max = 0
for i in rect:
if i[0] < x_min:
x_min = i[0]
if i[0] > x_max:
x_max = i[0]
if i[1] < y_min:
y_min = i[1]
if i[1] > y_max:
y_max = i[1]
# return ((x_min,y_min),(x_max,y_max))
return ((y_min, x_min), (y_max, x_max))
def get_mask(bounding_boxes):
matrix = np.zeros((50, 50))
for box in bounding_boxes:
if box != "name":
# print("X:",int(int(bounding_boxes[box][1])/20))
# print("Y:",int(int(bounding_boxes[box][3])/20))
for j in range(
int(int(bounding_boxes[box][1]) / 20),
int(int(bounding_boxes[box][3]) / 20),
):
for i in range(
int(int(bounding_boxes[box][0] / 20)),
int(int(bounding_boxes[box][2]) / 20),
):
if i >= 50:
i = 49
if j >= 50:
j = 49
matrix[j][i] = 1
return matrix
def bounding_box(matrix, img):
matrix = np.asarray(matrix).astype(np.uint8)
# print(type(matrix))
# print(matrix)
contours, hierarchy = cv2.findContours(
matrix, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE
)
contour_store = []
for i in contours:
contour_store.append(i.reshape((-1, 2)))
box_find = []
for i in contour_store:
f = determine_max(i)
box_find.append(f)
mat = np.zeros([50, 50])
for j in box_find:
if j[0] != j[1]:
for a in range(j[0][0], j[1][0]):
for b in range(j[0][1], j[1][1]):
mat[a][b] = 1
else:
mat[j[0][0] : j[1][0]] = 1
draw = ImageDraw.Draw(img)
for h in box_find:
draw.rectangle(
((h[0][1] * 5, h[0][0] * 5), (h[1][1] * 5, h[1][0] * 5)),
outline="black",
width=3,
)
plt.imshow(img)
plt.show()
# return box_find
def plot_gallery(model, transform):
imgs = [
"06_11.jpg",
"07_01.jpg",
"07_08.jpg",
"07_14.jpg",
"08_15.jpg",
"00_00.jpg",
]
url = "/Users/hrishikesh/Hrishikesh/Projects/Street View Text Recognition/SVT/svt1/img/"
for path in imgs:
print("\n")
er = model(
transform(Image.open(url + path).resize((250, 250)))
.view([-1, 3, 250, 250])
.double()
)
e = er.detach().numpy()
# print(max(e.flatten()))
e = e.reshape([50, 50]) > 0.5
plt.imshow(Image.open(url + path).resize((250, 250)))
plt.show()
im = Image.open(url + path).resize((250, 250))
bounding_box(e, im)
print("\n")
def crop_img(img, IMG_SIZE, factor=0.3):
width, height = img.size
new_arr = []
for x in range(0, width, 80):
for y in range(0, height, 80):
size = random.randint(20, 150)
new_arr.append(img.crop((x, y, x + size, y + size)))
# display(img.crop((x,y,x+size,y+size)))
return new_arr