banyapon commited on
Commit
f9e0010
·
1 Parent(s): e7e47ce

Add main space test file

Browse files
Files changed (1) hide show
  1. app.py +15 -5
app.py CHANGED
@@ -28,9 +28,12 @@ except Exception as e:
28
 
29
  def load_and_preprocess_image(image_path):
30
  image = load_rgb(image_path)
31
- padded_image, pads = pad(image, factor=32, border=cv2.BORDER_CONSTANT)
32
- x = torch.unsqueeze(tensor_from_rgb_image(image), 0) # Use original image, not padded
33
- return x, image, pads
 
 
 
34
 
35
  def segment_cloth(image_tensor, model, pads):
36
  with torch.no_grad():
@@ -56,19 +59,26 @@ def image_segmentation_and_inpainting(image, prompt="Chinese Red and Golder Armo
56
  temp_image_path = "temp_image.jpg"
57
  pil_image.save(temp_image_path)
58
 
59
- x, image, pads = load_and_preprocess_image(temp_image_path)
60
  mask = segment_cloth(x, model, pads)
61
 
 
 
 
62
  mask_path = "temp_mask.jpg"
63
  plt.imsave(mask_path, mask, cmap='gray')
64
 
65
  output_image = perform_inpainting(temp_image_path, mask_path, prompt)
66
- output_image = resize_and_upscale(output_image, 1280, 720) # You can adjust the size
 
 
67
 
68
  return output_image
69
  except Exception as e:
70
  raise gr.Error(f"Error processing image: {e}")
71
 
 
 
72
  with gr.Blocks() as demo:
73
  gr.Markdown("# Cloth Image Segmentation and Inpainting")
74
  with gr.Row():
 
28
 
29
  def load_and_preprocess_image(image_path):
30
  image = load_rgb(image_path)
31
+ # Pad the image to be divisible by 32
32
+ padded_image, pads = pad(image, factor=32, border=cv2.BORDER_CONSTANT)
33
+ transform = albu.Compose([albu.Normalize(p=1)], p=1)
34
+ x = transform(image=padded_image)["image"] # Use padded image here
35
+ x = torch.unsqueeze(tensor_from_rgb_image(x), 0)
36
+ return x, image, pads # Return original image and padding info for later unpadding
37
 
38
  def segment_cloth(image_tensor, model, pads):
39
  with torch.no_grad():
 
59
  temp_image_path = "temp_image.jpg"
60
  pil_image.save(temp_image_path)
61
 
62
+ x, original_image, pads = load_and_preprocess_image(temp_image_path)
63
  mask = segment_cloth(x, model, pads)
64
 
65
+ # Ensure mask has same dimensions as original image before saving
66
+ mask = unpad(mask, pads)
67
+
68
  mask_path = "temp_mask.jpg"
69
  plt.imsave(mask_path, mask, cmap='gray')
70
 
71
  output_image = perform_inpainting(temp_image_path, mask_path, prompt)
72
+
73
+ # Resize the output image to match the original image's dimensions
74
+ output_image = resize_and_upscale(output_image, original_image.shape[1], original_image.shape[0])
75
 
76
  return output_image
77
  except Exception as e:
78
  raise gr.Error(f"Error processing image: {e}")
79
 
80
+
81
+
82
  with gr.Blocks() as demo:
83
  gr.Markdown("# Cloth Image Segmentation and Inpainting")
84
  with gr.Row():