anuragshas commited on
Commit
f6a2955
1 Parent(s): 27b61f7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """ocrforcaptcha.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/161aX_CGT4Q3zAMkLeLSOZVDhMyMtsqPx
8
+ """
9
+
10
+ # Commented out IPython magic to ensure Python compatibility.
11
+ # %%capture
12
+ # !pip install gradio
13
+ # !pip install huggingface-hub
14
+
15
+ import tensorflow as tf
16
+ from tensorflow import keras
17
+ from tensorflow.keras import layers
18
+
19
+ from huggingface_hub import from_pretrained_keras
20
+
21
+ import numpy as np
22
+ import gradio as gr
23
+
24
+ characters = {'d', 'w', 'y', '4', 'f', '6', 'g', 'e', '3', '5', 'p', 'x', '2', 'c', '7', 'n', 'b', '8', 'm'}
25
+ max_length = 5
26
+ img_width = 200
27
+ img_height = 50
28
+
29
+ model = from_pretrained_keras("keras-io/ocr-for-captcha")
30
+
31
+ prediction_model = keras.models.Model(
32
+ model.get_layer(name="image").input, model.get_layer(name="dense2").output
33
+ )
34
+
35
+ # Mapping characters to integers
36
+ char_to_num = layers.StringLookup(
37
+ vocabulary=list(characters), mask_token=None
38
+ )
39
+
40
+ # Mapping integers back to original characters
41
+ num_to_char = layers.StringLookup(
42
+ vocabulary=char_to_num.get_vocabulary(), mask_token=None, invert=True
43
+ )
44
+
45
+ def decode_batch_predictions(pred):
46
+ input_len = np.ones(pred.shape[0]) * pred.shape[1]
47
+ # Use greedy search. For complex tasks, you can use beam search
48
+ results = keras.backend.ctc_decode(pred, input_length=input_len, greedy=False)[0][0][
49
+ :, :max_length
50
+ ]
51
+ # Iterate over the results and get back the text
52
+ output_text = []
53
+ for res in results:
54
+ res = tf.strings.reduce_join(num_to_char(res)).numpy().decode("utf-8")
55
+ output_text.append(res)
56
+ return output_text
57
+
58
+ def classify_image(img_path):
59
+ # 1. Read image
60
+ img = tf.io.read_file(img_path)
61
+ # 2. Decode and convert to grayscale
62
+ img = tf.io.decode_png(img, channels=1)
63
+ # 3. Convert to float32 in [0, 1] range
64
+ img = tf.image.convert_image_dtype(img, tf.float32)
65
+ # 4. Resize to the desired size
66
+ img = tf.image.resize(img, [img_height, img_width])
67
+ # 5. Transpose the image because we want the time
68
+ # dimension to correspond to the width of the image.
69
+ img = tf.transpose(img, perm=[1, 0, 2])
70
+ img = tf.expand_dims(img, axis=0)
71
+ preds = prediction_model.predict(img)
72
+ pred_text = decode_batch_predictions(preds)
73
+ return pred_text
74
+
75
+ image = gr.inputs.Image(type='filepath')
76
+ text = gr.outputs.Textbox()
77
+
78
+ iface = gr.Interface(classify_image,image,text,
79
+ title="OCR for CAPTCHA",
80
+ description = "Keras Implementation of OCR model for reading captcha 🤖🦹🏻",
81
+ article = "Author: <a href=\"https://huggingface.co/anuragshas\">Anurag Singh</a>"
82
+ )
83
+
84
+
85
+ iface.launch()
86
+