ishworrsubedii commited on
Commit
2e0953e
Β·
verified Β·
1 Parent(s): 4f0c4b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -31
app.py CHANGED
@@ -31,37 +31,53 @@ def process_mask(mask):
31
 
32
  @spaces.GPU
33
  def clothing_try_on(image, mask):
34
- """Perform clothing try-on using the provided image and binary mask."""
35
- orig_size = image.size
36
-
37
- # Process and ensure mask is binary
38
- mask = process_mask(mask)
39
-
40
- # Resize image and mask for Stable Diffusion
41
- image = image.resize((512, 512))
42
- mask = mask.resize((512, 512))
43
-
44
- # Prompt and negative prompt
45
- prompt = f"South Indian Saree, properly worn, natural setting, elegant, natural look, neckline without jewellery, simple"
46
- negative_prompt = "necklaces, jewellery, jewelry, necklace, neckpiece, garland, chain, neck wear, jewelled neck, jeweled neck, necklace on neck, jewellery on neck, accessories, watermark, text, changed background, wider body, narrower body, bad proportions, extra limbs, mutated hands, changed sizes, altered proportions, unnatural body proportions, blury, ugly"
47
-
48
- # Perform the inpainting using the Stable Diffusion pipeline
49
- output = pipeline(
50
- prompt=prompt,
51
- negative_prompt=negative_prompt,
52
- image=image,
53
- mask_image=mask,
54
- strength=0.95,
55
- guidance_score=9,
56
- ).images[0]
57
-
58
- # Resize the output back to the original size
59
- output = output.resize(orig_size)
60
-
61
- # Clean GPU memory
62
- clear_func()
63
-
64
- return output
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
  def launch_interface():
67
  """Launch the Gradio interface."""
 
31
 
32
  @spaces.GPU
33
  def clothing_try_on(image, mask):
34
+ jewellery_mask = Image.fromarray(
35
+ np.bitwise_and(np.array(mask), np.array(image))
36
+ )
37
+ arr_orig = np.array(grayscale(mask))
38
+
39
+ image = cv2.inpaint(np.array(image), arr_orig, 15, cv2.INPAINT_TELEA)
40
+ image = Image.fromarray(image)
41
+
42
+ arr = arr_orig.copy()
43
+ mask_y = np.where(arr == arr[arr != 0][0])[0][0]
44
+ arr[mask_y:, :] = 255
45
+
46
+ new = Image.fromarray(arr)
47
+
48
+ mask = new.copy()
49
+
50
+ orig_size = image.size
51
+
52
+ image = image.resize((512, 512))
53
+ mask = mask.resize((512, 512))
54
+
55
+ results = []
56
+ prompt = f" South Indian Saree, properly worn, natural setting, elegant, natural look, neckline without jewellery, simple"
57
+ negative_prompt = "necklaces, jewellery, jewelry, necklace, neckpiece, garland, chain, neck wear, jewelled neck, jeweled neck, necklace on neck, jewellery on neck, accessories, watermark, text, changed background, wider body, narrower body, bad proportions, extra limbs, mutated hands, changed sizes, altered proportions, unnatural body proportions, blury, ugly"
58
+
59
+ output = pipeline(
60
+ prompt=prompt,
61
+ negative_prompt=negative_prompt,
62
+ image=image,
63
+ mask_image=mask,
64
+ strength=0.95,
65
+ guidance_score=9,
66
+ # generator = torch.Generator("cuda").manual_seed(42)
67
+ ).images[0]
68
+
69
+ output = output.resize(orig_size)
70
+ temp_generated = np.bitwise_and(
71
+ np.array(output),
72
+ np.bitwise_not(np.array(Image.fromarray(arr_orig).convert("RGB"))),
73
+ )
74
+ results.append(temp_generated)
75
+
76
+ results = [
77
+ Image.fromarray(np.bitwise_or(x, np.array(jewellery_mask))) for x in results
78
+ ]
79
+ clear_func()
80
+ return results[0]
81
 
82
  def launch_interface():
83
  """Launch the Gradio interface."""