Stylique commited on
Commit
d785382
·
verified ·
1 Parent(s): e0218b9

Upload 2 files

Browse files
Files changed (1) hide show
  1. app.py +69 -6
app.py CHANGED
@@ -156,21 +156,41 @@ def process_garment(input_type, text_prompt, base_text_prompt, target_image, bas
156
 
157
  # Save target image to temp directory
158
  target_image_path = os.path.join(temp_dir, "target_image.jpg")
 
 
159
  if isinstance(target_image, str):
 
160
  shutil.copy(target_image, target_image_path)
161
- else:
 
 
 
 
 
162
  target_image.save(target_image_path)
 
 
 
 
163
 
164
  # Save base image if provided, or use default
165
  if base_image is not None:
166
  base_image_path = os.path.join(temp_dir, "base_image.jpg")
167
  if isinstance(base_image, str):
168
  shutil.copy(base_image, base_image_path)
169
- else:
 
 
 
170
  base_image.save(base_image_path)
 
 
 
 
171
  else:
172
  # If no base image, use a placeholder or default
173
  base_image_path = target_image_path
 
174
 
175
  # Configure for image-based processing
176
  config.update({
@@ -220,7 +240,18 @@ def process_garment(input_type, text_prompt, base_text_prompt, target_image, bas
220
 
221
  progress(0.1, desc="Initializing...")
222
 
 
 
 
 
 
 
 
 
 
 
223
  # Run the main processing loop
 
224
  loop(config)
225
 
226
  progress(0.9, desc="Processing complete, preparing output...")
@@ -230,43 +261,63 @@ def process_garment(input_type, text_prompt, base_text_prompt, target_image, bas
230
  glb_files = []
231
  image_files = []
232
 
 
 
233
  # First check for mesh files in mesh_final directory (priority)
234
  mesh_final_dir = Path(temp_dir) / "mesh_final"
235
  if mesh_final_dir.exists():
 
236
  for file_path in mesh_final_dir.rglob("*"):
237
  if file_path.is_file():
238
  if file_path.suffix.lower() == '.obj':
239
  obj_files.append(str(file_path))
 
240
  elif file_path.suffix.lower() == '.glb':
241
  glb_files.append(str(file_path))
 
 
 
242
 
243
  # Check other mesh directories
244
  for mesh_dir in Path(temp_dir).glob("mesh_*"):
245
  if mesh_dir.is_dir() and mesh_dir.name != 'mesh_final':
 
246
  for file_path in mesh_dir.rglob("*"):
247
  if file_path.is_file():
248
  if file_path.suffix.lower() == '.obj':
249
  obj_files.append(str(file_path))
 
250
  elif file_path.suffix.lower() == '.glb':
251
  glb_files.append(str(file_path))
 
252
 
253
  # Collect image files for visualization
254
  for file_path in Path(temp_dir).rglob("*"):
255
  if file_path.is_file() and file_path.suffix.lower() in ['.png', '.jpg', '.jpeg', '.gif', '.mp4']:
256
  image_files.append(str(file_path))
 
 
257
 
258
  # Prioritize output: GLB, OBJ, then images
259
  if glb_files:
 
260
  return glb_files[0] # Return first GLB file (best for web viewing)
261
  elif obj_files:
 
262
  return obj_files[0] # Return first OBJ file
263
  elif image_files:
 
264
  return image_files[0] # Return an image if no mesh was found
265
  else:
266
- return "Processing completed but no output files found."
 
267
 
268
  except Exception as e:
269
- return f"Error during processing: {str(e)}"
 
 
 
 
270
 
271
  def create_interface():
272
  """
@@ -278,6 +329,12 @@ def create_interface():
278
 
279
  This tool allows you to stylize 3D garments using text prompts or images. Generate a new 3D garment mesh (.obj/.glb)
280
  that can be used for virtual try-on applications.
 
 
 
 
 
 
281
  """)
282
 
283
  with gr.Row():
@@ -306,12 +363,18 @@ def create_interface():
306
  # Image inputs (hidden by default)
307
  with gr.Group(visible=False) as image_group:
308
  target_image = gr.Image(
309
- label="Target Garment Image"
 
 
310
  )
 
311
 
312
  base_image = gr.Image(
313
- label="Base Garment Image (Optional)"
 
 
314
  )
 
315
 
316
  # Custom mesh
317
  custom_mesh = gr.File(
 
156
 
157
  # Save target image to temp directory
158
  target_image_path = os.path.join(temp_dir, "target_image.jpg")
159
+
160
+ # Handle different possible image formats from Gradio
161
  if isinstance(target_image, str):
162
+ # Image is a file path
163
  shutil.copy(target_image, target_image_path)
164
+ elif isinstance(target_image, np.ndarray):
165
+ # Image is a numpy array from Gradio
166
+ img = Image.fromarray(target_image)
167
+ img.save(target_image_path)
168
+ elif hasattr(target_image, 'save'):
169
+ # Image is a PIL image
170
  target_image.save(target_image_path)
171
+ else:
172
+ return "Error: Unsupported target image format. Please try again."
173
+
174
+ print(f"Target image saved to {target_image_path}")
175
 
176
  # Save base image if provided, or use default
177
  if base_image is not None:
178
  base_image_path = os.path.join(temp_dir, "base_image.jpg")
179
  if isinstance(base_image, str):
180
  shutil.copy(base_image, base_image_path)
181
+ elif isinstance(base_image, np.ndarray):
182
+ img = Image.fromarray(base_image)
183
+ img.save(base_image_path)
184
+ elif hasattr(base_image, 'save'):
185
  base_image.save(base_image_path)
186
+ else:
187
+ # If format is unsupported, use target image
188
+ base_image_path = target_image_path
189
+ print(f"Base image saved to {base_image_path}")
190
  else:
191
  # If no base image, use a placeholder or default
192
  base_image_path = target_image_path
193
+ print("No base image provided, using target image as base")
194
 
195
  # Configure for image-based processing
196
  config.update({
 
240
 
241
  progress(0.1, desc="Initializing...")
242
 
243
+ # Print configuration for debugging
244
+ print("Starting processing with configuration:")
245
+ print(f"Mode: {'Image' if config.get('fashion_image', False) else 'Text'}")
246
+ if config.get('fashion_image', False):
247
+ print(f"Target image: {config['image_prompt']}")
248
+ print(f"Base image: {config['base_image_prompt']}")
249
+ else:
250
+ print(f"Target text: {config['text_prompt']}")
251
+ print(f"Base text: {config['base_text_prompt']}")
252
+
253
  # Run the main processing loop
254
+ progress(0.2, desc="Running garment generation...")
255
  loop(config)
256
 
257
  progress(0.9, desc="Processing complete, preparing output...")
 
261
  glb_files = []
262
  image_files = []
263
 
264
+ print("Searching for output files...")
265
+
266
  # First check for mesh files in mesh_final directory (priority)
267
  mesh_final_dir = Path(temp_dir) / "mesh_final"
268
  if mesh_final_dir.exists():
269
+ print(f"Found mesh_final directory at {mesh_final_dir}")
270
  for file_path in mesh_final_dir.rglob("*"):
271
  if file_path.is_file():
272
  if file_path.suffix.lower() == '.obj':
273
  obj_files.append(str(file_path))
274
+ print(f"Found OBJ file: {file_path}")
275
  elif file_path.suffix.lower() == '.glb':
276
  glb_files.append(str(file_path))
277
+ print(f"Found GLB file: {file_path}")
278
+ else:
279
+ print("mesh_final directory not found")
280
 
281
  # Check other mesh directories
282
  for mesh_dir in Path(temp_dir).glob("mesh_*"):
283
  if mesh_dir.is_dir() and mesh_dir.name != 'mesh_final':
284
+ print(f"Checking directory: {mesh_dir}")
285
  for file_path in mesh_dir.rglob("*"):
286
  if file_path.is_file():
287
  if file_path.suffix.lower() == '.obj':
288
  obj_files.append(str(file_path))
289
+ print(f"Found OBJ file: {file_path}")
290
  elif file_path.suffix.lower() == '.glb':
291
  glb_files.append(str(file_path))
292
+ print(f"Found GLB file: {file_path}")
293
 
294
  # Collect image files for visualization
295
  for file_path in Path(temp_dir).rglob("*"):
296
  if file_path.is_file() and file_path.suffix.lower() in ['.png', '.jpg', '.jpeg', '.gif', '.mp4']:
297
  image_files.append(str(file_path))
298
+
299
+ print(f"Found {len(glb_files)} GLB files, {len(obj_files)} OBJ files, and {len(image_files)} image files")
300
 
301
  # Prioritize output: GLB, OBJ, then images
302
  if glb_files:
303
+ print(f"Returning GLB file: {glb_files[0]}")
304
  return glb_files[0] # Return first GLB file (best for web viewing)
305
  elif obj_files:
306
+ print(f"Returning OBJ file: {obj_files[0]}")
307
  return obj_files[0] # Return first OBJ file
308
  elif image_files:
309
+ print(f"Returning image file: {image_files[0]}")
310
  return image_files[0] # Return an image if no mesh was found
311
  else:
312
+ print("No output files found")
313
+ return "Processing completed but no output files found. Please check the logs for more information."
314
 
315
  except Exception as e:
316
+ import traceback
317
+ error_details = traceback.format_exc()
318
+ print(f"Error during processing: {str(e)}")
319
+ print(f"Error details: {error_details}")
320
+ return f"Error during processing: {str(e)}. Please check the logs for more details."
321
 
322
  def create_interface():
323
  """
 
329
 
330
  This tool allows you to stylize 3D garments using text prompts or images. Generate a new 3D garment mesh (.obj/.glb)
331
  that can be used for virtual try-on applications.
332
+
333
+ ## How to use:
334
+ 1. Choose either **Text** or **Image** input mode using the radio button below
335
+ 2. For **Text** mode: Enter descriptions of your target and base garment styles
336
+ 3. For **Image** mode: Upload an image of your desired garment style
337
+ 4. Click "Generate 3D Garment" to create your 3D mesh file
338
  """)
339
 
340
  with gr.Row():
 
363
  # Image inputs (hidden by default)
364
  with gr.Group(visible=False) as image_group:
365
  target_image = gr.Image(
366
+ label="Target Garment Image",
367
+ type="pil",
368
+ image_mode="RGB"
369
  )
370
+ gr.Markdown("*Upload an image of the desired garment style*")
371
 
372
  base_image = gr.Image(
373
+ label="Base Garment Image (Optional)",
374
+ type="pil",
375
+ image_mode="RGB"
376
  )
377
+ gr.Markdown("*Upload a base garment image (optional)*")
378
 
379
  # Custom mesh
380
  custom_mesh = gr.File(