File size: 6,652 Bytes
528fb81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e6ec3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
528fb81
 
afeeaa2
528fb81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afeeaa2
528fb81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afeeaa2
528fb81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afeeaa2
528fb81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import requests
from tqdm import tqdm

def download_file(url, output_path):
    response = requests.get(url, stream=True)
    total_size = int(response.headers.get('content-length', 0))
    block_size = 1024  # 1 Kibibyte
    t = tqdm(total=total_size, unit='iB', unit_scale=True)
    with open(output_path, 'wb') as f:
        for data in response.iter_content(block_size):
            t.update(len(data))
            f.write(data)
    t.close()
    if total_size != 0 and t.n != total_size:
        print("ERROR: Something went wrong in download")
    else:
        print(f"βœ… Downloaded: {output_path}")

# Example: Download SAM checkpoint
sam_checkpoint_url = "https://huggingface.co/camenduru/segment_anything/resolve/main/sam_vit_h_4b8939.pth"
download_file(sam_checkpoint_url, "sam_vit_h_4b8939.pth")

# Example: Download Stable Diffusion checkpoint
sd_checkpoint_url = "https://huggingface.co/stabilityai/stable-diffusion-2-inpainting/resolve/main/model_index.json"
download_file(sd_checkpoint_url, "model_index.json")

###########################
import os
import requests
import zipfile
import io
import streamlit as st

def download_sam_repo():
    repo_url = "https://github.com/facebookresearch/segment-anything/archive/refs/heads/main.zip"
    repo_dir = "segment-anything-main"
    
    if not os.path.exists(repo_dir):
        st.info("πŸ”½ Downloading Segment Anything repo...")
        response = requests.get(repo_url)
        if response.status_code == 200:
            with zipfile.ZipFile(io.BytesIO(response.content)) as zip_ref:
                zip_ref.extractall(".")
            st.success("βœ… Segment Anything repo downloaded and extracted!")
        else:
            st.error(f"❌ Failed to download repo: {response.status_code}")
    else:
        st.info("βœ… Segment Anything repo already exists.")

# Call it at app start
download_sam_repo()


import sys
sys.path.append(os.path.abspath("segment-anything-main"))

from segment_anything import sam_model_registry, SamAutomaticMaskGenerator
##########################

sam = sam_model_registry["vit_h"](checkpoint="sam_vit_h_4b8939.pth")
#sam.to(device="cpu")

import streamlit as st
import torch
import numpy as np
import cv2
import matplotlib.pyplot as plt
from PIL import Image
from segment_anything import sam_model_registry, SamAutomaticMaskGenerator
from diffusers import StableDiffusionInpaintPipeline, EulerDiscreteScheduler
import copy

# ===========================
# Initialize SAM & Diffusion
# ===========================
@st.cache_resource
def load_sam():
    sam_checkpoint = "sam_vit_h_4b8939.pth"
    model_type = "vit_h"
    device = "cpu"

    sam = sam_model_registry[model_type](checkpoint=sam_checkpoint)
    sam.to(device=device)

    mask_generator = SamAutomaticMaskGenerator(
        model=sam,
        points_per_side=32,
        pred_iou_thresh=0.99,
        stability_score_thresh=0.92,
        crop_n_layers=1,
        crop_n_points_downscale_factor=2,
        min_mask_region_area=100,
    )
    return mask_generator

@st.cache_resource
def load_pipeline():
    model_dir = 'stabilityai/stable-diffusion-2-inpainting'
    scheduler = EulerDiscreteScheduler.from_pretrained(model_dir, subfolder='scheduler')

    pipe = StableDiffusionInpaintPipeline.from_pretrained(
        model_dir,
        scheduler=scheduler,
        torch_dtype=torch.float16,
        revision="fp16"
    )
    #pipe = pipe.to('cuda')
    pipe.enable_xformers_memory_efficient_attention()
    return pipe

# ===================
# Helper Functions
# ===================
def create_image_grid(original_image, images, names, rows, columns):
    images = copy.copy(images)
    names = copy.copy(names)

    images.insert(0, original_image)
    names.insert(0, "Original")

    fig, axes = plt.subplots(rows, columns, figsize=(15, 15))
    for idx, (img, name) in enumerate(zip(images, names)):
        row, col = divmod(idx, columns)
        axes[row, col].imshow(img)
        axes[row, col].set_title(name)
        axes[row, col].axis('off')
    for idx in range(len(images), rows * columns):
        row, col = divmod(idx, columns)
        axes[row, col].axis('off')
    plt.tight_layout()
    st.pyplot(fig)

# ===================
# Streamlit UI
# ===================
st.title("🎨 Segment & Inpaint Anything App")

uploaded_file = st.file_uploader("Upload an Image", type=['png', 'jpg', 'jpeg'])

if uploaded_file:
    source_image = Image.open(uploaded_file).convert("RGB")
    st.image(source_image, caption="Uploaded Image", use_column_width=True)

    # SAM
    mask_generator = load_sam()
    segmentation_image = np.asarray(source_image)
    masks = mask_generator.generate(segmentation_image)

    st.write(f"Number of segments detected: {len(masks)}")

    # Show masks
    fig, ax = plt.subplots(figsize=(10, 10))
    ax.imshow(source_image)
    for i, mask in enumerate(masks):
        m = mask['segmentation']
        color = np.random.random((1, 3)).tolist()[0]
        img = np.ones((m.shape[0], m.shape[1], 3))
        for j in range(3):
            img[:, :, j] = color[j]
        ax.imshow(np.dstack((img, m * 0.35)))
        contours, _ = cv2.findContours(m.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        if contours:
            cnt = contours[0]
            M = cv2.moments(cnt)
            if M["m00"] != 0:
                cx = int(M["m10"] / M["m00"])
                cy = int(M["m01"] / M["m00"])
                ax.text(cx, cy, str(i), color='white', fontsize=16, ha='center', va='center', fontweight='bold')
    ax.axis('off')
    st.pyplot(fig)

    # Ask user to choose
    mask_index = st.number_input(f"Choose Mask Index (0 to {len(masks)-1})", min_value=0, max_value=len(masks)-1, value=0)
    inpainting_prompt = st.text_input("Enter your Inpainting Prompt", "a skirt full of text")
    generate_btn = st.button("Generate Inpainting")

    if generate_btn:
        selected_mask = masks[mask_index]['segmentation']
        stable_diffusion_mask = Image.fromarray(selected_mask * 255).convert("RGB")

        pipe = load_pipeline()
        generator = torch.Generator(device="cpu").manual_seed(77)

        num_images = 4
        images = []
        for _ in range(num_images):
            image = pipe(
                prompt=inpainting_prompt,
                guidance_scale=7.5,
                num_inference_steps=60,
                generator=generator,
                image=source_image,
                mask_image=stable_diffusion_mask
            ).images[0]
            images.append(image)

        # Show Grid
        create_image_grid(source_image, images, [inpainting_prompt]*num_images, 2, 3)