-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathparse_captcha_back.py
executable file
·179 lines (148 loc) · 5.48 KB
/
parse_captcha_back.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
import sys
import os
import re
import subprocess
import tempfile
from PIL import Image
def parse_captcha(filename):
"""Return the text for thie image using Tesseract
"""
img = threshold(filename)
return tesseract(img)
def erase_block(filename):
"""Make little black block
"""
img = Image.open(filename)
pixdata = img.load()
for y in xrange(img.size[1]):
pixdata[0, y] = (255, 255, 255)
pixdata[10, y] = (255, 255, 255)
pixdata[19, y] = (255, 255, 255)
pixdata[28, y] = (255, 255, 255)
pixdata[img.size[0]-1,y] = (255, 255, 255)
pixdata[img.size[0]-2,y] = (255, 255, 255)
pixdata[img.size[0]-3,y] = (255, 255, 255)
pixdata[img.size[0]-4,y] = (255, 255, 255)
pixdata[img.size[0]-5,y] = (255, 255, 255)
for x in xrange(img.size[0]):
pixdata[x, 0] = (255, 255, 255)
pixdata[x, 1] = (255, 255, 255)
pixdata[x, 2] = (255, 255, 255)
pixdata[x, img.size[1]-1] = (255, 255, 255)
pixdata[x, img.size[1]-2] = (255, 255, 255)
pixdata[x, img.size[1]-3] = (255, 255, 255)
for y in xrange(1,img.size[1]-1):
for x in xrange(1,img.size[0]-1):
if pixdata[x, y][0] > 50 or pixdata[x,y][1] > 50 or pixdata[x,y][2] > 50:
# make dark color black
pixdata[x, y] = (255, 255, 255)
else:
# make light color white
num = 0
if pixdata[x+1, y][0] > 50 or pixdata[x+1,y][1] > 50 or pixdata[x+1,y][2] > 50:
num += 1
if pixdata[x-1, y][0] > 50 or pixdata[x-1,y][1] > 50 or pixdata[x-1,y][2] > 50:
num += 1
if pixdata[x, y+1][0] > 50 or pixdata[x,y+1][1] > 50 or pixdata[x,y+1][2] > 50:
num += 1
if pixdata[x, y-1][0] > 50 or pixdata[x,y-1][1] > 50 or pixdata[x,y-1][2] > 50:
num += 1
if (num == 4):
pixdata[x, y] = (255, 255, 255)
else:
pixdata[x, y] = (0,0,0)
img.save("erase_" + filename)
def split_block(filename):
"""Make little black block
"""
img = Image.open(filename)
pixdata = img.load()
for y in xrange(img.size[1]):
pixdata[0, y] = (255, 255, 255)
pixdata[img.size[0]-1,y] = (255, 255, 255)
pixdata[img.size[0]-2,y] = (255, 255, 255)
pixdata[img.size[0]-3,y] = (255, 255, 255)
pixdata[img.size[0]-4,y] = (255, 255, 255)
pixdata[img.size[0]-5,y] = (255, 255, 255)
for x in xrange(img.size[0]):
pixdata[x, 0] = (255, 255, 255)
pixdata[x, 1] = (255, 255, 255)
pixdata[x, 2] = (255, 255, 255)
pixdata[x, img.size[1]-1] = (255, 255, 255)
pixdata[x, img.size[1]-2] = (255, 255, 255)
pixdata[x, img.size[1]-3] = (255, 255, 255)
box = (1, 2, 10, 16)
region = img.crop(box)
region.save("CROPPED1" + filename)
box = (10, 2, 20, 16)
region = img.crop(box)
region.save("CROPPED2" + filename)
box = (20, 2, 28, 16)
region = img.crop(box)
region.save("CROPPED3" + filename)
box = (30, 2, 39, 16)
region = img.crop(box)
region.save("CROPPED4" + filename)
def threshold(filename, limit=50):
"""Make text more clear by thresholding all pixels above / below this limit to white / black
"""
# read in colour channels
img = Image.open(filename)
# resize to make more clearer
m = 1
img = img.resize((int(img.size[0]*m), int(img.size[1]*m))).convert('RGBA')
pixdata = img.load()
for y in xrange(img.size[1]):
for x in xrange(img.size[0]):
if pixdata[x, y][0] < limit:
# make dark color black
pixdata[x, y] = (0, 0, 0, 255)
else:
# make light color white
pixdata[x, y] = (255, 255, 255, 255)
img.save('threshold_' + filename)
return img.convert('L') # convert image to single channel greyscale
def call_command(*args):
"""call given command arguments, raise exception if error, and return output
"""
c = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = c.communicate()
if c.returncode != 0:
if error:
print error
print "Error running `%s'" % ' '.join(args)
return output
def tesseract(image, args):
"""Decode image with Tesseract
"""
# perform OCR
output_filename = image + ".txt"
#call_command('tesseract', input_file, output_filename, '-psm 10 digits')
os.system('tesseract ' + image + ' ' + output_filename + args )
# read in result from output file
result = open(output_filename+".txt").read()
os.remove(output_filename+".txt")
os.remove(input_file)
return clean(result)
def clean(s):
"""Standardize the OCR output
"""
# remove non-alpha numeric text
return re.sub('[\W]', '', s)
def getstr(filename):
erase_block(filename)
#threshold("erase_" + filename)
return tesseract("erase_" + filename, " -psm 6 digits")
if __name__ == '__main__':
filename = sys.argv[1]
print getstr(filename)
"""
split_block(filename)
filenames = "CROPPED"
if filenames:
for i in range(1,5):
img = threshold(filenames + str(i) +filename)
print 'Tesseract:', tesseract("threshold_CROPPED" + str(i) + filename," -psm 10 digits")
else:
print 'Usage: %s [image1] [image2] ...' % sys.argv[0]
"""