Update app.py
Browse files
app.py
CHANGED
@@ -3,59 +3,46 @@ import numpy as np
|
|
3 |
import gradio as gr
|
4 |
from tensorflow.keras.models import load_model
|
5 |
|
6 |
-
# Carica il modello salvato in formato Keras .h5
|
7 |
model = load_model('mio_modello.h5')
|
8 |
|
9 |
-
#
|
10 |
def preprocess_image(image):
|
11 |
-
# Convertire l'immagine in formato PIL se non lo è già
|
12 |
-
image = Image.fromarray(image)
|
13 |
|
14 |
-
|
15 |
image = image.convert("RGB")
|
16 |
-
|
17 |
-
# Ridimensiona l'immagine
|
18 |
image = image.resize((64, 64))
|
19 |
-
|
20 |
-
# Converti l'immagine in array NumPy e normalizza
|
21 |
-
image_array = np.array(image) / 255.0 # Normalizza i pixel tra 0 e 1
|
22 |
|
23 |
-
# Aggiungi una dimensione batch
|
24 |
image_array = np.expand_dims(image_array, axis=0) # (1, 64, 64, 3)
|
25 |
|
26 |
return image_array
|
27 |
|
28 |
-
# Funzione di classificazione con stampa delle predizioni
|
29 |
def classify_image(image):
|
30 |
-
# Preprocessare l'immagine
|
31 |
image_array = preprocess_image(image)
|
32 |
|
33 |
-
|
34 |
-
prediction = model.predict(image_array)[0] # Prendi la prima riga della predizione
|
35 |
|
36 |
-
# Verificare le predizioni grezze
|
37 |
print(f"Raw model predictions: {prediction}")
|
38 |
|
39 |
-
# Trova l'indice della classe con il punteggio più alto
|
40 |
predicted_class_idx = np.argmax(prediction)
|
41 |
|
42 |
-
# Classi (Chihuahua o Muffin)
|
43 |
class_labels = ['Chihuahua', 'Muffin']
|
44 |
|
45 |
-
# Stampa per verificare la classe e la confidenza
|
46 |
print(f"Predicted class: {class_labels[predicted_class_idx]} with confidence {prediction[predicted_class_idx]}")
|
47 |
|
48 |
-
# Restituisci le percentuali di confidenza per ciascuna classe
|
49 |
confidence_scores = {class_labels[i]: float(prediction[i]) for i in range(len(class_labels))}
|
50 |
|
51 |
return confidence_scores
|
52 |
|
53 |
-
|
|
|
|
|
54 |
gr.Interface(
|
55 |
-
fn=classify_image,
|
56 |
-
inputs=gr.Image(type="numpy"),
|
57 |
-
outputs=gr.Label(num_top_classes=2),
|
|
|
58 |
title="Chihuahua vs Muffin Classifier",
|
59 |
-
description="
|
60 |
).launch()
|
61 |
|
|
|
3 |
import gradio as gr
|
4 |
from tensorflow.keras.models import load_model
|
5 |
|
|
|
6 |
model = load_model('mio_modello.h5')
|
7 |
|
8 |
+
# Image processing
|
9 |
def preprocess_image(image):
|
|
|
|
|
10 |
|
11 |
+
image = Image.fromarray(image)
|
12 |
image = image.convert("RGB")
|
|
|
|
|
13 |
image = image.resize((64, 64))
|
14 |
+
image_array = np.array(image) / 255.0
|
|
|
|
|
15 |
|
|
|
16 |
image_array = np.expand_dims(image_array, axis=0) # (1, 64, 64, 3)
|
17 |
|
18 |
return image_array
|
19 |
|
|
|
20 |
def classify_image(image):
|
|
|
21 |
image_array = preprocess_image(image)
|
22 |
|
23 |
+
prediction = model.predict(image_array)[0]
|
|
|
24 |
|
|
|
25 |
print(f"Raw model predictions: {prediction}")
|
26 |
|
|
|
27 |
predicted_class_idx = np.argmax(prediction)
|
28 |
|
|
|
29 |
class_labels = ['Chihuahua', 'Muffin']
|
30 |
|
|
|
31 |
print(f"Predicted class: {class_labels[predicted_class_idx]} with confidence {prediction[predicted_class_idx]}")
|
32 |
|
|
|
33 |
confidence_scores = {class_labels[i]: float(prediction[i]) for i in range(len(class_labels))}
|
34 |
|
35 |
return confidence_scores
|
36 |
|
37 |
+
examples = ['chihuahua1.jpg', 'chihuahua2.jpg', 'chihuahua3.jpg', 'muffin1.jpg', 'muffin2.jpg']
|
38 |
+
|
39 |
+
# Gradio Interface
|
40 |
gr.Interface(
|
41 |
+
fn=classify_image,
|
42 |
+
inputs=gr.Image(type="numpy"),
|
43 |
+
outputs=gr.Label(num_top_classes=2),
|
44 |
+
examples=examples,
|
45 |
title="Chihuahua vs Muffin Classifier",
|
46 |
+
description="don't know if you should cuddle it or eat it? find out uploading a picture. PS: no muffin and no chihuahua got hurt in this project"
|
47 |
).launch()
|
48 |
|