-
Notifications
You must be signed in to change notification settings - Fork 1
/
SingleCaptchaRecognise.py
164 lines (106 loc) · 4.37 KB
/
SingleCaptchaRecognise.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
from __future__ import print_function
import cv2
from PIL import Image, ImageStat
import numpy as np
from scipy.ndimage.filters import median_filter
import os, glob
from scipy.misc import imsave
from scipy import ndimage
from selenium import webdriver
from singleCSV import gray_to_csv
from recognise import recognize_single
captcha_list = []
driver = webdriver.Chrome()
def download_captch():
def get_captcha(driver, element, path):
location = element.location
size = element.size
driver.save_screenshot(path)
image = Image.open(path)
left = location['x']
top = location['y'] - 18
right = location['x'] + size['width']
bottom = location['y'] + size['height'] -19
image = image.crop((left, top, right, bottom)) # defines crop points
image.save(path, 'png') # saves new cropped image
driver.maximize_window()
driver.get("http://www.snaphost.com/captcha/Demo.htm")
driver.implicitly_wait(30)
# driver.get('http://www.freelibros.org/wp-login.php')
# img = driver.find_element_by_xpath('html/body/div[1]/form/p[3]/label/img')
#img = driver.find_element_by_xpath('html/body/form/table[2]/tr[2]/td[2]/img')
img = driver.find_element_by_id('CaptchaImage')
get_captcha(driver, img, "captcha.png")
#driver.close()
img_orginal = Image.open("captcha.png")
img_crop = img_orginal.crop((25, 20, 180, 65))
img_crop.save("crop.png")
img_crop_read = cv2.imread("crop.png")
img_gray = cv2.cvtColor( img_crop_read, cv2.COLOR_RGB2GRAY )
(thresh, img_bw) = cv2.threshold(img_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
img_median = median_filter(img_bw, 5)
img_dilation = ndimage.grey_dilation(img_median, size=(3, 3))
cv2.imwrite("dilation.png", img_dilation)
kernel = np.array([[0, 0, 0], [1, 1, 1], [0, 0, 0]], dtype=np.uint8)
ekernel = np.array([[1, 1], [1, 1]], dtype=np.uint8)
im = cv2.imread("dilation.png")
im = 255-im
img2gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
cv2.imwrite("dil-grayed.png", img2gray)
img_bnw = cv2.imread("dil-grayed.png")
# cv2.imshow("bnw",img_bnw)
dilate = cv2.dilate(img_bnw, kernel)
erosion = cv2.erode(dilate, ekernel)
erosion_gray = cv2.cvtColor(erosion, cv2.COLOR_BGR2GRAY)
cv2.imwrite("erosed.png", erosion_gray)
def captch_ex(original_file):
my_original =cv2.imread(original_file)
# cv2.imshow("Current Image is ",my_original)
# cv2.waitKey(10000)
img2gray = cv2.cvtColor(my_original, cv2.COLOR_BGR2GRAY)
# img2gray = cv2.cvtColor(file_name, cv2.COLOR_BGR2GRAY)
contours, hierarchy = cv2.findContours(img2gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) # get contours
index = 0
our_contours= []
for contour in contours:
[x, y, w, h] = cv2.boundingRect(contour)
our_contours.append([x,y,w,h])
#print (our_contours)
our_contours.sort()
#print(our_contours)
for contour in our_contours:
# get rectangle bounding contour
[x, y, w, h] = contour
# Don't plot small false positives that aren't text
if w < 20 and h < 20:
continue
# cv2.imshow("Original",original_file)
# cv2.waitKey(100000)
#you can crop image and send to OCR,false detected will return no text
cropped = my_original[y:y + h , x : x + w]
res = cv2.resize(cropped,(28,28), interpolation=cv2.INTER_AREA)
cropped_to_gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
# cv2.imshow("Here it",cropped_to_gray)
# cv2.waitKey(1000)
# cv2.imshow("Each Cropped",cropped_to_gray)
# cv2.waitKey(100000)
s = 'image' + str(index)+ '.png'
#s = "./test-/" + file.split(".")[0] + ".png"
#s = ("./test-/%s.png" % (str(index)))
cv2.imwrite(s, cropped_to_gray)
gray_to_csv(s)
single_digit = recognize_single('single.csv')
captcha_list.append(single_digit)
index = index + 1
###################
download_captch()
original_file = "dil-grayed.png"
captch_ex(original_file)
print (captcha_list)
str_cap = ''.join(captcha_list)
print (str_cap)
fill_field = driver.find_element_by_id("skip_CaptchaCode")
fill_field.send_keys(str_cap)
submit_field = driver.find_element_by_id("skip_DemoSubmit")
submit_field.click()
confirm_element = driver.find_element_by_xpath("//*[@id='form1']/div[3]")