ocr-for-captcha / app.py
anuragshas's picture
Create app.py
f6a2955
raw
history blame
2.65 kB
# -*- coding: utf-8 -*-
"""ocrforcaptcha.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/161aX_CGT4Q3zAMkLeLSOZVDhMyMtsqPx
"""
# Commented out IPython magic to ensure Python compatibility.
# %%capture
# !pip install gradio
# !pip install huggingface-hub
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from huggingface_hub import from_pretrained_keras
import numpy as np
import gradio as gr
characters = {'d', 'w', 'y', '4', 'f', '6', 'g', 'e', '3', '5', 'p', 'x', '2', 'c', '7', 'n', 'b', '8', 'm'}
max_length = 5
img_width = 200
img_height = 50
model = from_pretrained_keras("keras-io/ocr-for-captcha")
prediction_model = keras.models.Model(
model.get_layer(name="image").input, model.get_layer(name="dense2").output
)
# Mapping characters to integers
char_to_num = layers.StringLookup(
vocabulary=list(characters), mask_token=None
)
# Mapping integers back to original characters
num_to_char = layers.StringLookup(
vocabulary=char_to_num.get_vocabulary(), mask_token=None, invert=True
)
def decode_batch_predictions(pred):
input_len = np.ones(pred.shape[0]) * pred.shape[1]
# Use greedy search. For complex tasks, you can use beam search
results = keras.backend.ctc_decode(pred, input_length=input_len, greedy=False)[0][0][
:, :max_length
]
# Iterate over the results and get back the text
output_text = []
for res in results:
res = tf.strings.reduce_join(num_to_char(res)).numpy().decode("utf-8")
output_text.append(res)
return output_text
def classify_image(img_path):
# 1. Read image
img = tf.io.read_file(img_path)
# 2. Decode and convert to grayscale
img = tf.io.decode_png(img, channels=1)
# 3. Convert to float32 in [0, 1] range
img = tf.image.convert_image_dtype(img, tf.float32)
# 4. Resize to the desired size
img = tf.image.resize(img, [img_height, img_width])
# 5. Transpose the image because we want the time
# dimension to correspond to the width of the image.
img = tf.transpose(img, perm=[1, 0, 2])
img = tf.expand_dims(img, axis=0)
preds = prediction_model.predict(img)
pred_text = decode_batch_predictions(preds)
return pred_text
image = gr.inputs.Image(type='filepath')
text = gr.outputs.Textbox()
iface = gr.Interface(classify_image,image,text,
title="OCR for CAPTCHA",
description = "Keras Implementation of OCR model for reading captcha 🤖🦹🏻",
article = "Author: <a href=\"https://huggingface.co/anuragshas\">Anurag Singh</a>"
)
iface.launch()