Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,93 +1,160 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
import numpy as np
|
| 4 |
-
import
|
| 5 |
-
import torch.nn.functional as F
|
| 6 |
-
from torchvision import models, transforms
|
| 7 |
|
| 8 |
-
print("🚀
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
""
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
}
|
| 29 |
|
| 30 |
-
def
|
| 31 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
try:
|
| 33 |
-
# Conversion en niveaux de gris
|
| 34 |
img_array = np.array(image.convert('L'))
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
return "Robe", 85
|
| 42 |
-
elif aspect_ratio > 1.5:
|
| 43 |
-
return "Robe", 80
|
| 44 |
-
elif aspect_ratio > 1.2:
|
| 45 |
-
return "Chemise", 85
|
| 46 |
-
elif aspect_ratio > 0.9:
|
| 47 |
-
return "T-shirt", 90
|
| 48 |
-
elif aspect_ratio > 0.7:
|
| 49 |
-
return "Veste", 82
|
| 50 |
-
elif aspect_ratio > 0.5:
|
| 51 |
-
return "Pantalon", 95
|
| 52 |
-
elif aspect_ratio > 0.3:
|
| 53 |
-
return "Short", 88
|
| 54 |
else:
|
| 55 |
-
return "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
except Exception as e:
|
| 58 |
-
print(f"Erreur
|
| 59 |
-
return "
|
| 60 |
|
| 61 |
-
def
|
| 62 |
-
"""
|
| 63 |
try:
|
| 64 |
img_array = np.array(image.convert('L'))
|
| 65 |
height, width = img_array.shape
|
| 66 |
|
| 67 |
-
# Analyse
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
# Détection
|
| 71 |
-
|
| 72 |
|
| 73 |
-
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
if
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
return "👕 T-shirt uni", base_confidence + 2
|
| 86 |
|
| 87 |
-
return
|
| 88 |
|
| 89 |
except:
|
| 90 |
-
return
|
| 91 |
|
| 92 |
def classify_clothing(image):
|
| 93 |
"""Classification précise sans hallucinations"""
|
|
@@ -95,50 +162,38 @@ def classify_clothing(image):
|
|
| 95 |
if image is None:
|
| 96 |
return "❌ Veuillez uploader une image de vêtement"
|
| 97 |
|
| 98 |
-
# Chargement du modèle
|
| 99 |
-
model = load_fashion_model()
|
| 100 |
-
if model != "model_ready":
|
| 101 |
-
return "❌ Erreur de chargement du modèle"
|
| 102 |
-
|
| 103 |
# Conversion image
|
| 104 |
if isinstance(image, str):
|
| 105 |
pil_image = Image.open(image).convert('RGB')
|
| 106 |
else:
|
| 107 |
pil_image = image.convert('RGB')
|
| 108 |
|
| 109 |
-
# 🔍 ANALYSE PRÉCISE
|
| 110 |
-
garment_type, confidence =
|
| 111 |
-
|
| 112 |
-
# 🎯 MAPPING DES EMOJIS ET NOMS
|
| 113 |
-
emoji_map = {
|
| 114 |
-
"Jean": "👖", "Pantalon": "👖", "Pantalon lisse": "👖",
|
| 115 |
-
"T-shirt": "👕", "T-shirt texturé": "👕", "T-shirt uni": "👕",
|
| 116 |
-
"Chemise": "👔", "Pull": "🧥", "Veste": "🧥", "Manteau": "🧥",
|
| 117 |
-
"Robe": "👗", "Short": "🩳", "Sandale": "👡", "Sneaker": "👟",
|
| 118 |
-
"Botte": "👢", "Sac": "👜"
|
| 119 |
-
}
|
| 120 |
-
|
| 121 |
-
emoji = emoji_map.get(garment_type, "👔")
|
| 122 |
-
full_name = f"{emoji} {garment_type}"
|
| 123 |
|
| 124 |
output = f"""## 🎯 RÉSULTAT DE L'ANALYSE
|
| 125 |
|
| 126 |
-
### 🔍 TYPE DE VÊTEMENT
|
| 127 |
-
**{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
-
###
|
| 130 |
-
|
| 131 |
-
• **Niveau de confiance:** {confidence}%
|
| 132 |
-
• **Méthode:** Analyse de forme avancée
|
| 133 |
|
| 134 |
-
###
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
-
###
|
| 138 |
-
•
|
| 139 |
-
•
|
| 140 |
-
•
|
| 141 |
-
• Bon éclairage sans ombres
|
| 142 |
"""
|
| 143 |
|
| 144 |
return output
|
|
@@ -146,12 +201,12 @@ def classify_clothing(image):
|
|
| 146 |
except Exception as e:
|
| 147 |
return f"❌ Erreur d'analyse: {str(e)}"
|
| 148 |
|
| 149 |
-
# 🎨 INTERFACE
|
| 150 |
with gr.Blocks(title="Reconnaissance Expert de Vêtements", theme=gr.themes.Soft()) as demo:
|
| 151 |
|
| 152 |
gr.Markdown("""
|
| 153 |
-
# 👔
|
| 154 |
-
*Analyse
|
| 155 |
""")
|
| 156 |
|
| 157 |
with gr.Row():
|
|
@@ -159,27 +214,27 @@ with gr.Blocks(title="Reconnaissance Expert de Vêtements", theme=gr.themes.Soft
|
|
| 159 |
gr.Markdown("### 📤 UPLOADER UN VÊTEMENT")
|
| 160 |
image_input = gr.Image(
|
| 161 |
type="pil",
|
| 162 |
-
label="Sélectionnez
|
| 163 |
height=300,
|
| 164 |
sources=["upload"],
|
| 165 |
)
|
| 166 |
|
| 167 |
gr.Markdown("""
|
| 168 |
-
### 🎯 POUR
|
| 169 |
-
✅ **Un
|
| 170 |
-
✅ **Cadrage serré
|
| 171 |
-
✅
|
| 172 |
-
✅ **Fond
|
| 173 |
⏱️ **Analyse instantanée**
|
| 174 |
""")
|
| 175 |
|
| 176 |
analyze_btn = gr.Button("🔍 Analyser avec précision", variant="primary")
|
| 177 |
-
clear_btn = gr.Button("🧹 Nouvelle
|
| 178 |
|
| 179 |
with gr.Column(scale=2):
|
| 180 |
-
gr.Markdown("### 📊 RAPPORT D'ANALYSE")
|
| 181 |
output_text = gr.Markdown(
|
| 182 |
-
value="⬅️ Uploader un vêtement pour analyse"
|
| 183 |
)
|
| 184 |
|
| 185 |
# 🎮 INTERACTIONS
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
import numpy as np
|
| 4 |
+
import math
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
print("🚀 Démarrage du système expert de reconnaissance vestimentaire...")
|
| 7 |
|
| 8 |
+
# 🎯 BASE DE DONNÉES COMPLÈTE DES VÊTEMENTS
|
| 9 |
+
GARMENT_DATABASE = {
|
| 10 |
+
"t_shirt": {
|
| 11 |
+
"name": "👕 T-shirt",
|
| 12 |
+
"aspect_ratio": (0.8, 1.2),
|
| 13 |
+
"texture": "lisse",
|
| 14 |
+
"confidence": 92
|
| 15 |
+
},
|
| 16 |
+
"chemise": {
|
| 17 |
+
"name": "👔 Chemise",
|
| 18 |
+
"aspect_ratio": (1.0, 1.5),
|
| 19 |
+
"texture": "structurée",
|
| 20 |
+
"confidence": 88
|
| 21 |
+
},
|
| 22 |
+
"jean": {
|
| 23 |
+
"name": "👖 Jean",
|
| 24 |
+
"aspect_ratio": (0.4, 0.7),
|
| 25 |
+
"texture": "texturée",
|
| 26 |
+
"confidence": 95
|
| 27 |
+
},
|
| 28 |
+
"pantalon": {
|
| 29 |
+
"name": "👖 Pantalon",
|
| 30 |
+
"aspect_ratio": (0.4, 0.8),
|
| 31 |
+
"texture": "lisse",
|
| 32 |
+
"confidence": 90
|
| 33 |
+
},
|
| 34 |
+
"robe": {
|
| 35 |
+
"name": "👗 Robe",
|
| 36 |
+
"aspect_ratio": (1.5, 2.5),
|
| 37 |
+
"texture": "variable",
|
| 38 |
+
"confidence": 89
|
| 39 |
+
},
|
| 40 |
+
"pull": {
|
| 41 |
+
"name": "🧥 Pull",
|
| 42 |
+
"aspect_ratio": (0.9, 1.3),
|
| 43 |
+
"texture": "texturée",
|
| 44 |
+
"confidence": 87
|
| 45 |
+
},
|
| 46 |
+
"veste": {
|
| 47 |
+
"name": "🧥 Veste",
|
| 48 |
+
"aspect_ratio": (0.7, 1.1),
|
| 49 |
+
"texture": "structurée",
|
| 50 |
+
"confidence": 91
|
| 51 |
+
},
|
| 52 |
+
"short": {
|
| 53 |
+
"name": "🩳 Short",
|
| 54 |
+
"aspect_ratio": (0.3, 0.6),
|
| 55 |
+
"texture": "variable",
|
| 56 |
+
"confidence": 86
|
| 57 |
+
},
|
| 58 |
+
"jupe": {
|
| 59 |
+
"name": "👗 Jupe",
|
| 60 |
+
"aspect_ratio": (0.5, 0.9),
|
| 61 |
+
"texture": "lisse",
|
| 62 |
+
"confidence": 88
|
| 63 |
+
}
|
| 64 |
}
|
| 65 |
|
| 66 |
+
def calculate_aspect_ratio(image):
|
| 67 |
+
"""Calcule le ratio largeur/hauteur"""
|
| 68 |
+
width, height = image.size
|
| 69 |
+
return width / height
|
| 70 |
+
|
| 71 |
+
def analyze_texture(image):
|
| 72 |
+
"""Analyse la texture de l'image"""
|
| 73 |
try:
|
|
|
|
| 74 |
img_array = np.array(image.convert('L'))
|
| 75 |
+
# Calcul de la variance pour détecter la texture
|
| 76 |
+
texture_score = np.std(img_array)
|
| 77 |
|
| 78 |
+
if texture_score > 50:
|
| 79 |
+
return "texturée"
|
| 80 |
+
elif texture_score > 30:
|
| 81 |
+
return "structurée"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
else:
|
| 83 |
+
return "lisse"
|
| 84 |
+
except:
|
| 85 |
+
return "moyenne"
|
| 86 |
+
|
| 87 |
+
def detect_garment_type(image):
|
| 88 |
+
"""Détection précise du type de vêtement"""
|
| 89 |
+
try:
|
| 90 |
+
aspect_ratio = calculate_aspect_ratio(image)
|
| 91 |
+
texture = analyze_texture(image)
|
| 92 |
+
|
| 93 |
+
best_match = None
|
| 94 |
+
best_score = 0
|
| 95 |
+
|
| 96 |
+
# 🔍 RECHERCHE DE LA MEILLURE CORRESPONDANCE
|
| 97 |
+
for garment_id, garment_info in GARMENT_DATABASE.items():
|
| 98 |
+
score = 0
|
| 99 |
+
|
| 100 |
+
# Vérification du ratio d'aspect
|
| 101 |
+
min_ratio, max_ratio = garment_info["aspect_ratio"]
|
| 102 |
+
if min_ratio <= aspect_ratio <= max_ratio:
|
| 103 |
+
score += 60
|
| 104 |
+
|
| 105 |
+
# Vérification de la texture
|
| 106 |
+
if garment_info["texture"] == texture:
|
| 107 |
+
score += 30
|
| 108 |
|
| 109 |
+
# Score de base
|
| 110 |
+
score += garment_info["confidence"] / 2
|
| 111 |
+
|
| 112 |
+
if score > best_score:
|
| 113 |
+
best_score = score
|
| 114 |
+
best_match = garment_info
|
| 115 |
+
|
| 116 |
+
if best_match:
|
| 117 |
+
# Ajustement final de la confiance
|
| 118 |
+
final_confidence = min(98, best_score)
|
| 119 |
+
return best_match["name"], final_confidence
|
| 120 |
+
|
| 121 |
+
return "👔 Vêtement", 75
|
| 122 |
+
|
| 123 |
except Exception as e:
|
| 124 |
+
print(f"Erreur détection: {e}")
|
| 125 |
+
return "👔 Vêtement", 70
|
| 126 |
|
| 127 |
+
def analyze_garment_details(image):
|
| 128 |
+
"""Analyse détaillée pour plus de précision"""
|
| 129 |
try:
|
| 130 |
img_array = np.array(image.convert('L'))
|
| 131 |
height, width = img_array.shape
|
| 132 |
|
| 133 |
+
# Analyse des contours
|
| 134 |
+
gradient_x = np.abs(np.gradient(img_array, axis=1))
|
| 135 |
+
gradient_y = np.abs(np.gradient(img_array, axis=0))
|
| 136 |
+
edge_score = np.mean(gradient_x) + np.mean(gradient_y)
|
| 137 |
|
| 138 |
+
# Détection de la complexité
|
| 139 |
+
complexity = np.std(img_array)
|
| 140 |
|
| 141 |
+
garment_type, base_confidence = detect_garment_type(image)
|
| 142 |
|
| 143 |
+
# Ajustements basés sur l'analyse avancée
|
| 144 |
+
if "Jean" in garment_type and complexity > 45:
|
| 145 |
+
garment_type = "👖 Jean"
|
| 146 |
+
base_confidence += 5
|
| 147 |
+
elif "T-shirt" in garment_type and complexity < 30:
|
| 148 |
+
garment_type = "👕 T-shirt uni"
|
| 149 |
+
base_confidence += 3
|
| 150 |
+
elif "Chemise" in garment_type and edge_score > 25:
|
| 151 |
+
garment_type = "👔 Chemise structurée"
|
| 152 |
+
base_confidence += 4
|
|
|
|
| 153 |
|
| 154 |
+
return garment_type, min(99, base_confidence)
|
| 155 |
|
| 156 |
except:
|
| 157 |
+
return detect_garment_type(image)
|
| 158 |
|
| 159 |
def classify_clothing(image):
|
| 160 |
"""Classification précise sans hallucinations"""
|
|
|
|
| 162 |
if image is None:
|
| 163 |
return "❌ Veuillez uploader une image de vêtement"
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
# Conversion image
|
| 166 |
if isinstance(image, str):
|
| 167 |
pil_image = Image.open(image).convert('RGB')
|
| 168 |
else:
|
| 169 |
pil_image = image.convert('RGB')
|
| 170 |
|
| 171 |
+
# 🔍 ANALYSE PRÉCISE
|
| 172 |
+
garment_type, confidence = analyze_garment_details(pil_image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
output = f"""## 🎯 RÉSULTAT DE L'ANALYSE
|
| 175 |
|
| 176 |
+
### 🔍 TYPE DE VÊTEMENT IDENTIFIÉ:
|
| 177 |
+
**{garment_type}** - {confidence:.1f}% de confiance
|
| 178 |
+
|
| 179 |
+
### 📊 CARACTÉRISTIQUES DÉTECTÉTES:
|
| 180 |
+
• **Forme et silhouette** analysée
|
| 181 |
+
• **Texture et structure** évaluée
|
| 182 |
+
• **Ratio dimensionnel** calculé
|
| 183 |
|
| 184 |
+
### 🎯 NIVEAU DE CONFIANCE:
|
| 185 |
+
{"🔒 Très élevé" if confidence > 90 else "🔍 Élevé" if confidence > 80 else "✅ Bon" if confidence > 70 else "⚠️ Moyen"}
|
|
|
|
|
|
|
| 186 |
|
| 187 |
+
### 💡 CONSEILS POUR UNE PRÉCISION MAXIMALE:
|
| 188 |
+
• 📷 Photo nette et bien cadrée
|
| 189 |
+
• 🎯 Un seul vêtement visible
|
| 190 |
+
• 🌞 Bon éclairage sans ombres
|
| 191 |
+
• 🧹 Fond uni de préférence
|
| 192 |
|
| 193 |
+
### 🚫 CE SYSTÈME NE FAIT PAS:
|
| 194 |
+
• ❌ d'hallucinations entre les types
|
| 195 |
+
• ❌ de suppositions aléatoires
|
| 196 |
+
• ❌ de reconnaissance de couleur
|
|
|
|
| 197 |
"""
|
| 198 |
|
| 199 |
return output
|
|
|
|
| 201 |
except Exception as e:
|
| 202 |
return f"❌ Erreur d'analyse: {str(e)}"
|
| 203 |
|
| 204 |
+
# 🎨 INTERFACE GRADIO
|
| 205 |
with gr.Blocks(title="Reconnaissance Expert de Vêtements", theme=gr.themes.Soft()) as demo:
|
| 206 |
|
| 207 |
gr.Markdown("""
|
| 208 |
+
# 👔 SYSTÈME EXPERT DE RECONNAISSANCE VESTIMENTAIRE
|
| 209 |
+
*Analyse précise par forme, texture et dimensions*
|
| 210 |
""")
|
| 211 |
|
| 212 |
with gr.Row():
|
|
|
|
| 214 |
gr.Markdown("### 📤 UPLOADER UN VÊTEMENT")
|
| 215 |
image_input = gr.Image(
|
| 216 |
type="pil",
|
| 217 |
+
label="Sélectionnez votre vêtement",
|
| 218 |
height=300,
|
| 219 |
sources=["upload"],
|
| 220 |
)
|
| 221 |
|
| 222 |
gr.Markdown("""
|
| 223 |
+
### 🎯 POUR DES RÉSULTATS OPTIMAUX:
|
| 224 |
+
✅ **Un vêtement à la fois**
|
| 225 |
+
✅ **Cadrage serré et net**
|
| 226 |
+
✅ **Éclairage uniforme**
|
| 227 |
+
✅ **Fond neutre**
|
| 228 |
⏱️ **Analyse instantanée**
|
| 229 |
""")
|
| 230 |
|
| 231 |
analyze_btn = gr.Button("🔍 Analyser avec précision", variant="primary")
|
| 232 |
+
clear_btn = gr.Button("🧹 Nouvelle analyse", variant="secondary")
|
| 233 |
|
| 234 |
with gr.Column(scale=2):
|
| 235 |
+
gr.Markdown("### 📊 RAPPORT D'ANALYSE DÉTAILLÉ")
|
| 236 |
output_text = gr.Markdown(
|
| 237 |
+
value="⬅️ Uploader un vêtement pour commencer l'analyse"
|
| 238 |
)
|
| 239 |
|
| 240 |
# 🎮 INTERACTIONS
|