perc1val commited on
Commit
f07b0a9
1 Parent(s): 237d884

Upload 7 files

Browse files
Files changed (7) hide show
  1. 3p4nn.png +0 -0
  2. README.md +45 -0
  3. app.py +69 -0
  4. dd764.png +0 -0
  5. gitattributes.txt +27 -0
  6. requirements.txt +1 -0
  7. vocab.txt +20 -0
3p4nn.png ADDED
README.md ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ ---
10
+
11
+ # Configuration
12
+
13
+ `title`: _string_
14
+ Display title for the Space
15
+
16
+ `emoji`: _string_
17
+ Space emoji (emoji-only character allowed)
18
+
19
+ `colorFrom`: _string_
20
+ Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
21
+
22
+ `colorTo`: _string_
23
+ Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
24
+
25
+ `sdk`: _string_
26
+ Can be either `gradio`, `streamlit`, or `static`
27
+
28
+ `sdk_version` : _string_
29
+ Only applicable for `streamlit` SDK.
30
+ See [doc](https://hf.co/docs/hub/spaces) for more info on supported versions.
31
+
32
+ `app_file`: _string_
33
+ Path to your main application file (which contains either `gradio` or `streamlit` Python code, or `static` html code).
34
+ Path is relative to the root of the repository.
35
+
36
+ `models`: _List[string]_
37
+ HF model IDs (like "gpt2" or "deepset/roberta-base-squad2") used in the Space.
38
+ Will be parsed automatically from your code if not specified here.
39
+
40
+ `datasets`: _List[string]_
41
+ HF dataset IDs (like "common_voice" or "oscar-corpus/OSCAR-2109") used in the Space.
42
+ Will be parsed automatically from your code if not specified here.
43
+
44
+ `pinned`: _boolean_
45
+ Whether the Space stays on top of your list.
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
+ # ánh xạ các số nguyên trở lại thành các ký tự gốc
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
+ # Sử dụng thuật toán Greedy Search
31
+ results = keras.backend.ctc_decode(pred, input_length=input_len, greedy=True)[0][0][
32
+ :, :max_length
33
+ ]
34
+ # lặp qua các kết quả và nhận lại 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
+ # đọc ảnh
43
+ img = tf.io.read_file(img_path)
44
+ # giải mã và chuyển đổi ảnh thành ảnh xám
45
+ img = tf.io.decode_png(img, channels=1)
46
+ # chuyển đổi ảnh sang định dạng float32 trong khoảng [0, 1]
47
+ img = tf.image.convert_image_dtype(img, tf.float32)
48
+ # thay đổi kích thước của ảnh thành kích thước mong muốn
49
+ img = tf.image.resize(img, [img_height, img_width])
50
+ # chuyển vị ảnh sao cho chiều thời gian tương ứng với chiều rộng của ảnh
51
+ img = tf.transpose(img, perm=[1, 0, 2])
52
+ img = tf.expand_dims(img, axis=0)
53
+ preds = prediction_model.predict(img)
54
+ pred_text = decode_batch_predictions(preds)
55
+ return pred_text[0]
56
+
57
+ image = gr.inputs.Image(type='filepath')
58
+ text = gr.outputs.Textbox()
59
+
60
+ iface = gr.Interface(classify_image,image,text,
61
+ title="Giải Captcha",
62
+ description = "mô hình OCR sử dụng Keras để đọc Captcha 🤖🦹🏻",
63
+ examples = ["dd764.png","3p4nn.png"]
64
+ )
65
+
66
+
67
+ iface.launch()
68
+
69
+
dd764.png ADDED
gitattributes.txt 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
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