diyanqi anuragshas commited on
Commit
12a5888
·
0 Parent(s):

Duplicate from keras-io/ocr-for-captcha

Browse files

Co-authored-by: Anurag Singh <[email protected]>

Files changed (7) hide show
  1. .gitattributes +27 -0
  2. 3p4nn.png +0 -0
  3. README.md +46 -0
  4. app.py +70 -0
  5. dd764.png +0 -0
  6. requirements.txt +1 -0
  7. vocab.txt +20 -0
.gitattributes ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bin.* filter=lfs diff=lfs merge=lfs -text
5
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.model filter=lfs diff=lfs merge=lfs -text
12
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
13
+ *.onnx filter=lfs diff=lfs merge=lfs -text
14
+ *.ot filter=lfs diff=lfs merge=lfs -text
15
+ *.parquet filter=lfs diff=lfs merge=lfs -text
16
+ *.pb filter=lfs diff=lfs merge=lfs -text
17
+ *.pt filter=lfs diff=lfs merge=lfs -text
18
+ *.pth filter=lfs diff=lfs merge=lfs -text
19
+ *.rar filter=lfs diff=lfs merge=lfs -text
20
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
21
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
22
+ *.tflite filter=lfs diff=lfs merge=lfs -text
23
+ *.tgz filter=lfs diff=lfs merge=lfs -text
24
+ *.xz filter=lfs diff=lfs merge=lfs -text
25
+ *.zip filter=lfs diff=lfs merge=lfs -text
26
+ *.zstandard filter=lfs diff=lfs merge=lfs -text
27
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
3p4nn.png ADDED
README.md ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: OCR For Captcha
3
+ emoji: 🤖
4
+ colorFrom: yellow
5
+ colorTo: red
6
+ sdk: gradio
7
+ app_file: app.py
8
+ pinned: false
9
+ duplicated_from: keras-io/ocr-for-captcha
10
+ ---
11
+
12
+ # Configuration
13
+
14
+ `title`: _string_
15
+ Display title for the Space
16
+
17
+ `emoji`: _string_
18
+ Space emoji (emoji-only character allowed)
19
+
20
+ `colorFrom`: _string_
21
+ Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
22
+
23
+ `colorTo`: _string_
24
+ Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
25
+
26
+ `sdk`: _string_
27
+ Can be either `gradio`, `streamlit`, or `static`
28
+
29
+ `sdk_version` : _string_
30
+ Only applicable for `streamlit` SDK.
31
+ See [doc](https://hf.co/docs/hub/spaces) for more info on supported versions.
32
+
33
+ `app_file`: _string_
34
+ Path to your main application file (which contains either `gradio` or `streamlit` Python code, or `static` html code).
35
+ Path is relative to the root of the repository.
36
+
37
+ `models`: _List[string]_
38
+ HF model IDs (like "gpt2" or "deepset/roberta-base-squad2") used in the Space.
39
+ Will be parsed automatically from your code if not specified here.
40
+
41
+ `datasets`: _List[string]_
42
+ HF dataset IDs (like "common_voice" or "oscar-corpus/OSCAR-2109") used in the Space.
43
+ Will be parsed automatically from your code if not specified here.
44
+
45
+ `pinned`: _boolean_
46
+ Whether the Space stays on top of your list.
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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="OCR for CAPTCHA",
63
+ description = "Keras Implementation of OCR model for reading captcha 🤖🦹🏻",
64
+ 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>",
65
+ examples = ["dd764.png","3p4nn.png"]
66
+ )
67
+
68
+
69
+ iface.launch()
70
+
dd764.png ADDED
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ tensorflow>2.6
vocab.txt ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [UNK]
2
+ 8
3
+ 6
4
+ m
5
+ x
6
+ d
7
+ y
8
+ w
9
+ 2
10
+ 7
11
+ n
12
+ g
13
+ 5
14
+ c
15
+ f
16
+ p
17
+ e
18
+ 3
19
+ 4
20
+ b