gaur3009 commited on
Commit
0be295c
·
verified ·
1 Parent(s): 80364f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -6
app.py CHANGED
@@ -19,11 +19,13 @@ def detect_dress_and_warp(hoodie_img, design_img, warp_strength, scale):
19
  if contours:
20
  largest_contour = max(contours, key=cv2.contourArea)
21
  x, y, w, h = cv2.boundingRect(largest_contour)
 
22
  else:
23
- x, y, w, h = 100, 200, hoodie.size[0]//2, hoodie.size[1]//2 # Default placement
24
 
25
  # Resize and warp design
26
- design = design.resize((int(w * scale), int(h * scale)))
 
27
  design_cv = cv2.cvtColor(np.array(design), cv2.COLOR_RGBA2BGRA)
28
 
29
  # Apply warp effect
@@ -32,11 +34,15 @@ def detect_dress_and_warp(hoodie_img, design_img, warp_strength, scale):
32
  design_cv = cv2.warpAffine(design_cv, warp_map, (cols, rows))
33
  design = Image.fromarray(cv2.cvtColor(design_cv, cv2.COLOR_BGRA2RGBA))
34
 
 
 
 
 
35
  # Create a blank image with same size as hoodie
36
  temp_layer = Image.new("RGBA", hoodie.size, (0, 0, 0, 0))
37
 
38
- # Paste design onto detected dress region
39
- temp_layer.paste(design, (x, y), design)
40
 
41
  # Blend the layers
42
  final_image = Image.alpha_composite(hoodie, temp_layer)
@@ -56,7 +62,7 @@ demo = gr.Interface(
56
  ],
57
  outputs=gr.Image(type="pil", label="Generated Mockup"),
58
  title="Rookus Hoodie Mockup Generator",
59
- description="Upload a hoodie mockup and your design to generate a realistic preview with automatic dress detection and warping."
60
  )
61
 
62
- demo.launch()
 
19
  if contours:
20
  largest_contour = max(contours, key=cv2.contourArea)
21
  x, y, w, h = cv2.boundingRect(largest_contour)
22
+ center_x, center_y = x + w // 2, y + h // 2
23
  else:
24
+ center_x, center_y = hoodie.size[0] // 2, hoodie.size[1] // 2 # Default center placement
25
 
26
  # Resize and warp design
27
+ new_w, new_h = int(w * scale), int(h * scale)
28
+ design = design.resize((new_w, new_h))
29
  design_cv = cv2.cvtColor(np.array(design), cv2.COLOR_RGBA2BGRA)
30
 
31
  # Apply warp effect
 
34
  design_cv = cv2.warpAffine(design_cv, warp_map, (cols, rows))
35
  design = Image.fromarray(cv2.cvtColor(design_cv, cv2.COLOR_BGRA2RGBA))
36
 
37
+ # Calculate placement at the center of the detected dress
38
+ paste_x = center_x - new_w // 2
39
+ paste_y = center_y - new_h // 2
40
+
41
  # Create a blank image with same size as hoodie
42
  temp_layer = Image.new("RGBA", hoodie.size, (0, 0, 0, 0))
43
 
44
+ # Paste design onto detected dress center
45
+ temp_layer.paste(design, (paste_x, paste_y), design)
46
 
47
  # Blend the layers
48
  final_image = Image.alpha_composite(hoodie, temp_layer)
 
62
  ],
63
  outputs=gr.Image(type="pil", label="Generated Mockup"),
64
  title="Rookus Hoodie Mockup Generator",
65
+ description="Upload a hoodie mockup and your design to generate a realistic preview with automatic dress detection and warping at the center of the dress."
66
  )
67
 
68
+ demo.launch()