|
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_effnet.h5' |
|
model = load_model(model_path) |
|
|
|
|
|
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) |
|
|
|
return f"Predicted Class: {predicted_class}" |
|
|
|
|
|
iface = gr.Interface(fn=classify_image, |
|
inputs="image", |
|
outputs="text", |
|
live=True) |
|
|
|
|
|
iface.launch() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|