wangxinhe commited on
Commit
75ee810
·
verified ·
1 Parent(s): ee7d647

Create application

Browse files
Files changed (2) hide show
  1. app.py +48 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ from huggingface_hub import from_pretrained_keras
4
+ from tensorflow import keras
5
+
6
+ num_to_char = keras.layers.StringLookup(
7
+ vocabulary=sorted(
8
+ set("abcdefghijklmnpqrstuvwxyz123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".upper())
9
+ ),
10
+ mask_token=None,
11
+ invert=True,
12
+ )
13
+
14
+ model = from_pretrained_keras("wangxinhe/luogu-captcha-recognition", compile=False)
15
+
16
+ # Get the prediction model by extracting layers till the output layer
17
+ prediction_model = keras.models.Model(
18
+ model.input[0], model.get_layer(name="dense2").output
19
+ )
20
+ prediction_model.summary()
21
+
22
+
23
+ def ocr(img):
24
+ # Convert to float32 in [0, 1] range
25
+ img = tf.image.convert_image_dtype(img, tf.float32)
26
+ # Transpose the image because we want the time
27
+ # dimension to correspond to the width of the image.
28
+ img = tf.transpose(img, perm=[1, 0, 2])
29
+
30
+ preds = prediction_model(tf.expand_dims(img, axis=0))
31
+
32
+ # Use greedy search. For complex tasks, you can use beam search
33
+ results = keras.backend.ctc_decode(
34
+ preds, input_length=[preds.shape[1]], greedy=True
35
+ )[0][0][:, :4]
36
+ return tf.strings.reduce_join(num_to_char(results[0])).numpy().decode("ascii")
37
+
38
+
39
+ iface = gr.Interface(
40
+ fn=ocr,
41
+ inputs=gr.Image(
42
+ value="https://www.luogu.com.cn/api/verify/captcha",
43
+ sources=["upload", "clipboard"],
44
+ label="CAPTCHA image",
45
+ ),
46
+ outputs="textbox",
47
+ )
48
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio<5,>=4
2
+ huggingface-hub
3
+ tensorflow<3,>=2