Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,6 @@ import requests
|
|
| 7 |
from io import BytesIO
|
| 8 |
import numpy as np
|
| 9 |
import os
|
| 10 |
-
from pathlib import Path
|
| 11 |
import tempfile
|
| 12 |
|
| 13 |
# 🔥 MODÈLE SPÉCIALISÉ DANS LA MODE
|
|
@@ -30,32 +29,18 @@ except Exception as e:
|
|
| 30 |
processor = None
|
| 31 |
model = None
|
| 32 |
|
| 33 |
-
# 🎯 LABELS COMPRÉHENSIBLES POUR LA MODE (
|
| 34 |
FASHION_LABELS = {
|
| 35 |
0: "T-shirt", 1: "Pantalon", 2: "Pull", 3: "Robe", 4: "Manteau",
|
| 36 |
5: "Sandale", 6: "Chemise", 7: "Sneaker", 8: "Sac", 9: "Botte",
|
| 37 |
10: "Veste", 11: "Jupe", 12: "Short", 13: "Chaussures", 14: "Accessoire"
|
| 38 |
-
}
|
| 39 |
-
|
| 40 |
-
def convert_heic_to_jpeg(image_path):
|
| 41 |
-
"""Convertit les HEIC en JPEG si nécessaire"""
|
| 42 |
-
try:
|
| 43 |
-
if isinstance(image_path, str) and image_path.lower().endswith('.heic'):
|
| 44 |
-
# Conversion HEIC → JPEG
|
| 45 |
-
img = Image.open(image_path)
|
| 46 |
-
jpeg_path = image_path.replace('.heic', '.jpeg')
|
| 47 |
-
img.convert('RGB').save(jpeg_path, 'JPEG')
|
| 48 |
-
return jpeg_path
|
| 49 |
-
except:
|
| 50 |
-
pass
|
| 51 |
-
return image_path
|
| 52 |
|
| 53 |
def preprocess_image(image):
|
| 54 |
"""Prétraitement robuste des images"""
|
| 55 |
try:
|
| 56 |
-
# Si c'est un chemin de fichier
|
| 57 |
if isinstance(image, str):
|
| 58 |
-
image = convert_heic_to_jpeg(image)
|
| 59 |
image = Image.open(image)
|
| 60 |
|
| 61 |
# Conversion en RGB
|
|
@@ -79,23 +64,13 @@ def classify_fashion(image):
|
|
| 79 |
if processor is None or model is None:
|
| 80 |
return "⚠️ Modèle en cours de chargement... Patientez 30s"
|
| 81 |
|
| 82 |
-
# 📸 Gestion
|
| 83 |
try:
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
if image.lower().endswith('.heic'):
|
| 87 |
-
# Conversion HEIC → JPEG
|
| 88 |
-
img = Image.open(image)
|
| 89 |
-
with tempfile.NamedTemporaryFile(suffix='.jpg', delete=False) as tmp:
|
| 90 |
-
img.convert('RGB').save(tmp.name, 'JPEG', quality=95)
|
| 91 |
-
processed_image = Image.open(tmp.name)
|
| 92 |
-
os.unlink(tmp.name) # Nettoyage
|
| 93 |
-
else:
|
| 94 |
-
processed_image = Image.open(image)
|
| 95 |
else:
|
| 96 |
-
# Image normale
|
| 97 |
processed_image = image
|
| 98 |
-
|
| 99 |
# Conversion en RGB si nécessaire
|
| 100 |
if processed_image.mode != 'RGB':
|
| 101 |
processed_image = processed_image.convert('RGB')
|
|
@@ -122,4 +97,94 @@ def classify_fashion(image):
|
|
| 122 |
results = []
|
| 123 |
for i in range(len(top_indices[0])):
|
| 124 |
label_idx = top_indices[0][i].item()
|
| 125 |
-
label_name = FASHION_LABELS.get(label_idx, f"Catégorie {label_idx}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from io import BytesIO
|
| 8 |
import numpy as np
|
| 9 |
import os
|
|
|
|
| 10 |
import tempfile
|
| 11 |
|
| 12 |
# 🔥 MODÈLE SPÉCIALISÉ DANS LA MODE
|
|
|
|
| 29 |
processor = None
|
| 30 |
model = None
|
| 31 |
|
| 32 |
+
# 🎯 LABELS COMPRÉHENSIBLES POUR LA MODE (CORRIGÉ)
|
| 33 |
FASHION_LABELS = {
|
| 34 |
0: "T-shirt", 1: "Pantalon", 2: "Pull", 3: "Robe", 4: "Manteau",
|
| 35 |
5: "Sandale", 6: "Chemise", 7: "Sneaker", 8: "Sac", 9: "Botte",
|
| 36 |
10: "Veste", 11: "Jupe", 12: "Short", 13: "Chaussures", 14: "Accessoire"
|
| 37 |
+
} # ✅ PARENTHÈSE FERMANTE AJOUTÉE ICI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
def preprocess_image(image):
|
| 40 |
"""Prétraitement robuste des images"""
|
| 41 |
try:
|
| 42 |
+
# Si c'est un chemin de fichier
|
| 43 |
if isinstance(image, str):
|
|
|
|
| 44 |
image = Image.open(image)
|
| 45 |
|
| 46 |
# Conversion en RGB
|
|
|
|
| 64 |
if processor is None or model is None:
|
| 65 |
return "⚠️ Modèle en cours de chargement... Patientez 30s"
|
| 66 |
|
| 67 |
+
# 📸 Gestion de l'image
|
| 68 |
try:
|
| 69 |
+
if isinstance(image, str):
|
| 70 |
+
processed_image = Image.open(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
else:
|
|
|
|
| 72 |
processed_image = image
|
| 73 |
+
|
| 74 |
# Conversion en RGB si nécessaire
|
| 75 |
if processed_image.mode != 'RGB':
|
| 76 |
processed_image = processed_image.convert('RGB')
|
|
|
|
| 97 |
results = []
|
| 98 |
for i in range(len(top_indices[0])):
|
| 99 |
label_idx = top_indices[0][i].item()
|
| 100 |
+
label_name = FASHION_LABELS.get(label_idx, f"Catégorie {label_idx}")
|
| 101 |
+
score = top_probs[0][i].item() * 100
|
| 102 |
+
results.append({"label": label_name, "score": score})
|
| 103 |
+
|
| 104 |
+
# 📋 AFFICHAGE DES RÉSULTATS
|
| 105 |
+
output = "## 🎯 RÉSULTATS DE CLASSIFICATION:\n\n"
|
| 106 |
+
|
| 107 |
+
for i, result in enumerate(results):
|
| 108 |
+
if result['score'] > 5: # Seuil minimal de 5%
|
| 109 |
+
output += f"{i+1}. **{result['label']}** - {result['score']:.1f}%\n"
|
| 110 |
+
|
| 111 |
+
output += f"\n---\n"
|
| 112 |
+
output += f"📊 **Format supporté:** JPEG, PNG, WebP\n"
|
| 113 |
+
output += f"⚠️ **Format non supporté:** HEIC (Apple)\n"
|
| 114 |
+
|
| 115 |
+
output += "\n💡 **Conseils:**\n"
|
| 116 |
+
output += "• Convertir HEIC en JPEG avec votre téléphone\n"
|
| 117 |
+
output += "• Utiliser des images nettes et bien éclairées\n"
|
| 118 |
+
output += "• Cadrer le vêtement au centre\n"
|
| 119 |
+
|
| 120 |
+
return output
|
| 121 |
+
|
| 122 |
+
except Exception as e:
|
| 123 |
+
return f"❌ Erreur de traitement: {str(e)}\n\n🔧 Contactez le support si le problème persiste"
|
| 124 |
+
|
| 125 |
+
# 🖼️ EXEMPLES DE TEST (URLs fiables)
|
| 126 |
+
EXAMPLE_URLS = [
|
| 127 |
+
"https://images.unsplash.com/photo-1558769132-cb1aea458c5e?w=400", # T-shirt
|
| 128 |
+
"https://images.unsplash.com/photo-1594633312681-425c7b97ccd1?w=400", # Robe
|
| 129 |
+
"https://images.unsplash.com/photo-1529111290557-82f6d5c6cf85?w=400", # Chemise
|
| 130 |
+
]
|
| 131 |
+
|
| 132 |
+
# 🎨 INTERFACE SIMPLIFIÉE
|
| 133 |
+
with gr.Blocks(title="Classificateur de Vêtements", theme=gr.themes.Soft()) as demo:
|
| 134 |
+
|
| 135 |
+
gr.Markdown("""
|
| 136 |
+
# 👗 CLASSIFICATEUR DE VÊTEMENTS
|
| 137 |
+
*Supporte: JPEG, PNG, WebP | ❌ HEIC non supporté*
|
| 138 |
+
""")
|
| 139 |
+
|
| 140 |
+
with gr.Row():
|
| 141 |
+
with gr.Column(scale=1):
|
| 142 |
+
gr.Markdown("### 📤 UPLOADER UNE IMAGE")
|
| 143 |
+
image_input = gr.Image(
|
| 144 |
+
type="filepath",
|
| 145 |
+
label="Sélectionnez une image",
|
| 146 |
+
height=300,
|
| 147 |
+
sources=["upload"],
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
gr.Markdown("""
|
| 151 |
+
### 📋 FORMATS SUPPORTÉS
|
| 152 |
+
✅ **JPEG** - Recommandé
|
| 153 |
+
✅ **PNG** - Supporté
|
| 154 |
+
✅ **WebP** - Supporté
|
| 155 |
+
❌ **HEIC** - Non supporté (format Apple)
|
| 156 |
+
""")
|
| 157 |
+
|
| 158 |
+
classify_btn = gr.Button("🚀 Classifier", variant="primary")
|
| 159 |
+
|
| 160 |
+
with gr.Column(scale=2):
|
| 161 |
+
gr.Markdown("### 📊 RÉSULTATS")
|
| 162 |
+
output_text = gr.Markdown(
|
| 163 |
+
value="⬅️ Uploader une image JPEG, PNG ou WebP"
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
# 🎯 EXEMPLES
|
| 167 |
+
gr.Markdown("### 🖼️ EXEMPLES DE TEST")
|
| 168 |
+
gr.Examples(
|
| 169 |
+
examples=EXAMPLE_URLS,
|
| 170 |
+
inputs=image_input,
|
| 171 |
+
outputs=output_text,
|
| 172 |
+
fn=classify_fashion,
|
| 173 |
+
label="Cliquez sur un exemple pour tester"
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
# 🎮 INTERACTION
|
| 177 |
+
classify_btn.click(
|
| 178 |
+
fn=classify_fashion,
|
| 179 |
+
inputs=[image_input],
|
| 180 |
+
outputs=output_text
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
# ⚙️ CONFIGURATION
|
| 184 |
+
if __name__ == "__main__":
|
| 185 |
+
demo.launch(
|
| 186 |
+
server_name="0.0.0.0",
|
| 187 |
+
server_port=7860,
|
| 188 |
+
share=False,
|
| 189 |
+
debug=True
|
| 190 |
+
)
|