-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScamCanner.py
243 lines (215 loc) · 7.26 KB
/
ScamCanner.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
from logreg import logr
import cv2
import numpy as np
import os
from transform import make_document
import matplotlib.pyplot as plt
import math
import scipy.ndimage.filters as filters
import scipy.ndimage as ndimage
def findy(r,t,x):
return(int((r-x*math.cos(t))/math.sin(t)))
def findx(r,t,y):
return(int((r-y*math.sin(t))/math.cos(t)))
def intersection(r,t,r1,t1):
a=np.array([[math.cos(t),math.sin(t)],[math.cos(t1),math.sin(t1)]])
det=np.linalg.det(a)
if det<0.001:
return None
else:
x=(r*math.sin(t1)-r1*math.sin(t))/det
y=(-r*math.cos(t1)+r1*math.cos(t))/det
return int(x),int(y)
def dist(pt1,pt2):
return math.sqrt((pt1[0]-pt2[0])**2+(pt1[1]-pt2[1])**2)
def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):
dim = None
(h, w) = image.shape[:2]
if width is None and height is None:
return image,1
if width is None:
r = height / float(h)
dim = (int(w * r), height)
else:
r = width / float(w)
dim = (width, int(h * r))
resized = cv2.resize(image, dim, interpolation = inter)
return resized,1/r
def HoughLines(edges,x_max,y_max):
theta_max = 1.0 * math.pi
theta_min = -1.0 * math.pi / 2.0
r_min = 0.0
r_max = math.hypot(x_max, y_max)
r_dim = 200
theta_dim = 300
hough_space = np.zeros((r_dim,theta_dim))
for edge in edges:
for itheta in range(theta_dim):
x,y=edge.pt
theta = 1.0 * itheta * (theta_max - theta_min) / theta_dim + theta_min
r = x * math.cos(theta) + y * math.sin(theta)
ir = round(r_dim * ( 1.0 * r ) / r_max)
if ir>=0 and ir<r_dim:
hough_space[ir,itheta] = hough_space[ir,itheta] + 1
neighborhood_size = 20
threshold = 20
data_max = filters.maximum_filter(hough_space, neighborhood_size)
maxima = (hough_space == data_max)
data_min = filters.minimum_filter(hough_space, neighborhood_size)
diff = ((data_max - data_min) > threshold)
maxima[diff == 0] = 0
labeled, num_objects = ndimage.label(maxima)
slices = ndimage.find_objects(labeled)
temp=[]
for dy,dx in slices:
x_center = (dx.start + dx.stop - 1)/2
y_center = (dy.start + dy.stop - 1)/2
temp.append((hough_space[int(y_center)][int(x_center)],x_center,y_center))
temp=sorted(temp,reverse=True)
#print(len(temp),temp)
x,y=[],[]
r=[]
theta=[]
for _,i,j in temp:
b=True
c=True
for x1,y1 in zip(x,y):
if dist((x1,y1),(i,j))<10:#checking for close lying hough peaks
b=False
break
if y1<10 and j<10 and (abs(x1-i)<10 or abs(x1-i) in range(190,210)):
c=False
break
if b and c:
x.append(i)
y.append(j)
r.append((1.0 * j * r_max ) / r_dim)
theta.append(1.0 * i * (theta_max - theta_min) / theta_dim + theta_min)
plt.imshow(hough_space, origin='lower')
plt.plot(x,y, 'ro')
cnt=1
for i,j in zip(x,y):
plt.text(i+1,j+1,str(cnt))
cnt+=1
plt.savefig('hough_space_maximas.png', bbox_inches = 'tight')
plt.close()
return r,theta
inputDir='TEST'
lr=logr()
surf = cv2.xfeatures2d.SURF_create(75)
for file in os.listdir(inputDir):
img_path = './' + inputDir + '/' + file
image=cv2.imread(img_path)
org=image.copy()
if(image.shape[0] > image.shape[1]):
ratio=image.shape[0]/720
image,ratio = image_resize(image, height=720)
else:
ratio=image.shape[1]/720
image,ratio = image_resize(image, width=720)
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
kernel = np.ones((7,7), np.float32)
gray=cv2.morphologyEx(gray,cv2.MORPH_CLOSE,kernel)
kernel_sharpening = np.array([[-1,-1,-1],
[-1, 9,-1],
[-1,-1,-1]])
sharp = cv2.filter2D(gray, -1, kernel_sharpening)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
cl1 = clahe.apply(sharp)
kps, descs = surf.detectAndCompute(cl1, None)
edges=[]
nedges=[]
for i in range(len(kps)):
prob=lr.pred(descs[i].reshape(1,-1))
if prob==1:
edges.append(kps[i])
else:
nedges.append(kps[i])
rem=[]
left=[]
kernel_size = 25
thre = 3
for i in range(len(edges)):
x = (edges[i]).pt[0]
y = (edges[i]).pt[1]
count = 0
for j in range(len(edges)):
if j==i:
continue
if dist(edges[j].pt,(x,y))<=kernel_size:
count += 1
if count<thre:
rem.append(edges[i])
else:
left.append(edges[i])
edges=left
out_img = np.copy(image)
cv2.drawKeypoints(image, edges, out_img, (0,0,255), flags = cv2.DRAW_MATCHES_FLAGS_DRAW_OVER_OUTIMG)
cv2.drawKeypoints(image, nedges, out_img, (255,0,0), flags = cv2.DRAW_MATCHES_FLAGS_DRAW_OVER_OUTIMG)
cv2.drawKeypoints(image, rem, out_img, (0,255,0), flags = cv2.DRAW_MATCHES_FLAGS_DRAW_OVER_OUTIMG)
try:
x_max = image.shape[1]
y_max = image.shape[0]
d,theta = HoughLines(edges,x_max,y_max)#,min_dist=0)#,min_angle=0,threshold=0,num_peaks=20)
#print(len(d))
#print(d,theta)
cnt=1
for r,t in zip(d,theta):
s=math.sin(t)
c=math.cos(t)
x=r*c
y=r*s
x1=x+10000*s
y1=y-10000*c
x2=x-10000*s
y2=y+10000*c
color=(0,255,255)
if cnt<=4:
color=(0,102,255)
cv2.line(out_img,(int(x1),int(y1)),(int(x2),int(y2)),color,2)
cnt+=1
if len(d)<4:
raise Exception("ONE OF THE EDGES IS MISSING OR NOT CLEAR!!")
corners=[]
d1=d[:4]
theta1=theta[:4]
for r,t in zip(d1,theta1):
for r1,t1 in zip(d1,theta1):
if r==r1 and t==t1:
continue
pt=intersection(r,t,r1,t1)
if pt!=None and pt[0]>=0 and pt[0]<x_max and pt[1]>=0 and pt[1]<y_max:
corners.append(pt)
if len(corners)<4:
raise Exception("THE CORNER IS OUT OF THE PICTURE")
cont=[]
for i in range(len(corners)):
temp=[]
temp.append(corners[i][0])
temp.append(corners[i][1])
temp=np.asarray(temp)
temp1=[]
temp1.append(temp)
temp1=np.asarray(temp1)
cont.append(temp)
cont=np.asarray(cont)
#hull=[]
#hull.append(cv2.convexHull(cont,False))
#cv2.drawContours(out_img, hull, 0, (255,255,0),1)
warped = make_document(org, [cont], ratio)
if(warped.shape[0] > warped.shape[1]):
warp,_ = image_resize(warped, height=1000)
else:
warp,_ = image_resize(warped, width=1000)
cv2.imshow("doc", warp)
#cv2.imshow("window", out_img)
key=cv2.waitKey(0)
if key==ord("d"):
raise Exception("ERROR")
cv2.destroyAllWindows()
#os.remove(img_path)
except Exception as e:
print(e)
cv2.imshow("ERROR",out_img)
cv2.waitKey(0)
cv2.destroyAllWindows()