Vuvo11 commited on
Commit
9a3aeae
·
verified ·
1 Parent(s): 1c1ffb3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import numpy as np
3
+ import cv2
4
+ import gradio as gr
5
+ from segment_anything import sam_model_registry, SamAutomaticMaskGenerator
6
+ from tqdm import tqdm
7
+ from diffusers import StableDiffusionInpaintPipeline, DDIMScheduler
8
+ from PIL import Image
9
+
10
+ # Load mô hình SAM
11
+ sam = sam_model_registry["vit_h"](checkpoint="models/sam_vit_h_4b8939.pth")
12
+ mask_generator = SamAutomaticMaskGenerator(sam)
13
+
14
+ # Load mô hình Stable Diffusion
15
+ scheduler = DDIMScheduler.from_pretrained("runwayml/stable-diffusion-inpainting", subfolder="scheduler")
16
+ pipe = StableDiffusionInpaintPipeline.from_pretrained(
17
+ "runwayml/stable-diffusion-inpainting",
18
+ scheduler=scheduler,
19
+ torch_dtype=torch.float32,
20
+ cache_dir="./models",
21
+ low_cpu_mem_usage=True
22
+ ).to("cuda" if torch.cuda.is_available() else "cpu")
23
+
24
+ pipe.enable_attention_slicing()
25
+
26
+ # Hàm xử lý ảnh
27
+ def inpaint(image, mask, prompt):
28
+ # Convert to PIL
29
+ original_image = Image.fromarray(image).convert("RGB")
30
+ mask_image = Image.fromarray(mask).convert("L")
31
+
32
+ # Resize images to 512x512
33
+ original_image = original_image.resize((512, 512))
34
+ mask_image = mask_image.resize((512, 512))
35
+
36
+ # Inpainting AI
37
+ output = pipe(prompt=prompt, image=original_image, mask_image=mask_image, num_inference_steps=25).images[0]
38
+ return np.array(output)
39
+
40
+ # UI với Gradio
41
+ interface = gr.Interface(
42
+ fn=inpaint,
43
+ inputs=[
44
+ gr.Image(type="numpy", label="Upload Image"),
45
+ gr.Image(type="numpy", label="Upload Mask"),
46
+ gr.Textbox(label="Prompt (Describe what to add)")
47
+ ],
48
+ outputs=gr.Image(label="Generated Image"),
49
+ title="AI Furniture Inpainting",
50
+ description="Upload an image of a room and a mask where furniture should be added."
51
+ )
52
+
53
+ if __name__ == "__main__":
54
+ interface.launch()