Monikashyapa commited on
Commit
51a8997
·
verified ·
1 Parent(s): 567e8e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -14
app.py CHANGED
@@ -1,9 +1,7 @@
1
- # app.py
2
  import gradio as gr
3
  import cv2
4
  import numpy as np
5
  from gradio_client import Client
6
- import os
7
 
8
  # -----------------------------
9
  # Local "try-on" function
@@ -15,7 +13,6 @@ def tryon_local(person_img, garment_img, seed, randomize_seed):
15
  h_person, w_person = person_img.shape[:2]
16
  h_garment, w_garment = garment_img.shape[:2]
17
 
18
- # Resize garment to fit person width
19
  scale = w_person / w_garment
20
  new_w = int(w_garment * scale)
21
  new_h = int(h_garment * scale)
@@ -25,12 +22,12 @@ def tryon_local(person_img, garment_img, seed, randomize_seed):
25
  y_offset = 0
26
  x_offset = max(0, (w_person - new_w) // 2)
27
 
28
- if garment_resized.shape[2] == 4: # RGBA
29
  alpha = garment_resized[:, :, 3] / 255.0
30
  for c in range(3):
31
  overlay[y_offset:y_offset+new_h, x_offset:x_offset+new_w, c] = \
32
  alpha * garment_resized[:, :, c] + (1 - alpha) * overlay[y_offset:y_offset+new_h, x_offset:x_offset+new_w, c]
33
- else: # RGB
34
  overlay[y_offset:y_offset+new_h, x_offset:x_offset+new_w] = garment_resized
35
 
36
  info = "Success (local simulation)"
@@ -42,18 +39,14 @@ def tryon_local(person_img, garment_img, seed, randomize_seed):
42
  hunyuan_client = Client("tencent/Hunyuan3D-2.1")
43
 
44
  def tryon_to_3d(person_img, garment_img, seed, randomize_seed):
45
- # Run local try-on simulation first
46
  tryon_img, seed_used, tryon_info = tryon_local(person_img, garment_img, seed, randomize_seed)
47
  if tryon_img is None:
48
  return None, "Try-on failed"
49
 
50
  try:
51
- # Convert image to bytes for API
52
- _, img_encoded = cv2.imencode(".png", tryon_img)
53
- img_bytes = img_encoded.tobytes()
54
-
55
- # Call Hunyuan3D API
56
- result_3d = hunyuan_client.predict(img_bytes, api_name="/predict")
57
  return result_3d, tryon_info
58
  except Exception as e:
59
  return None, f"Hunyuan3D API error: {e}"
@@ -92,5 +85,4 @@ with gr.Blocks(css=css) as app:
92
  outputs=[output_img, result_info]
93
  )
94
 
95
- # Launch Gradio app
96
- app.launch(server_name="0.0.0.0", server_port=7860, share=False)
 
 
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
4
  from gradio_client import Client
 
5
 
6
  # -----------------------------
7
  # Local "try-on" function
 
13
  h_person, w_person = person_img.shape[:2]
14
  h_garment, w_garment = garment_img.shape[:2]
15
 
 
16
  scale = w_person / w_garment
17
  new_w = int(w_garment * scale)
18
  new_h = int(h_garment * scale)
 
22
  y_offset = 0
23
  x_offset = max(0, (w_person - new_w) // 2)
24
 
25
+ if garment_resized.shape[2] == 4:
26
  alpha = garment_resized[:, :, 3] / 255.0
27
  for c in range(3):
28
  overlay[y_offset:y_offset+new_h, x_offset:x_offset+new_w, c] = \
29
  alpha * garment_resized[:, :, c] + (1 - alpha) * overlay[y_offset:y_offset+new_h, x_offset:x_offset+new_w, c]
30
+ else:
31
  overlay[y_offset:y_offset+new_h, x_offset:x_offset+new_w] = garment_resized
32
 
33
  info = "Success (local simulation)"
 
39
  hunyuan_client = Client("tencent/Hunyuan3D-2.1")
40
 
41
  def tryon_to_3d(person_img, garment_img, seed, randomize_seed):
42
+ # Run local simulation first
43
  tryon_img, seed_used, tryon_info = tryon_local(person_img, garment_img, seed, randomize_seed)
44
  if tryon_img is None:
45
  return None, "Try-on failed"
46
 
47
  try:
48
+ # Call the Hunyuan3D predict function directly
49
+ result_3d = hunyuan_client.predict(person_img, garment_img)
 
 
 
 
50
  return result_3d, tryon_info
51
  except Exception as e:
52
  return None, f"Hunyuan3D API error: {e}"
 
85
  outputs=[output_img, result_info]
86
  )
87
 
88
+ app.launch()