Spaces:
Running
Running
yizhangliu
commited on
Commit
•
08d22d8
1
Parent(s):
f3f61d9
Update app.py
Browse files
app.py
CHANGED
@@ -120,13 +120,14 @@ def load_img_1_(nparr, gray: bool = False):
|
|
120 |
return np_img, alpha_channel
|
121 |
|
122 |
model = None
|
123 |
-
def
|
124 |
global model
|
125 |
|
126 |
# input = request.files
|
127 |
# RGB
|
128 |
# origin_image_bytes = input["image"].read()
|
129 |
-
|
|
|
130 |
print(f'liuyz_2_here_', type(image), image.shape)
|
131 |
|
132 |
image_pil = Image.fromarray(image)
|
@@ -138,7 +139,7 @@ def model_process_1(image, mask):
|
|
138 |
#image, alpha_channel = load_img(image)
|
139 |
# Origin image shape: (512, 512, 3)
|
140 |
|
141 |
-
alpha_channel =
|
142 |
original_shape = image.shape
|
143 |
interpolation = cv2.INTER_CUBIC
|
144 |
|
@@ -188,7 +189,7 @@ def model_process_1(image, mask):
|
|
188 |
print(f"Resized image shape: {image.shape} / {image[250][250]}")
|
189 |
|
190 |
# mask, _ = load_img(mask, gray=True)
|
191 |
-
mask = np.array(mask_pil)
|
192 |
mask = resize_max_size(mask, size_limit=size_limit, interpolation=interpolation)
|
193 |
print(f"mask image shape: {mask.shape} / {type(mask)} / {mask[250][250]}")
|
194 |
|
@@ -201,10 +202,7 @@ def model_process_1(image, mask):
|
|
201 |
print(f"process time: {(time.time() - start) * 1000}ms, {res_np_img.shape} / {res_np_img[250][250]}")
|
202 |
|
203 |
torch.cuda.empty_cache()
|
204 |
-
|
205 |
-
image.save(f'./result_image.png')
|
206 |
-
return image
|
207 |
-
'''
|
208 |
if alpha_channel is not None:
|
209 |
if alpha_channel.shape[:2] != res_np_img.shape[:2]:
|
210 |
alpha_channel = cv2.resize(
|
@@ -213,12 +211,16 @@ def model_process_1(image, mask):
|
|
213 |
res_np_img = np.concatenate(
|
214 |
(res_np_img, alpha_channel[:, :, np.newaxis]), axis=-1
|
215 |
)
|
216 |
-
|
|
|
|
|
|
|
|
|
217 |
ext = get_image_ext(origin_image_bytes)
|
218 |
return ext
|
219 |
'''
|
220 |
|
221 |
-
def
|
222 |
global model
|
223 |
# {'image': '/tmp/tmp8mn9xw93.png', 'mask': '/tmp/tmpn5ars4te.png'}
|
224 |
# input = request.files
|
@@ -355,7 +357,7 @@ def predict(input):
|
|
355 |
print(f'liuyz_3_', image.convert("RGB").resize((512, 512)).shape)
|
356 |
# mask = dict["mask"] # .convert("RGB") #.resize((512, 512))
|
357 |
'''
|
358 |
-
|
359 |
|
360 |
# output = mask #output.images[0]
|
361 |
# output = pipe(prompt = prompt, image=init_image, mask_image=mask,guidance_scale=7.5)
|
|
|
120 |
return np_img, alpha_channel
|
121 |
|
122 |
model = None
|
123 |
+
def model_process(input):
|
124 |
global model
|
125 |
|
126 |
# input = request.files
|
127 |
# RGB
|
128 |
# origin_image_bytes = input["image"].read()
|
129 |
+
image = input['image']
|
130 |
+
mask = input['mask']
|
131 |
print(f'liuyz_2_here_', type(image), image.shape)
|
132 |
|
133 |
image_pil = Image.fromarray(image)
|
|
|
139 |
#image, alpha_channel = load_img(image)
|
140 |
# Origin image shape: (512, 512, 3)
|
141 |
|
142 |
+
alpha_channel = np.ones((image.shape[0],image.shape[1]))*255
|
143 |
original_shape = image.shape
|
144 |
interpolation = cv2.INTER_CUBIC
|
145 |
|
|
|
189 |
print(f"Resized image shape: {image.shape} / {image[250][250]}")
|
190 |
|
191 |
# mask, _ = load_img(mask, gray=True)
|
192 |
+
# mask = np.array(mask_pil)
|
193 |
mask = resize_max_size(mask, size_limit=size_limit, interpolation=interpolation)
|
194 |
print(f"mask image shape: {mask.shape} / {type(mask)} / {mask[250][250]}")
|
195 |
|
|
|
202 |
print(f"process time: {(time.time() - start) * 1000}ms, {res_np_img.shape} / {res_np_img[250][250]}")
|
203 |
|
204 |
torch.cuda.empty_cache()
|
205 |
+
|
|
|
|
|
|
|
206 |
if alpha_channel is not None:
|
207 |
if alpha_channel.shape[:2] != res_np_img.shape[:2]:
|
208 |
alpha_channel = cv2.resize(
|
|
|
211 |
res_np_img = np.concatenate(
|
212 |
(res_np_img, alpha_channel[:, :, np.newaxis]), axis=-1
|
213 |
)
|
214 |
+
image = Image.fromarray(res_np_img)
|
215 |
+
image.save(f'./result_image.png')
|
216 |
+
return image
|
217 |
+
|
218 |
+
'''
|
219 |
ext = get_image_ext(origin_image_bytes)
|
220 |
return ext
|
221 |
'''
|
222 |
|
223 |
+
def model_process_2(input): #image, mask):
|
224 |
global model
|
225 |
# {'image': '/tmp/tmp8mn9xw93.png', 'mask': '/tmp/tmpn5ars4te.png'}
|
226 |
# input = request.files
|
|
|
357 |
print(f'liuyz_3_', image.convert("RGB").resize((512, 512)).shape)
|
358 |
# mask = dict["mask"] # .convert("RGB") #.resize((512, 512))
|
359 |
'''
|
360 |
+
output = model_process(input) # dict["image"], dict["mask"])
|
361 |
|
362 |
# output = mask #output.images[0]
|
363 |
# output = pipe(prompt = prompt, image=init_image, mask_image=mask,guidance_scale=7.5)
|