yizhangliu commited on
Commit
6717f64
1 Parent(s): 72b7fc6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -12
app.py CHANGED
@@ -318,18 +318,87 @@ def model_process_filepath(input): #image, mask):
318
  image = Image.open(io.BytesIO(numpy_to_bytes(res_np_img, ext)))
319
  image.save(f'./result_image.png')
320
  return image # image
321
- '''
322
- ext = get_image_ext(origin_image_bytes)
323
 
324
- response = make_response(
325
- send_file(
326
- io.BytesIO(numpy_to_bytes(res_np_img, ext)),
327
- mimetype=f"image/{ext}",
328
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
329
  )
330
- response.headers["X-Seed"] = str(config.sd_seed)
331
- return response
332
- '''
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333
 
334
  model = ModelManager(
335
  name='lama',
@@ -377,9 +446,26 @@ def predict(input):
377
  # mask = dict["mask"] # .convert("RGB") #.resize((512, 512))
378
  '''
379
  if image_type == 'filepath':
380
- output = model_process_filepath(input) # dict["image"], dict["mask"])
 
 
 
 
 
 
 
 
381
  elif image_type == 'pil':
382
- output = model_process_pil(input)
 
 
 
 
 
 
 
 
 
383
 
384
  # output = mask #output.images[0]
385
  # output = pipe(prompt = prompt, image=init_image, mask_image=mask,guidance_scale=7.5)
 
318
  image = Image.open(io.BytesIO(numpy_to_bytes(res_np_img, ext)))
319
  image.save(f'./result_image.png')
320
  return image # image
 
 
321
 
322
+ def model_process(image, mask, alpha_channel, ext):
323
+ global model
324
+ original_shape = image.shape
325
+ interpolation = cv2.INTER_CUBIC
326
+
327
+ # image_pil = Image.fromarray(image)
328
+ # mask_pil = Image.fromarray(mask).convert("L")
329
+
330
+ size_limit = "Original"
331
+ print(f'size_limit_2_ = {size_limit}')
332
+ if size_limit == "Original":
333
+ size_limit = max(image.shape)
334
+ else:
335
+ size_limit = int(size_limit)
336
+ print(f'size_limit_3_ = {size_limit}')
337
+
338
+ config = Config(
339
+ ldm_steps=25,
340
+ ldm_sampler='plms',
341
+ zits_wireframe=True,
342
+ hd_strategy='Original',
343
+ hd_strategy_crop_margin=196,
344
+ hd_strategy_crop_trigger_size=1280,
345
+ hd_strategy_resize_limit=2048,
346
+ prompt='',
347
+ use_croper=False,
348
+ croper_x=0,
349
+ croper_y=0,
350
+ croper_height=512,
351
+ croper_width=512,
352
+ sd_mask_blur=5,
353
+ sd_strength=0.75,
354
+ sd_steps=50,
355
+ sd_guidance_scale=7.5,
356
+ sd_sampler='ddim',
357
+ sd_seed=42,
358
+ cv2_flag='INPAINT_NS',
359
+ cv2_radius=5,
360
  )
361
+
362
+ print(f'config/alpha_channel/size_limit = {config} / {alpha_channel} / {size_limit}')
363
+ if config.sd_seed == -1:
364
+ config.sd_seed = random.randint(1, 999999999)
365
+
366
+ logger.info(f"Origin image shape: {original_shape}")
367
+ print(f"Origin image shape: {original_shape} / {image[250][250]}")
368
+ image = resize_max_size(image, size_limit=size_limit, interpolation=interpolation)
369
+ logger.info(f"Resized image shape: {image.shape} / {type(image)}")
370
+ print(f"Resized image shape: {image.shape} / {image[250][250]}")
371
+
372
+ mask = resize_max_size(mask, size_limit=size_limit, interpolation=interpolation)
373
+ print(f"mask image shape: {mask.shape} / {type(mask)} / {mask[250][250]} / {alpha_channel}")
374
+
375
+ if model is None:
376
+ return None
377
+
378
+ start = time.time()
379
+ res_np_img = model(image, mask, config)
380
+ logger.info(f"process time: {(time.time() - start) * 1000}ms, {res_np_img.shape}")
381
+ print(f"process time_1_: {(time.time() - start) * 1000}ms, {alpha_channel.shape}, {res_np_img.shape} / {res_np_img[250][250]} / {res_np_img.dtype}")
382
+
383
+ torch.cuda.empty_cache()
384
+
385
+ if alpha_channel is not None:
386
+ print(f"liuyz_here_10_: {alpha_channel.shape} / {alpha_channel.dtype} / {res_np_img.dtype}")
387
+ if alpha_channel.shape[:2] != res_np_img.shape[:2]:
388
+ print(f"liuyz_here_20_: {alpha_channel.shape} / {res_np_img.shape}")
389
+ alpha_channel = cv2.resize(
390
+ alpha_channel, dsize=(res_np_img.shape[1], res_np_img.shape[0])
391
+ )
392
+ print(f"liuyz_here_30_: {alpha_channel.shape} / {res_np_img.shape} / {alpha_channel.dtype} / {res_np_img.dtype}")
393
+ res_np_img = np.concatenate(
394
+ (res_np_img, alpha_channel[:, :, np.newaxis]), axis=-1
395
+ )
396
+ print(f"liuyz_here_40_: {alpha_channel.shape} / {res_np_img.shape} / {alpha_channel.dtype} / {res_np_img.dtype}")
397
+
398
+ print(f"process time_2_: {(time.time() - start) * 1000}ms, {alpha_channel.shape}, {res_np_img.shape} / {res_np_img[250][250]} / {res_np_img.dtype} /{ext}")
399
+
400
+ image = Image.open(io.BytesIO(numpy_to_bytes(res_np_img, ext)))
401
+ return image # image
402
 
403
  model = ModelManager(
404
  name='lama',
 
446
  # mask = dict["mask"] # .convert("RGB") #.resize((512, 512))
447
  '''
448
  if image_type == 'filepath':
449
+ # input: {'image': '/tmp/tmp8mn9xw93.png', 'mask': '/tmp/tmpn5ars4te.png'}
450
+ origin_image_bytes = read_content(input["image"])
451
+ print(f'origin_image_bytes = ', type(origin_image_bytes), len(origin_image_bytes))
452
+ image, _ = load_img(origin_image_bytes)
453
+ mask, _ = load_img(read_content(input["mask"]), gray=True)
454
+ alpha_channel = (np.ones((image.shape[0],image.shape[1]))*255).astype(np.uint8)
455
+ ext = get_image_ext(origin_image_bytes)
456
+
457
+ output = model_process(image, mask, alpha_channel, ext)
458
  elif image_type == 'pil':
459
+ # input: {'image': pil, 'mask': pil}
460
+ image_pil = input['image']
461
+ mask_pil = input['mask']
462
+
463
+ image = np.array(image_pil)
464
+ mask = np.array(mask_pil.convert("L"))
465
+ alpha_channel = (np.ones((image.shape[0],image.shape[1]))*255).astype(np.uint8)
466
+ ext = 'png'
467
+
468
+ output = model_process(image, mask, alpha_channel, ext)
469
 
470
  # output = mask #output.images[0]
471
  # output = pipe(prompt = prompt, image=init_image, mask_image=mask,guidance_scale=7.5)