File size: 11,453 Bytes
81b1a0e
 
 
 
 
e797135
6284dc0
 
7776a83
6284dc0
 
e797135
6284dc0
 
e797135
54516d1
 
5d10050
7776a83
54516d1
6be00d8
e797135
81b1a0e
53ff575
81b1a0e
7776a83
621c740
 
 
7776a83
 
 
621c740
 
 
 
 
7776a83
621c740
 
 
 
 
 
 
 
 
 
7776a83
621c740
 
 
81b1a0e
6284dc0
81b1a0e
1592dab
81b1a0e
 
 
6284dc0
81b1a0e
 
 
a10635a
 
22bfe4c
e4862f5
1592dab
bcfa392
a10635a
 
 
 
f70bf31
 
a10635a
81b1a0e
7776a83
 
 
 
 
d967d62
33f3505
d967d62
e797135
741bf59
7776a83
4420101
7776a83
0e5a7e4
4bb8a82
a0ef2a3
bfe6e38
33f3505
b59df1c
741bf59
7776a83
741bf59
22bfe4c
7776a83
22bfe4c
7776a83
22bfe4c
7776a83
 
741bf59
7776a83
741bf59
5023a18
741bf59
 
5023a18
741bf59
7776a83
 
 
 
 
 
 
741bf59
85f9120
53ff575
85f9120
 
 
53ff575
741bf59
7776a83
 
 
 
53ff575
7776a83
 
741bf59
33f3505
eefba1b
621c740
53ff575
 
741bf59
5023a18
7776a83
 
 
 
 
 
 
 
 
5023a18
741bf59
 
 
 
8a9ec25
 
7776a83
 
 
 
 
 
 
 
 
 
 
 
 
 
741bf59
 
a63a768
7776a83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a63a768
7776a83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1acca69
 
7776a83
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import os
import cv2
import numpy as np
import torch
import gradio as gr
import spaces

from glob import glob
from typing import Tuple, Optional

from PIL import Image
from gradio_imageslider import ImageSlider
from transformers import AutoModelForImageSegmentation
from torchvision import transforms

import requests
from io import BytesIO
import zipfile
import random

torch.set_float32_matmul_precision('high')
torch.jit.script = lambda f: f

device = "cuda" if torch.cuda.is_available() else "cpu"

### 이미지 후처리 함수들 ###
def refine_foreground(image, mask, r=90):
    if mask.size != image.size:
        mask = mask.resize(image.size)
    image_np = np.array(image) / 255.0
    mask_np = np.array(mask) / 255.0
    estimated_foreground = FB_blur_fusion_foreground_estimator_2(image_np, mask_np, r=r)
    image_masked = Image.fromarray((estimated_foreground * 255.0).astype(np.uint8))
    return image_masked

def FB_blur_fusion_foreground_estimator_2(image, alpha, r=90):
    alpha = alpha[:, :, None]
    F, blur_B = FB_blur_fusion_foreground_estimator(image, image, image, alpha, r)
    return FB_blur_fusion_foreground_estimator(image, F, blur_B, alpha, r=6)[0]

def FB_blur_fusion_foreground_estimator(image, F, B, alpha, r=90):
    if isinstance(image, Image.Image):
        image = np.array(image) / 255.0
    blurred_alpha = cv2.blur(alpha, (r, r))[:, :, None]
    blurred_FA = cv2.blur(F * alpha, (r, r))
    blurred_F = blurred_FA / (blurred_alpha + 1e-5)
    blurred_B1A = cv2.blur(B * (1 - alpha), (r, r))
    blurred_B = blurred_B1A / ((1 - blurred_alpha) + 1e-5)
    F = blurred_F + alpha * (image - alpha * blurred_F - (1 - alpha) * blurred_B)
    F = np.clip(F, 0, 1)
    return F, blurred_B

class ImagePreprocessor():
    def __init__(self, resolution: Tuple[int, int] = (1024, 1024)) -> None:
        self.transform_image = transforms.Compose([
            transforms.Resize(resolution),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
        ])
    def proc(self, image: Image.Image) -> torch.Tensor:
        image = self.transform_image(image)
        return image

usage_to_weights_file = {
    'General': 'BiRefNet',
    'General-HR': 'BiRefNet_HR',
    'General-Lite': 'BiRefNet_lite',
    'General-Lite-2K': 'BiRefNet_lite-2K',
    'Matting': 'BiRefNet-matting',
    'Portrait': 'BiRefNet-portrait',
    'DIS': 'BiRefNet-DIS5K',
    'HRSOD': 'BiRefNet-HRSOD',
    'COD': 'BiRefNet-COD',
    'DIS-TR_TEs': 'BiRefNet-DIS5K-TR_TEs',
    'General-legacy': 'BiRefNet-legacy'
}

# 초기 모델 로딩 (기본: General)
birefnet = AutoModelForImageSegmentation.from_pretrained(
    '/'.join(('zhengpeng7', usage_to_weights_file['General'])), 
    trust_remote_code=True
)
birefnet.to(device)
birefnet.eval(); birefnet.half()

@spaces.GPU
def predict(images, resolution, weights_file):
    assert images is not None, 'Images cannot be None.'
    global birefnet
    # 선택된 가중치로 모델 재로딩
    _weights_file = '/'.join(('zhengpeng7', usage_to_weights_file[weights_file] if weights_file is not None else usage_to_weights_file['General']))
    print('Using weights: {}.'.format(_weights_file))
    birefnet = AutoModelForImageSegmentation.from_pretrained(_weights_file, trust_remote_code=True)
    birefnet.to(device)
    birefnet.eval(); birefnet.half()

    try:
        resolution_list = [int(int(reso)//32*32) for reso in resolution.strip().split('x')]
    except:
        if weights_file == 'General-HR':
            resolution_list = [2048, 2048]
        elif weights_file == 'General-Lite-2K':
            resolution_list = [2560, 1440]
        else:
            resolution_list = [1024, 1024]
        print('Invalid resolution input. Automatically changed to default.')

    # 이미지가 단일 객체인지, 리스트(배치)인지 확인
    if isinstance(images, list):
        tab_is_batch = True
    else:
        images = [images]
        tab_is_batch = False

    save_paths = []
    save_dir = 'preds-BiRefNet'
    if tab_is_batch and not os.path.exists(save_dir):
        os.makedirs(save_dir)
    
    outputs = []
    for idx, image_src in enumerate(images):
        if isinstance(image_src, str):
            if os.path.isfile(image_src):
                image_ori = Image.open(image_src)
            else:
                response = requests.get(image_src)
                image_data = BytesIO(response.content)
                image_ori = Image.open(image_data)
        else:
            if isinstance(image_src, np.ndarray):
                image_ori = Image.fromarray(image_src)
            else:
                image_ori = image_src.convert('RGB')
        image = image_ori.convert('RGB')
        preprocessor = ImagePreprocessor(resolution=tuple(resolution_list))
        image_proc = preprocessor.proc(image).unsqueeze(0)
        with torch.no_grad():
            preds = birefnet(image_proc.to(device).half())[-1].sigmoid().cpu()
        pred = preds[0].squeeze()
        pred_pil = transforms.ToPILImage()(pred)
        image_masked = refine_foreground(image, pred_pil)
        image_masked.putalpha(pred_pil.resize(image.size))
        torch.cuda.empty_cache()
        if tab_is_batch:
            file_path = os.path.join(save_dir, "{}.png".format(
                os.path.splitext(os.path.basename(image_src))[0] if isinstance(image_src, str) else f"img_{idx}"
            ))
            image_masked.save(file_path)
            save_paths.append(file_path)
            outputs.append(image_masked)
        else:
            outputs = [image_masked, image_ori]
    
    if tab_is_batch:
        zip_file_path = os.path.join(save_dir, "{}.zip".format(save_dir))
        with zipfile.ZipFile(zip_file_path, 'w') as zipf:
            for file in save_paths:
                zipf.write(file, os.path.basename(file))
        return save_paths, zip_file_path
    else:
        # 반환값을 리스트 형태로 만들어 ImageSlider에서 표시되도록 함.
        return outputs

# 예제 데이터 (이미지, URL, 배치)
examples_image = [[path, "1024x1024", "General"] for path in glob('examples/*')]
examples_text = [[url, "1024x1024", "General"] for url in ["https://hips.hearstapps.com/hmg-prod/images/gettyimages-1229892983-square.jpg"]]
examples_batch = [[file, "1024x1024", "General"] for file in glob('examples/*')]

descriptions = (
    "Upload a picture, our model will extract a highly accurate segmentation of the subject in it.\n"
    "The resolution used in our training was `1024x1024`, which is suggested for good results! "
    "`2048x2048` is suggested for BiRefNet_HR.\n"
    "Our codes can be found at https://github.com/ZhengPeng7/BiRefNet.\n"
    "We also maintain the HF model of BiRefNet at https://huggingface.co/ZhengPeng7/BiRefNet for easier access."
)

# UI 개선을 위한 CSS
css = """
body {
    background: linear-gradient(135deg, #667eea, #764ba2);
    font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
    color: #333;
    margin: 0;
    padding: 0;
}
.gradio-container {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 15px;
    padding: 30px 40px;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
    margin: 40px auto;
    max-width: 1200px;
}
.gradio-container h1 {
    color: #333;
    text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2);
}
.fillable { 
    width: 95% !important; 
    max-width: unset !important;
}
#examples_container {
    margin: auto;
    width: 90%;
}
#examples_row {
    justify-content: center;
}
.sidebar {
    background: rgba(255, 255, 255, 0.98);
    border-radius: 10px;
    padding: 20px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
}
button, .btn {
    background: linear-gradient(90deg, #ff8a00, #e52e71);
    border: none;
    color: #fff;
    padding: 12px 24px;
    text-transform: uppercase;
    font-weight: bold;
    letter-spacing: 1px;
    border-radius: 5px;
    cursor: pointer;
    transition: transform 0.2s ease-in-out;
}
button:hover, .btn:hover {
    transform: scale(1.05);
}
"""

title = """
<h1 align="center" style="margin-bottom: 0.2em;">BiRefNet Demo for Subject Extraction</h1>
<p align="center" style="font-size:1.1em; color:#555;">
    Upload an image or provide an image URL to extract the subject with high-precision segmentation.
</p>
"""

with gr.Blocks(css=css, title="BiRefNet Demo") as demo:
    gr.Markdown(title)
    with gr.Tabs():
        with gr.Tab("Image"):
            with gr.Row():
                with gr.Column(scale=1):
                    image_input = gr.Image(type='pil', label='Upload an Image')
                    resolution_input = gr.Textbox(lines=1, placeholder="e.g., 1024x1024", label="Resolution")
                    weights_radio = gr.Radio(list(usage_to_weights_file.keys()), value="General", label="Weights")
                    predict_btn = gr.Button("Predict")
                with gr.Column(scale=2):
                    output_slider = ImageSlider(label="BiRefNet's Prediction", type="pil")
            gr.Examples(examples=examples_image, inputs=[image_input, resolution_input, weights_radio], label="Examples")
        with gr.Tab("Text"):
            with gr.Row():
                with gr.Column(scale=1):
                    image_url = gr.Textbox(label="Paste an Image URL")
                    resolution_input_text = gr.Textbox(lines=1, placeholder="e.g., 1024x1024", label="Resolution")
                    weights_radio_text = gr.Radio(list(usage_to_weights_file.keys()), value="General", label="Weights")
                    predict_btn_text = gr.Button("Predict")
                with gr.Column(scale=2):
                    output_slider_text = ImageSlider(label="BiRefNet's Prediction", type="pil")
            gr.Examples(examples=examples_text, inputs=[image_url, resolution_input_text, weights_radio_text], label="Examples")
        with gr.Tab("Batch"):
            with gr.Row():
                with gr.Column(scale=1):
                    file_input = gr.File(label="Upload Multiple Images", type="filepath", file_count="multiple")
                    resolution_input_batch = gr.Textbox(lines=1, placeholder="e.g., 1024x1024", label="Resolution")
                    weights_radio_batch = gr.Radio(list(usage_to_weights_file.keys()), value="General", label="Weights")
                    predict_btn_batch = gr.Button("Predict")
                with gr.Column(scale=2):
                    output_gallery = gr.Gallery(label="BiRefNet's Predictions", scale=1)
                    zip_output = gr.File(label="Download Masked Images")
            gr.Examples(examples=examples_batch, inputs=[file_input, resolution_input_batch, weights_radio_batch], label="Examples")
    with gr.Row():
        gr.Markdown("<p align='center'>Model by <a href='https://huggingface.co/ZhengPeng7/BiRefNet'>ZhengPeng7/BiRefNet</a></p>")

    # 각 탭의 Predict 버튼과 predict 함수 연결
    predict_btn.click(
        fn=predict,
        inputs=[image_input, resolution_input, weights_radio],
        outputs=output_slider
    )
    predict_btn_text.click(
        fn=predict,
        inputs=[image_url, resolution_input_text, weights_radio_text],
        outputs=output_slider_text
    )
    predict_btn_batch.click(
        fn=predict,
        inputs=[file_input, resolution_input_batch, weights_radio_batch],
        outputs=[output_gallery, zip_output]
    )

if __name__ == "__main__":
    demo.launch(share=False, debug=True)