-
Notifications
You must be signed in to change notification settings - Fork 0
/
captcha_resolver.py
75 lines (53 loc) · 2.32 KB
/
captcha_resolver.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
import os
import pickle
import cv2
import numpy as np
from imutils import paths
from tensorflow.keras.models import load_model
from captcha_treatment import treat_images
from helpers import resize_to_fit
def break_captcha():
# Import trained model and import translator
with open('labels_model.dat', 'rb') as translator_file:
lb = pickle.load(translator_file)
model = load_model('model_trained.hdf5')
# Use the model to resolve captchas
treat_images('resolve', destiny_folder="resolve")
############################################################
files = list(paths.list_images('resolve'))
for file in files:
image = cv2.imread(file)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Convert image to binary
_, binary_image = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV)
# Find contours of each letter
contours, _ = cv2.findContours(
binary_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
letters_area = []
# Filter contours that are letters
for contour in contours:
(x, y, width, height) = cv2.boundingRect(contour)
area = cv2.contourArea(contour)
if area > 115:
letters_area.append((x, y, width, height))
# Order letters by axis x
letters_area = sorted(letters_area, key=lambda x: x[0])
# Draw countours and separate the letters in individual files
final_image = cv2.merge([image] * 3)
prediction = []
for rectangle in letters_area:
x, y, width, height = rectangle
letter_image = image[y-2:y+height+2, x-2:x+width+2]
# Resize letter to 20x20
letter_image = resize_to_fit(letter_image, 20, 20)
# Treatment to Keras recognize image
letter_image = np.expand_dims(letter_image, axis=2)
letter_image = np.expand_dims(letter_image, axis=0)
# Pass the letter to IA identify
predicted_letter = model.predict(letter_image)
predicted_letter = lb.inverse_transform(predicted_letter)[0]
prediction.append(predicted_letter)
prediction_text = ''.join(prediction)
print(prediction_text)
return prediction_text
############################################################