AfshinMA commited on
Commit
b6ef0f5
·
verified ·
1 Parent(s): 29a5b7b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -43
app.py CHANGED
@@ -1,44 +1,48 @@
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)
 
1
+ import requests
2
+ import tensorflow as tf
3
+ import gradio as gr
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
+ image = gr.Image(interactive=True, label="Upload Image")
35
+ label = gr.Label(num_top_classes=3, label="Top Predictions")
36
+
37
+ demo = gr.Interface(
38
+ title="Image Classifier Keras",
39
+ fn= classify_image,
40
+ inputs= image,
41
+ outputs= label,
42
+ examples= [["./images/banana.jpg"], ["./images/car.jpg"], ["./images/guitar.jpg"], ["./images/lion.jpg"]], # Use valid URLs or local paths
43
+ theme= "default",
44
+ css= ".footer{display:none !important}"
45
+ )
46
+
47
+ if __name__ == "__main__":
48
  demo.launch(share=True)