gowthambhat commited on
Commit
55f9a6c
β€’
1 Parent(s): cd59874

Upload 14 files

Browse files
Files changed (14) hide show
  1. 268g2.png +0 -0
  2. 36nx4.png +0 -0
  3. 3bnyf.png +0 -0
  4. 3p4nn.png +0 -0
  5. 5p8fm.png +0 -0
  6. 8y6b3.png +0 -0
  7. README.md +6 -6
  8. app.py +69 -0
  9. dd764.png +0 -0
  10. mnef5.png +0 -0
  11. requirements.txt +1 -0
  12. vocab.txt +20 -0
  13. ydd3g.png +0 -0
  14. yxd7m.png +0 -0
268g2.png ADDED
36nx4.png ADDED
3bnyf.png ADDED
3p4nn.png ADDED
5p8fm.png ADDED
8y6b3.png ADDED
README.md CHANGED
@@ -1,12 +1,12 @@
1
  ---
2
- title: Ocr Cgip
3
- emoji: πŸ’¬
4
  colorFrom: yellow
5
- colorTo: purple
6
- sdk: gradio
7
- sdk_version: 4.36.1
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
 
1
  ---
2
+ title: Optical Char
3
+ emoji: πŸš€
4
  colorFrom: yellow
5
+ colorTo: yellow
6
+ sdk: streamlit
7
+ sdk_version: 1.36.0
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
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
+
dd764.png ADDED
mnef5.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
ydd3g.png ADDED
yxd7m.png ADDED