-
Notifications
You must be signed in to change notification settings - Fork 0
/
test3.py
159 lines (141 loc) · 4.6 KB
/
test3.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
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
def detect_box(image,line_min_width = 15):
gray_scale=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
th1,img_bin = cv2.threshold(gray_scale,150,225,cv2.THRESH_BINARY)
kernal6h = np.ones((1,line_min_width), np.uint8)
kernal6v = np.ones((line_min_width,1), np.uint8)
img_bin_h = cv2.morphologyEx(~img_bin, cv2.MORPH_OPEN, kernal6h)
img_bin_v = cv2.morphologyEx(~img_bin, cv2.MORPH_OPEN, kernal6v)
img_bin_final=img_bin_h|img_bin_v
final_kernel = np.ones((3, 3), np.uint8)
img_bin_final=cv2.dilate(img_bin_final,final_kernel,iterations=1)
_, labels, stats,_ = cv2.connectedComponentsWithStats(~img_bin_final, connectivity=8, ltype=cv2.CV_32S)
return stats,labels
def plot(image,cmap=None):
plt.figure(figsize=(15, 15))
plt.imshow(image,cmap=cmap)
plt.show()
def imshow_components(labels):
### creating a hsv image, with a unique hue value for each label
label_hue = np.uint8(179*labels/np.max(labels))
### making saturation and volume to be 255
empty_channel = 255*np.ones_like(label_hue)
labeled_img = cv2.merge([label_hue, empty_channel, empty_channel])
### converting the hsv image to BGR image
labeled_img = cv2.cvtColor(labeled_img, cv2.COLOR_HSV2BGR)
labeled_img[label_hue==0] = 0
### returning the color image for visualising Connected Componenets
return labeled_img
def check_ticked(image, checkbox, threshold = 127):
x,y,w,h = checkbox
low = 0
high = 0
for i in range(w):
for j in range(h):
if image[y + j][x + i] < threshold:
low += 1
else:
high += 1
if low * 100 / (low + high) > 5:
return True
return False
def pos_to_index(form, pos):
posx = 0
posy = 0
while pos > 0:
pos -= 1
len_aux = len(form[posx])
if posy >= len_aux - 1:
posy = 0
posx += 1
else:
posy += 1
return posx, posy
def get_checkboxes(image, stats):
result = []
height, width, _ = image.shape
for x,y,w,h,_ in stats[2:]:
relative_w = w * 100 / width
relative_h = h * 100 / height
if relative_w < 0.8 and relative_h < 0.6 and relative_w > 0.5 and relative_h > 0.4:
#if True:
cv2.rectangle(image, (x - 1, y - 1), (x + w + 1, y + h + 1), (0, 255, 0), 1)
result.append([x - 1, y - 1, w + 1, h + 1])
print(relative_w, relative_h)
return result
def get_form_shape(checkboxes, threshold = 10):
rows = []
last_y = -11
i = -1
for box in checkboxes:
if abs (box[1] - last_y) > threshold:
i += 1
last_y = box[1]
rows.append([0])
else:
rows[i].append(0)
return rows
def check_ticked(image, checkbox, threshold = 127):
x,y,w,h = checkbox
low = 0
high = 0
for i in range(w):
for j in range(h):
if image[y + j][x + i] < threshold:
low += 1
else:
high += 1
if low * 100 / (low + high) > 5:
return True
return False
def pos_to_index(form, pos):
posx = 0
posy = 0
while pos > 0:
pos -= 1
len_aux = len(form[posx])
if posy >= len_aux - 1:
posy = 0
posx += 1
else:
posy += 1
return posx, posy
def pretty_print(form):
for iterator, mini_list in enumerate(form):
print("Question " + str(iterator + 1) + ":")
for option, element in enumerate(mini_list):
print("option " + chr(option + 65) + ": " + str(element))
print("\n")
def main():
image_path='second.jpg'
image=cv2.imread(image_path)
height, width, channels = image.shape
stats,labels = detect_box(image, line_min_width = 1)
plot_flag=False
save_output=True
out_folder='outs'
os.makedirs(out_folder,exist_ok=True)
checkboxes = get_checkboxes(image, stats)
form = get_form_shape(checkboxes)
image_path2 = 'checkedsecond.png'
image2=cv2.imread(image_path2)
gray = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
counter = 0
for box in checkboxes:
check = check_ticked(gray, box)
if check == True:
posx, posy = pos_to_index(form, counter)
form[posx][posy] += 1
counter += 1
pretty_print(form)
if plot_flag:
#plot(cc_out)
plot(image)
if save_output:
#cv2.imwrite(os.path.join(out_folder,f'cc_{image_path}'),cc_out)
cv2.imwrite(os.path.join(out_folder,f'out_{image_path}'),image)
if __name__ == "__main__":
main()