Spaces:
Sleeping
Sleeping
keerthi-balaji
commited on
Commit
·
a9d2065
1
Parent(s):
24e1415
Add application file
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ import requests
|
|
3 |
import gradio as gr
|
4 |
|
5 |
# Load the InceptionNet model
|
6 |
-
inception_net = tf.keras.applications.MobileNetV2()
|
7 |
|
8 |
# Download human-readable labels for ImageNet
|
9 |
response = requests.get("https://git.io/JJkYN")
|
@@ -12,28 +12,29 @@ labels = response.text.split("\n")
|
|
12 |
# Define the function to classify an image
|
13 |
def classify_image(image):
|
14 |
# Preprocess the user-uploaded image
|
15 |
-
image = image.
|
16 |
image = tf.keras.applications.mobilenet_v2.preprocess_input(image)
|
17 |
-
|
|
|
18 |
# Make predictions using the MobileNetV2 model
|
19 |
prediction = inception_net.predict(image).flatten()
|
20 |
-
|
21 |
# Get the top 3 predicted labels with their confidence scores
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
25 |
return {top_classes[i]: top_scores[i] for i in range(3)}
|
26 |
|
27 |
# Create the Gradio interface
|
28 |
iface = gr.Interface(
|
29 |
fn=classify_image,
|
30 |
-
inputs=gr.Image(shape=(224, 224)),
|
31 |
-
outputs=gr.Label(num_top_classes=3),
|
32 |
live=True,
|
33 |
-
capture_session=True, # This captures the user's uploaded image
|
34 |
title="Image Classification",
|
35 |
description="Upload an image, and the model will classify it into the top 3 categories.",
|
36 |
)
|
37 |
|
38 |
# Launch the Gradio interface
|
39 |
-
iface.launch()
|
|
|
3 |
import gradio as gr
|
4 |
|
5 |
# Load the InceptionNet model
|
6 |
+
inception_net = tf.keras.applications.MobileNetV2(weights="imagenet")
|
7 |
|
8 |
# Download human-readable labels for ImageNet
|
9 |
response = requests.get("https://git.io/JJkYN")
|
|
|
12 |
# Define the function to classify an image
|
13 |
def classify_image(image):
|
14 |
# Preprocess the user-uploaded image
|
15 |
+
image = tf.image.resize(image, [224, 224])
|
16 |
image = tf.keras.applications.mobilenet_v2.preprocess_input(image)
|
17 |
+
image = tf.expand_dims(image, axis=0)
|
18 |
+
|
19 |
# Make predictions using the MobileNetV2 model
|
20 |
prediction = inception_net.predict(image).flatten()
|
21 |
+
|
22 |
# Get the top 3 predicted labels with their confidence scores
|
23 |
+
top_indices = prediction.argsort()[-3:][::-1]
|
24 |
+
top_classes = [labels[i] for i in top_indices]
|
25 |
+
top_scores = [float(prediction[i]) for i in top_indices]
|
26 |
+
|
27 |
return {top_classes[i]: top_scores[i] for i in range(3)}
|
28 |
|
29 |
# Create the Gradio interface
|
30 |
iface = gr.Interface(
|
31 |
fn=classify_image,
|
32 |
+
inputs=gr.inputs.Image(shape=(224, 224)),
|
33 |
+
outputs=gr.outputs.Label(num_top_classes=3),
|
34 |
live=True,
|
|
|
35 |
title="Image Classification",
|
36 |
description="Upload an image, and the model will classify it into the top 3 categories.",
|
37 |
)
|
38 |
|
39 |
# Launch the Gradio interface
|
40 |
+
iface.launch()
|