File size: 2,881 Bytes
f6a2955
 
 
 
 
 
 
 
 
a158ece
 
f6a2955
 
a158ece
f6a2955
 
 
 
 
5dae5d6
 
 
f6a2955
 
 
0155159
f6a2955
 
 
 
 
d84fc0b
f6a2955
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1d2d4e
f6a2955
 
 
 
 
 
1969b0a
f6a2955
a940779
 
f6a2955
a940779
f6a2955
 
231bcfd
85d8257
f6a2955
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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

max_length = 55
img_width = 240
img_height = 50

model = from_pretrained_keras("napatswift/ocr-vl", compile=False)

prediction_model = keras.models.Model(
    model.get_layer(name="image").input, model.get_layer(name="dense2").output
)

vocab = [' ', '(', ')', '+', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', '=', '\xa0', 'ก', 'ข', 'ค', 'ฆ', 'ง', 'จ', 'ฉ', 'ช', 'ซ', 'ฌ', 'ญ',
         'ฎ', 'ฏ', 'ฐ', 'ฑ', 'ฒ', 'ณ', 'ด', 'ต', 'ถ', 'ท', 'ธ', 'น', 'บ', 'ป', 'ผ', 'ฝ', 'พ', 'ฟ','ภ', 'ม', 'ย', 'ร', 'ฤ', 'ล', 'ฦ', 'ว', 'ศ', 'ษ', 'ส', 'ห', 'ฬ', 'อ', 'ฮ',
         'ฯ', 'ะ', 'ั', 'า', 'ำ', 'ิ', 'ี', 'ึ', 'ื', 'ุ', 'ู', 'เ', 'แ', 'โ', 'ใ', 'ไ', 'ๅ', 'ๆ', '็', '่', '้', '๊', '๋', '์', '๐', '๑', '๒', '๓', '๔', '๕', '๖', '๗', '๘', '๙']

# Mapping integers back to original characters
num_to_char = layers.StringLookup(
    vocabulary=vocab, 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=True)[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_with_pad(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[0]
  
image = gr.Image(type='filepath')
text = gr.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>. Based on the keras example from <a href=\"https://keras.io/examples/vision/captcha_ocr/\">A_K_Nain</a>",
        examples = ["dd764.png","3p4nn.png"]
)


iface.launch()