Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,125 +1,168 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
-
from PIL import Image
|
| 4 |
import numpy as np
|
| 5 |
-
import cv2
|
| 6 |
|
| 7 |
-
# Catégories
|
| 8 |
FASHION_CATEGORIES = [
|
| 9 |
"t-shirt", "button-down shirt", "polo shirt",
|
| 10 |
"sweatshirt", "hoodie", "sweater",
|
| 11 |
"jacket", "coat", "blazer",
|
| 12 |
"dress", "long dress", "short dress",
|
| 13 |
"skirt", "long skirt", "short skirt",
|
| 14 |
-
"jeans", "pants", "shorts",
|
| 15 |
"sneakers", "boots", "heels", "sandals"
|
| 16 |
]
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
# Réduction de taille pour meilleures performances
|
| 31 |
-
image = image.resize((512, 512))
|
| 32 |
-
|
| 33 |
-
return image
|
| 34 |
|
| 35 |
-
|
| 36 |
-
"""Suppression professionnelle de l'arrière-plan"""
|
| 37 |
-
# Segmentation
|
| 38 |
-
segments = seg_pipe(image)
|
| 39 |
-
if not segments:
|
| 40 |
-
return image
|
| 41 |
-
|
| 42 |
-
# Trouver le vêtement principal
|
| 43 |
-
largest_segment = max(segments, key=lambda x: np.sum(x['mask']))
|
| 44 |
-
mask = np.array(largest_segment['mask'])
|
| 45 |
-
|
| 46 |
-
# Application du masque
|
| 47 |
-
image_np = np.array(image)
|
| 48 |
-
masked_image = np.zeros_like(image_np)
|
| 49 |
-
masked_image[mask > 0] = image_np[mask > 0]
|
| 50 |
-
|
| 51 |
-
return Image.fromarray(masked_image)
|
| 52 |
|
| 53 |
-
def
|
| 54 |
-
"""
|
| 55 |
try:
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
|
| 62 |
-
#
|
| 63 |
predictions = class_pipe(
|
| 64 |
-
|
| 65 |
candidate_labels=FASHION_CATEGORIES,
|
| 66 |
-
hypothesis_template="a
|
| 67 |
multi_label=False
|
| 68 |
)
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
if not
|
| 74 |
-
return "❌ No confident prediction. Try a clearer image.",
|
| 75 |
|
| 76 |
# Formatage des résultats
|
| 77 |
-
result_text = "🎯 **
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
result_text += f"{i+1}. **{pred['label']}**: {pred['score']*100:.1f}%\n"
|
| 80 |
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
except Exception as e:
|
| 84 |
return f"❌ Error: {str(e)}", None
|
| 85 |
|
| 86 |
-
# Interface
|
| 87 |
-
with gr.Blocks(
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
with gr.Row():
|
| 91 |
-
with gr.Column():
|
| 92 |
gr.Markdown("### 📤 Upload Image")
|
| 93 |
-
image_input = gr.Image(
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
with gr.Column():
|
| 97 |
-
gr.Markdown("
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
-
#
|
| 102 |
-
gr.
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
""")
|
| 110 |
|
| 111 |
-
#
|
| 112 |
-
|
| 113 |
-
fn=
|
| 114 |
inputs=image_input,
|
| 115 |
outputs=[output_text, output_image]
|
| 116 |
)
|
| 117 |
|
| 118 |
image_input.upload(
|
| 119 |
-
fn=
|
| 120 |
inputs=image_input,
|
| 121 |
outputs=[output_text, output_image]
|
| 122 |
)
|
| 123 |
|
| 124 |
if __name__ == "__main__":
|
| 125 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
|
| 6 |
+
# Catégories de vêtements bien définies
|
| 7 |
FASHION_CATEGORIES = [
|
| 8 |
"t-shirt", "button-down shirt", "polo shirt",
|
| 9 |
"sweatshirt", "hoodie", "sweater",
|
| 10 |
"jacket", "coat", "blazer",
|
| 11 |
"dress", "long dress", "short dress",
|
| 12 |
"skirt", "long skirt", "short skirt",
|
| 13 |
+
"jeans", "pants", "shorts", "leggings",
|
| 14 |
"sneakers", "boots", "heels", "sandals"
|
| 15 |
]
|
| 16 |
|
| 17 |
+
print("🔧 Loading classification model...")
|
| 18 |
+
# Modèle principal pour l'analyse globale
|
| 19 |
+
class_pipe = pipeline(
|
| 20 |
+
"zero-shot-image-classification",
|
| 21 |
+
model="openai/clip-vit-base-patch32"
|
| 22 |
+
)
|
| 23 |
|
| 24 |
+
# Modèle de secours pour confirmation
|
| 25 |
+
backup_pipe = pipeline(
|
| 26 |
+
"image-classification",
|
| 27 |
+
model="google/vit-base-patch16-224"
|
| 28 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
print("✅ Models loaded successfully!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
def analyze_complete_image(image):
|
| 33 |
+
"""Analyse l'image ENTIÈRE sans segmentation"""
|
| 34 |
try:
|
| 35 |
+
if image is None:
|
| 36 |
+
return "❌ Please upload an image first", None
|
| 37 |
+
|
| 38 |
+
# Conversion en PIL Image si nécessaire
|
| 39 |
+
if isinstance(image, np.ndarray):
|
| 40 |
+
image = Image.fromarray(image)
|
| 41 |
|
| 42 |
+
# Réduction de taille pour de meilleures performances
|
| 43 |
+
image = image.resize((512, 512))
|
| 44 |
|
| 45 |
+
# 🔥 ANALYSE PRINCIPALE - Image entière
|
| 46 |
predictions = class_pipe(
|
| 47 |
+
image,
|
| 48 |
candidate_labels=FASHION_CATEGORIES,
|
| 49 |
+
hypothesis_template="a complete photo of {}",
|
| 50 |
multi_label=False
|
| 51 |
)
|
| 52 |
|
| 53 |
+
# 🔥 ANALYSE DE CONFIRMATION avec modèle secondaire
|
| 54 |
+
backup_preds = backup_pipe(image)
|
| 55 |
+
|
| 56 |
+
# Filtrage des résultats peu confidentiels
|
| 57 |
+
confident_predictions = [
|
| 58 |
+
p for p in predictions
|
| 59 |
+
if p['score'] > 0.15 # Seuil de confiance augmenté
|
| 60 |
+
]
|
| 61 |
|
| 62 |
+
if not confident_predictions:
|
| 63 |
+
return "❌ No confident prediction. Try a clearer image.", image
|
| 64 |
|
| 65 |
# Formatage des résultats
|
| 66 |
+
result_text = "🎯 **Fashion Analysis Results:**\n\n"
|
| 67 |
+
result_text += "**Main predictions:**\n"
|
| 68 |
+
|
| 69 |
+
for i, pred in enumerate(confident_predictions[:3]):
|
| 70 |
result_text += f"{i+1}. **{pred['label']}**: {pred['score']*100:.1f}%\n"
|
| 71 |
|
| 72 |
+
# Ajouter la prédiction du modèle de secours
|
| 73 |
+
if backup_preds:
|
| 74 |
+
result_text += f"\n**Secondary model suggests**: {backup_preds[0]['label']}\n"
|
| 75 |
+
result_text += f"**Confidence**: {backup_preds[0]['score']*100:.1f}%"
|
| 76 |
+
|
| 77 |
+
# Conseils basés sur la prédiction
|
| 78 |
+
top_pred = confident_predictions[0]['label']
|
| 79 |
+
result_text += f"\n\n💡 **Tip**: For better accuracy, make sure the {top_pred} is clearly visible and centered."
|
| 80 |
+
|
| 81 |
+
return result_text, image
|
| 82 |
|
| 83 |
except Exception as e:
|
| 84 |
return f"❌ Error: {str(e)}", None
|
| 85 |
|
| 86 |
+
# Interface optimisée
|
| 87 |
+
with gr.Blocks(
|
| 88 |
+
title="Fashion AI - Complete Image Analysis",
|
| 89 |
+
theme=gr.themes.Soft(),
|
| 90 |
+
css="""
|
| 91 |
+
.gradio-container { max-width: 900px; margin: auto; }
|
| 92 |
+
.header { text-align: center; margin-bottom: 20px; }
|
| 93 |
+
"""
|
| 94 |
+
) as demo:
|
| 95 |
+
|
| 96 |
+
gr.Markdown("""
|
| 97 |
+
<div class='header'>
|
| 98 |
+
<h1>👗 Fashion AI - Complete Image Analysis</h1>
|
| 99 |
+
<p>Analyzes the ENTIRE image without cropping or segmentation</p>
|
| 100 |
+
</div>
|
| 101 |
+
""")
|
| 102 |
|
| 103 |
with gr.Row():
|
| 104 |
+
with gr.Column(scale=1):
|
| 105 |
gr.Markdown("### 📤 Upload Image")
|
| 106 |
+
image_input = gr.Image(
|
| 107 |
+
type="pil",
|
| 108 |
+
label="Upload Complete Image",
|
| 109 |
+
height=300
|
| 110 |
+
)
|
| 111 |
+
analyze_btn = gr.Button(
|
| 112 |
+
"🔍 Analyze Complete Image",
|
| 113 |
+
variant="primary",
|
| 114 |
+
size="lg"
|
| 115 |
+
)
|
| 116 |
|
| 117 |
+
with gr.Column(scale=1):
|
| 118 |
+
gr.Markdown("### 📊 Analysis Results")
|
| 119 |
+
output_text = gr.Markdown(
|
| 120 |
+
label="Results",
|
| 121 |
+
show_label=False
|
| 122 |
+
)
|
| 123 |
+
output_image = gr.Image(
|
| 124 |
+
label="Original Image (for reference)",
|
| 125 |
+
interactive=False,
|
| 126 |
+
height=300
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
# Section d'instructions
|
| 130 |
+
with gr.Row():
|
| 131 |
with gr.Column():
|
| 132 |
+
gr.Markdown("""
|
| 133 |
+
### 💡 Best Practices:
|
| 134 |
+
- ✅ **Full garment visible** - don't crop
|
| 135 |
+
- ✅ **Good lighting** - no shadows
|
| 136 |
+
- ✅ **Neutral background** - less distraction
|
| 137 |
+
- ✅ **Single item** - one piece per photo
|
| 138 |
+
- ✅ **Clear view** - front angle preferred
|
| 139 |
+
""")
|
| 140 |
|
| 141 |
+
# Section d'exemples
|
| 142 |
+
with gr.Row():
|
| 143 |
+
gr.Markdown("""
|
| 144 |
+
### 🎯 Examples of good images:
|
| 145 |
+
- Full t-shirt visible on plain background
|
| 146 |
+
- Complete dress without cropping
|
| 147 |
+
- Entire pair of jeans clearly visible
|
| 148 |
+
""")
|
|
|
|
| 149 |
|
| 150 |
+
# Événements
|
| 151 |
+
analyze_btn.click(
|
| 152 |
+
fn=analyze_complete_image,
|
| 153 |
inputs=image_input,
|
| 154 |
outputs=[output_text, output_image]
|
| 155 |
)
|
| 156 |
|
| 157 |
image_input.upload(
|
| 158 |
+
fn=analyze_complete_image,
|
| 159 |
inputs=image_input,
|
| 160 |
outputs=[output_text, output_image]
|
| 161 |
)
|
| 162 |
|
| 163 |
if __name__ == "__main__":
|
| 164 |
+
demo.launch(
|
| 165 |
+
server_name="0.0.0.0",
|
| 166 |
+
server_port=7860,
|
| 167 |
+
share=False
|
| 168 |
+
)
|