yuragoithf
commited on
Commit
·
0de8536
1
Parent(s):
15f8afb
Upload app.py
Browse files
app.py
CHANGED
@@ -4,43 +4,73 @@ import numpy as np
|
|
4 |
import gdown
|
5 |
from PIL import Image
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
"
|
11 |
-
"
|
12 |
-
|
13 |
-
|
14 |
-
"dog",
|
15 |
-
"frog",
|
16 |
-
"horse",
|
17 |
-
"ship",
|
18 |
-
"truck",
|
19 |
-
]
|
20 |
|
21 |
-
|
22 |
-
url = "https://drive.google.com/uc?id=12700bE-pomYKoVQ214VrpBoJ7akXcTpL"
|
23 |
-
output = "modelV2Lmixed.keras"
|
24 |
-
gdown.download(url, output, quiet=False)
|
25 |
|
26 |
-
|
|
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
gr.Interface(
|
40 |
-
fn=classify_image,
|
41 |
-
inputs=gr.inputs.Image(shape=(32, 32)),
|
42 |
-
outputs=gr.outputs.Label(num_top_classes=3),
|
43 |
-
examples=["03_cat.jpg", "05_dog.jpg"],
|
44 |
-
theme="default",
|
45 |
-
css=".footer{display:none !important}",
|
46 |
-
).launch()
|
|
|
4 |
import gdown
|
5 |
from PIL import Image
|
6 |
|
7 |
+
input_shape = (32, 32, 3)
|
8 |
+
resized_shape = (224, 224, 3)
|
9 |
+
num_classes = 10
|
10 |
+
labels = {
|
11 |
+
0: "plane",
|
12 |
+
1: "car",
|
13 |
+
2: "bird",
|
14 |
+
3: "cat",
|
15 |
+
4: "deer",
|
16 |
+
5: "dog",
|
17 |
+
6: "frog",
|
18 |
+
7: "horse",
|
19 |
+
8: "ship",
|
20 |
+
9: "truck",
|
21 |
+
}
|
22 |
|
23 |
+
# Download the model file
|
24 |
+
def download_model():
|
25 |
+
url = "https://drive.google.com/uc?id=12700bE-pomYKoVQ214VrpBoJ7akXcTpL"
|
26 |
+
output = "modelV2Lmixed.keras"
|
27 |
+
gdown.download(url, output, quiet=False)
|
28 |
+
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
model_file = download_model()
|
|
|
|
|
|
|
31 |
|
32 |
+
# Load the model
|
33 |
+
model = tf.keras.models.load_model(model_file)
|
34 |
|
35 |
+
# Perform image classification
|
36 |
+
def predict_class(image):
|
37 |
+
img = tf.cast(image, tf.float32)
|
38 |
+
img = tf.image.resize(img, [input_shape[0], input_shape[1]])
|
39 |
+
img = np.expand_dims(img, axis=0)
|
40 |
+
prediction = model.predict(img)
|
41 |
+
return prediction[0]
|
42 |
|
43 |
+
# UI Design
|
44 |
+
def classify_image(image):
|
45 |
+
pred = predict_class(image)
|
46 |
+
class_names = [
|
47 |
+
"plane",
|
48 |
+
"car",
|
49 |
+
"bird",
|
50 |
+
"cat",
|
51 |
+
"deer",
|
52 |
+
"dog",
|
53 |
+
"frog",
|
54 |
+
"horse",
|
55 |
+
"ship",
|
56 |
+
"truck",
|
57 |
+
]
|
58 |
|
59 |
+
probabilities = tf.nn.softmax(pred)
|
60 |
+
top_indices = tf.argsort(probabilities, direction='DESCENDING')
|
61 |
+
top_classes = [class_names[idx] for idx in top_indices]
|
62 |
+
top_probs = [probabilities[idx] for idx in top_indices]
|
63 |
|
64 |
+
output = "<h3>Top 3 Predictions:</h3>"
|
65 |
+
for i in range(3):
|
66 |
+
output += f"<p>{top_classes[i]}: {top_probs[i]*100:.2f}%</p>"
|
67 |
+
|
68 |
+
return output
|
69 |
+
|
70 |
+
inputs = gr.inputs.Image(label="Upload an image")
|
71 |
+
outputs = gr.outputs.HTML()
|
72 |
+
|
73 |
+
title = "<h1 style='text-align: center;'>Image Classifier</h1>"
|
74 |
+
description = "Upload an image and get the top 3 predictions."
|
75 |
|
76 |
+
gr.Interface(fn=classify_image, inputs=inputs, outputs=outputs, title=title, description=description).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|