Spaces:
Sleeping
Sleeping
Add main space app.py file
Browse files
app.py
CHANGED
|
@@ -9,6 +9,7 @@ from PIL import Image
|
|
| 9 |
from iglovikov_helper_functions.utils.image_utils import load_rgb, pad, unpad
|
| 10 |
from iglovikov_helper_functions.dl.pytorch.utils import tensor_from_rgb_image
|
| 11 |
from cloths_segmentation.pre_trained_models import create_model
|
|
|
|
| 12 |
|
| 13 |
# Load Cloth Segmentation Model (Ensure this is available)
|
| 14 |
model = create_model("Unet_2020-10-30")
|
|
@@ -16,6 +17,7 @@ model.eval()
|
|
| 16 |
|
| 17 |
# Load Inpainting Model (Ensure this is available)
|
| 18 |
pipe = StableDiffusionInpaintPipeline.from_pretrained("runwayml/stable-diffusion-inpainting")
|
|
|
|
| 19 |
|
| 20 |
def load_and_preprocess_image(image_path):
|
| 21 |
image = load_rgb(image_path)
|
|
@@ -35,8 +37,7 @@ def segment_cloth(image_tensor, model, pads):
|
|
| 35 |
def perform_inpainting(image_path, mask_path, prompt):
|
| 36 |
image = Image.open(image_path)
|
| 37 |
mask_image = Image.open(mask_path).convert("L") # Convert to single-channel grayscale
|
| 38 |
-
mask_image = mask_image.resize(image.size)
|
| 39 |
-
|
| 40 |
output_image = pipe(prompt=prompt, image=image, mask_image=mask_image).images[0]
|
| 41 |
return output_image
|
| 42 |
|
|
@@ -44,24 +45,27 @@ def resize_and_upscale(image, new_width, new_height):
|
|
| 44 |
resized_img = cv2.resize(np.array(image), (new_width, new_height), interpolation=cv2.INTER_CUBIC)
|
| 45 |
return Image.fromarray(resized_img)
|
| 46 |
|
| 47 |
-
import gradio as gr
|
| 48 |
|
| 49 |
def image_segmentation_and_inpainting(image, prompt="Chinese Red and Golder Armor"):
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
mask = segment_cloth(x, model, pads)
|
| 52 |
|
| 53 |
-
# Save mask temporarily
|
| 54 |
mask_path = "temp_mask.jpg"
|
| 55 |
plt.imsave(mask_path, mask, cmap='gray')
|
| 56 |
|
| 57 |
-
output_image = perform_inpainting(
|
| 58 |
-
output_image = resize_and_upscale(output_image, 1280, 720) #
|
| 59 |
-
return output_image
|
| 60 |
|
|
|
|
| 61 |
|
| 62 |
with gr.Blocks() as demo:
|
| 63 |
gr.Markdown("# Cloth Image Segmentation and Inpainting")
|
| 64 |
-
|
| 65 |
with gr.Row():
|
| 66 |
with gr.Column():
|
| 67 |
image_input = gr.Image(label="Upload Image")
|
|
@@ -72,4 +76,4 @@ with gr.Blocks() as demo:
|
|
| 72 |
|
| 73 |
run_button.click(fn=image_segmentation_and_inpainting, inputs=[image_input, prompt_input], outputs=image_output)
|
| 74 |
|
| 75 |
-
demo.launch(share=True)
|
|
|
|
| 9 |
from iglovikov_helper_functions.utils.image_utils import load_rgb, pad, unpad
|
| 10 |
from iglovikov_helper_functions.dl.pytorch.utils import tensor_from_rgb_image
|
| 11 |
from cloths_segmentation.pre_trained_models import create_model
|
| 12 |
+
import gradio as gr
|
| 13 |
|
| 14 |
# Load Cloth Segmentation Model (Ensure this is available)
|
| 15 |
model = create_model("Unet_2020-10-30")
|
|
|
|
| 17 |
|
| 18 |
# Load Inpainting Model (Ensure this is available)
|
| 19 |
pipe = StableDiffusionInpaintPipeline.from_pretrained("runwayml/stable-diffusion-inpainting")
|
| 20 |
+
pipe.to("cuda") # If you have a GPU, this will run the model on it
|
| 21 |
|
| 22 |
def load_and_preprocess_image(image_path):
|
| 23 |
image = load_rgb(image_path)
|
|
|
|
| 37 |
def perform_inpainting(image_path, mask_path, prompt):
|
| 38 |
image = Image.open(image_path)
|
| 39 |
mask_image = Image.open(mask_path).convert("L") # Convert to single-channel grayscale
|
| 40 |
+
mask_image = mask_image.resize(image.size)
|
|
|
|
| 41 |
output_image = pipe(prompt=prompt, image=image, mask_image=mask_image).images[0]
|
| 42 |
return output_image
|
| 43 |
|
|
|
|
| 45 |
resized_img = cv2.resize(np.array(image), (new_width, new_height), interpolation=cv2.INTER_CUBIC)
|
| 46 |
return Image.fromarray(resized_img)
|
| 47 |
|
|
|
|
| 48 |
|
| 49 |
def image_segmentation_and_inpainting(image, prompt="Chinese Red and Golder Armor"):
|
| 50 |
+
pil_image = Image.fromarray(image.astype('uint8')) # Gradio provides image as np.array
|
| 51 |
+
|
| 52 |
+
temp_image_path = "temp_image.jpg"
|
| 53 |
+
pil_image.save(temp_image_path)
|
| 54 |
+
|
| 55 |
+
x, image, pads = load_and_preprocess_image(temp_image_path)
|
| 56 |
mask = segment_cloth(x, model, pads)
|
| 57 |
|
|
|
|
| 58 |
mask_path = "temp_mask.jpg"
|
| 59 |
plt.imsave(mask_path, mask, cmap='gray')
|
| 60 |
|
| 61 |
+
output_image = perform_inpainting(temp_image_path, mask_path, prompt)
|
| 62 |
+
output_image = resize_and_upscale(output_image, 1280, 720) # You can adjust the size
|
|
|
|
| 63 |
|
| 64 |
+
return output_image
|
| 65 |
|
| 66 |
with gr.Blocks() as demo:
|
| 67 |
gr.Markdown("# Cloth Image Segmentation and Inpainting")
|
| 68 |
+
|
| 69 |
with gr.Row():
|
| 70 |
with gr.Column():
|
| 71 |
image_input = gr.Image(label="Upload Image")
|
|
|
|
| 76 |
|
| 77 |
run_button.click(fn=image_segmentation_and_inpainting, inputs=[image_input, prompt_input], outputs=image_output)
|
| 78 |
|
| 79 |
+
demo.launch(share=True)
|