|
import gradio as gr |
|
import tensorflow as tf |
|
from tensorflow.keras.models import load_model |
|
import numpy as np |
|
import cv2 |
|
|
|
|
|
model_path = './model_checkpoint_manual_resnet.h5' |
|
model = load_model(model_path) |
|
|
|
class_names = ['ADONIS', 'AFRICAN GIANT SWALLOWTAIL', 'AMERICAN SNOOT', 'AN 88', 'APPOLLO', 'ARCIGERA FLOWER MOTH', 'ATALA', 'ATLAS MOTH', 'BANDED ORANGE HELICONIAN', 'BANDED PEACOCK'] |
|
|
|
|
|
def preprocess_image(img): |
|
|
|
if isinstance(img, str): |
|
|
|
img = cv2.imread(img) |
|
img = cv2.resize(img, (224, 224)) |
|
img = img / 255.0 |
|
img = np.expand_dims(img, axis=0) |
|
elif isinstance(img, np.ndarray): |
|
|
|
img = cv2.resize(img, (224, 224)) |
|
img = img / 255.0 |
|
img = np.expand_dims(img, axis=0) |
|
else: |
|
raise ValueError("Unsupported input type. Please provide a file path or a NumPy array.") |
|
|
|
return img |
|
|
|
|
|
def classify_image(img): |
|
|
|
img = preprocess_image(img) |
|
|
|
|
|
predictions = model.predict(img) |
|
|
|
|
|
predicted_class = np.argmax(predictions) |
|
|
|
|
|
predicted_class_name = class_names[predicted_class] |
|
|
|
return f"Predicted Class: {predicted_class_name}" |
|
|
|
|
|
iface = gr.Interface(fn=classify_image, |
|
inputs="image", |
|
outputs="text", |
|
live=True) |
|
|
|
|
|
iface.launch() |