tastelikefeet commited on
Commit
239f98e
1 Parent(s): 4d1c498
app.py ADDED
@@ -0,0 +1,302 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ##!/usr/bin/python3
2
+ # -*- coding: utf-8 -*-
3
+ # @Time : 2023-06-01
4
+ # @Author : ashui(Binghui Chen)
5
+ from sympy import im
6
+ from versions import RELEASE_NOTE, VERSION
7
+
8
+ import time
9
+ import cv2
10
+ import gradio as gr
11
+ import numpy as np
12
+ import random
13
+ import math
14
+ import uuid
15
+ import torch
16
+ from torch import autocast
17
+
18
+ from src.util import resize_image, HWC3, call_with_messages, upload_np_2_oss
19
+ from src.virtualmodel import call_virtualmodel
20
+ from src.person_detect import call_person_detect
21
+ from src.background_generation import call_bg_genration
22
+
23
+ import sys, os
24
+
25
+ from PIL import Image, ImageFilter, ImageOps, ImageDraw
26
+
27
+ from segment_anything import SamPredictor, sam_model_registry
28
+
29
+ mobile_sam = sam_model_registry['vit_h'](checkpoint='models/sam_vit_h_4b8939.pth').to("cuda")
30
+ mobile_sam.eval()
31
+ mobile_predictor = SamPredictor(mobile_sam)
32
+ colors = [(255, 0, 0), (0, 255, 0)]
33
+ markers = [1, 5]
34
+
35
+ # - - - - - examples - - - - - #
36
+ # 输入图地址, 文本, 背景图地址, index, []
37
+ image_examples = [
38
+ ["imgs/000.jpg", "一位年轻女性身穿短袖,展示一台手机", None, 0, []],
39
+ ["imgs/001.jpg", "一位年轻女性身穿短袖,手持杯子", None, 1, []],
40
+ ["imgs/003.png", "一名女子身穿黑色西服,背景蓝色", "imgs/003_bg.jpg", 2, []],
41
+ ["imgs/002.png", "一名年轻女性身穿裙子摆拍,背景是蓝色的", "imgs/002_bg.png", 3, []],
42
+ ["imgs/bg_gen/base_imgs/1cdb9b1e6daea6a1b85236595d3e43d6.png", "水滴飞溅", None, 4, []],
43
+ ["imgs/bg_gen/base_imgs/1cdb9b1e6daea6a1b85236595d3e43d6.png", "", "imgs/bg_gen/ref_imgs/df9a93ac2bca12696a9166182c4bf02ad9679aa5.jpg", 5, []],
44
+ ["imgs/bg_gen/base_imgs/IMG_2941.png", "在沙漠地面上", None, 6, []],
45
+ ["imgs/bg_gen/base_imgs/b2b1ed243364473e49d2e478e4f24413.png","白色地面,白色背景,光线射入,佳能",None,7,[]],
46
+ ]
47
+
48
+ img = "image_gallery/"
49
+ files = os.listdir(img)
50
+ files = sorted(files)
51
+ showcases = []
52
+ for idx, name in enumerate(files):
53
+ temp = os.path.join(os.path.dirname(__file__), img, name)
54
+ showcases.append(temp)
55
+
56
+ def process(input_image, original_image, original_mask, selected_points, source_background, prompt, face_prompt):
57
+ if original_image is None or original_mask is None or len(selected_points)==0:
58
+ raise gr.Error('请上传输入图片并通过点击鼠标选择需要保留的物体.')
59
+
60
+ # load example image
61
+ if isinstance(original_image, int):
62
+ image_name = image_examples[original_image][0]
63
+ original_image = cv2.imread(image_name)
64
+ original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)
65
+
66
+ original_mask = np.clip(255 - original_mask, 0, 255).astype(np.uint8)
67
+
68
+ request_id = str(uuid.uuid4())
69
+ input_image_url = upload_np_2_oss(original_image, request_id+".png")
70
+ input_mask_url = upload_np_2_oss(original_mask, request_id+"_mask.png")
71
+ source_background_url = "" if source_background is None else upload_np_2_oss(source_background, request_id+"_bg.png")
72
+
73
+ # person detect: [[x1,y1,x2,y2,score],]
74
+ det_res = call_person_detect(input_image_url)
75
+
76
+ res = []
77
+ if len(det_res)>0:
78
+ if len(prompt)==0:
79
+ raise gr.Error('请输入prompt')
80
+ res = call_virtualmodel(input_image_url, input_mask_url, source_background_url, prompt, face_prompt)
81
+ else:
82
+ ### 这里接入主图背景生成
83
+ if len(prompt)==0:
84
+ prompt=None
85
+ ref_image_url=None if source_background_url =='' else source_background_url
86
+ original_mask=original_mask[:,:,:1]
87
+ base_image=np.concatenate([original_image, original_mask],axis=2)
88
+ base_image_url=upload_np_2_oss(base_image, request_id+"_base.png")
89
+ res=call_bg_genration(base_image_url,ref_image_url,prompt,ref_prompt_weight=0.5)
90
+
91
+ return res, request_id, True
92
+
93
+ block = gr.Blocks(
94
+ css="css/style.css",
95
+ theme=gr.themes.Soft(
96
+ radius_size=gr.themes.sizes.radius_none,
97
+ text_size=gr.themes.sizes.text_md
98
+ )
99
+ ).queue(concurrency_count=3)
100
+ with block:
101
+ with gr.Row():
102
+ with gr.Column():
103
+
104
+ gr.HTML(f"""
105
+ </br>
106
+ <div class="baselayout" style="text-shadow: white 0.01rem 0.01rem 0.4rem; position:fixed; z-index: 9999; top:0; left:0;right:0; background-size:100% 100%">
107
+ <h1 style="text-align:center; color:white; font-size:3rem; position: relative;"> ReplaceAnything (V{VERSION})</h1>
108
+ </div>
109
+ </br>
110
+ </br>
111
+ <div style="text-align: center;">
112
+ <h1 >ReplaceAnything as you want: Ultra-high quality content replacement</h1>
113
+ <div style="display: flex; justify-content: center; align-items: center; text-align: center;">
114
+ <a href=""></a>
115
+ <a href='https://aigcdesigngroup.github.io/replace-anything/'><img src='https://img.shields.io/badge/Project_Page-ReplaceAnything-green' alt='Project Page'></a>
116
+ <a href='https://github.com/AIGCDesignGroup/ReplaceAnything'><img src='https://img.shields.io/badge/Github-Repo-blue'></a>
117
+ </div>
118
+ </br>
119
+ <h3> 我们发现,在严格保持某个“物体ID”不变的情况下生成新的内容有着很大的市场需求,同时也是具有挑战性的。为此,我们提出了ReplaceAnything框架。它可以用于很多场景,比如<b>人体替换、服装替换、物体替换以及背景替换</b>等等。</h3>
120
+ <h5 style="margin: 0; color: red">如果你认为该项目有所帮助的话,不妨给我们Github点个Star以便获取最新的项目进展.</h5>
121
+ </br>
122
+ </div>
123
+ """)
124
+
125
+ with gr.Tabs(elem_classes=["Tab"]):
126
+ with gr.TabItem("作品广场"):
127
+ gr.Gallery(value=showcases,
128
+ height=800,
129
+ columns=4,
130
+ object_fit="scale-down"
131
+ )
132
+ with gr.TabItem("创作图像"):
133
+ with gr.Accordion(label="🧭 操作指南:", open=True, elem_id="accordion"):
134
+ with gr.Row(equal_height=True):
135
+ with gr.Row(elem_id="ShowCase"):
136
+ gr.Image(value="showcase/ra.gif")
137
+ gr.Markdown("""
138
+ - ⭐️ <b>step1:</b>在“输入图像”中上传or选择Example里面的一张图片
139
+ - ⭐️ <b>step2:</b>通过点击鼠标选择图像中希望保留的物体
140
+ - ⭐️ <b>step3:</b>输入对应的参数,例如prompt等,点击Run进行生成
141
+ - ⭐️ <b>step4 (可选):</b>此外支持换背景操作,上传目标风格背景,执行完step3后点击Run进行生成
142
+ """)
143
+ with gr.Row():
144
+ with gr.Column():
145
+ with gr.Column(elem_id="Input"):
146
+ with gr.Row():
147
+ with gr.Tabs(elem_classes=["feedback"]):
148
+ with gr.TabItem("输入图像"):
149
+ input_image = gr.Image(type="numpy", label="输入图",scale=2)
150
+ original_image = gr.State(value=None,label="索引")
151
+ original_mask = gr.State(value=None)
152
+ selected_points = gr.State([],label="点选坐标")
153
+ with gr.Row(elem_id="Seg"):
154
+ radio = gr.Radio(['前景点选', '背景点选'], label='分割点选: ', value='前景点选',scale=2)
155
+ undo_button = gr.Button('撤销点选至上一步', elem_id="btnSEG",scale=1)
156
+ prompt = gr.Textbox(label="Prompt (支持中英文)", placeholder="请输入期望的文本描述",value='',lines=1)
157
+ run_button = gr.Button("生成图像(Run)",elem_id="btn")
158
+
159
+ with gr.Accordion("更多输入参数 (推荐使用)", open=False, elem_id="accordion1"):
160
+ with gr.Row(elem_id="Image"):
161
+ with gr.Tabs(elem_classes=["feedback1"]):
162
+ with gr.TabItem("风格背景图输入(可选项)"):
163
+ source_background = gr.Image(type="numpy", label="背景图")
164
+
165
+ face_prompt = gr.Textbox(label="人脸 Prompt (支持中英文)", value='good face, beautiful face, best quality')
166
+ with gr.Column():
167
+ with gr.Tabs(elem_classes=["feedback"]):
168
+ with gr.TabItem("输出结果"):
169
+ result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery", preview=True)
170
+ recommend=gr.Button("推荐至作品广场",elem_id="recBut")
171
+ request_id=gr.State(value="")
172
+ gallery_flag=gr.State(value=False)
173
+ with gr.Row():
174
+ with gr.Box():
175
+ def process_example(input_image, prompt, source_background, original_image, selected_points):
176
+ return input_image, prompt, source_background, original_image, []
177
+ example = gr.Examples(
178
+ label="输入图示例",
179
+ examples=image_examples,
180
+ inputs=[input_image, prompt, source_background, original_image, selected_points],
181
+ outputs=[input_image, prompt, source_background, original_image, selected_points],
182
+ fn=process_example,
183
+ run_on_click=True,
184
+ examples_per_page=10
185
+ )
186
+
187
+ # once user upload an image, the original image is stored in `original_image`
188
+ def store_img(img):
189
+ # 图片太大传输太慢了
190
+ if min(img.shape[0], img.shape[1]) > 1024:
191
+ img = resize_image(img, 1024)
192
+ return img, img, [], None # when new image is uploaded, `selected_points` should be empty
193
+
194
+ input_image.upload(
195
+ store_img,
196
+ [input_image],
197
+ [input_image, original_image, selected_points, source_background]
198
+ )
199
+
200
+ # user click the image to get points, and show the points on the image
201
+ def segmentation(img, sel_pix):
202
+ # online show seg mask
203
+ points = []
204
+ labels = []
205
+ for p, l in sel_pix:
206
+ points.append(p)
207
+ labels.append(l)
208
+ mobile_predictor.set_image(img if isinstance(img, np.ndarray) else np.array(img))
209
+ with torch.no_grad():
210
+ with autocast("cuda"):
211
+ masks, _, _ = mobile_predictor.predict(point_coords=np.array(points), point_labels=np.array(labels), multimask_output=False)
212
+
213
+ output_mask = np.ones((masks.shape[1], masks.shape[2], 3))*255
214
+ for i in range(3):
215
+ output_mask[masks[0] == True, i] = 0.0
216
+
217
+ mask_all = np.ones((masks.shape[1], masks.shape[2], 3))
218
+ color_mask = np.random.random((1, 3)).tolist()[0]
219
+ for i in range(3):
220
+ mask_all[masks[0] == True, i] = color_mask[i]
221
+ masked_img = img / 255 * 0.3 + mask_all * 0.7
222
+ masked_img = masked_img*255
223
+ ## draw points
224
+ for point, label in sel_pix:
225
+ cv2.drawMarker(masked_img, point, colors[label], markerType=markers[label], markerSize=20, thickness=5)
226
+ return masked_img, output_mask
227
+
228
+ def get_point(img, sel_pix, point_type, evt: gr.SelectData):
229
+ if point_type == '前景点选':
230
+ sel_pix.append((evt.index, 1)) # append the foreground_point
231
+ elif point_type == '背景点选':
232
+ sel_pix.append((evt.index, 0)) # append the background_point
233
+ else:
234
+ sel_pix.append((evt.index, 1)) # default foreground_point
235
+
236
+ if isinstance(img, int):
237
+ image_name = image_examples[img][0]
238
+ img = cv2.imread(image_name)
239
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
240
+
241
+ # online show seg mask
242
+ masked_img, output_mask = segmentation(img, sel_pix)
243
+ return masked_img.astype(np.uint8), output_mask
244
+
245
+ input_image.select(
246
+ get_point,
247
+ [original_image, selected_points, radio],
248
+ [input_image, original_mask],
249
+ )
250
+
251
+ # undo the selected point
252
+ def undo_points(orig_img, sel_pix):
253
+ # draw points
254
+ output_mask = None
255
+ if len(sel_pix) != 0:
256
+ if isinstance(orig_img, int): # if orig_img is int, the image if select from examples
257
+ temp = cv2.imread(image_examples[orig_img][0])
258
+ temp = cv2.cvtColor(temp, cv2.COLOR_BGR2RGB)
259
+ else:
260
+ temp = orig_img.copy()
261
+ sel_pix.pop()
262
+ # online show seg mask
263
+ if len(sel_pix) !=0:
264
+ temp, output_mask = segmentation(temp, sel_pix)
265
+ return temp.astype(np.uint8), output_mask
266
+ else:
267
+ gr.Error("暂无“上一步”可撤销")
268
+
269
+ undo_button.click(
270
+ undo_points,
271
+ [original_image, selected_points],
272
+ [input_image, original_mask]
273
+ )
274
+
275
+ def upload_to_img_gallery(img, res, re_id, flag):
276
+ if flag:
277
+ if isinstance(img, int):
278
+ image_name = image_examples[img][0]
279
+ img = cv2.imread(image_name)
280
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
281
+ _ = upload_np_2_oss(img, name=re_id+"_ori.jpg", gallery=True)
282
+ for idx, r in enumerate(res):
283
+ r = cv2.imread(r['name'])
284
+ r = cv2.cvtColor(r, cv2.COLOR_BGR2RGB)
285
+ _ = upload_np_2_oss(r, name=re_id+f"_res_{idx}.jpg", gallery=True)
286
+ flag=False
287
+ gr.Info("图片已经被上传完毕,待审核")
288
+ else:
289
+ gr.Info("暂无图片可推荐,或者已经推荐过一次了")
290
+ return flag
291
+
292
+ recommend.click(
293
+ upload_to_img_gallery,
294
+ [original_image, result_gallery, request_id, gallery_flag],
295
+ [gallery_flag]
296
+ )
297
+
298
+ ips=[input_image, original_image, original_mask, selected_points, source_background, prompt, face_prompt]
299
+ run_button.click(fn=process, inputs=ips, outputs=[result_gallery, request_id, gallery_flag])
300
+
301
+
302
+ block.launch(server_name='0.0.0.0', share=False, server_port=7687)
css/0.png ADDED
css/style.css ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ .baselayout{
3
+ background: url('https://img.alicdn.com/imgextra/i1/O1CN016hd0V91ilWY5Xr24B_!!6000000004453-2-tps-2882-256.png') no-repeat;
4
+ }
5
+ #btn {
6
+ background-color: #336699;
7
+ color: white;
8
+ }
9
+ #recBut {
10
+ background-color: #bb5252;
11
+ color: white;
12
+ width: 30%;
13
+ margin: auto;
14
+ }
15
+ #btnSEG {
16
+ background-color: #D5F3F4;
17
+ color: black;
18
+ }
19
+ #btnCHAT {
20
+ background-color: #B6DBF2;
21
+ color: black;
22
+ }
23
+ #accordion {
24
+ background-color: transparent;
25
+ }
26
+ #accordion1 {
27
+ background-color: #ecedee;
28
+ }
29
+ .feedback button.selected{
30
+ background-color: #6699CC;
31
+ color: white !important;
32
+ }
33
+ .feedback1 button.selected{
34
+ background-color: #839ab2;
35
+ color: white !important;
36
+ }
37
+ .Tab button.selected{
38
+ color: red;
39
+ font-weight: bold;
40
+ }
41
+ #Image {
42
+ width: 60%;
43
+ margin:auto;
44
+ }
45
+ #ShowCase {
46
+ width: 30%;
47
+ flex:none !important;
48
+ }
49
+
50
+ #Input {
51
+ border-style:solid;
52
+ border-width:1px;
53
+ border-color:#000000
54
+ }
55
+ #Seg {
56
+ min-width: min(100px, 100%) !important;
57
+ width: 100%;
58
+ margin:auto;
59
+ }
models/DOWNLOAD_MODEL_HERE.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ 模型链接
2
+ https://vision-poster.oss-cn-shanghai.aliyuncs.com/ashui/sam_vit_h_4b8939.pth?OSSAccessKeyId=LTAI5tSPYbksBzcmooNHCYif&Expires=3599001703148669&Signature=TYznO77DKFjGNn92SnR9RbucOlU%3D
models/sam_vit_h_4b8939.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a7bf3b02f3ebf1267aba913ff637d9a2d5c33d3173bb679e46d9f338c26f262e
3
+ size 2564550879
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ dashscope
2
+ git+https://gitee.com/lllcho/segment-anything.git
versions.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ VERSION = '1.0.0'
2
+ RELEASE_NOTE = '''V1.0.0: 2023/12/20
3
+ - init
4
+ '''
5
+