gridflowai's picture
Upload 4 files
e242a23
import gradio as gr
import joblib
import numpy as np
from PIL import Image
from sklearn.preprocessing import StandardScaler
# Load the trained SVM model and scaler
clf = joblib.load('svm_model.pkl')
scaler = joblib.load('scaler.pkl')
# Define a function to preprocess and classify the image
def classify_image(input_image):
# Check if the input is a NumPy array
if isinstance(input_image, np.ndarray):
# Preprocess the input image
image = Image.fromarray(input_image)
image = image.resize((64, 64))
image = np.array(image)
image = image / 255.0 # Normalize the pixel values
flattened_image = image.flatten()
# Scale the image using the same scaler used during training
scaled_image = scaler.transform([flattened_image])
# Make a prediction using the SVM model
prediction = clf.predict(scaled_image)
# Interpret the prediction
if prediction[0] == 1:
label = "Cat"
else:
label = "Dog"
return label
else:
return "Invalid input. Please provide a valid image."
# Create a Gradio interface
iface = gr.Interface(fn=classify_image,
inputs="image",
outputs="text",
live=True,
capture_session=True)
# Launch the Gradio interface
iface.launch(server_name="0.0.0.0", server_port=7860)