Spaces:
Sleeping
Sleeping
de
Browse files
app.py
CHANGED
@@ -8,6 +8,7 @@ from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, Eule
|
|
8 |
import torch
|
9 |
from collections import Counter
|
10 |
import random
|
|
|
11 |
|
12 |
ratios_map = {
|
13 |
0.5:{"width":704,"height":1408},
|
@@ -30,6 +31,7 @@ ratios_map = {
|
|
30 |
}
|
31 |
ratios = np.array(list(ratios_map.keys()))
|
32 |
|
|
|
33 |
controlnet = ControlNetModel.from_pretrained(
|
34 |
"yeq6x/Image2PositionColor_v3",
|
35 |
torch_dtype=torch.float16
|
@@ -43,13 +45,14 @@ pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
|
|
43 |
offload_state_dict=True,
|
44 |
).to('cuda').to(torch.float16)
|
45 |
|
46 |
-
pipe.scheduler = EulerAncestralDiscreteScheduler(
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
)
|
|
|
53 |
pipe.force_zeros_for_empty_prompt = False
|
54 |
|
55 |
def get_size(init_image):
|
@@ -78,147 +81,148 @@ def generate_(prompt, negative_prompt, pose_image, input_image, num_steps, contr
|
|
78 |
|
79 |
@spaces.GPU
|
80 |
def process(input_image, prompt, negative_prompt, num_steps, controlnet_conditioning_scale, seed):
|
81 |
-
|
82 |
# resize input_image to 1024x1024
|
83 |
input_image = resize_image(input_image)
|
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 |
-
def convert_pil_to_opencv(pil_image):
|
112 |
-
|
113 |
-
|
114 |
-
def inv_func(y,
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
def create_point_cloud(img1, img2):
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
def point_cloud_to_glb(points, colors):
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
|
158 |
-
|
159 |
|
160 |
-
def visualize_3d(image1, image2):
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
|
166 |
-
|
167 |
-
|
168 |
|
169 |
-
|
170 |
-
|
171 |
|
172 |
-
|
173 |
|
174 |
-
def scale_image(original_image):
|
175 |
-
|
176 |
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
|
184 |
-
|
185 |
|
186 |
-
|
187 |
|
188 |
-
def get_edge_mode_color(img, edge_width=10):
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
|
195 |
-
|
196 |
-
|
197 |
|
198 |
-
|
199 |
-
|
200 |
|
201 |
-
|
202 |
|
203 |
-
def paste_image(resized_img):
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
|
213 |
-
|
214 |
|
215 |
-
def outpaint_image(image):
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
|
221 |
-
|
222 |
|
223 |
block = gr.Blocks().queue()
|
224 |
|
@@ -241,30 +245,30 @@ with block:
|
|
241 |
ips = [input_image, prompt, negative_prompt, num_steps, controlnet_conditioning_scale, seed]
|
242 |
run_button.click(fn=process, inputs=ips, outputs=[pose_image_output, generated_image_output])
|
243 |
|
244 |
-
gr.Markdown("## Position Map Visualizer")
|
245 |
|
246 |
-
with gr.Row():
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
img1.input(outpaint_image, inputs=img1, outputs=img1)
|
267 |
-
predict_map_btn.click(predict_image, inputs=[img1, prompt, negative_prompt, controlnet_conditioning_scale], outputs=img2)
|
268 |
-
visualize_3d_btn.click(visualize_3d, inputs=[img2, img1], outputs=reconstruction_output)
|
269 |
|
270 |
block.launch(debug = True)
|
|
|
8 |
import torch
|
9 |
from collections import Counter
|
10 |
import random
|
11 |
+
from controlnet_aux import OpenposeDetector
|
12 |
|
13 |
ratios_map = {
|
14 |
0.5:{"width":704,"height":1408},
|
|
|
31 |
}
|
32 |
ratios = np.array(list(ratios_map.keys()))
|
33 |
|
34 |
+
openpose = OpenposeDetector.from_pretrained('lllyasviel/ControlNet')
|
35 |
controlnet = ControlNetModel.from_pretrained(
|
36 |
"yeq6x/Image2PositionColor_v3",
|
37 |
torch_dtype=torch.float16
|
|
|
45 |
offload_state_dict=True,
|
46 |
).to('cuda').to(torch.float16)
|
47 |
|
48 |
+
# pipe.scheduler = EulerAncestralDiscreteScheduler(
|
49 |
+
# beta_start=0.00085,
|
50 |
+
# beta_end=0.012,
|
51 |
+
# beta_schedule="scaled_linear",
|
52 |
+
# num_train_timesteps=1000,
|
53 |
+
# steps_offset=1
|
54 |
+
# )
|
55 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
56 |
pipe.force_zeros_for_empty_prompt = False
|
57 |
|
58 |
def get_size(init_image):
|
|
|
81 |
|
82 |
@spaces.GPU
|
83 |
def process(input_image, prompt, negative_prompt, num_steps, controlnet_conditioning_scale, seed):
|
|
|
84 |
# resize input_image to 1024x1024
|
85 |
input_image = resize_image(input_image)
|
86 |
|
87 |
+
pose_image = openpose(input_image, include_body=True, include_hand=True, include_face=True)
|
88 |
+
|
89 |
+
images = generate_(prompt, negative_prompt, pose_image, input_image, num_steps, controlnet_conditioning_scale, seed)
|
90 |
+
|
91 |
+
return [pose_image,images[0]]
|
92 |
+
|
93 |
+
# @spaces.GPU
|
94 |
+
# def predict_image(cond_image, prompt, negative_prompt, controlnet_conditioning_scale):
|
95 |
+
# print("predict position map")
|
96 |
+
# global pipe
|
97 |
+
# generator = torch.Generator()
|
98 |
+
# generator.manual_seed(random.randint(0, 2147483647))
|
99 |
+
# image = pipe(
|
100 |
+
# prompt,
|
101 |
+
# negative_prompt=negative_prompt,
|
102 |
+
# image = cond_image,
|
103 |
+
# width=1024,
|
104 |
+
# height=1024,
|
105 |
+
# guidance_scale=8,
|
106 |
+
# num_inference_steps=20,
|
107 |
+
# generator=generator,
|
108 |
+
# guess_mode = True,
|
109 |
+
# controlnet_conditioning_scale = controlnet_conditioning_scale
|
110 |
+
# ).images[0]
|
111 |
|
112 |
+
# return image
|
113 |
+
|
114 |
+
|
115 |
+
# def convert_pil_to_opencv(pil_image):
|
116 |
+
# return np.array(pil_image)
|
117 |
+
|
118 |
+
# def inv_func(y,
|
119 |
+
# c = -712.380100,
|
120 |
+
# a = 137.375240,
|
121 |
+
# b = 192.435866):
|
122 |
+
# return (np.exp((y - c) / a) - np.exp(-c/a)) / 964.8468371292845
|
123 |
+
|
124 |
+
# def create_point_cloud(img1, img2):
|
125 |
+
# if img1.shape != img2.shape:
|
126 |
+
# raise ValueError("Both images must have the same dimensions.")
|
127 |
+
|
128 |
+
# h, w, _ = img1.shape
|
129 |
+
# points = []
|
130 |
+
# colors = []
|
131 |
+
# for y in range(h):
|
132 |
+
# for x in range(w):
|
133 |
+
# # ピクセル位置 (x, y) のRGBをXYZとして取得
|
134 |
+
# r, g, b = img1[y, x]
|
135 |
+
# r = inv_func(r) * 0.9
|
136 |
+
# g = inv_func(g) / 1.7 * 0.6
|
137 |
+
# b = inv_func(b)
|
138 |
+
# r *= 150
|
139 |
+
# g *= 150
|
140 |
+
# b *= 150
|
141 |
+
# points.append([g, b, r]) # X, Y, Z
|
142 |
+
# # 対応するピクセル��置の画像2の色を取得
|
143 |
+
# colors.append(img2[y, x] / 255.0) # 色は0〜1にスケール
|
144 |
+
|
145 |
+
# return np.array(points), np.array(colors)
|
146 |
+
|
147 |
+
# def point_cloud_to_glb(points, colors):
|
148 |
+
# # Open3Dでポイントクラウドを作成
|
149 |
+
# pc = o3d.geometry.PointCloud()
|
150 |
+
# pc.points = o3d.utility.Vector3dVector(points)
|
151 |
+
# pc.colors = o3d.utility.Vector3dVector(colors)
|
152 |
|
153 |
+
# # 一時的にPLY形式で保存
|
154 |
+
# temp_ply_file = "temp_output.ply"
|
155 |
+
# o3d.io.write_point_cloud(temp_ply_file, pc)
|
156 |
|
157 |
+
# # PLYをGLBに変換
|
158 |
+
# mesh = trimesh.load(temp_ply_file)
|
159 |
+
# glb_file = "output.glb"
|
160 |
+
# mesh.export(glb_file)
|
161 |
|
162 |
+
# return glb_file
|
163 |
|
164 |
+
# def visualize_3d(image1, image2):
|
165 |
+
# print("Processing...")
|
166 |
+
# # PIL画像をOpenCV形式に変換
|
167 |
+
# img1 = convert_pil_to_opencv(image1)
|
168 |
+
# img2 = convert_pil_to_opencv(image2)
|
169 |
|
170 |
+
# # ポイントクラウド生成
|
171 |
+
# points, colors = create_point_cloud(img1, img2)
|
172 |
|
173 |
+
# # GLB形式に変換
|
174 |
+
# glb_file = point_cloud_to_glb(points, colors)
|
175 |
|
176 |
+
# return glb_file
|
177 |
|
178 |
+
# def scale_image(original_image):
|
179 |
+
# aspect_ratio = original_image.width / original_image.height
|
180 |
|
181 |
+
# if original_image.width > original_image.height:
|
182 |
+
# new_width = 1024
|
183 |
+
# new_height = round(new_width / aspect_ratio)
|
184 |
+
# else:
|
185 |
+
# new_height = 1024
|
186 |
+
# new_width = round(new_height * aspect_ratio)
|
187 |
|
188 |
+
# resized_original = original_image.resize((new_width, new_height), Image.LANCZOS)
|
189 |
|
190 |
+
# return resized_original
|
191 |
|
192 |
+
# def get_edge_mode_color(img, edge_width=10):
|
193 |
+
# # 外周の10ピクセル領域を取得
|
194 |
+
# left = img.crop((0, 0, edge_width, img.height)) # 左端
|
195 |
+
# right = img.crop((img.width - edge_width, 0, img.width, img.height)) # 右端
|
196 |
+
# top = img.crop((0, 0, img.width, edge_width)) # 上端
|
197 |
+
# bottom = img.crop((0, img.height - edge_width, img.width, img.height)) # 下端
|
198 |
|
199 |
+
# # 各領域のピクセルデータを取得して結合
|
200 |
+
# colors = list(left.getdata()) + list(right.getdata()) + list(top.getdata()) + list(bottom.getdata())
|
201 |
|
202 |
+
# # 最頻値(mode)を計算
|
203 |
+
# mode_color = Counter(colors).most_common(1)[0][0] # 最も頻繁に出現する色を取得
|
204 |
|
205 |
+
# return mode_color
|
206 |
|
207 |
+
# def paste_image(resized_img):
|
208 |
+
# # 外周10pxの最頻値を背景色に設定
|
209 |
+
# mode_color = get_edge_mode_color(resized_img, edge_width=10)
|
210 |
+
# mode_background = Image.new("RGBA", (1024, 1024), mode_color)
|
211 |
+
# mode_background = mode_background.convert('RGB')
|
212 |
|
213 |
+
# x = (1024 - resized_img.width) // 2
|
214 |
+
# y = (1024 - resized_img.height) // 2
|
215 |
+
# mode_background.paste(resized_img, (x, y))
|
216 |
|
217 |
+
# return mode_background
|
218 |
|
219 |
+
# def outpaint_image(image):
|
220 |
+
# if type(image) == type(None):
|
221 |
+
# return None
|
222 |
+
# resized_img = scale_image(image)
|
223 |
+
# image = paste_image(resized_img)
|
224 |
|
225 |
+
# return image
|
226 |
|
227 |
block = gr.Blocks().queue()
|
228 |
|
|
|
245 |
ips = [input_image, prompt, negative_prompt, num_steps, controlnet_conditioning_scale, seed]
|
246 |
run_button.click(fn=process, inputs=ips, outputs=[pose_image_output, generated_image_output])
|
247 |
|
248 |
+
# gr.Markdown("## Position Map Visualizer")
|
249 |
|
250 |
+
# with gr.Row():
|
251 |
+
# with gr.Column():
|
252 |
+
# with gr.Row():
|
253 |
+
# img1 = gr.Image(type="pil", label="color Image", height=300)
|
254 |
+
# img2 = gr.Image(type="pil", label="map Image", height=300)
|
255 |
+
# prompt = gr.Textbox("position map, 1girl, white background", label="Prompt")
|
256 |
+
# negative_prompt = gr.Textbox("lowres, bad anatomy, bad hands, bad feet, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry", label="Negative Prompt")
|
257 |
+
# controlnet_conditioning_scale = gr.Slider(label="ControlNet conditioning scale", minimum=0.1, maximum=2.0, value=0.6, step=0.05)
|
258 |
+
# predict_map_btn = gr.Button("Predict Position Map")
|
259 |
+
# visualize_3d_btn = gr.Button("Generate 3D Point Cloud")
|
260 |
+
# with gr.Column():
|
261 |
+
# reconstruction_output = gr.Model3D(label="3D Viewer", height=600)
|
262 |
+
# gr.Examples(
|
263 |
+
# examples=[
|
264 |
+
# ["resources/source/000006.png", "resources/target/000006.png"],
|
265 |
+
# ["resources/source/006420.png", "resources/target/006420.png"],
|
266 |
+
# ],
|
267 |
+
# inputs=[img1, img2]
|
268 |
+
# )
|
269 |
+
|
270 |
+
# img1.input(outpaint_image, inputs=img1, outputs=img1)
|
271 |
+
# predict_map_btn.click(predict_image, inputs=[img1, prompt, negative_prompt, controlnet_conditioning_scale], outputs=img2)
|
272 |
+
# visualize_3d_btn.click(visualize_3d, inputs=[img2, img1], outputs=reconstruction_output)
|
273 |
|
274 |
block.launch(debug = True)
|