Update app.py
Browse files
app.py
CHANGED
|
@@ -43,7 +43,7 @@ def build_model():
|
|
| 43 |
|
| 44 |
model = build_model()
|
| 45 |
|
| 46 |
-
# If you trained your model:
|
| 47 |
# model.load_weights("model.h5")
|
| 48 |
|
| 49 |
|
|
@@ -64,12 +64,16 @@ def preprocess_image(image):
|
|
| 64 |
# -------------------
|
| 65 |
def decode_predictions(pred):
|
| 66 |
input_len = np.ones(pred.shape[0]) * pred.shape[1]
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
output_text = ""
|
| 70 |
for res in results.numpy()[0]:
|
| 71 |
if res != -1:
|
| 72 |
output_text += CHARACTERS[res]
|
|
|
|
| 73 |
return output_text
|
| 74 |
|
| 75 |
|
|
@@ -83,15 +87,33 @@ def predict(image):
|
|
| 83 |
return text
|
| 84 |
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
# -------------------
|
| 87 |
# Gradio Interface
|
| 88 |
# -------------------
|
| 89 |
interface = gr.Interface(
|
| 90 |
-
fn=
|
| 91 |
-
inputs=
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
| 95 |
)
|
| 96 |
|
| 97 |
interface.launch()
|
|
|
|
| 43 |
|
| 44 |
model = build_model()
|
| 45 |
|
| 46 |
+
# If you trained your model, uncomment:
|
| 47 |
# model.load_weights("model.h5")
|
| 48 |
|
| 49 |
|
|
|
|
| 64 |
# -------------------
|
| 65 |
def decode_predictions(pred):
|
| 66 |
input_len = np.ones(pred.shape[0]) * pred.shape[1]
|
| 67 |
+
|
| 68 |
+
results = tf.keras.backend.ctc_decode(
|
| 69 |
+
pred, input_length=input_len, greedy=True
|
| 70 |
+
)[0][0]
|
| 71 |
|
| 72 |
output_text = ""
|
| 73 |
for res in results.numpy()[0]:
|
| 74 |
if res != -1:
|
| 75 |
output_text += CHARACTERS[res]
|
| 76 |
+
|
| 77 |
return output_text
|
| 78 |
|
| 79 |
|
|
|
|
| 87 |
return text
|
| 88 |
|
| 89 |
|
| 90 |
+
# -------------------
|
| 91 |
+
# Combined Handler
|
| 92 |
+
# -------------------
|
| 93 |
+
def handle_input(image, typed_text):
|
| 94 |
+
# If user typed text → return it directly
|
| 95 |
+
if typed_text is not None and typed_text.strip() != "":
|
| 96 |
+
return typed_text
|
| 97 |
+
|
| 98 |
+
# If image uploaded → run prediction
|
| 99 |
+
if image is not None:
|
| 100 |
+
return predict(image)
|
| 101 |
+
|
| 102 |
+
return "Please upload an image or enter text."
|
| 103 |
+
|
| 104 |
+
|
| 105 |
# -------------------
|
| 106 |
# Gradio Interface
|
| 107 |
# -------------------
|
| 108 |
interface = gr.Interface(
|
| 109 |
+
fn=handle_input,
|
| 110 |
+
inputs=[
|
| 111 |
+
gr.Image(type="numpy", label="Upload Handwritten Image"),
|
| 112 |
+
gr.Textbox(label="Or Type Text Manually")
|
| 113 |
+
],
|
| 114 |
+
outputs=gr.Textbox(label="Output Text"),
|
| 115 |
+
title="✍️ Handwritten Line Text Recognition",
|
| 116 |
+
description="Upload a handwritten line image OR type text manually"
|
| 117 |
)
|
| 118 |
|
| 119 |
interface.launch()
|