/
amazon_captcha_solver.py
69 lines (56 loc) · 2.38 KB
/
amazon_captcha_solver.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
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 30 00:25:12 2017
@author: Xianyang
"""
import numpy as np
import argparse
import cv2
import os
from PIL import Image
import pytesseract
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract'
def captcha_solver(path,threshold):
image = cv2.imread(path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
colmean = image.sum(axis=0)/70
colmean_index = np.where(colmean < threshold)
min_val = np.min(colmean_index)
max_val = np.max(colmean_index)
colmean_index = list(colmean_index)
separators = []
for i in np.arange(0,len(colmean_index[0]) - 1):
if colmean_index[0][i] != colmean_index[0][i+1] - 1:
separators.append(colmean_index[0][i])
if len(separators) == 5:
cv2.imwrite('Digit1.jpg', image[:,min_val:separators[0]])
cv2.imwrite('Digit2.jpg', image[:,separators[0]+1:separators[1]])
cv2.imwrite('Digit3.jpg', image[:,separators[1]+1:separators[2]])
cv2.imwrite('Digit4.jpg', image[:,separators[2]+1:separators[3]])
cv2.imwrite('Digit5.jpg', image[:,separators[3]+1:separators[4]])
cv2.imwrite('Digit6.jpg', image[:,separators[4]+1:max_val])
string = []
for i in np.arange(1,7):
img = Image.open('Digit'+str(i)+'.jpg')
# converted to have an alpha layer
im2 = img.convert('RGBA')
if i in [1,3,5]:
# rotated image
rot = im2.rotate(15, expand=1)
else:
# rotated image
rot = im2.rotate(-15, expand=1)
# a white image same size as rotated image
fff = Image.new('RGBA', rot.size, (255,)*4)
# create a composite image using the alpha layer of rot as a mask
out = Image.composite(rot, fff, rot)
out.convert(img.mode).save('pic.jpg')
char = pytesseract.image_to_string(Image.open('pic.jpg'),
config='-c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ -psm 10')[0].upper()
if i == 1:
string = char
else:
string += char
else:
string='Cannot solve Captcha'
return(string)