IM-417-classifier / app-working.py
storesource's picture
Rename app.py to app-working.py
fa97210 verified
import gradio as gr
import tensorflow as tf
from PIL import Image
import numpy as np
import os
import cv2
model_path = "IM_417_128.keras"
print("Loading Model...")
model = tf.keras.models.load_model(model_path)
model.summary()
labelled_images = [f"labelled/{i}.png" for i in range(1, 418)]
# Define regression function
def predict_regression(image):
# Preprocess image
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
image = image.resize((128, 128)).convert('L') #resize the image to 128x128 and converts it to gray scale
image = np.array(image)
# Predict
predictions = model.predict(image[None, ...]) # Assuming single regression value
predicted_output = np.argmax(predictions, axis=1)
predicted_label = f"labelled/{str(predicted_output[0])}.png"
print(f"prediction IM-417 character :: {predicted_label} :: source {predicted_output}")
output_data = cv2.imread(predicted_label, cv2.COLOR_BGR2GRAY)
return cv2.resize(output_data, (128, 128))
# Create Gradio interface
input_image = gr.Image()
output_image = gr.Image(height=150, width=150)
interface = gr.Interface(fn=predict_regression,
inputs=input_image,
outputs=output_image,
examples=labelled_images,
description="A simple mlp classification model for image classification using the sign list from 'The Indus Scripts: Texts, Concordance and Tables'"
)
interface.launch()