Leo71288 commited on
Commit
12f9c4c
·
verified ·
1 Parent(s): 365c743

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -29
app.py CHANGED
@@ -1,42 +1,42 @@
1
  import gradio as gr
2
- from keras.models import load_model
3
- from PIL import Image, ImageOps
4
  import numpy as np
 
 
5
 
6
- # Charger le modèle Keras
7
- model = load_model("keras_model.h5", compile=False)
8
 
9
- # Charger les labels
10
- with open("labels.txt", "r") as file:
11
- class_names = [line.strip() for line in file.readlines()]
 
 
12
 
13
- # Fonction de prédiction
14
- def predict_image(img):
15
- size = (224, 224)
16
- image = ImageOps.fit(img, size, Image.Resampling.LANCZOS)
17
- image_array = np.asarray(image)
18
- normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
19
 
20
- # Charger l'image dans le bon format
21
- data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
22
- data[0] = normalized_image_array
 
 
23
 
24
- # Faire une prédiction
25
- prediction = model.predict(data)
26
- index = np.argmax(prediction)
27
- class_name = class_names[index]
28
- confidence_score = float(prediction[0][index])
29
 
30
- return {class_name: confidence_score}
31
 
32
  # Interface Gradio
33
- iface = gr.Interface(
34
- fn=predict_image,
35
- inputs=gr.Image(type="pil"),
36
- outputs=gr.Label(),
37
- title="Classification d'Images avec Keras",
38
- description="Upload une image et obtenez la classe prédite avec le score de confiance.",
 
39
  )
40
 
41
  # Lancer l'application
42
- iface.launch()
 
 
1
  import gradio as gr
2
+ import tensorflow as tf
 
3
  import numpy as np
4
+ from tensorflow.keras.models import load_model
5
+ from tensorflow.keras.preprocessing.image import load_img, img_to_array
6
 
7
+ # Charger le modèle (avec correction)
8
+ custom_objects = {"DepthwiseConv2D": tf.keras.layers.DepthwiseConv2D}
9
 
10
+ try:
11
+ model = load_model("keras_model.h5", custom_objects=custom_objects, compile=False)
12
+ print("✅ Modèle chargé avec succès !")
13
+ except Exception as e:
14
+ print(f"⚠️ Erreur lors du chargement du modèle : {e}")
15
 
16
+ # Définir les classes du modèle (à adapter selon ton dataset)
17
+ class_labels = ["Classe 1", "Classe 2", "Classe 3"] # Remplace par tes propres classes
 
 
 
 
18
 
19
+ # Fonction de prédiction
20
+ def predict(image):
21
+ img = image.resize((224, 224)) # Adapter à la taille d'entrée du modèle
22
+ img_array = img_to_array(img) / 255.0 # Normalisation
23
+ img_array = np.expand_dims(img_array, axis=0) # Ajouter une dimension batch
24
 
25
+ predictions = model.predict(img_array)[0] # Obtenir les prédictions
26
+ confidences = {class_labels[i]: float(predictions[i]) for i in range(len(class_labels))}
 
 
 
27
 
28
+ return confidences
29
 
30
  # Interface Gradio
31
+ interface = gr.Interface(
32
+ fn=predict,
33
+ inputs=gr.Image(type="pil"), # Permet d'uploader une image
34
+ outputs=gr.Label(num_top_classes=3), # Affiche les probabilités des classes
35
+ title="🌟 Modèle Keras avec Gradio",
36
+ description="Téléverse une image et laisse le modèle faire sa prédiction.",
37
+ theme="compact"
38
  )
39
 
40
  # Lancer l'application
41
+ if __name__ == "__main__":
42
+ interface.launch(share=True) # `share=True` génère un lien public