Spaces:
Sleeping
Sleeping
File size: 1,576 Bytes
15640ed 1eff51b 15640ed 1eff51b 15640ed 1eff51b 15640ed 75cb6c9 146d122 75cb6c9 1eff51b 15640ed 1eff51b 15640ed 1eff51b 15640ed d266e0b 15640ed |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import gradio as gr
import tensorflow as tf
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
from PIL import Image
import numpy as np
# Load pre-trained ResNet50 model + higher level layers
model = ResNet50(weights='imagenet')
#chameleon = load_img("example_1.jpeg", output_type="pil")
def classify_image(img):
# Resize the image to 224x224 pixels (required input size for ResNet50)
img = img.resize((224, 224))
# Convert the image to array
img_array = np.array(img)
# Expand dimensions to match the shape required by the model
img_array = np.expand_dims(img_array, axis=0)
# Preprocess the image
img_array = preprocess_input(img_array)
# Predict the classification
predictions = model.predict(img_array)
# Decode predictions into readable labels
decoded_predictions = decode_predictions(predictions, top=3)[0]
# Format the output
return {label: float(probability) for (_, label, probability) in decoded_predictions}
with gr.Blocks(theme="Hev832/Applio") as iface:
gr.Interface(
fn=classify_image, # Function to call for predictions
inputs=gr.Image(type="pil"), # Input is an image
outputs=gr.Label(num_top_classes=3), # Output is a label with top 3 predictions
title="Contextual Image Classification",
description="Upload an image, and the model will classify it based on the context.",
#examples=[chameleon],
)
# Launch the interface
iface.launch()
|