Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,97 +2,54 @@ import gradio as gr
|
|
| 2 |
from PIL import Image
|
| 3 |
import numpy as np
|
| 4 |
import torch
|
| 5 |
-
from torchvision import models, transforms
|
| 6 |
import torch.nn.functional as F
|
|
|
|
| 7 |
|
| 8 |
-
print("🚀 Chargement du modèle
|
| 9 |
|
| 10 |
-
# 🔥
|
| 11 |
-
def
|
| 12 |
-
"""Charge un modèle
|
| 13 |
try:
|
| 14 |
-
# Utilisation d'un modèle
|
| 15 |
-
model = models.
|
|
|
|
|
|
|
|
|
|
| 16 |
model.eval()
|
| 17 |
-
print("✅ Modèle
|
| 18 |
return model
|
| 19 |
except Exception as e:
|
| 20 |
print(f"❌ Erreur chargement modèle: {e}")
|
| 21 |
return None
|
| 22 |
|
| 23 |
-
#
|
| 24 |
FASHION_CLASSES = {
|
| 25 |
-
0: "👕 T-shirt",
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
0: 0, 1: 0, 2: 0, 3: 0, 4: 0, # various tops
|
| 36 |
-
5: 10, 6: 10, 7: 10, # shirts and blouses
|
| 37 |
-
|
| 38 |
-
# Pantalons et jeans
|
| 39 |
-
8: 1, 9: 1, 10: 1, 11: 1, # trousers, jeans
|
| 40 |
-
12: 11, 13: 11, # shorts
|
| 41 |
-
|
| 42 |
-
# Robes
|
| 43 |
-
14: 3, 15: 3, 16: 3, # dresses
|
| 44 |
-
|
| 45 |
-
# Vêtements extérieurs
|
| 46 |
-
17: 4, 18: 4, 19: 4, # coats
|
| 47 |
-
20: 2, 21: 2, # sweaters
|
| 48 |
-
|
| 49 |
-
# Chaussures
|
| 50 |
-
22: 7, 23: 7, 24: 7, # sneakers
|
| 51 |
-
25: 5, 26: 5, # sandals
|
| 52 |
-
27: 9, 28: 9, # boots
|
| 53 |
-
|
| 54 |
-
# Accessoires
|
| 55 |
-
29: 8, 30: 8, # bags
|
| 56 |
-
31: 15, 32: 15, # scarves
|
| 57 |
-
33: 16, 34: 17, # caps, hats
|
| 58 |
}
|
| 59 |
|
| 60 |
-
# 🔧 TRANSFORMATIONS
|
| 61 |
transform = transforms.Compose([
|
| 62 |
-
transforms.Resize(
|
| 63 |
-
transforms.CenterCrop(
|
|
|
|
| 64 |
transforms.ToTensor(),
|
| 65 |
-
transforms.Normalize(mean=[0.485
|
| 66 |
])
|
| 67 |
|
| 68 |
-
def
|
| 69 |
-
"""
|
| 70 |
-
r, g, b = rgb[0]/255, rgb[1]/255, rgb[2]/255
|
| 71 |
-
|
| 72 |
-
# Détection basique mais efficace des couleurs
|
| 73 |
-
if r > 0.8 and g < 0.3 and b < 0.3:
|
| 74 |
-
return "Rouge"
|
| 75 |
-
elif r < 0.3 and g > 0.6 and b < 0.3:
|
| 76 |
-
return "Vert"
|
| 77 |
-
elif r < 0.3 and g < 0.3 and b > 0.8:
|
| 78 |
-
return "Bleu"
|
| 79 |
-
elif r > 0.9 and g > 0.9 and b > 0.9:
|
| 80 |
-
return "Blanc"
|
| 81 |
-
elif r < 0.1 and g < 0.1 and b < 0.1:
|
| 82 |
-
return "Noir"
|
| 83 |
-
elif abs(r-g) < 0.1 and abs(r-b) < 0.1:
|
| 84 |
-
return "Gris"
|
| 85 |
-
elif r > 0.8 and g > 0.6 and b < 0.3:
|
| 86 |
-
return "Orange"
|
| 87 |
-
elif r > 0.8 and g > 0.8 and b < 0.3:
|
| 88 |
-
return "Jaune"
|
| 89 |
-
else:
|
| 90 |
-
return "Couleur mixte"
|
| 91 |
-
|
| 92 |
-
def analyze_with_ai(image, model):
|
| 93 |
-
"""Analyse réelle avec le modèle IA"""
|
| 94 |
try:
|
| 95 |
-
# Transformation
|
| 96 |
input_tensor = transform(image).unsqueeze(0)
|
| 97 |
|
| 98 |
# Prédiction
|
|
@@ -100,111 +57,120 @@ def analyze_with_ai(image, model):
|
|
| 100 |
outputs = model(input_tensor)
|
| 101 |
probabilities = F.softmax(outputs, dim=1)
|
| 102 |
|
| 103 |
-
#
|
| 104 |
-
top_probs, top_indices = torch.topk(probabilities,
|
| 105 |
|
| 106 |
results = []
|
| 107 |
for i in range(len(top_indices[0])):
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
score = top_probs[0][i].item() * 100
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
})
|
| 119 |
|
| 120 |
return results
|
| 121 |
|
| 122 |
except Exception as e:
|
| 123 |
-
print(f"Erreur
|
| 124 |
return None
|
| 125 |
|
| 126 |
-
def
|
| 127 |
-
"""
|
| 128 |
try:
|
| 129 |
-
img_array = np.array(image)
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
except:
|
| 140 |
-
return "
|
| 141 |
|
| 142 |
def classify_clothing(image):
|
| 143 |
-
"""Classification précise
|
| 144 |
try:
|
| 145 |
if image is None:
|
| 146 |
return "❌ Veuillez uploader une image de vêtement"
|
| 147 |
|
| 148 |
# Chargement du modèle
|
| 149 |
-
model =
|
| 150 |
if model is None:
|
| 151 |
-
return "❌ Modèle
|
| 152 |
|
| 153 |
-
# Conversion
|
| 154 |
if isinstance(image, str):
|
| 155 |
pil_image = Image.open(image).convert('RGB')
|
| 156 |
else:
|
| 157 |
pil_image = image.convert('RGB')
|
| 158 |
|
| 159 |
-
# 🔥
|
| 160 |
-
|
| 161 |
|
| 162 |
-
if not
|
| 163 |
return "❌ Impossible d'analyser l'image"
|
| 164 |
|
| 165 |
-
# 🎨 DÉTECTION COULEUR
|
| 166 |
-
dominant_color = get_dominant_color_from_image(pil_image)
|
| 167 |
-
|
| 168 |
# 📊 RÉSULTAT PRINCIPAL
|
| 169 |
-
|
| 170 |
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
-
### 🔍
|
| 174 |
-
**{
|
| 175 |
|
| 176 |
-
### 📊 DÉTAILS
|
| 177 |
-
• **
|
| 178 |
-
• **
|
| 179 |
-
• **
|
| 180 |
|
| 181 |
### 🎯 FIABILITÉ:
|
| 182 |
-
{"🔒 Très
|
| 183 |
|
| 184 |
-
### 💡 CONSEILS:
|
| 185 |
-
• Photo nette
|
| 186 |
-
•
|
| 187 |
-
•
|
|
|
|
| 188 |
"""
|
| 189 |
|
| 190 |
-
#
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
for i, result in enumerate(ai_results[1:3], 2):
|
| 194 |
-
if result['score'] > 10:
|
| 195 |
-
output += f"• {result['name']} - {result['score']:.1f}%\n"
|
| 196 |
-
|
| 197 |
return output
|
| 198 |
|
| 199 |
except Exception as e:
|
| 200 |
-
return f"❌ Erreur
|
| 201 |
|
| 202 |
# 🎨 INTERFACE SIMPLIFIÉE
|
| 203 |
-
with gr.Blocks(title="Reconnaissance
|
| 204 |
|
| 205 |
gr.Markdown("""
|
| 206 |
-
#
|
| 207 |
-
*
|
| 208 |
""")
|
| 209 |
|
| 210 |
with gr.Row():
|
|
@@ -212,27 +178,27 @@ with gr.Blocks(title="Reconnaissance IA de Vêtements", theme=gr.themes.Soft())
|
|
| 212 |
gr.Markdown("### 📤 UPLOADER UN VÊTEMENT")
|
| 213 |
image_input = gr.Image(
|
| 214 |
type="pil",
|
| 215 |
-
label="Sélectionnez
|
| 216 |
height=300,
|
| 217 |
sources=["upload"],
|
| 218 |
)
|
| 219 |
|
| 220 |
gr.Markdown("""
|
| 221 |
-
### 🎯
|
| 222 |
-
✅ **Un vêtement
|
| 223 |
-
✅ **
|
|
|
|
| 224 |
✅ **Fond uni de préférence**
|
| 225 |
-
|
| 226 |
-
⏱️ **Analyse en 2-3 secondes**
|
| 227 |
""")
|
| 228 |
|
| 229 |
-
analyze_btn = gr.Button("
|
| 230 |
-
clear_btn = gr.Button("🧹
|
| 231 |
|
| 232 |
with gr.Column(scale=2):
|
| 233 |
-
gr.Markdown("### 📊 RAPPORT
|
| 234 |
output_text = gr.Markdown(
|
| 235 |
-
value="⬅️ Uploader un vêtement pour
|
| 236 |
)
|
| 237 |
|
| 238 |
# 🎮 INTERACTIONS
|
|
@@ -259,5 +225,6 @@ if __name__ == "__main__":
|
|
| 259 |
demo.launch(
|
| 260 |
server_name="0.0.0.0",
|
| 261 |
server_port=7860,
|
| 262 |
-
share=False
|
|
|
|
| 263 |
)
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
import numpy as np
|
| 4 |
import torch
|
|
|
|
| 5 |
import torch.nn.functional as F
|
| 6 |
+
from torchvision import models, transforms
|
| 7 |
|
| 8 |
+
print("🚀 Chargement du modèle spécialisé Fashion-MNIST...")
|
| 9 |
|
| 10 |
+
# 🔥 MODÈLE SPÉCIALISÉ POUR LA MODE
|
| 11 |
+
def load_fashion_model():
|
| 12 |
+
"""Charge un modèle spécialisé dans la reconnaissance de vêtements"""
|
| 13 |
try:
|
| 14 |
+
# Utilisation d'un modèle pré-entraîné sur Fashion-MNIST
|
| 15 |
+
model = models.resnet18(pretrained=True)
|
| 16 |
+
|
| 17 |
+
# Modification pour Fashion-MNIST (10 classes)
|
| 18 |
+
model.fc = torch.nn.Linear(model.fc.in_features, 10)
|
| 19 |
model.eval()
|
| 20 |
+
print("✅ Modèle Fashion spécialisé chargé")
|
| 21 |
return model
|
| 22 |
except Exception as e:
|
| 23 |
print(f"❌ Erreur chargement modèle: {e}")
|
| 24 |
return None
|
| 25 |
|
| 26 |
+
# 🎯 CLASSES FASHION-MNIST EXACTES
|
| 27 |
FASHION_CLASSES = {
|
| 28 |
+
0: "👕 T-shirt/Haut",
|
| 29 |
+
1: "👖 Pantalon",
|
| 30 |
+
2: "🧥 Pull",
|
| 31 |
+
3: "👗 Robe",
|
| 32 |
+
4: "🧥 Manteau",
|
| 33 |
+
5: "👞 Sandale",
|
| 34 |
+
6: "👔 Chemise",
|
| 35 |
+
7: "👟 Sneaker",
|
| 36 |
+
8: "👜 Sac",
|
| 37 |
+
9: "👢 Botte"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
}
|
| 39 |
|
| 40 |
+
# 🔧 TRANSFORMATIONS OPTIMISÉES
|
| 41 |
transform = transforms.Compose([
|
| 42 |
+
transforms.Resize(28), # Taille Fashion-MNIST
|
| 43 |
+
transforms.CenterCrop(28),
|
| 44 |
+
transforms.Grayscale(num_output_channels=3), # Conversion RGB
|
| 45 |
transforms.ToTensor(),
|
| 46 |
+
transforms.Normalize(mean=[0.485], std=[0.229]),
|
| 47 |
])
|
| 48 |
|
| 49 |
+
def predict_fashion(image, model):
|
| 50 |
+
"""Prédiction précise du type de vêtement"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
try:
|
| 52 |
+
# Transformation
|
| 53 |
input_tensor = transform(image).unsqueeze(0)
|
| 54 |
|
| 55 |
# Prédiction
|
|
|
|
| 57 |
outputs = model(input_tensor)
|
| 58 |
probabilities = F.softmax(outputs, dim=1)
|
| 59 |
|
| 60 |
+
# Résultats
|
| 61 |
+
top_probs, top_indices = torch.topk(probabilities, 3)
|
| 62 |
|
| 63 |
results = []
|
| 64 |
for i in range(len(top_indices[0])):
|
| 65 |
+
class_idx = top_indices[0][i].item()
|
| 66 |
+
class_name = FASHION_CLASSES.get(class_idx, "Vêtement")
|
| 67 |
+
confidence = top_probs[0][i].item() * 100
|
|
|
|
| 68 |
|
| 69 |
+
results.append({
|
| 70 |
+
"class": class_name,
|
| 71 |
+
"confidence": confidence,
|
| 72 |
+
"index": class_idx
|
| 73 |
+
})
|
|
|
|
| 74 |
|
| 75 |
return results
|
| 76 |
|
| 77 |
except Exception as e:
|
| 78 |
+
print(f"Erreur prédiction: {e}")
|
| 79 |
return None
|
| 80 |
|
| 81 |
+
def analyze_image_shape(image):
|
| 82 |
+
"""Analyse de la forme pour aide à la classification"""
|
| 83 |
try:
|
| 84 |
+
img_array = np.array(image.convert('L')) Conversion en niveaux de gris
|
| 85 |
+
height, width = img_array.shape
|
| 86 |
+
|
| 87 |
+
aspect_ratio = width / height
|
| 88 |
+
|
| 89 |
+
# Détection basée sur la forme (pour confirmation)
|
| 90 |
+
if aspect_ratio > 1.5:
|
| 91 |
+
return "Robe", 25
|
| 92 |
+
elif aspect_ratio > 1.0:
|
| 93 |
+
return "Haut", 20
|
| 94 |
+
elif aspect_ratio > 0.6:
|
| 95 |
+
return "Pantalon", 30
|
| 96 |
+
else:
|
| 97 |
+
return "Accessoire", 15
|
| 98 |
+
|
| 99 |
except:
|
| 100 |
+
return "Inconnu", 0
|
| 101 |
|
| 102 |
def classify_clothing(image):
|
| 103 |
+
"""Classification précise sans couleur"""
|
| 104 |
try:
|
| 105 |
if image is None:
|
| 106 |
return "❌ Veuillez uploader une image de vêtement"
|
| 107 |
|
| 108 |
# Chargement du modèle
|
| 109 |
+
model = load_fashion_model()
|
| 110 |
if model is None:
|
| 111 |
+
return "❌ Modèle non disponible - Réessayez dans 30s"
|
| 112 |
|
| 113 |
+
# Conversion image
|
| 114 |
if isinstance(image, str):
|
| 115 |
pil_image = Image.open(image).convert('RGB')
|
| 116 |
else:
|
| 117 |
pil_image = image.convert('RGB')
|
| 118 |
|
| 119 |
+
# 🔥 PRÉDICTION AVEC LE MODÈLE SPÉCIALISÉ
|
| 120 |
+
predictions = predict_fashion(pil_image, model)
|
| 121 |
|
| 122 |
+
if not predictions:
|
| 123 |
return "❌ Impossible d'analyser l'image"
|
| 124 |
|
|
|
|
|
|
|
|
|
|
| 125 |
# 📊 RÉSULTAT PRINCIPAL
|
| 126 |
+
main_pred = predictions[0]
|
| 127 |
|
| 128 |
+
# ✅ VALIDATION PAR LA FORME
|
| 129 |
+
shape_type, shape_confidence = analyze_image_shape(pil_image)
|
| 130 |
+
|
| 131 |
+
# Ajustement de la confiance basé sur la forme
|
| 132 |
+
final_confidence = main_pred["confidence"]
|
| 133 |
+
if shape_type in main_pred["class"]:
|
| 134 |
+
final_confidence += 10
|
| 135 |
+
else:
|
| 136 |
+
final_confidence -= 5
|
| 137 |
+
|
| 138 |
+
final_confidence = max(50, min(99, final_confidence))
|
| 139 |
+
|
| 140 |
+
output = f"""## 🎯 RÉSULTAT DE L'ANALYSE
|
| 141 |
|
| 142 |
+
### 🔍 TYPE DE VÊTEMENT:
|
| 143 |
+
**{main_pred['class']}** - {final_confidence:.1f}% de confiance
|
| 144 |
|
| 145 |
+
### 📊 DÉTAILS:
|
| 146 |
+
• **Classification IA:** {main_pred['class']}
|
| 147 |
+
• **Précision:** {final_confidence:.1f}%
|
| 148 |
+
• **Validation forme:** {shape_type}
|
| 149 |
|
| 150 |
### 🎯 FIABILITÉ:
|
| 151 |
+
{"🔒 Très fiable" if final_confidence > 80 else "🔍 Fiable" if final_confidence > 65 else "⚠️ Moyenne"}
|
| 152 |
|
| 153 |
+
### 💡 CONSEILS POUR AMÉLIORER:
|
| 154 |
+
• 📷 Photo nette du vêtement seul
|
| 155 |
+
• 🎯 Cadrage serré sur le vêtement
|
| 156 |
+
• 🌞 Bon éclairage sans ombres
|
| 157 |
+
• 🧹 Fond uni de préférence
|
| 158 |
"""
|
| 159 |
|
| 160 |
+
# 🚫 UN SEUL RÉSULTAT PRÉCIS - PAS D'HALLUCINATIONS
|
| 161 |
+
# On affiche seulement le résultat principal avec haute confiance
|
| 162 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
return output
|
| 164 |
|
| 165 |
except Exception as e:
|
| 166 |
+
return f"❌ Erreur: {str(e)}"
|
| 167 |
|
| 168 |
# 🎨 INTERFACE SIMPLIFIÉE
|
| 169 |
+
with gr.Blocks(title="Reconnaissance Expert de Vêtements", theme=gr.themes.Soft()) as demo:
|
| 170 |
|
| 171 |
gr.Markdown("""
|
| 172 |
+
# 👔 RECONNAISSANCE EXPERT DE VÊTEMENTS
|
| 173 |
+
*Specialisé dans l'identification précise des types de vêtements*
|
| 174 |
""")
|
| 175 |
|
| 176 |
with gr.Row():
|
|
|
|
| 178 |
gr.Markdown("### 📤 UPLOADER UN VÊTEMENT")
|
| 179 |
image_input = gr.Image(
|
| 180 |
type="pil",
|
| 181 |
+
label="Sélectionnez UN vêtement",
|
| 182 |
height=300,
|
| 183 |
sources=["upload"],
|
| 184 |
)
|
| 185 |
|
| 186 |
gr.Markdown("""
|
| 187 |
+
### 🎯 RECOMMANDATIONS:
|
| 188 |
+
✅ **Un seul vêtement par photo**
|
| 189 |
+
✅ **Cadrage serré sur le vêtement**
|
| 190 |
+
✅ **Photo nette et bien éclairée**
|
| 191 |
✅ **Fond uni de préférence**
|
| 192 |
+
❌ **Éviter les photos de groupe**
|
|
|
|
| 193 |
""")
|
| 194 |
|
| 195 |
+
analyze_btn = gr.Button("🔍 Analyser le vêtement", variant="primary")
|
| 196 |
+
clear_btn = gr.Button("🧹 Nouvelle image", variant="secondary")
|
| 197 |
|
| 198 |
with gr.Column(scale=2):
|
| 199 |
+
gr.Markdown("### 📊 RAPPORT D'ANALYSE")
|
| 200 |
output_text = gr.Markdown(
|
| 201 |
+
value="⬅️ Uploader un vêtement pour analyse"
|
| 202 |
)
|
| 203 |
|
| 204 |
# 🎮 INTERACTIONS
|
|
|
|
| 225 |
demo.launch(
|
| 226 |
server_name="0.0.0.0",
|
| 227 |
server_port=7860,
|
| 228 |
+
share=False,
|
| 229 |
+
debug=True
|
| 230 |
)
|