-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathrecognise.py
More file actions
108 lines (79 loc) · 3.42 KB
/
Copy pathrecognise.py
File metadata and controls
108 lines (79 loc) · 3.42 KB
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
# Code from http://blog.c22.cc/2010/10/12/python-ocr-or-how-to-break-captchas/
# and http://stackoverflow.com/questions/14640509/python-error-when-importing-image-to-string-from-tesseract
# $ tesseract input-NEAREST.tif example -psm 6
from PIL import Image, ImageFilter, ImageChops
from pytesseract import image_to_string
import cv2
import numpy
def preprocess_image_using_pil(image_path):
# unblur, sharpen filters
img = Image.open(image_path)
img = img.convert("RGBA")
pixdata = img.load()
# Make the letters bolder for easier recognition
for y in xrange(img.size[1]):
for x in xrange(img.size[0]):
if pixdata[x, y][0] < 90:
pixdata[x, y] = (0, 0, 0, 255)
for y in xrange(img.size[1]):
for x in xrange(img.size[0]):
if pixdata[x, y][1] < 136:
pixdata[x, y] = (0, 0, 0, 255)
for y in xrange(img.size[1]):
for x in xrange(img.size[0]):
if pixdata[x, y][2] > 0:
pixdata[x, y] = (255, 255, 255, 255)
# And sharpen it
img.filter(ImageFilter.SHARPEN)
img.save("input-black.gif")
# Make the image bigger (needed for OCR)
basewidth = 1000 # in pixels
im_orig = Image.open('input-black.gif')
wpercent = (basewidth/float(im_orig.size[0]))
hsize = int((float(im_orig.size[1])*float(wpercent)))
big = img.resize((basewidth, hsize), Image.ANTIALIAS)
# tesseract-ocr only works with TIF so save the bigger image in that format
ext = ".tif"
tif_file = "input-NEAREST.tif"
big.save(tif_file)
return tif_file
def get_captcha_text_from_captcha_image(captcha_path):
# Preprocess the image befor OCR
tif_file = preprocess_image_using_opencv(captcha_path)
# Perform OCR using tesseract-ocr library
image = Image.open(tif_file)
ocr_text = image_to_string(image, config="-psm 6")
alphanumeric_text = ''.join(e for e in ocr_text)
return alphanumeric_text
def binarize_image_using_pil(captcha_path, binary_image_path='input-black-n-white.gif'):
im = Image.open(captcha_path).convert('L')
for i in range(im.size[0]):
for j in range(im.size[1]):
if im.getpixel((i,j)) > 127:
im.putpixel((i,j), 255)
else:
im.putpixel((i,j), 0)
im.save(binary_image_path)
return binary_image_path
def binarize_image_using_opencv(captcha_path, binary_image_path='input-black-n-white.jpg'):
img = cv2.imread(captcha_path)
im_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
(thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
# although thresh is used below, gonna pick something suitable
im_bw = cv2.threshold(im_gray, thresh, 255, cv2.THRESH_BINARY)[1]
cv2.imwrite(binary_image_path, im_bw)
return binary_image_path
def preprocess_image_using_opencv(captcha_path):
bin_image_path = binarize_image_using_opencv(captcha_path)
im_bin = Image.open(bin_image_path)
basewidth = 340 # in pixels
wpercent = (basewidth/float(im_bin.size[0]))
hsize = int((float(im_bin.size[1])*float(wpercent)))
big = im_bin.resize((basewidth, hsize), Image.NEAREST)
# tesseract-ocr only works with TIF so save the bigger image in that format
ext = ".tif"
tif_file = "input-NEAREST.tif"
big.save(tif_file)
return tif_file
if __name__ == "__main__":
print get_captcha_text_from_captcha_image("test.jpg")