Spaces:
fantos
/
Runtime error

arxivgpt kim commited on
Commit
cffeaa2
·
verified ·
1 Parent(s): 8f831fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -16
app.py CHANGED
@@ -29,36 +29,38 @@ def resize_image(image):
29
 
30
 
31
  def process(image):
 
 
 
 
 
 
32
 
33
- # prepare input
34
- orig_image = Image.fromarray(image)
35
- w,h = orig_im_size = orig_image.size
36
  image = resize_image(orig_image)
37
  im_np = np.array(image)
38
- im_tensor = torch.tensor(im_np, dtype=torch.float32).permute(2,0,1)
39
- im_tensor = torch.unsqueeze(im_tensor,0)
40
- im_tensor = torch.divide(im_tensor,255.0)
41
- im_tensor = normalize(im_tensor,[0.5,0.5,0.5],[1.0,1.0,1.0])
42
  if torch.cuda.is_available():
43
- im_tensor=im_tensor.cuda()
44
 
45
- #inference
46
- result=net(im_tensor)
47
  # post process
48
- result = torch.squeeze(F.interpolate(result[0][0], size=(h,w), mode='bilinear') ,0)
49
  ma = torch.max(result)
50
  mi = torch.min(result)
51
- result = (result-mi)/(ma-mi)
52
  # image to pil
53
- im_array = (result*255).cpu().data.numpy().astype(np.uint8)
54
  pil_im = Image.fromarray(np.squeeze(im_array))
55
  # paste the mask on the original image
56
- new_im = Image.new("RGBA", pil_im.size, (0,0,0,0))
57
  new_im.paste(orig_image, mask=pil_im)
58
- # new_orig_image = orig_image.convert('RGBA')
59
 
60
  return new_im
61
- # return [new_orig_image, new_im]
62
 
63
  def calculate_position(org_size, add_size, position):
64
  if position == "상단 좌측":
 
29
 
30
 
31
  def process(image):
32
+ # 이미지가 numpy 배열인 경우에만 PIL.Image 객체로 변환
33
+ if isinstance(image, np.ndarray):
34
+ orig_image = Image.fromarray(image)
35
+ else:
36
+ # 이미 PIL.Image.Image 객체인 경우, 변환 없이 사용
37
+ orig_image = image
38
 
39
+ w, h = orig_im_size = orig_image.size
 
 
40
  image = resize_image(orig_image)
41
  im_np = np.array(image)
42
+ im_tensor = torch.tensor(im_np, dtype=torch.float32).permute(2, 0, 1)
43
+ im_tensor = torch.unsqueeze(im_tensor, 0)
44
+ im_tensor = torch.divide(im_tensor, 255.0)
45
+ im_tensor = normalize(im_tensor, [0.5, 0.5, 0.5], [1.0, 1.0, 1.0])
46
  if torch.cuda.is_available():
47
+ im_tensor = im_tensor.cuda()
48
 
49
+ # inference
50
+ result = net(im_tensor)
51
  # post process
52
+ result = torch.squeeze(F.interpolate(result[0][0], size=(h, w), mode='bilinear'), 0)
53
  ma = torch.max(result)
54
  mi = torch.min(result)
55
+ result = (result - mi) / (ma - mi)
56
  # image to pil
57
+ im_array = (result * 255).cpu().data.numpy().astype(np.uint8)
58
  pil_im = Image.fromarray(np.squeeze(im_array))
59
  # paste the mask on the original image
60
+ new_im = Image.new("RGBA", pil_im.size, (0, 0, 0, 0))
61
  new_im.paste(orig_image, mask=pil_im)
 
62
 
63
  return new_im
 
64
 
65
  def calculate_position(org_size, add_size, position):
66
  if position == "상단 좌측":