noxo8888 commited on
Commit
9c39d26
Β·
verified Β·
1 Parent(s): 128eced

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +209 -0
app.py ADDED
@@ -0,0 +1,209 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from diffusers import StableDiffusionInpaintPipeline, StableDiffusionXLInpaintPipeline
4
+ from PIL import Image
5
+ import numpy as np
6
+ from transformers import SegformerImageProcessor, SegformerForSemanticSegmentation
7
+ import cv2
8
+
9
+ class RafayyVirtualTryOn:
10
+ def __init__(self):
11
+ # Initialize SDXL for better quality
12
+ self.inpaint_model = StableDiffusionXLInpaintPipeline.from_pretrained(
13
+ "stabilityai/stable-diffusion-xl-base-1.0",
14
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
15
+ variant="fp16"
16
+ )
17
+ if torch.cuda.is_available():
18
+ self.inpaint_model.to("cuda")
19
+
20
+ # Initialize enhanced segmentation model
21
+ self.segmenter = SegformerForSemanticSegmentation.from_pretrained("mattmdjaga/segformer_b2_clothes")
22
+ self.processor = SegformerImageProcessor.from_pretrained("mattmdjaga/segformer_b2_clothes")
23
+
24
+ def enhance_mask(self, mask):
25
+ """Enhance the clothing mask for better results"""
26
+ kernel = np.ones((5,5), np.uint8)
27
+ mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
28
+ mask = cv2.GaussianBlur(mask, (5,5), 0)
29
+ return mask
30
+
31
+ def get_clothing_mask(self, image):
32
+ """Extract and enhance clothing mask"""
33
+ inputs = self.processor(images=image, return_tensors="pt")
34
+ outputs = self.segmenter(**inputs)
35
+ logits = outputs.logits.squeeze()
36
+
37
+ clothing_mask = (logits.argmax(0) == 5).float().numpy()
38
+ clothing_mask = (clothing_mask * 255).astype(np.uint8)
39
+ clothing_mask = self.enhance_mask(clothing_mask)
40
+
41
+ return Image.fromarray(clothing_mask)
42
+
43
+ def try_on(self, original_image, prompt, style_strength=0.7, progress=gr.Progress()):
44
+ """Enhanced virtual try-on with style control"""
45
+ try:
46
+ progress(0, desc="Initializing...")
47
+
48
+ # Image preprocessing
49
+ original_image = Image.fromarray(original_image)
50
+ if original_image.mode != "RGB":
51
+ original_image = original_image.convert("RGB")
52
+
53
+ progress(0.2, desc="Analyzing clothing...")
54
+ mask = self.get_clothing_mask(original_image)
55
+
56
+ # Enhanced prompt engineering
57
+ style_prompts = {
58
+ "quality": "ultra detailed, 8k uhd, high quality, professional photo",
59
+ "lighting": "perfect lighting, studio lighting, professional photography",
60
+ "realism": "hyperrealistic, photorealistic, highly detailed"
61
+ }
62
+
63
+ full_prompt = f"""
64
+ A person wearing {prompt}, {style_prompts['quality']},
65
+ {style_prompts['lighting']}, {style_prompts['realism']}
66
+ """
67
+
68
+ negative_prompt = """
69
+ low quality, blurry, distorted, deformed, unrealistic,
70
+ bad proportions, bad lighting, oversaturated, undersaturated
71
+ """
72
+
73
+ progress(0.4, desc="Generating new clothing...")
74
+ result = self.inpaint_model(
75
+ prompt=full_prompt,
76
+ negative_prompt=negative_prompt,
77
+ image=original_image,
78
+ mask_image=mask,
79
+ num_inference_steps=50,
80
+ guidance_scale=7.5 * style_strength
81
+ ).images[0]
82
+
83
+ progress(1.0, desc="Finalizing...")
84
+ return result
85
+
86
+ except Exception as e:
87
+ raise gr.Error(f"Processing Error: {str(e)}")
88
+
89
+ # Initialize the model
90
+ model = RafayyVirtualTryOn()
91
+
92
+ # Custom CSS with professional styling
93
+ custom_css = """
94
+ .gradio-container {
95
+ font-family: 'Poppins', sans-serif;
96
+ max-width: 1200px !important;
97
+ margin: auto !important;
98
+ }
99
+ #component-0 {
100
+ max-width: 100% !important;
101
+ margin-bottom: 20px !important;
102
+ }
103
+ .gr-button {
104
+ background: linear-gradient(90deg, #2193b0, #6dd5ed) !important;
105
+ border: none !important;
106
+ color: white !important;
107
+ font-weight: 600 !important;
108
+ }
109
+ .gr-button:hover {
110
+ background: linear-gradient(90deg, #6dd5ed, #2193b0) !important;
111
+ transform: translateY(-2px);
112
+ box-shadow: 0 5px 15px rgba(33, 147, 176, 0.3) !important;
113
+ transition: all 0.3s ease;
114
+ }
115
+ .gr-input {
116
+ border: 2px solid #e0e0e0 !important;
117
+ border-radius: 8px !important;
118
+ padding: 12px !important;
119
+ }
120
+ .gr-input:focus {
121
+ border-color: #2193b0 !important;
122
+ box-shadow: 0 0 0 2px rgba(33, 147, 176, 0.2) !important;
123
+ }
124
+ .gr-panel {
125
+ border-radius: 12px !important;
126
+ box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1) !important;
127
+ }
128
+ .footer {
129
+ background: linear-gradient(to right, #f8f9fa, #e9ecef);
130
+ padding: 20px;
131
+ border-radius: 10px;
132
+ margin-top: 30px;
133
+ }
134
+ """
135
+
136
+ # Create enhanced Gradio interface
137
+ demo = gr.Interface(
138
+ fn=model.try_on,
139
+ inputs=[
140
+ gr.Image(label="πŸ“Έ Upload Your Photo", type="numpy"),
141
+ gr.Textbox(
142
+ label="🎨 Describe New Clothing",
143
+ placeholder="e.g., 'elegant black suit with silk lapels', 'designer red dress with gold accents'",
144
+ lines=2
145
+ ),
146
+ gr.Slider(
147
+ label="Style Strength",
148
+ minimum=0.1,
149
+ maximum=1.0,
150
+ value=0.7,
151
+ step=0.1
152
+ )
153
+ ],
154
+ outputs=gr.Image(label="✨ Your New Look", type="pil"),
155
+ title="🌟 Rafayy's Professional Virtual Try-On Studio 🌟",
156
+ description="""
157
+ <div style="text-align: center; max-width: 800px; margin: 0 auto; padding: 20px;">
158
+ <h3 style="color: #2193b0; font-size: 24px; margin-bottom: 10px;">Transform Your Style with AI</h3>
159
+ <p style="color: #666; font-size: 16px;">Experience the future of fashion with our advanced virtual try-on technology.</p>
160
+ <div style="margin-top: 20px; padding: 15px; background: rgba(33, 147, 176, 0.1); border-radius: 10px;">
161
+ <p style="font-weight: bold; color: #2193b0;">Premium Features:</p>
162
+ <p>βœ“ High-Resolution Output</p>
163
+ <p>βœ“ Advanced Clothing Detection</p>
164
+ <p>βœ“ Professional Style Enhancement</p>
165
+ </div>
166
+ </div>
167
+ """,
168
+ examples=[
169
+ ["example1.jpg", "luxury black suit with silk details", 0.8],
170
+ ["example2.jpg", "designer white dress with lace accents", 0.7],
171
+ ["example3.jpg", "professional navy blazer with gold buttons", 0.9],
172
+ ["example4.jpg", "casual denim jacket with vintage wash", 0.6]
173
+ ],
174
+ css=custom_css
175
+ )
176
+
177
+ # Enhanced footer with professional information
178
+ demo.footer = """
179
+ <div class="footer">
180
+ <div style="text-align: center;">
181
+ <h4 style="color: #2193b0; margin-bottom: 15px;">🎯 Professional Tips for Best Results</h4>
182
+ <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 20px;">
183
+ <div>
184
+ <p style="font-weight: bold;">πŸ“Έ Photo Guidelines</p>
185
+ <p>β€’ Use well-lit, front-facing photos</p>
186
+ <p>β€’ Ensure clear visibility of clothing</p>
187
+ <p>β€’ Avoid complex backgrounds</p>
188
+ </div>
189
+ <div>
190
+ <p style="font-weight: bold;">✍️ Description Tips</p>
191
+ <p>β€’ Be specific about style details</p>
192
+ <p>β€’ Include color and material</p>
193
+ <p>β€’ Mention design elements</p>
194
+ </div>
195
+ <div>
196
+ <p style="font-weight: bold;">βš™οΈ Settings</p>
197
+ <p>β€’ Adjust style strength as needed</p>
198
+ <p>β€’ Higher values for bold changes</p>
199
+ <p>β€’ Lower values for subtle effects</p>
200
+ </div>
201
+ </div>
202
+ <p style="margin-top: 20px; color: #666;">Β© 2024 Rafayy AI Studio | Professional Virtual Try-On Service</p>
203
+ </div>
204
+ </div>
205
+ """
206
+
207
+ # Launch the app
208
+ if __name__ == "__main__":
209
+ demo.launch()