zaghamrasool commited on
Commit
1088ada
·
verified ·
1 Parent(s): 9c91c6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -58
app.py CHANGED
@@ -13,9 +13,7 @@ garm_list_path = [os.path.join(example_path, "cloth", garm) for garm in garm_lis
13
  human_list = os.listdir(os.path.join(example_path, "human"))
14
  human_list_path = [os.path.join(example_path, "human", human) for human in human_list]
15
 
16
- def mock_tryon(person_img, garment_img, seed, randomize_seed, progress=gr.Progress()):
17
- progress(0, desc="Starting mock try-on...")
18
-
19
  if person_img is None or garment_img is None:
20
  gr.Warning("Please upload both images!")
21
  return None, None, "Error: Empty image"
@@ -23,76 +21,58 @@ def mock_tryon(person_img, garment_img, seed, randomize_seed, progress=gr.Progre
23
  if randomize_seed:
24
  seed = random.randint(0, MAX_SEED)
25
 
26
- progress(0.3, desc="Processing person image...")
27
- # Convert to grayscale for demo (replace with actual try-on logic)
28
- person_gray = cv2.cvtColor(person_img, cv2.COLOR_RGB2GRAY)
29
- person_gray = cv2.cvtColor(person_gray, cv2.COLOR_GRAY2RGB)
30
-
31
- progress(0.6, desc="Adding garment...")
32
- # Resize garment to fit person (demo only)
33
- garment_resized = cv2.resize(garment_img, (person_img.shape[1], person_img.shape[0]))
34
-
35
- progress(0.8, desc="Blending images...")
36
- # Simple alpha blending (replace with real try-on)
37
- alpha = 0.7
38
- result = cv2.addWeighted(person_gray, 1-alpha, garment_resized, alpha, 0)
39
-
40
- progress(1.0, desc="Done!")
41
- return result, seed, "Mock try-on complete"
42
-
43
- def load_description(fp):
44
- with open(fp, 'r', encoding='utf-8') as f:
45
- return f.read()
46
 
47
  css = """
48
- #col-left { margin: 0 auto; max-width: 430px; }
49
- #col-mid { margin: 0 auto; max-width: 430px; }
50
- #col-right { margin: 0 auto; max-width: 430px; }
51
  #col-showcase { margin: 0 auto; max-width: 1100px; }
52
  #button { color: blue; }
53
  """
54
 
55
- with gr.Blocks(css=css) as Tryon:
56
- gr.HTML(load_description("assets/title.md"))
57
-
58
  with gr.Row():
59
  with gr.Column(elem_id="col-left"):
60
- gr.HTML("<div style='text-align: center; font-size: 20px;'>Step 1. Upload person ⬇️</div>")
61
- imgs = gr.Image(label="Person", sources='upload', type="numpy")
62
- gr.Examples(inputs=imgs, examples_per_page=12, examples=human_list_path)
63
 
64
  with gr.Column(elem_id="col-mid"):
65
- gr.HTML("<div style='text-align: center; font-size: 20px;'>Step 2. Upload garment ⬇️</div>")
66
- garm_img = gr.Image(label="Garment", sources='upload', type="numpy")
67
- gr.Examples(inputs=garm_img, examples_per_page=12, examples=garm_list_path)
68
 
69
  with gr.Column(elem_id="col-right"):
70
- gr.HTML("<div style='text-align: center; font-size: 20px;'>Step 3. Click Run</div>")
71
- image_out = gr.Image(label="Result")
72
  with gr.Row():
73
- seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
74
- randomize_seed = gr.Checkbox(label="Random seed", value=True)
75
  with gr.Row():
76
- seed_used = gr.Number(label="Seed used")
77
- result_info = gr.Text(label="Status")
78
- test_button = gr.Button("Run", elem_id="button")
79
 
80
- test_button.click(
81
  fn=mock_tryon,
82
- inputs=[imgs, garm_img, seed, randomize_seed],
83
- outputs=[image_out, seed_used, result_info],
84
- concurrency_limit=5
85
  )
86
 
87
- with gr.Column(elem_id="col-showcase"):
88
- gr.HTML("<div style='text-align: center; font-size: 20px;'>Examples</div>")
89
- gr.Examples(
90
- examples=[
91
- [human_list_path[0], garm_list_path[0]], # First human + first garment
92
- [human_list_path[1], garm_list_path[1]], # Second pair
93
- ],
94
- inputs=[imgs, garm_img],
95
- outputs=[image_out]
96
- )
97
-
98
- Tryon.queue().launch()
 
13
  human_list = os.listdir(os.path.join(example_path, "human"))
14
  human_list_path = [os.path.join(example_path, "human", human) for human in human_list]
15
 
16
+ def mock_tryon(person_img, garment_img, seed, randomize_seed):
 
 
17
  if person_img is None or garment_img is None:
18
  gr.Warning("Please upload both images!")
19
  return None, None, "Error: Empty image"
 
21
  if randomize_seed:
22
  seed = random.randint(0, MAX_SEED)
23
 
24
+ # Simple mock processing
25
+ try:
26
+ # Convert person to grayscale
27
+ person_gray = cv2.cvtColor(person_img, cv2.COLOR_RGB2GRAY)
28
+ person_gray = cv2.cvtColor(person_gray, cv2.COLOR_GRAY2RGB)
29
+
30
+ # Resize garment
31
+ garment_resized = cv2.resize(garment_img, (person_img.shape[1], person_img.shape[0]))
32
+
33
+ # Blend images
34
+ result = cv2.addWeighted(person_gray, 0.3, garment_resized, 0.7, 0)
35
+
36
+ return result, seed, "Mock try-on complete"
37
+ except Exception as e:
38
+ gr.Error(f"Processing error: {str(e)}")
39
+ return None, seed, f"Error: {str(e)}"
 
 
 
 
40
 
41
  css = """
42
+ #col-left, #col-mid, #col-right { margin: 0 auto; max-width: 430px; }
 
 
43
  #col-showcase { margin: 0 auto; max-width: 1100px; }
44
  #button { color: blue; }
45
  """
46
 
47
+ with gr.Blocks(css=css) as app:
48
+ gr.Markdown("# Virtual Try-On Demo (Mock)")
49
+
50
  with gr.Row():
51
  with gr.Column(elem_id="col-left"):
52
+ gr.Markdown("### Step 1: Upload Person Image")
53
+ person_img = gr.Image(label="Person", type="numpy")
54
+ gr.Examples(examples=human_list_path, inputs=person_img)
55
 
56
  with gr.Column(elem_id="col-mid"):
57
+ gr.Markdown("### Step 2: Upload Garment")
58
+ garment_img = gr.Image(label="Garment", type="numpy")
59
+ gr.Examples(examples=garm_list_path, inputs=garment_img)
60
 
61
  with gr.Column(elem_id="col-right"):
62
+ gr.Markdown("### Step 3: Get Result")
63
+ output_img = gr.Image(label="Result")
64
  with gr.Row():
65
+ seed = gr.Slider(0, MAX_SEED, label="Seed", value=0)
66
+ random_seed = gr.Checkbox(label="Random Seed", value=True)
67
  with gr.Row():
68
+ seed_used = gr.Number(label="Seed Used")
69
+ status = gr.Textbox(label="Status")
70
+ run_btn = gr.Button("Run", variant="primary")
71
 
72
+ run_btn.click(
73
  fn=mock_tryon,
74
+ inputs=[person_img, garment_img, seed, random_seed],
75
+ outputs=[output_img, seed_used, status]
 
76
  )
77
 
78
+ app.launch(share=True) # Critical fix: Added share=True