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