Spaces:
Paused
Paused
arxivgpt kim commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,9 @@ import torch.nn.functional as F
|
|
| 4 |
from torchvision.transforms.functional import normalize
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
import gradio as gr
|
| 7 |
-
from gradio_imageslider import ImageSlider
|
| 8 |
from briarmbg import BriaRMBG
|
| 9 |
import PIL
|
| 10 |
from PIL import Image
|
| 11 |
-
from typing import Tuple
|
| 12 |
-
|
| 13 |
import os
|
| 14 |
import requests
|
| 15 |
from moviepy.editor import VideoFileClip
|
|
@@ -22,13 +19,10 @@ def search_pexels_images(query):
|
|
| 22 |
response = requests.get(url, headers=headers)
|
| 23 |
data = response.json()
|
| 24 |
|
| 25 |
-
# ๊ณ ํด์๋ ์ด๋ฏธ์ง URL๋ง ์ ํํ์ฌ ๋ฆฌ์คํธ ์์ฑ
|
| 26 |
images_urls = []
|
| 27 |
for photo in data.get('photos', []):
|
| 28 |
-
# 'large2x' ํด์๋์ ์ด๋ฏธ์ง๊ฐ ์ ๊ณต๋๋ ๊ฒฝ์ฐ, ํด๋น URL ์ฌ์ฉ
|
| 29 |
if 'src' in photo and 'large2x' in photo['src']:
|
| 30 |
images_urls.append(photo['src']['large2x'])
|
| 31 |
-
# 'large2x' ํด์๋์ ์ด๋ฏธ์ง๊ฐ ์๋ ๊ฒฝ์ฐ, 'large' ๋๋ 'original'์ ๋์ฒด๋ก ์ฌ์ฉ
|
| 32 |
elif 'large' in photo['src']:
|
| 33 |
images_urls.append(photo['src']['large'])
|
| 34 |
elif 'original' in photo['src']:
|
|
@@ -36,36 +30,29 @@ def search_pexels_images(query):
|
|
| 36 |
|
| 37 |
return images_urls
|
| 38 |
|
| 39 |
-
|
| 40 |
def show_search_results(query):
|
| 41 |
images_urls = search_pexels_images(query)
|
| 42 |
return images_urls
|
| 43 |
-
|
| 44 |
|
| 45 |
-
net=BriaRMBG()
|
| 46 |
-
# model_path = "./model1.pth"
|
| 47 |
model_path = hf_hub_download("briaai/RMBG-1.4", 'model.pth')
|
| 48 |
if torch.cuda.is_available():
|
| 49 |
net.load_state_dict(torch.load(model_path))
|
| 50 |
-
net=net.cuda()
|
| 51 |
else:
|
| 52 |
-
net.load_state_dict(torch.load(model_path,map_location="cpu"))
|
| 53 |
-
net.eval()
|
| 54 |
|
| 55 |
-
|
| 56 |
def resize_image(image):
|
| 57 |
image = image.convert('RGB')
|
| 58 |
model_input_size = (1024, 1024)
|
| 59 |
image = image.resize(model_input_size, Image.BILINEAR)
|
| 60 |
return image
|
| 61 |
|
| 62 |
-
|
| 63 |
def process(image):
|
| 64 |
-
# ์ด๋ฏธ์ง๊ฐ numpy ๋ฐฐ์ด์ธ ๊ฒฝ์ฐ์๋ง PIL.Image ๊ฐ์ฒด๋ก ๋ณํ
|
| 65 |
if isinstance(image, np.ndarray):
|
| 66 |
orig_image = Image.fromarray(image)
|
| 67 |
else:
|
| 68 |
-
# ์ด๋ฏธ PIL.Image.Image ๊ฐ์ฒด์ธ ๊ฒฝ์ฐ, ๋ณํ ์์ด ์ฌ์ฉ
|
| 69 |
orig_image = image
|
| 70 |
|
| 71 |
w, h = orig_im_size = orig_image.size
|
|
@@ -78,48 +65,34 @@ def process(image):
|
|
| 78 |
if torch.cuda.is_available():
|
| 79 |
im_tensor = im_tensor.cuda()
|
| 80 |
|
| 81 |
-
# inference
|
| 82 |
result = net(im_tensor)
|
| 83 |
-
# post process
|
| 84 |
result = torch.squeeze(F.interpolate(result[0][0], size=(h, w), mode='bilinear'), 0)
|
| 85 |
ma = torch.max(result)
|
| 86 |
mi = torch.min(result)
|
| 87 |
result = (result - mi) / (ma - mi)
|
| 88 |
-
# image to pil
|
| 89 |
im_array = (result * 255).cpu().data.numpy().astype(np.uint8)
|
| 90 |
pil_im = Image.fromarray(np.squeeze(im_array))
|
| 91 |
-
# paste the mask on the original image
|
| 92 |
new_im = Image.new("RGBA", pil_im.size, (0, 0, 0, 0))
|
| 93 |
new_im.paste(orig_image, mask=pil_im)
|
| 94 |
|
| 95 |
return new_im
|
| 96 |
|
| 97 |
def calculate_position(org_size, add_size, position):
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
elif position == "ํ๋จ ์ข์ธก":
|
| 111 |
-
return (0, org_size[1] - add_size[1])
|
| 112 |
-
elif position == "ํ๋จ ๊ฐ์ด๋ฐ":
|
| 113 |
-
return ((org_size[0] - add_size[0]) // 2, org_size[1] - add_size[1])
|
| 114 |
-
elif position == "ํ๋จ ์ฐ์ธก":
|
| 115 |
-
return (org_size[0] - add_size[0], org_size[1] - add_size[1])
|
| 116 |
-
|
| 117 |
|
| 118 |
def merge(org_image, add_image, scale, position, display_size):
|
| 119 |
-
# ์ฌ์ฉ์๊ฐ ์ ํํ ๋์คํ๋ ์ด ํฌ๊ธฐ์ ๋ฐ๋ผ ๊ฒฐ๊ณผ ์ด๋ฏธ์ง ํฌ๊ธฐ ์กฐ์
|
| 120 |
display_width, display_height = map(int, display_size.split('x'))
|
| 121 |
-
|
| 122 |
-
# ์ด๋ฏธ์ง ๋ณํฉ ๋ก์ง
|
| 123 |
scale_percentage = scale / 100.0
|
| 124 |
new_size = (int(add_image.width * scale_percentage), int(add_image.height * scale_percentage))
|
| 125 |
add_image = add_image.resize(new_size, Image.Resampling.LANCZOS)
|
|
@@ -129,54 +102,30 @@ def merge(org_image, add_image, scale, position, display_size):
|
|
| 129 |
merged_image.paste(org_image, (0, 0))
|
| 130 |
merged_image.paste(add_image, position, add_image)
|
| 131 |
|
| 132 |
-
# ๊ฒฐ๊ณผ ์ด๋ฏธ์ง ๋์คํ๋ ์ด ํฌ๊ธฐ ์กฐ์
|
| 133 |
final_image = merged_image.resize((display_width, display_height), Image.Resampling.LANCZOS)
|
| 134 |
|
| 135 |
return final_image
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
org_image
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
btn_merge = gr.Button("Merge Images")
|
| 161 |
-
result_merge = gr.Image()
|
| 162 |
-
|
| 163 |
-
btn_merge.click(
|
| 164 |
-
fn=merge,
|
| 165 |
-
inputs=[org_image, add_image, scale, position, display_size],
|
| 166 |
-
outputs=result_merge,
|
| 167 |
-
)
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
with gr.TabItem("Image Search"):
|
| 171 |
-
with gr.Column():
|
| 172 |
-
gr.Markdown("### FREE Image Search")
|
| 173 |
-
search_query = gr.Textbox(label="์ฌ์ง ๊ฒ์")
|
| 174 |
-
search_btn = gr.Button("๊ฒ์")
|
| 175 |
-
images_output = gr.Gallery(label="๊ฒ์ ๊ฒฐ๊ณผ ์ด๋ฏธ์ง")
|
| 176 |
-
search_btn.click(
|
| 177 |
-
fn=show_search_results,
|
| 178 |
-
inputs=search_query,
|
| 179 |
-
outputs=images_output
|
| 180 |
-
)
|
| 181 |
-
|
| 182 |
-
demo.launch()
|
|
|
|
| 4 |
from torchvision.transforms.functional import normalize
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
import gradio as gr
|
|
|
|
| 7 |
from briarmbg import BriaRMBG
|
| 8 |
import PIL
|
| 9 |
from PIL import Image
|
|
|
|
|
|
|
| 10 |
import os
|
| 11 |
import requests
|
| 12 |
from moviepy.editor import VideoFileClip
|
|
|
|
| 19 |
response = requests.get(url, headers=headers)
|
| 20 |
data = response.json()
|
| 21 |
|
|
|
|
| 22 |
images_urls = []
|
| 23 |
for photo in data.get('photos', []):
|
|
|
|
| 24 |
if 'src' in photo and 'large2x' in photo['src']:
|
| 25 |
images_urls.append(photo['src']['large2x'])
|
|
|
|
| 26 |
elif 'large' in photo['src']:
|
| 27 |
images_urls.append(photo['src']['large'])
|
| 28 |
elif 'original' in photo['src']:
|
|
|
|
| 30 |
|
| 31 |
return images_urls
|
| 32 |
|
|
|
|
| 33 |
def show_search_results(query):
|
| 34 |
images_urls = search_pexels_images(query)
|
| 35 |
return images_urls
|
|
|
|
| 36 |
|
| 37 |
+
net = BriaRMBG()
|
|
|
|
| 38 |
model_path = hf_hub_download("briaai/RMBG-1.4", 'model.pth')
|
| 39 |
if torch.cuda.is_available():
|
| 40 |
net.load_state_dict(torch.load(model_path))
|
| 41 |
+
net = net.cuda()
|
| 42 |
else:
|
| 43 |
+
net.load_state_dict(torch.load(model_path, map_location="cpu"))
|
| 44 |
+
net.eval()
|
| 45 |
|
|
|
|
| 46 |
def resize_image(image):
|
| 47 |
image = image.convert('RGB')
|
| 48 |
model_input_size = (1024, 1024)
|
| 49 |
image = image.resize(model_input_size, Image.BILINEAR)
|
| 50 |
return image
|
| 51 |
|
|
|
|
| 52 |
def process(image):
|
|
|
|
| 53 |
if isinstance(image, np.ndarray):
|
| 54 |
orig_image = Image.fromarray(image)
|
| 55 |
else:
|
|
|
|
| 56 |
orig_image = image
|
| 57 |
|
| 58 |
w, h = orig_im_size = orig_image.size
|
|
|
|
| 65 |
if torch.cuda.is_available():
|
| 66 |
im_tensor = im_tensor.cuda()
|
| 67 |
|
|
|
|
| 68 |
result = net(im_tensor)
|
|
|
|
| 69 |
result = torch.squeeze(F.interpolate(result[0][0], size=(h, w), mode='bilinear'), 0)
|
| 70 |
ma = torch.max(result)
|
| 71 |
mi = torch.min(result)
|
| 72 |
result = (result - mi) / (ma - mi)
|
|
|
|
| 73 |
im_array = (result * 255).cpu().data.numpy().astype(np.uint8)
|
| 74 |
pil_im = Image.fromarray(np.squeeze(im_array))
|
|
|
|
| 75 |
new_im = Image.new("RGBA", pil_im.size, (0, 0, 0, 0))
|
| 76 |
new_im.paste(orig_image, mask=pil_im)
|
| 77 |
|
| 78 |
return new_im
|
| 79 |
|
| 80 |
def calculate_position(org_size, add_size, position):
|
| 81 |
+
positions = {
|
| 82 |
+
"Center": ((org_size[0] - add_size[0]) // 2, (org_size[1] - add_size[1]) // 2),
|
| 83 |
+
"Top Left": (0, 0),
|
| 84 |
+
"Top Center": ((org_size[0] - add_size[0]) // 2, 0),
|
| 85 |
+
"Top Right": (org_size[0] - add_size[0], 0),
|
| 86 |
+
"Center Left": (0, (org_size[1] - add_size[1]) // 2),
|
| 87 |
+
"Center Right": (org_size[0] - add_size[0], (org_size[1] - add_size[1]) // 2),
|
| 88 |
+
"Bottom Left": (0, org_size[1] - add_size[1]),
|
| 89 |
+
"Bottom Center": ((org_size[0] - add_size[0]) // 2, org_size[1] - add_size[1]),
|
| 90 |
+
"Bottom Right": (org_size[0] - add_size[0], org_size[1] - add_size[1])
|
| 91 |
+
}
|
| 92 |
+
return positions.get(position, positions['Center'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
def merge(org_image, add_image, scale, position, display_size):
|
|
|
|
| 95 |
display_width, display_height = map(int, display_size.split('x'))
|
|
|
|
|
|
|
| 96 |
scale_percentage = scale / 100.0
|
| 97 |
new_size = (int(add_image.width * scale_percentage), int(add_image.height * scale_percentage))
|
| 98 |
add_image = add_image.resize(new_size, Image.Resampling.LANCZOS)
|
|
|
|
| 102 |
merged_image.paste(org_image, (0, 0))
|
| 103 |
merged_image.paste(add_image, position, add_image)
|
| 104 |
|
|
|
|
| 105 |
final_image = merged_image.resize((display_width, display_height), Image.Resampling.LANCZOS)
|
| 106 |
|
| 107 |
return final_image
|
| 108 |
|
| 109 |
+
css = """
|
| 110 |
+
footer {
|
| 111 |
+
visibility: hidden;
|
| 112 |
+
}
|
| 113 |
+
"""
|
| 114 |
+
|
| 115 |
+
with gr.Blocks(css=css) as demo:
|
| 116 |
+
with gr.Column():
|
| 117 |
+
org_image = gr.Image(label="Background", type='pil', image_mode='RGBA', height=400)
|
| 118 |
+
add_image = gr.Image(label="Foreground", type='pil', image_mode='RGBA', height=400)
|
| 119 |
+
scale = gr.Slider(minimum=10, maximum=200, step=1, value=100, label="Scale of Foreground Image (%)")
|
| 120 |
+
position = gr.Radio(choices=["Center", "Top Left", "Top Center", "Top Right", "Center Left", "Center Right", "Bottom Left", "Bottom Center", "Bottom Right"], value="Center", label="Position of Foreground Image")
|
| 121 |
+
display_size = gr.Textbox(value="1024x768", label="Display Size (Width x Height)")
|
| 122 |
+
btn_merge = gr.Button("Merge Images")
|
| 123 |
+
result_merge = gr.Image()
|
| 124 |
+
|
| 125 |
+
btn_merge.click(
|
| 126 |
+
fn=merge,
|
| 127 |
+
inputs=[org_image, add_image, scale, position, display_size],
|
| 128 |
+
outputs=result_merge,
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|