Spaces:
Sleeping
Sleeping
Add main space app.py update
Browse files- app.py +31 -21
- requirements.txt +3 -2
app.py
CHANGED
|
@@ -1,23 +1,33 @@
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
import cv2
|
| 3 |
-
import
|
| 4 |
import albumentations as albu
|
| 5 |
from pylab import imshow
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
from diffusers import StableDiffusionInpaintPipeline
|
| 8 |
-
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 |
import gradio as gr
|
| 13 |
|
| 14 |
# Load Cloth Segmentation Model (Ensure this is available)
|
| 15 |
-
|
| 16 |
-
model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
|
@@ -36,7 +46,7 @@ def segment_cloth(image_tensor, model, pads):
|
|
| 36 |
|
| 37 |
def perform_inpainting(image_path, mask_path, prompt):
|
| 38 |
image = Image.open(image_path)
|
| 39 |
-
mask_image = Image.open(mask_path).convert("L")
|
| 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
|
|
@@ -47,25 +57,25 @@ def resize_and_upscale(image, new_width, new_height):
|
|
| 47 |
|
| 48 |
|
| 49 |
def image_segmentation_and_inpainting(image, prompt="Chinese Red and Golder Armor"):
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 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")
|
|
|
|
| 1 |
+
import torch
|
| 2 |
import numpy as np
|
| 3 |
import cv2
|
| 4 |
+
from PIL import Image
|
| 5 |
import albumentations as albu
|
| 6 |
from pylab import imshow
|
| 7 |
import matplotlib.pyplot as plt
|
| 8 |
from diffusers import StableDiffusionInpaintPipeline
|
|
|
|
| 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 |
+
try:
|
| 16 |
+
model = create_model("Unet_2020-10-30")
|
| 17 |
+
model.eval()
|
| 18 |
+
except Exception as e:
|
| 19 |
+
raise RuntimeError(f"Error loading segmentation model: {e}")
|
| 20 |
+
|
| 21 |
+
# Load Inpainting Model
|
| 22 |
+
try:
|
| 23 |
+
pipe = StableDiffusionInpaintPipeline.from_pretrained(
|
| 24 |
+
"runwayml/stable-diffusion-inpainting",
|
| 25 |
+
torch_dtype=torch.float16
|
| 26 |
+
)
|
| 27 |
+
pipe.to("cuda")
|
| 28 |
+
except Exception as e:
|
| 29 |
+
raise RuntimeError(f"Error loading inpainting model: {e}")
|
| 30 |
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
def load_and_preprocess_image(image_path):
|
| 33 |
image = load_rgb(image_path)
|
|
|
|
| 46 |
|
| 47 |
def perform_inpainting(image_path, mask_path, prompt):
|
| 48 |
image = Image.open(image_path)
|
| 49 |
+
mask_image = Image.open(mask_path).convert("L")
|
| 50 |
mask_image = mask_image.resize(image.size)
|
| 51 |
output_image = pipe(prompt=prompt, image=image, mask_image=mask_image).images[0]
|
| 52 |
return output_image
|
|
|
|
| 57 |
|
| 58 |
|
| 59 |
def image_segmentation_and_inpainting(image, prompt="Chinese Red and Golder Armor"):
|
| 60 |
+
try:
|
| 61 |
+
pil_image = Image.fromarray(image.astype('uint8'))
|
| 62 |
+
temp_image_path = "temp_image.jpg"
|
| 63 |
+
pil_image.save(temp_image_path)
|
| 64 |
|
| 65 |
+
x, image, pads = load_and_preprocess_image(temp_image_path)
|
| 66 |
+
mask = segment_cloth(x, model, 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 |
+
output_image = resize_and_upscale(output_image, 1280, 720)
|
| 73 |
+
return output_image
|
| 74 |
+
except Exception as e:
|
| 75 |
+
raise gr.Error(f"Error processing image: {e}")
|
|
|
|
|
|
|
| 76 |
|
| 77 |
with gr.Blocks() as demo:
|
| 78 |
gr.Markdown("# Cloth Image Segmentation and Inpainting")
|
|
|
|
| 79 |
with gr.Row():
|
| 80 |
with gr.Column():
|
| 81 |
image_input = gr.Image(label="Upload Image")
|
requirements.txt
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
torch
|
|
|
|
| 2 |
albumentations
|
| 3 |
matplotlib
|
| 4 |
diffusers
|
| 5 |
transformers
|
| 6 |
-
iglovikov_helper_functions
|
| 7 |
-
cloths_segmentation
|
| 8 |
opencv-python-headless
|
| 9 |
gradio
|
| 10 |
numpy==1.23.5 # Example version; adjust if necessary
|
|
|
|
| 1 |
torch
|
| 2 |
+
accelerate
|
| 3 |
albumentations
|
| 4 |
matplotlib
|
| 5 |
diffusers
|
| 6 |
transformers
|
| 7 |
+
iglovikov_helper_functions
|
| 8 |
+
cloths_segmentation
|
| 9 |
opencv-python-headless
|
| 10 |
gradio
|
| 11 |
numpy==1.23.5 # Example version; adjust if necessary
|