|
import streamlit as st |
|
from PIL import Image |
|
import tensorflow as tf |
|
import numpy as np |
|
from keras.preprocessing.image import img_to_array |
|
from tensorflow.keras.models import load_model |
|
import os |
|
|
|
|
|
class CTCLayer(tf.keras.layers.Layer): |
|
def __init__(self, name=None): |
|
super().__init__(name=name) |
|
self.loss_fn = tf.keras.backend.ctc_batch_cost |
|
|
|
def call(self, y_true, y_pred, input_length, label_length): |
|
|
|
|
|
loss = self.loss_fn(y_true, y_pred, input_length, label_length) |
|
self.add_loss(loss) |
|
|
|
|
|
return loss |
|
|
|
|
|
@st.cache_resource |
|
def load_model(): |
|
model_path = "model_ocr.h5" |
|
model = tf.keras.models.load_model(model_path, custom_objects={"CTCLayer": CTCLayer}) |
|
return model |
|
|
|
model = load_model() |
|
|
|
|
|
|
|
img_width, img_height = 200, 50 |
|
|
|
|
|
max_length = 50 |
|
|
|
|
|
characters = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
|
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', |
|
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', |
|
'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', |
|
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', |
|
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] |
|
|
|
|
|
|
|
def prepare_image(img): |
|
|
|
img = img.resize((img_width, img_height)) |
|
|
|
img_array = img_to_array(img) / 255.0 |
|
|
|
img_array = np.expand_dims(img_array, axis=0) |
|
img_array = np.transpose(img_array, (0, 2, 1, 3)) |
|
return img_array |
|
|
|
|
|
def decode_batch_predictions(pred): |
|
pred_texts = [] |
|
|
|
|
|
for i in range(pred.shape[0]): |
|
|
|
pred_indices = np.argmax(pred[i], axis=-1) |
|
|
|
|
|
pred_text = ''.join([characters[int(c)] for c in pred_indices if c not in [-1, 0]]) |
|
|
|
|
|
pred_texts.append(pred_text) |
|
|
|
return pred_texts |
|
|
|
|
|
def run(): |
|
st.title("OCR Model Deployment") |
|
|
|
|
|
img_file = st.file_uploader("Choose an Image", type=["jpg", "png"]) |
|
|
|
if img_file is not None: |
|
img = Image.open(img_file).convert('L') |
|
st.image(img, use_column_width=True) |
|
|
|
|
|
upload_dir = './upload_images/' |
|
os.makedirs(upload_dir, exist_ok=True) |
|
save_image_path = os.path.join(upload_dir, img_file.name) |
|
with open(save_image_path, "wb") as f: |
|
f.write(img_file.getbuffer()) |
|
|
|
|
|
pred_texts = prepare_image(img) |
|
|
|
|
|
st.success(f"**Predicted Text: {pred_texts[0]}**") |
|
|
|
if __name__ == "__main__": |
|
run() |
|
|