Spaces:
fantos
/
Runtime error

arxivgpt kim commited on
Commit
dc146a8
·
verified ·
1 Parent(s): 597ebe6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -19
app.py CHANGED
@@ -22,32 +22,33 @@ def resize_image(image, model_input_size=(1024, 1024)):
22
  image = image.resize(model_input_size, Image.BILINEAR)
23
  return image
24
 
25
- def process(image, background_image=None):
26
- # 이미지 전처리
27
- orig_image = Image.fromarray(image.astype(np.uint8))
28
- resized_image = resize_image(orig_image)
29
- im_tensor = torch.tensor(np.array(resized_image), dtype=torch.float32).permute(2, 0, 1).unsqueeze(0) / 255.0
 
 
30
  im_tensor = normalize(im_tensor, [0.5, 0.5, 0.5], [1.0, 1.0, 1.0])
31
  if torch.cuda.is_available():
32
  im_tensor = im_tensor.cuda()
33
-
34
- # 배경 제거 모델 추론
35
  with torch.no_grad():
36
- output = net(im_tensor)
37
- output = F.interpolate(output[0][0], size=orig_image.size[::-1], mode='bilinear', align_corners=False)
38
- output = torch.sigmoid(output).cpu().numpy().squeeze()
39
- mask = (output * 255).astype(np.uint8)
40
- new_im = Image.fromarray(mask).convert("L")
 
41
 
42
- result_image = Image.new("RGBA", orig_image.size)
43
- orig_image_rgba = orig_image.convert("RGBA")
44
- result_image.paste(orig_image_rgba, mask=new_im)
 
45
 
46
- # 배경 이미지가 제공된 경우, 합성
47
- if background_image is not None:
48
- result_image = merge_images(background_image, result_image)
49
 
50
- return result_image
51
 
52
  def merge_images(background_image, foreground_image):
53
  """
 
22
  image = image.resize(model_input_size, Image.BILINEAR)
23
  return image
24
 
25
+ def process(image):
26
+ # prepare input
27
+ orig_image = Image.fromarray(image).convert("RGBA")
28
+ w, h = orig_im_size = orig_image.size
29
+ image = resize_image(orig_image)
30
+ im_np = np.array(image)
31
+ im_tensor = torch.tensor(im_np, dtype=torch.float32).permute(2, 0, 1).unsqueeze(0) / 255.0
32
  im_tensor = normalize(im_tensor, [0.5, 0.5, 0.5], [1.0, 1.0, 1.0])
33
  if torch.cuda.is_available():
34
  im_tensor = im_tensor.cuda()
35
+
36
+ # inference
37
  with torch.no_grad():
38
+ result = net(im_tensor)
39
+
40
+ # post process
41
+ result = torch.squeeze(F.interpolate(result[0][0], size=(h, w), mode='bilinear', align_corners=False), 0)
42
+ result = torch.sigmoid(result) # 마스크의 투명도를 결정하는 확률 값으로 변환
43
+ mask = (result * 255).byte().cpu().numpy() # 마스크를 0~255 사이의 값으로 변환
44
 
45
+ # 마스크를 RGBA 이미지로 변환하고, 마스크의 알파 채널을 사용
46
+ mask_image = Image.fromarray(mask).convert("L")
47
+ final_image = Image.new("RGBA", orig_image.size)
48
+ final_image.paste(orig_image, mask=mask_image)
49
 
50
+ return final_image
 
 
51
 
 
52
 
53
  def merge_images(background_image, foreground_image):
54
  """