Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
import cv2
|
| 6 |
+
from categories import FASHION_CATEGORIES # Importe ta liste de catégories
|
| 7 |
+
|
| 8 |
+
# --- Configuration et Chargement des Modèles ---
|
| 9 |
+
# Ces modèles sont chargés une seule fois au démarrage de l'application
|
| 10 |
+
print("Loading segmentation model... This might take a minute.")
|
| 11 |
+
# Modèle pour détourer et isoler le vêtement
|
| 12 |
+
seg_pipe = pipeline("image-segmentation", model="mattmdjaga/segformer_b2_clothes")
|
| 13 |
+
|
| 14 |
+
print("Loading fashion classification model...")
|
| 15 |
+
# Modèle de classification spécialisé dans la mode (FashionCLIP)
|
| 16 |
+
class_pipe = pipeline("zero-shot-image-classification", model="edwardjross/fashion-clip")
|
| 17 |
+
print("Models loaded successfully!")
|
| 18 |
+
|
| 19 |
+
# --- Fonctions de Prétraitement ---
|
| 20 |
+
def get_largest_segment(segments):
|
| 21 |
+
"""Trouve le segment le plus grand (le vêtement principal) parmi les résultats de segmentation."""
|
| 22 |
+
largest_area = 0
|
| 23 |
+
largest_segment = None
|
| 24 |
+
for segment in segments:
|
| 25 |
+
mask = segment['mask']
|
| 26 |
+
area = np.sum(mask) # Calcule la surface du masque
|
| 27 |
+
if area > largest_area:
|
| 28 |
+
largest_area = area
|
| 29 |
+
largest_segment = segment
|
| 30 |
+
return largest_segment
|
| 31 |
+
|
| 32 |
+
def crop_to_mask(image, mask):
|
| 33 |
+
"""
|
| 34 |
+
Recadre l'image sur la zone délimitée par le masque.
|
| 35 |
+
Cela permet de supprimer tout le fond inutile.
|
| 36 |
+
"""
|
| 37 |
+
mask_np = np.array(mask)
|
| 38 |
+
# Trouve les coordonnées des pixels blancs du masque
|
| 39 |
+
y, x = np.where(mask_np > 0)
|
| 40 |
+
if len(x) == 0 or len(y) == 0:
|
| 41 |
+
return image # Retourne l'image originale si le masque est vide
|
| 42 |
+
|
| 43 |
+
x_min, x_max = np.min(x), np.max(x)
|
| 44 |
+
y_min, y_max = np.min(y), np.max(y)
|
| 45 |
+
|
| 46 |
+
# Recadre l'image
|
| 47 |
+
cropped_image = image.crop((x_min, y_min, x_max, y_max))
|
| 48 |
+
return cropped_image
|
| 49 |
+
|
| 50 |
+
# --- Fonction Principale de Classification ---
|
| 51 |
+
def classify_image(input_image):
|
| 52 |
+
"""
|
| 53 |
+
Fonction appelée par Gradio quand l'utilisateur upload une image.
|
| 54 |
+
1. Segmentation pour isoler le vêtement.
|
| 55 |
+
2. Classification sur le vêtement isolé.
|
| 56 |
+
"""
|
| 57 |
+
# Convertit l'image Gradio en PIL Image
|
| 58 |
+
pil_image = Image.fromarray(input_image)
|
| 59 |
+
|
| 60 |
+
# ÉTAPE 1: SEGMENTATION
|
| 61 |
+
segments = seg_pipe(pil_image)
|
| 62 |
+
main_item = get_largest_segment(segments)
|
| 63 |
+
|
| 64 |
+
if main_item is None:
|
| 65 |
+
return "No clothing item detected. Please try another image."
|
| 66 |
+
|
| 67 |
+
# ÉTAPE 2: PRÉTRAITEMENT - On isole le vêtement
|
| 68 |
+
isolated_image = crop_to_mask(pil_image, main_item['mask'])
|
| 69 |
+
|
| 70 |
+
# ÉTAPE 3: CLASSIFICATION FINE
|
| 71 |
+
# On utilise le modèle FashionCLIP pour comparer l'image à notre liste de catégories
|
| 72 |
+
predictions = class_pipe(isolated_image, candidate_labels=FASHION_CATEGORIES)
|
| 73 |
+
|
| 74 |
+
# Formatage des résultats pour l'affichage
|
| 75 |
+
result_text = "Classification Results (on isolated garment):\n"
|
| 76 |
+
for pred in predictions:
|
| 77 |
+
# Affiche le score en pourcentage
|
| 78 |
+
result_text += f"- {pred['label']}: {pred['score']*100:.2f}%\n"
|
| 79 |
+
|
| 80 |
+
# On retourne aussi l'image isolée pour debugger et voir ce que l'IA a vraiment analysé
|
| 81 |
+
return result_text, isolated_image
|
| 82 |
+
|
| 83 |
+
# --- Interface Gradio ---
|
| 84 |
+
# Création de l'interface utilisateur
|
| 85 |
+
with gr.Blocks(title="Fashion Category Classifier") as demo:
|
| 86 |
+
gr.Markdown("# 👗 Fashion Category Classifier")
|
| 87 |
+
gr.Markdown("Upload a picture of a clothing item. The AI will isolate it and classify it.")
|
| 88 |
+
|
| 89 |
+
with gr.Row():
|
| 90 |
+
with gr.Column():
|
| 91 |
+
image_input = gr.Image(label="Upload Clothing Image", type="numpy")
|
| 92 |
+
classify_btn = gr.Button("Classify!")
|
| 93 |
+
|
| 94 |
+
with gr.Column():
|
| 95 |
+
label_output = gr.Textbox(label="Classification Results", lines=6)
|
| 96 |
+
image_output = gr.Image(label="Isolated Garment (what the AI analyzed)", type="pil")
|
| 97 |
+
|
| 98 |
+
# Lie le bouton à la fonction
|
| 99 |
+
classify_btn.click(
|
| 100 |
+
fn=classify_image,
|
| 101 |
+
inputs=image_input,
|
| 102 |
+
outputs=[label_output, image_output]
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
# Quelques exemples pour que l'utilisateur teste facilement
|
| 106 |
+
gr.Examples(
|
| 107 |
+
examples=[ # Tu devras ajouter tes propres exemples d'images
|
| 108 |
+
"examples/t-shirt.jpg",
|
| 109 |
+
"examples/dress.jpg",
|
| 110 |
+
"examples/jacket.jpg"
|
| 111 |
+
],
|
| 112 |
+
inputs=image_input
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
# Lance l'application
|
| 116 |
+
if __name__ == "__main__":
|
| 117 |
+
demo.launch(debug=True)
|