Spaces:
Runtime error
Runtime error
sharmaarush
commited on
Commit
•
29fad46
1
Parent(s):
192c276
initial commit
Browse files- README.md +27 -6
- app.py +91 -0
- requirements.txt +8 -0
README.md
CHANGED
@@ -1,13 +1,34 @@
|
|
1 |
---
|
2 |
-
title: Edit
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 4.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
11 |
---
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: Edit Image
|
3 |
+
emoji: 💻
|
4 |
+
colorFrom: purple
|
5 |
+
colorTo: purple
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.26.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
11 |
---
|
12 |
|
13 |
+
Background_editor
|
14 |
+
|
15 |
+
#Setting up locally:
|
16 |
+
|
17 |
+
1. git clone the repo and cd into the directory ` cd Edit_image`
|
18 |
+
2. install the dependencies using ` pip install -r requirements.txt`
|
19 |
+
3. run the app file using ` gradio app.py`
|
20 |
+
|
21 |
+
# Approach:
|
22 |
+
|
23 |
+
Reference: 'https://huggingface.co/docs/diffusers/en/using-diffusers/inpaint'
|
24 |
+
|
25 |
+
Model used: `runwayml/stable-diffusion-inpainting`
|
26 |
+
|
27 |
+
1. Loading the required image
|
28 |
+
2. Creating a binary mask for the image
|
29 |
+
3. Invert the mask created
|
30 |
+
4. Convert the mask into an image to be passes into the model pipline
|
31 |
+
5. Pass the inverted mask image into the pipeline along with the prompt
|
32 |
+
|
33 |
+
Collab Notebook link (easier and also can use gpu for faster compute)
|
34 |
+
Hosted on hugging face (`https://huggingface.co/spaces/sharmaarush/Edit_image`) this might take time because running on cpu
|
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from diffusers.utils import load_image, make_image_grid
|
2 |
+
import gradio as gr
|
3 |
+
import cv2
|
4 |
+
import numpy as np
|
5 |
+
from diffusers import StableDiffusionControlNetInpaintPipeline, ControlNetModel, UniPCMultistepScheduler
|
6 |
+
import torch
|
7 |
+
|
8 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
9 |
+
|
10 |
+
controlnet = ControlNetModel.from_pretrained("lllyasviel/control_v11p_sd15_inpaint", torch_dtype=torch.float16, use_safetensors=True)
|
11 |
+
pipe = StableDiffusionControlNetInpaintPipeline.from_pretrained(
|
12 |
+
"runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16, use_safetensors=True
|
13 |
+
).to(device)
|
14 |
+
|
15 |
+
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
|
16 |
+
pipe.enable_model_cpu_offload()
|
17 |
+
|
18 |
+
def create_mask(img):
|
19 |
+
img = cv2.imread(img, cv2.IMREAD_GRAYSCALE)
|
20 |
+
|
21 |
+
# Create a binary mask
|
22 |
+
mask = cv2.threshold(img, 254, 255, cv2.THRESH_BINARY)[1]
|
23 |
+
|
24 |
+
# Invert the mask
|
25 |
+
inverted_mask = cv2.bitwise_not(mask)
|
26 |
+
return mask
|
27 |
+
|
28 |
+
def load_and_resize_images(image_path, mask_path, target_size=(512, 512)):
|
29 |
+
init_image = load_image(image_path)
|
30 |
+
init_image = init_image.resize(target_size)
|
31 |
+
|
32 |
+
mask_image = load_image(mask_path)
|
33 |
+
mask_image = mask_image.resize(target_size)
|
34 |
+
|
35 |
+
return init_image, mask_image
|
36 |
+
|
37 |
+
def make_inpaint_condition(image, image_mask):
|
38 |
+
image = np.array(image.convert("RGB")).astype(np.float32) / 255.0
|
39 |
+
image_mask = np.array(image_mask.convert("L")).astype(np.float32) / 255.0
|
40 |
+
|
41 |
+
assert image.shape[0:1] == image_mask.shape[0:1]
|
42 |
+
image[image_mask > 0.5] = -1.0 # set as masked pixel
|
43 |
+
image = np.expand_dims(image, 0).transpose(0, 3, 1, 2)
|
44 |
+
image = torch.from_numpy(image)
|
45 |
+
return image
|
46 |
+
|
47 |
+
def generate_inpainting(init_image, mask_image, prompt, negative_prompt=None):
|
48 |
+
control_image = make_inpaint_condition(init_image, mask_image)
|
49 |
+
|
50 |
+
output = pipe(
|
51 |
+
prompt=prompt,
|
52 |
+
negative_prompt=negative_prompt,
|
53 |
+
num_inference_steps=50,
|
54 |
+
eta=1.0,
|
55 |
+
image=init_image,
|
56 |
+
mask_image=mask_image,
|
57 |
+
control_image=control_image,
|
58 |
+
).images[0]
|
59 |
+
|
60 |
+
return output
|
61 |
+
|
62 |
+
def process_images_and_generate_inpainting(image_path, mask_path, prompt, negative_prompt=None):
|
63 |
+
init_image, mask_image = load_and_resize_images(image_path, mask_path)
|
64 |
+
output_image = generate_inpainting(init_image, mask_image, prompt, negative_prompt)
|
65 |
+
return output_image
|
66 |
+
|
67 |
+
maskGen = gr.Interface(
|
68 |
+
fn=create_mask,
|
69 |
+
inputs=gr.Image(type='filepath', label="Original Image"),
|
70 |
+
outputs=gr.Image(label="Masked Image Image"),
|
71 |
+
allow_flagging="never")
|
72 |
+
|
73 |
+
inpaintImg = gr.Interface(
|
74 |
+
fn=process_images_and_generate_inpainting,
|
75 |
+
inputs=[
|
76 |
+
gr.Image(type='filepath', label="Original Image"),
|
77 |
+
gr.Image(type='filepath', label="Masked Image"),
|
78 |
+
gr.Textbox(label="Prompt"),
|
79 |
+
gr.Textbox(label="Negative Prompt")
|
80 |
+
],
|
81 |
+
outputs=[
|
82 |
+
gr.Image(label="Result Image")
|
83 |
+
],
|
84 |
+
allow_flagging="never")
|
85 |
+
|
86 |
+
demo = gr.TabbedInterface(
|
87 |
+
[maskGen, inpaintImg],
|
88 |
+
["Generate Mask", "Make new Image"]
|
89 |
+
)
|
90 |
+
|
91 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
diffusers
|
2 |
+
gradio
|
3 |
+
opencv-python-headless
|
4 |
+
torch
|
5 |
+
torchvision
|
6 |
+
transformers
|
7 |
+
accelerate
|
8 |
+
|