Spaces:
Sleeping
Sleeping
checkpoint GFPGANS rollback
Browse files
app.py
CHANGED
|
@@ -1,6 +1,3 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from gradio import themes
|
| 3 |
-
from PIL import Image
|
| 4 |
import numpy as np
|
| 5 |
import cv2
|
| 6 |
import torch
|
|
@@ -8,9 +5,11 @@ import albumentations as albu
|
|
| 8 |
from pylab import imshow
|
| 9 |
import matplotlib.pyplot as plt
|
| 10 |
from diffusers import StableDiffusionInpaintPipeline
|
|
|
|
| 11 |
from iglovikov_helper_functions.utils.image_utils import load_rgb, pad, unpad
|
| 12 |
from iglovikov_helper_functions.dl.pytorch.utils import tensor_from_rgb_image
|
| 13 |
-
from cloths_segmentation.pre_trained_models import create_model
|
|
|
|
| 14 |
|
| 15 |
# Load Cloth Segmentation Model (Ensure this is available)
|
| 16 |
try:
|
|
@@ -25,15 +24,6 @@ try:
|
|
| 25 |
except Exception as e:
|
| 26 |
raise RuntimeError(f"Error loading inpainting model: {e}")
|
| 27 |
|
| 28 |
-
try:
|
| 29 |
-
from gfpgan import GFPGANer
|
| 30 |
-
except ModuleNotFoundError:
|
| 31 |
-
import subprocess
|
| 32 |
-
subprocess.run(["pip", "install", "gfpgan"])
|
| 33 |
-
|
| 34 |
-
# Initialize GFPGAN
|
| 35 |
-
gfpgan = GFPGANer(model_path='https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth',upscale=1, device=torch.device('cpu'))
|
| 36 |
-
|
| 37 |
|
| 38 |
def load_and_preprocess_image(image_path):
|
| 39 |
image = load_rgb(image_path)
|
|
@@ -82,23 +72,26 @@ def image_segmentation_and_inpainting(image, prompt="Chinese Red and Golder Armo
|
|
| 82 |
# Resize the output image to match the original image's dimensions
|
| 83 |
output_image = resize_and_upscale(output_image, original_image.shape[1], original_image.shape[0])
|
| 84 |
|
| 85 |
-
|
| 86 |
-
_, _, output = gfpgan.enhance(np.array(output_image), has_aligned=False, only_center_face=False, paste_back=True)
|
| 87 |
-
enhanced_image = Image.fromarray(output)
|
| 88 |
-
|
| 89 |
-
return output_image, enhanced_image # Return both images
|
| 90 |
-
|
| 91 |
except Exception as e:
|
| 92 |
raise gr.Error(f"Error processing image: {e}")
|
| 93 |
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
theme=themes.Soft(primary_hue="blue", secondary_hue="blue"),
|
| 98 |
-
) as demo:
|
| 99 |
with gr.Row():
|
| 100 |
# Header with Image and Description
|
| 101 |
-
gr.HTML(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
with gr.Row():
|
| 104 |
with gr.Column():
|
|
@@ -107,12 +100,7 @@ with gr.Blocks(
|
|
| 107 |
run_button = gr.Button("Run")
|
| 108 |
with gr.Column():
|
| 109 |
image_output = gr.Image(label="Result")
|
| 110 |
-
enhanced_image_output = gr.Image(label="Enhanced Result") # Output for enhanced image
|
| 111 |
|
| 112 |
-
run_button.click(
|
| 113 |
-
fn=image_segmentation_and_inpainting,
|
| 114 |
-
inputs=[image_input, prompt_input],
|
| 115 |
-
outputs=[image_output, enhanced_image_output] # Update outputs
|
| 116 |
-
)
|
| 117 |
|
| 118 |
demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
import cv2
|
| 3 |
import torch
|
|
|
|
| 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 |
try:
|
|
|
|
| 24 |
except Exception as e:
|
| 25 |
raise RuntimeError(f"Error loading inpainting model: {e}")
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
def load_and_preprocess_image(image_path):
|
| 29 |
image = load_rgb(image_path)
|
|
|
|
| 72 |
# Resize the output image to match the original image's dimensions
|
| 73 |
output_image = resize_and_upscale(output_image, original_image.shape[1], original_image.shape[0])
|
| 74 |
|
| 75 |
+
return output_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
except Exception as e:
|
| 77 |
raise gr.Error(f"Error processing image: {e}")
|
| 78 |
|
| 79 |
|
| 80 |
+
|
| 81 |
+
with gr.Blocks() as demo:
|
|
|
|
|
|
|
| 82 |
with gr.Row():
|
| 83 |
# Header with Image and Description
|
| 84 |
+
gr.HTML(
|
| 85 |
+
"""
|
| 86 |
+
<div style="display: flex; align-items: center;">
|
| 87 |
+
<img src="https://ant.dpu.ac.th/wp-content/uploads/2024/04/dpulogo.png" style="width: 100px; margin-right: 20px;">
|
| 88 |
+
<div>
|
| 89 |
+
<h1>Cloth Image Segmentation and Inpainting</h1>
|
| 90 |
+
<p>This research project explores cloth segmentation using a specialized library, followed by inpainting with Stable Diffusion using a new prompt. It is conducted by the College of Creative Design and Entertainment Technology, Dhurakij Pundit University, in the lab of Asst. Prof. Banyapon Poolsawas under the MIT License.</p>
|
| 91 |
+
</div>
|
| 92 |
+
</div>
|
| 93 |
+
"""
|
| 94 |
+
)
|
| 95 |
|
| 96 |
with gr.Row():
|
| 97 |
with gr.Column():
|
|
|
|
| 100 |
run_button = gr.Button("Run")
|
| 101 |
with gr.Column():
|
| 102 |
image_output = gr.Image(label="Result")
|
|
|
|
| 103 |
|
| 104 |
+
run_button.click(fn=image_segmentation_and_inpainting, inputs=[image_input, prompt_input], outputs=image_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
demo.launch(share=True)
|