Spaces:
Running
Running
# Define function to predict captcha | |
import cv2 | |
import numpy as np | |
import string | |
import gradio as gr | |
import tensorflow as tf | |
import os | |
model = tf.keras.models.load_model("captcha2.h5") | |
symbols = string.ascii_lowercase + "0123456789" | |
def predict(img): | |
# Resize image | |
img = cv2.resize(img, (200, 50), interpolation=cv2.INTER_AREA) | |
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
# Normalize image | |
img = img / 255.0 | |
# Pass image to model | |
res = np.array(model.predict(img[np.newaxis, :, :, np.newaxis])) | |
# Decode prediction | |
ans = np.reshape(res, (5, 36)) | |
l_ind = [] | |
probs = [] | |
for a in ans: | |
l_ind.append(np.argmax(a)) | |
capt = '' | |
for l in l_ind: | |
capt += symbols[l] | |
return capt | |
def captcha(img): | |
prediction = predict(img) | |
return prediction | |
gr.Interface(fn=captcha, inputs="image", outputs="text", | |
title="Enter Alphanumeric Captcha Image" , examples=[ | |
os.path.join(os.path.dirname(__file__), "./2cegf.png"), | |
os.path.join(os.path.dirname(__file__), "./yf424.png"), | |
os.path.join(os.path.dirname(__file__), "./yxd7m.png") | |
]).launch() | |