ishworrsubedii commited on
Commit
23143ca
Β·
verified Β·
1 Parent(s): 8fa81d3

update: code refactor

Browse files
Files changed (1) hide show
  1. app.py +16 -12
app.py CHANGED
@@ -32,8 +32,8 @@ class NecklaceTryOn:
32
  torch.cuda.empty_cache()
33
  gc.collect()
34
 
35
- @spaces.GPU
36
- def clothing_try_on_n_necklace_try_on(self, image, jewellery):
37
  image = np.array(image)
38
  copy_image = image.copy()
39
  jewellery = np.array(jewellery)
@@ -97,26 +97,30 @@ class NecklaceTryOn:
97
 
98
  gc.collect()
99
 
100
- image = Image.fromarray(result.astype(np.uint8))
101
- mask = Image.fromarray(binaryMask.astype(np.uint8)).convert("RGB")
 
 
 
 
102
 
103
  jewellery_mask = Image.fromarray(
104
- np.bitwise_and(np.array(mask), np.array(image))
105
  )
106
  arr_orig = np.array(grayscale(mask))
107
 
108
- image = cv2.inpaint(np.array(image), arr_orig, 15, cv2.INPAINT_TELEA)
109
- image = Image.fromarray(image)
110
 
111
  arr = arr_orig.copy()
112
  mask_y = np.where(arr == arr[arr != 0][0])[0][0]
113
  arr[mask_y:, :] = 255
114
 
115
- new = Image.fromarray(arr)
116
- mask = new.copy()
117
 
118
- orig_size = image.size
119
- image = image.resize((512, 512))
120
  mask = mask.resize((512, 512))
121
 
122
  results = []
@@ -126,7 +130,7 @@ class NecklaceTryOn:
126
  output = self.pipeline(
127
  prompt=prompt,
128
  negative_prompt=negative_prompt,
129
- image=image,
130
  mask_image=mask,
131
  strength=0.95,
132
  guidance_score=9,
 
32
  torch.cuda.empty_cache()
33
  gc.collect()
34
 
35
+ def apply_necklace(self, image, jewellery):
36
+ """Apply necklace on the image and return modified image and mask."""
37
  image = np.array(image)
38
  copy_image = image.copy()
39
  jewellery = np.array(jewellery)
 
97
 
98
  gc.collect()
99
 
100
+ return Image.fromarray(result.astype(np.uint8)), Image.fromarray(binaryMask.astype(np.uint8)).convert("RGB")
101
+
102
+ @spaces.GPU
103
+ def clothing_try_on_n_necklace_try_on(self, image, jewellery):
104
+ """Main method for clothing and necklace try-on."""
105
+ result_image, mask = self.apply_necklace(image, jewellery)
106
 
107
  jewellery_mask = Image.fromarray(
108
+ np.bitwise_and(np.array(mask), np.array(result_image))
109
  )
110
  arr_orig = np.array(grayscale(mask))
111
 
112
+ result_image = cv2.inpaint(np.array(result_image), arr_orig, 15, cv2.INPAINT_TELEA)
113
+ result_image = Image.fromarray(result_image)
114
 
115
  arr = arr_orig.copy()
116
  mask_y = np.where(arr == arr[arr != 0][0])[0][0]
117
  arr[mask_y:, :] = 255
118
 
119
+ new_mask = Image.fromarray(arr)
120
+ mask = new_mask.copy()
121
 
122
+ orig_size = result_image.size
123
+ result_image = result_image.resize((512, 512))
124
  mask = mask.resize((512, 512))
125
 
126
  results = []
 
130
  output = self.pipeline(
131
  prompt=prompt,
132
  negative_prompt=negative_prompt,
133
+ image=result_image,
134
  mask_image=mask,
135
  strength=0.95,
136
  guidance_score=9,