|
import gradio as gr |
|
import tensorflow as tf |
|
from tensorflow.keras.models import load_model |
|
import numpy as np |
|
from PIL import Image |
|
import cv2 |
|
|
|
|
|
model = load_model("model_ocr.h5") |
|
|
|
|
|
def preprocess_image(image): |
|
image = np.array(image.convert('L')) |
|
image = cv2.resize(image, (width, height)) |
|
image = image / 255.0 |
|
image = np.expand_dims(image, axis=-1) |
|
image = np.expand_dims(image, axis=0) |
|
return image |
|
|
|
|
|
def predict(image): |
|
preprocessed_image = preprocess_image(image) |
|
prediction = model.predict(preprocessed_image) |
|
predicted_text = decode_prediction(prediction) |
|
return predicted_text |
|
|
|
|
|
interface = gr.Interface( |
|
fn=predict, |
|
inputs=gr.Image(type="pil"), |
|
outputs="text", |
|
title="Captcha OCR", |
|
description="An OCR model to read captcha text from images." |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
interface.launch() |
|
|