saaara commited on
Commit
73dd2e6
·
verified ·
1 Parent(s): 033ae25

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ import numpy as np
3
+ import gradio as gr
4
+ from tensorflow.keras.preprocessing import image
5
+ from sklearn.preprocessing import LabelEncoder
6
+ import matplotlib.pyplot as plt
7
+ from PIL import Image
8
+
9
+ # Dictionnaire des sous-catégories
10
+ subcategory_dict = {
11
+ "Furniture": ["Home Decor"],
12
+ "Home Decor": [
13
+ "Poufs & Ottomans", "Rugs", "Antique items", "Brass Lamps",
14
+ "Candle Holders", "Pottery", "Kilim poufs", "Pillow Covers",
15
+ "Wall Decor", "Straw Lamps"
16
+ ],
17
+ # Ajoutez d'autres catégories ici
18
+ }
19
+
20
+ # Fonction pour charger et prétraiter l'image
21
+ def preprocess_image(img):
22
+ img = img.resize((224, 224)) # Redimensionner
23
+ img_array = np.array(img) / 255.0 # Normaliser
24
+ img_array = np.expand_dims(img_array, axis=0) # Ajouter une dimension batch
25
+ return img_array
26
+
27
+ # Fonction pour prédire la catégorie, le prix et la sous-catégorie
28
+ def predict_image(img):
29
+ # Prétraiter l'image
30
+ img_array = preprocess_image(img)
31
+
32
+ # Faire les prédictions
33
+ category_pred, price_pred = model.predict(img_array)
34
+
35
+ # Décoder la catégorie
36
+ category_pred_class = np.argmax(category_pred, axis=1)[0]
37
+ category_name = label_encoder.inverse_transform([category_pred_class])[0]
38
+
39
+ # Trouver les sous-catégories correspondantes
40
+ subcategories = subcategory_dict.get(category_name, [])
41
+
42
+ # Préparer les résultats
43
+ results = {
44
+ "Category": category_name,
45
+ "Price ($)": f"{price_pred[0][0]:.2f}",
46
+ "Subcategories": subcategories
47
+ }
48
+ return results
49
+
50
+ # Charger le modèle pré-entraîné
51
+ # Assurez-vous que le chemin du modèle et de l'encodeur sont corrects
52
+ model = tf.keras.models.load_model('trained_model.h5')
53
+ label_encoder = LabelEncoder()
54
+ label_encoder.classes_ = np.load('path_to_label_encoder_classes.npy')
55
+
56
+ # Interface Gradio
57
+ interface = gr.Interface(
58
+ fn=predict_image,
59
+ inputs=gr.Image(type="pil"),
60
+ outputs=[
61
+ gr.Label(label="Category"),
62
+ gr.Text(label="Price ($)"),
63
+ gr.Text(label="Subcategories")
64
+ ],
65
+ title="Image Classification with TensorFlow",
66
+ description="Upload an image to predict its category, price, and subcategories."
67
+ )
68
+
69
+ # Lancer l'interface
70
+ interface.launch()