-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathselepublic.py
260 lines (253 loc) · 8.33 KB
/
selepublic.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
import numpy as np
import sys
from tensorflow.keras import models
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.preprocessing.image import load_img
import cv2 as cv
import tensorflow as tf
from PIL import Image
import time
def split_digits_in_img(img_array):
x_list = list()
img1 = np.zeros((80,215,1), np.uint8)
img = np.zeros((80,215,1), np.uint8)
gray1 = img_array
gray2 = img_array
img=gray2
for t in range(2):
for x in range(80):
for y in range(215):
if x>=6 and x<=74 and y<= 213:
if img[x][y]==112:
if (img[x+1][y]==140 and img[x+2][y]==140 ) or (img[x-1][y]==140 and img[x-2][y]==140) and img[x+1][y]!=255 and img[x-1][y]!=255 and img[x+4][y]!=255:
img[x][y]=(140)
for t in range(2):
for x in range(80):
for y in range(215):
if x<=76 and y>=3 and y<=200:
if img[x][y]==112:
if (img[x][y-1]==140 and (img[x][y-2]==140 or img[x][y+5]==140 ) and img[x-1][y]!=255 and img[x-1][y]!=112 and img[x +3][y]!=255):
img[x][y]=(140)
##for t in range(8):
for x in range(80):
for y in range(215):
img1[x][y]=(img[x][y])
if x>=3 and x<=75 and y>=2 and y<=212:
if img[x][y]==112 or img[x][y]==117:
countt=0
if img[x+1][y]==140:
countt+=1
if img[x-1][y]==140:
countt+=1
if img[x][y+1]==140:
countt+=1
if img[x][y-1]==140:
countt+=1
if img[x+1][y+1]==140:
countt+=1
if img[x-1][y-1]==140:
countt+=1
if img[x+1][y-1]==140:
countt+=1
if img[x-1][y+1]==140:
countt+=1
if countt>=5:
img1[x][y]=(140)
a=np.array(140)
mask=cv.inRange(img1,a,a)
cv.bitwise_not(mask, mask)
ret, binary = cv.threshold(mask, 0, 255, cv.THRESH_BINARY_INV or cv.THRESH_OTSU)
kernel = cv.getStructuringElement(cv.MORPH_RECT, (1, 2))
binl = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel)
cv.bitwise_not(binl, binl)
##切
xrec=0
yrec=0
for x in range(80):
county = 0
for y in range(215):
if binl[x][y]==0:
county+=1
if county>=20:
yrec=x
break
for y in range(215):
countx=0
for x in range(80):
if binl[x][y]==0:
countx+=1
if countx>=8:
xrec=y
break
binl = binl[yrec-5:yrec+32, xrec-4:xrec+96]
for t in range(2):
for x in range(35):
for y in range(92):
if x>=1 and x<=32:
if binl[x][y]==255:
if (binl[x+1][y]==0 or binl[x+2][y]==0) and binl[x-1][y]==0:
binl[x][y]=(0)
for x in range(35):
for y in range(92):
if y>=1 and y<=94:
if binl[x][y]==0:
if (binl[x][y+1]==255 and binl[x][y-1]==255):
binl[x][y]=(255)
gray=binl
li=[]
j=5
while j<97 :
count=0
countleft=0
countright=0
countright1=0
count1=0
countup=0
countmid=0
countlow=0
for i in range(36):
if gray[i][j]==255:
count+=1
if gray[i][j+1]==255:
countright+=1
if gray[i][j+3]==255:
countright1+=1
if i>=0 and i<=12:
if gray[i][j]==255:
countup+=1
if i>12 and i<=24:
if gray[i][j]==0:
countmid+=1
if i>24 and i<=37:
if gray[i][j]==255:
countlow+=1
if count>=35 and countright1>35:
li.append(j+2)
j+=18
count1+=1
elif count>25 and countright>35 and countup>8 and countlow<3:
li.append(j+1)
j+=18
count1+=1
elif countup>8 and countmid>6 and countlow>8 and count1>0:
li.append(j+1)
j+=18
count1+=1
elif count>=33:
li.append(j+1)
j+=18
count1+=1
elif count==36:
li.append(j+2)
j+=18
count1+=1
else:
j+=1
if count1==2:
break
if(len(li)<3):
print('reload')
return
tmp=0
ttmp=li[0]
cc=0
ccc=0
for w in range(4):
a=gray[0:36,tmp:ttmp+1]
if len(a[0])>=38:
mid=int(len(a[0])/2)
a1=a[0:36,0:mid]
a2=a[0:36,mid:(mid*2)-1]
size=a1.shape
wet=size[1]
wet1=int(int(40-wet)/2)
wett=wet1
if wet+wet1*2<40:
wett=wett+1
img_patch = cv.copyMakeBorder(a1, 0, 0, wet1, wett, cv.BORDER_CONSTANT, value=255)
x_list.append(tf.expand_dims(img_patch/255.0,2))
if(w>=3): break
size1=a2.shape
wet2=size1[1]
wet3=int(int(40-wet2)/2)
wettt=wet3
if wet2+wet3*2<40:
wettt=wettt+1
img_patch2 = cv.copyMakeBorder(a2, 0, 0, wet3, wettt, cv.BORDER_CONSTANT, value=255)
x_list.append(tf.expand_dims(img_patch2/255.0,2))
if(cc>=2):break
tmp=li[cc]
if(cc<2):
ttmp=li[cc+1]-2
else:
ttmp=94
cc+=1
else:
size=a.shape
wet0=size[1]
wet11=int(int(40-wet0)/2)
wett1=wet11
if wet0+wet11*2<40:
wett1=wett1+1
img_patch3 = cv.copyMakeBorder(a, 0, 0, wet11, wett1, cv.BORDER_CONSTANT, value=255)
x_list.append(tf.expand_dims(img_patch3/255.0,2))
if(cc>2):break
tmp=li[cc]
if(cc<2):
ttmp=li[cc+1]-2
else:
ttmp=95
cc+=1
return x_list
##Main program
Browser = webdriver.Chrome()
LoginUrl= ('https://e3new.nctu.edu.tw/login/index.php')
## NOTICE you should input your username and password there.
UserName= ('xxxxxx')
UserPass= ('xxxxxx')
Browser.set_window_size(1280,1024)
for i in range(5):
if(i==0):
print("First try!")
else:
print(str(i+1)+" Retrying....")
Browser.get(LoginUrl)
with open('captcha1.png', 'wb') as file:
file.write(Browser.find_element_by_xpath('/html/body/div[5]/div[1]/div[1]/form[1]/img').screenshot_as_png)
img_rows = None
img_cols = None
digits_in_img=4
model = None
np.set_printoptions(suppress=True, linewidth=150, precision=9, formatter={'float': '{: 0.9f}'.format})
if os.path.isfile('cnn_model.h5'):
print("Model found.")
model = models.load_model('cnn_model.h5')
else:
print('No trained model found.')
exit(-1)
img_filename = 'captcha1.png'
img_array=cv.imread(img_filename,cv.IMREAD_GRAYSCALE)
os.remove('captcha1.png')
x_list = split_digits_in_img(img_array)
if(len(x_list)==0):
continue
varification_code = list()
for i in range(digits_in_img):
confidences = model.predict(np.array([x_list[i]]), verbose=0)
result_class = model.predict_classes(np.array([x_list[i]]), verbose=0)
varification_code.append(result_class[0])
##print('Digit {0}: Confidence=> {1} Predict=> {2}'.format(i + 1, np.squeeze(confidences), np.squeeze(result_class)))
final=''
for i in range(4):
final+=str(varification_code[i])
captcha=final
Browser.find_element_by_id('username').send_keys(UserName)
Browser.find_element_by_id('password').send_keys(UserPass)
Browser.find_element_by_name('captcha_code').send_keys(captcha)
Browser.find_element_by_id('password').send_keys(Keys.ENTER)
time.sleep(1)
strs = Browser.find_element_by_id("username").text
##Browser.quit()