Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
from tensorflow.keras.models import load_model
|
| 5 |
+
import numpy as np
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import cv2
|
| 8 |
+
|
| 9 |
+
# Load the trained OCR model
|
| 10 |
+
model = load_model("path/to/your_model.h5")
|
| 11 |
+
|
| 12 |
+
# Define a function to preprocess the image
|
| 13 |
+
def preprocess_image(image):
|
| 14 |
+
image = np.array(image.convert('L')) # Convert to grayscale
|
| 15 |
+
image = cv2.resize(image, (width, height)) # Resize to expected input shape
|
| 16 |
+
image = image / 255.0 # Normalize pixel values
|
| 17 |
+
image = np.expand_dims(image, axis=-1) # Add channel dimension if necessary
|
| 18 |
+
image = np.expand_dims(image, axis=0) # Add batch dimension
|
| 19 |
+
return image
|
| 20 |
+
|
| 21 |
+
# Define the prediction function
|
| 22 |
+
def predict(image):
|
| 23 |
+
preprocessed_image = preprocess_image(image)
|
| 24 |
+
prediction = model.predict(preprocessed_image)
|
| 25 |
+
predicted_text = decode_prediction(prediction) # Implement your decoding function here
|
| 26 |
+
return predicted_text
|
| 27 |
+
|
| 28 |
+
# Create a Gradio interface
|
| 29 |
+
interface = gr.Interface(
|
| 30 |
+
fn=predict,
|
| 31 |
+
inputs=gr.inputs.Image(type="pil"),
|
| 32 |
+
outputs="text",
|
| 33 |
+
title="Captcha OCR",
|
| 34 |
+
description="An OCR model to read captcha text from images."
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# Launch the app
|
| 38 |
+
if __name__ == "__main__":
|
| 39 |
+
interface.launch()
|