-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse.py
67 lines (50 loc) · 1.89 KB
/
parse.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
import cv2
import pickle
import shutil
import os
import argparse
import numpy as np
from imutils import paths
from download import fetch_captcha
from crop import crop_text, crop_image
from train import TrainingState, DataLoader
import tensorflow as tf
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.compat.v1.InteractiveSession(config=config)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='parse captcha in reality')
parser.add_argument('-t', '--text_model', type=str, required=True, help='text model dir')
parser.add_argument('-i', '--image_model', type=str, required=True, help='image model dir')
parser.add_argument('-c', '--captcha', type=str, help='captcha file path')
args = vars(parser.parse_args())
if not args['captcha']:
captcha = fetch_captcha()
captcha = cv2.imdecode(np.frombuffer(captcha, np.uint8), cv2.IMREAD_COLOR)
else:
captcha = cv2.imread(args['captcha'])
# text
state = TrainingState(args['text_model'])
loader = DataLoader('text')
text = crop_text(captcha)
text = loader.preprocess(text)
model = state.load_best_model()
res = model.predict(np.array([text]))
le_path = os.path.join(args['text_model'], 'label_encoder.pkl')
le = loader.load_label_encoder(le_path)
print("text label:")
print(le.inverse_transform(res))
# image
state = TrainingState(args['image_model'])
loader = DataLoader('image')
images = []
for i in crop_image(captcha):
images.append(loader.preprocess(i))
model = state.load_best_model()
res = model.predict(np.array(images))
le_path = os.path.join(args['image_model'], 'label_encoder.pkl')
le = loader.load_label_encoder(le_path)
print("image labels:")
print(le.inverse_transform(res))
cv2.imshow('press any key to escape', captcha)
cv2.waitKey(0)