Spaces:
Sleeping
Sleeping
File size: 1,323 Bytes
5253dd8 ad2d334 5253dd8 ad2d334 a9d2065 5253dd8 a9d2065 5253dd8 ad2d334 a9d2065 5253dd8 a9d2065 5253dd8 a9d2065 5253dd8 6967eff ad2d334 5253dd8 6967eff |
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 |
import tensorflow as tf
import requests
import gradio as gr
import numpy as np
# Load the MobileNetV2 model
inception_net = tf.keras.applications.MobileNetV2(weights="imagenet")
# Download human-readable labels for ImageNet
response = requests.get("https://git.io/JJkYN")
labels = response.text.split("\n")
# Define the function to classify an image
def classify_image(image):
# Preprocess the user-uploaded image
image = tf.image.resize(image, [224, 224])
image = tf.keras.applications.mobilenet_v2.preprocess_input(image)
image = np.expand_dims(image, axis=0)
# Make predictions using the MobileNetV2 model
prediction = inception_net.predict(image).flatten()
# Get the top 3 predicted labels with their confidence scores
top_indices = prediction.argsort()[-3:][::-1]
top_classes = [labels[i] for i in top_indices]
top_scores = [float(prediction[i]) for i in top_indices]
return {top_classes[i]: top_scores[i] for i in range(3)}
# Create the Gradio interface
iface = gr.Interface(
fn=classify_image,
inputs=gr.Image(type="numpy"),
outputs=gr.Label(num_top_classes=3),
live=True,
title="Image Classification",
description="Upload an image, and the model will classify it into the top 3 categories.",
)
# Launch the Gradio interface
iface.launch()
|