-
Notifications
You must be signed in to change notification settings - Fork 1
/
predict.py
50 lines (33 loc) · 1.16 KB
/
predict.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
import tensorflow as tf
from tensorflow.keras.preprocessing.image import img_to_array, load_img
import Preprocess
import matplotlib.pyplot as plt
import os
import shutil
import numpy as np
import cv2
from PIL import Image
# Load data
def predict_captcha(filepath):
Preprocess.extract_digits(filepath,"temp_captcha_digit_folder")
model = tf.keras.models.load_model("model/captcha-CNN-2")
input_filepaths=[]
for i in range(1,7):
input_filepaths.append("temp_captcha_digit_folder/"+str(i)+".jpg")
res=get_prediction(model,input_filepaths)
shutil.rmtree("temp_captcha_digit_folder")
return ''.join(map(str, res))
def get_prediction(model,filepath_list):
img_size = (23, 15) # fixed size for resizing the images
X = []
for filepath in filepath_list:
img = load_img(filepath, target_size=img_size, color_mode="grayscale")
img_arr = img_to_array(img)
X.append(img_arr)
X = np.array(X)
# Preprocess the data
X = X / 255.0 # normalize pixel values
y_pred = model(X)
y_pred = np.argmax(y_pred, axis=1)
return y_pred
# print(predict_captcha("captchas/13-307896.jpg")) #example usage