Spaces:
Runtime error
Runtime error
gowthambhat
commited on
Commit
•
3a64784
1
Parent(s):
1693af9
Delete app.py
Browse files
app.py
DELETED
@@ -1,66 +0,0 @@
|
|
1 |
-
import tensorflow as tf
|
2 |
-
from tensorflow import keras
|
3 |
-
from tensorflow.keras import layers
|
4 |
-
from huggingface_hub import snapshot_download
|
5 |
-
|
6 |
-
import numpy as np
|
7 |
-
import gradio as gr
|
8 |
-
|
9 |
-
max_length = 5
|
10 |
-
img_width = 200
|
11 |
-
img_height = 50
|
12 |
-
|
13 |
-
# Download the model from Hugging Face
|
14 |
-
model_path = snapshot_download(repo_id="keras-io/ocr-for-captcha")
|
15 |
-
|
16 |
-
# Load the TensorFlow SavedModel using TFSMLayer
|
17 |
-
prediction_layer = keras.layers.TFSMLayer(model_path, call_endpoint='serving_default')
|
18 |
-
|
19 |
-
with open("vocab.txt", "r") as f:
|
20 |
-
vocab = f.read().splitlines()
|
21 |
-
|
22 |
-
# Mapping integers back to original characters
|
23 |
-
num_to_char = layers.StringLookup(
|
24 |
-
vocabulary=vocab, mask_token=None, invert=True
|
25 |
-
)
|
26 |
-
|
27 |
-
def decode_batch_predictions(pred):
|
28 |
-
input_len = np.ones(pred.shape[0]) * pred.shape[1]
|
29 |
-
# Use greedy search. For complex tasks, you can use beam search
|
30 |
-
results = keras.backend.ctc_decode(pred, input_length=input_len, greedy=True)[0][0][
|
31 |
-
:, :max_length
|
32 |
-
]
|
33 |
-
# Iterate over the results and get back the text
|
34 |
-
output_text = []
|
35 |
-
for res in results:
|
36 |
-
res = tf.strings.reduce_join(num_to_char(res)).numpy().decode("utf-8")
|
37 |
-
output_text.append(res)
|
38 |
-
return output_text
|
39 |
-
|
40 |
-
def classify_image(img_path):
|
41 |
-
# 1. Read image
|
42 |
-
img = tf.io.read_file(img_path)
|
43 |
-
# 2. Decode and convert to grayscale
|
44 |
-
img = tf.io.decode_png(img, channels=1)
|
45 |
-
# 3. Convert to float32 in [0, 1] range
|
46 |
-
img = tf.image.convert_image_dtype(img, tf.float32)
|
47 |
-
# 4. Resize to the desired size
|
48 |
-
img = tf.image.resize(img, [img_height, img_width])
|
49 |
-
# 5. Transpose the image because we want the time
|
50 |
-
# dimension to correspond to the width of the image.
|
51 |
-
img = tf.transpose(img, perm=[1, 0, 2])
|
52 |
-
img = tf.expand_dims(img, axis=0)
|
53 |
-
preds = prediction_layer(img) # Use the TFSMLayer for prediction
|
54 |
-
pred_text = decode_batch_predictions(preds)
|
55 |
-
return pred_text[0]
|
56 |
-
|
57 |
-
image = gr.inputs.Image(type='filepath')
|
58 |
-
text = gr.outputs.Textbox()
|
59 |
-
|
60 |
-
iface = gr.Interface(classify_image, image, text,
|
61 |
-
title="CGIP CAPTCHA RECOGNITION OCR",
|
62 |
-
description="Keras Implementation of OCR model for reading captcha 🤖🦹🏻",
|
63 |
-
examples=["dd764.png", "3p4nn.png", "ydd3g.png", "268g2.png", "36nx4.png", "3bnyf.png", "5p8fm.png", "8y6b3.png", "mnef5.png", "yxd7m.png"]
|
64 |
-
)
|
65 |
-
|
66 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|