Spaces:
Sleeping
Sleeping
Upload 6 files
Browse files- app.py +44 -0
- images/banana.jpg +0 -0
- images/car.jpg +0 -0
- images/guitar.jpg +0 -0
- images/lion.jpg +0 -0
- requirements.txt +0 -0
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
from tensorflow import keras
|
4 |
+
|
5 |
+
def classify_image(input_image):
|
6 |
+
# Download human-readable labels for ImageNet.
|
7 |
+
try:
|
8 |
+
response = requests.get("https://git.io/JJkYN")
|
9 |
+
response.raise_for_status() # Ensure the request was successful
|
10 |
+
labels = response.text.split("\n")
|
11 |
+
except Exception as e:
|
12 |
+
print("Error fetching labels:", e)
|
13 |
+
labels = ["Unknown"] * 1000 # Fallback in case the request fails
|
14 |
+
|
15 |
+
# Load the MobileNetV2 model
|
16 |
+
inception_net = keras.applications.MobileNetV2(
|
17 |
+
input_shape=(224, 224, 3),
|
18 |
+
alpha=1.0,
|
19 |
+
include_top=True,
|
20 |
+
weights="imagenet",
|
21 |
+
classes=1000,
|
22 |
+
classifier_activation="softmax"
|
23 |
+
)
|
24 |
+
|
25 |
+
# Resize the input image to the expected size
|
26 |
+
input_image = input_image.reshape((1, 224, 224, 3)) # Reshape for a single prediction
|
27 |
+
input_image = keras.applications.mobilenet_v2.preprocess_input(input_image)
|
28 |
+
|
29 |
+
# Perform prediction
|
30 |
+
prediction = inception_net.predict(input_image).flatten()
|
31 |
+
confidences = {labels[i]: float(prediction[i]) for i in range(len(labels))}
|
32 |
+
return confidences
|
33 |
+
|
34 |
+
demo = gr.Interface(
|
35 |
+
fn=classify_image,
|
36 |
+
inputs=gr.Image(interactive=True, label="Upload Image"),
|
37 |
+
outputs=gr.Label(num_top_classes=3, label="Top Predictions"),
|
38 |
+
examples=["./images/banana.jpg", "./images/car.jpg", "./images/guitar.jpg", "./images/lion.jpg"], # Use valid URLs or local paths
|
39 |
+
theme="default",
|
40 |
+
css=".footer{display:none !important}"
|
41 |
+
)
|
42 |
+
|
43 |
+
if __name__ == "__main__":
|
44 |
+
demo.launch(share=True)
|
images/banana.jpg
ADDED
images/car.jpg
ADDED
images/guitar.jpg
ADDED
images/lion.jpg
ADDED
requirements.txt
ADDED
Binary file (2.96 kB). View file
|
|