MODLI commited on
Commit
42ebc82
·
verified ·
1 Parent(s): f69ea27

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -75
app.py CHANGED
@@ -1,125 +1,168 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
- from PIL import Image, ImageOps, ImageFilter
4
  import numpy as np
5
- import cv2
6
 
7
- # Catégories précises et distinctes
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
- # Chargement des modèles
19
- print("🔧 Loading models...")
20
- seg_pipe = pipeline("image-segmentation", model="mattmdjaga/segformer_b2_clothes")
21
- class_pipe = pipeline("zero-shot-image-classification", model="openai/clip-vit-base-patch32")
22
- print("✅ Models loaded!")
 
23
 
24
- def preprocess_image(image):
25
- """Prétraitement avancé de l'image"""
26
- # Conversion en numpy array si nécessaire
27
- if isinstance(image, np.ndarray):
28
- image = Image.fromarray(image)
29
-
30
- # Réduction de taille pour meilleures performances
31
- image = image.resize((512, 512))
32
-
33
- return image
34
 
35
- def remove_background(image):
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 classify_fashion(image):
54
- """Classification précise"""
55
  try:
56
- # Préprocessing
57
- processed_image = preprocess_image(image)
 
 
 
 
58
 
59
- # Suppression de l'arrière-plan
60
- isolated_image = remove_background(processed_image)
61
 
62
- # Classification avec paramètres optimisés
63
  predictions = class_pipe(
64
- isolated_image,
65
  candidate_labels=FASHION_CATEGORIES,
66
- hypothesis_template="a clear photo of {}",
67
  multi_label=False
68
  )
69
 
70
- # Filtrage des résultats (seulement > 10% de confiance)
71
- filtered_predictions = [p for p in predictions if p['score'] > 0.1]
 
 
 
 
 
 
72
 
73
- if not filtered_predictions:
74
- return "❌ No confident prediction. Try a clearer image.", isolated_image
75
 
76
  # Formatage des résultats
77
- result_text = "🎯 **Top Predictions:**\n\n"
78
- for i, pred in enumerate(filtered_predictions[:3]):
 
 
79
  result_text += f"{i+1}. **{pred['label']}**: {pred['score']*100:.1f}%\n"
80
 
81
- return result_text, isolated_image
 
 
 
 
 
 
 
 
 
82
 
83
  except Exception as e:
84
  return f"❌ Error: {str(e)}", None
85
 
86
- # Interface améliorée
87
- with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {max-width: 800px;}") as demo:
88
- gr.Markdown("# 👗 **Fashion AI - Professional Classifier**")
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
  with gr.Row():
91
- with gr.Column():
92
  gr.Markdown("### 📤 Upload Image")
93
- image_input = gr.Image(type="pil", label="Clothing Image")
94
- process_btn = gr.Button("🚀 Analyze Image", variant="primary")
 
 
 
 
 
 
 
 
95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  with gr.Column():
97
- gr.Markdown("### 📊 Results")
98
- output_text = gr.Markdown(label="Analysis")
99
- output_image = gr.Image(label="Processed Image", interactive=False)
 
 
 
 
 
100
 
101
- # Instructions
102
- gr.Markdown("""
103
- ### 💡 **Pro Tips for Best Results:**
104
- - Use clear, well-lit photos
105
- - Center the clothing item
106
- - Use plain backgrounds when possible
107
- - Avoid multiple items in one photo
108
- - ❌ Don't use blurry or dark images
109
- """)
110
 
111
- # Events
112
- process_btn.click(
113
- fn=classify_fashion,
114
  inputs=image_input,
115
  outputs=[output_text, output_image]
116
  )
117
 
118
  image_input.upload(
119
- fn=classify_fashion,
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
+ )