File size: 1,578 Bytes
7c3ddde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-
"""

Created on Sun Feb 15 16:14:13 2026



@author: ottav

"""

import tensorflow as tf
import numpy as np
import gradio as gr
from PIL import Image

#CARICAMENTO DEL MODELLO
model = tf.keras.models.load_model('modello_guida_autonoma_cifar10.h5')

#FUNZIONE DI CLASSIFICAZIONE
def classifica_immagine(img):
    # img arriva già come oggetto PIL grazie a type="pil" in Gradio
    
    # Ridimensiono a 32x32 (la dimensione usata da CIFAR-10)
    img = img.resize((32, 32))
    
    # Convertoo in array e normalizziamo (0-1)
    img_array = np.array(img).astype('float32') / 255.0
    
    # Aggiungo la dimensione batch: diventa (1, 32, 32, 3)
    img_array = np.expand_dims(img_array, axis=0)
    
    # Predizione
    prediction = model.predict(img_array)[0]
    
    # prediction[0] = Veicolo, prediction[1] = Animale
    return {
        "Veicolo": float(prediction[0]),
        "Animale": float(prediction[1])
    }

#INTERFACCIA GRAFICA
# sito
interface = gr.Interface(
    fn=classifica_immagine,               # La funzione da usare
    inputs=gr.Image(type="pil", label="Carica un'immagine"), # Input immagine
    outputs=gr.Label(num_top_classes=2),  # Output barre percentuali
    title="Autonomous Driving Recognition",
    description="Carica una foto (o prendila da Google). La rete neurale deciderà se è un **Veicolo** o un **Animale**.",
    # Esempi pronti da cliccare 
    examples=[["camion.jpg"], ["gatto.jpg"]] 
)

# LANCIO DELL'APP
if __name__ == "__main__":
    interface.launch()