Spaces:
Sleeping
Sleeping
de
Browse files
app.py
CHANGED
@@ -9,6 +9,7 @@ import cv2
|
|
9 |
import gradio as gr
|
10 |
from torchvision import transforms
|
11 |
from controlnet_aux import OpenposeDetector
|
|
|
12 |
|
13 |
ratios_map = {
|
14 |
0.5:{"width":704,"height":1408},
|
@@ -106,139 +107,139 @@ def process(input_image, prompt, negative_prompt, num_steps, controlnet_conditio
|
|
106 |
return [pose_image,images[0]]
|
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 |
block = gr.Blocks().queue()
|
244 |
|
|
|
9 |
import gradio as gr
|
10 |
from torchvision import transforms
|
11 |
from controlnet_aux import OpenposeDetector
|
12 |
+
import random
|
13 |
|
14 |
ratios_map = {
|
15 |
0.5:{"width":704,"height":1408},
|
|
|
107 |
return [pose_image,images[0]]
|
108 |
|
109 |
|
110 |
+
@spaces.GPU
|
111 |
+
def predict_image(cond_image, prompt, negative_prompt, controlnet_conditioning_scale):
|
112 |
+
print("predict position map")
|
113 |
+
global pipe
|
114 |
+
generator = torch.Generator()
|
115 |
+
generator.manual_seed(random.randint(0, 2147483647))
|
116 |
+
image = pipe(
|
117 |
+
prompt,
|
118 |
+
negative_prompt=negative_prompt,
|
119 |
+
image = cond_image,
|
120 |
+
width=1024,
|
121 |
+
height=1024,
|
122 |
+
guidance_scale=8,
|
123 |
+
num_inference_steps=20,
|
124 |
+
generator=generator,
|
125 |
+
guess_mode = True,
|
126 |
+
controlnet_conditioning_scale = controlnet_conditioning_scale
|
127 |
+
).images[0]
|
128 |
|
129 |
+
return image
|
130 |
+
|
131 |
+
|
132 |
+
def convert_pil_to_opencv(pil_image):
|
133 |
+
return np.array(pil_image)
|
134 |
+
|
135 |
+
def inv_func(y,
|
136 |
+
c = -712.380100,
|
137 |
+
a = 137.375240,
|
138 |
+
b = 192.435866):
|
139 |
+
return (np.exp((y - c) / a) - np.exp(-c/a)) / 964.8468371292845
|
140 |
+
|
141 |
+
def create_point_cloud(img1, img2):
|
142 |
+
if img1.shape != img2.shape:
|
143 |
+
raise ValueError("Both images must have the same dimensions.")
|
144 |
+
|
145 |
+
h, w, _ = img1.shape
|
146 |
+
points = []
|
147 |
+
colors = []
|
148 |
+
for y in range(h):
|
149 |
+
for x in range(w):
|
150 |
+
# ピクセル位置 (x, y) のRGBをXYZとして取得
|
151 |
+
r, g, b = img1[y, x]
|
152 |
+
r = inv_func(r) * 0.9
|
153 |
+
g = inv_func(g) / 1.7 * 0.6
|
154 |
+
b = inv_func(b)
|
155 |
+
r *= 150
|
156 |
+
g *= 150
|
157 |
+
b *= 150
|
158 |
+
points.append([g, b, r]) # X, Y, Z
|
159 |
+
# 対応するピクセル位置の画像2の色を取得
|
160 |
+
colors.append(img2[y, x] / 255.0) # 色は0〜1にスケール
|
161 |
+
|
162 |
+
return np.array(points), np.array(colors)
|
163 |
+
|
164 |
+
def point_cloud_to_glb(points, colors):
|
165 |
+
# Open3Dでポイントクラウドを作成
|
166 |
+
pc = o3d.geometry.PointCloud()
|
167 |
+
pc.points = o3d.utility.Vector3dVector(points)
|
168 |
+
pc.colors = o3d.utility.Vector3dVector(colors)
|
169 |
|
170 |
+
# 一時的にPLY形式で保存
|
171 |
+
temp_ply_file = "temp_output.ply"
|
172 |
+
o3d.io.write_point_cloud(temp_ply_file, pc)
|
173 |
|
174 |
+
# PLYをGLBに変換
|
175 |
+
mesh = trimesh.load(temp_ply_file)
|
176 |
+
glb_file = "output.glb"
|
177 |
+
mesh.export(glb_file)
|
178 |
|
179 |
+
return glb_file
|
180 |
|
181 |
+
def visualize_3d(image1, image2):
|
182 |
+
print("Processing...")
|
183 |
+
# PIL画像をOpenCV形式に変換
|
184 |
+
img1 = convert_pil_to_opencv(image1)
|
185 |
+
img2 = convert_pil_to_opencv(image2)
|
186 |
|
187 |
+
# ポイントクラウド生成
|
188 |
+
points, colors = create_point_cloud(img1, img2)
|
189 |
|
190 |
+
# GLB形式に変換
|
191 |
+
glb_file = point_cloud_to_glb(points, colors)
|
192 |
|
193 |
+
return glb_file
|
194 |
|
195 |
+
def scale_image(original_image):
|
196 |
+
aspect_ratio = original_image.width / original_image.height
|
197 |
|
198 |
+
if original_image.width > original_image.height:
|
199 |
+
new_width = 1024
|
200 |
+
new_height = round(new_width / aspect_ratio)
|
201 |
+
else:
|
202 |
+
new_height = 1024
|
203 |
+
new_width = round(new_height * aspect_ratio)
|
204 |
|
205 |
+
resized_original = original_image.resize((new_width, new_height), Image.LANCZOS)
|
206 |
|
207 |
+
return resized_original
|
208 |
|
209 |
+
def get_edge_mode_color(img, edge_width=10):
|
210 |
+
# 外周の10ピクセル領域を取得
|
211 |
+
left = img.crop((0, 0, edge_width, img.height)) # 左端
|
212 |
+
right = img.crop((img.width - edge_width, 0, img.width, img.height)) # 右端
|
213 |
+
top = img.crop((0, 0, img.width, edge_width)) # 上端
|
214 |
+
bottom = img.crop((0, img.height - edge_width, img.width, img.height)) # 下端
|
215 |
|
216 |
+
# 各領域のピクセルデータを取得して結合
|
217 |
+
colors = list(left.getdata()) + list(right.getdata()) + list(top.getdata()) + list(bottom.getdata())
|
218 |
|
219 |
+
# 最頻値(mode)を計算
|
220 |
+
mode_color = Counter(colors).most_common(1)[0][0] # 最も頻繁に出現する色を取得
|
221 |
|
222 |
+
return mode_color
|
223 |
|
224 |
+
def paste_image(resized_img):
|
225 |
+
# 外周10pxの最頻値を背景色に設定
|
226 |
+
mode_color = get_edge_mode_color(resized_img, edge_width=10)
|
227 |
+
mode_background = Image.new("RGBA", (1024, 1024), mode_color)
|
228 |
+
mode_background = mode_background.convert('RGB')
|
229 |
|
230 |
+
x = (1024 - resized_img.width) // 2
|
231 |
+
y = (1024 - resized_img.height) // 2
|
232 |
+
mode_background.paste(resized_img, (x, y))
|
233 |
|
234 |
+
return mode_background
|
235 |
|
236 |
+
def outpaint_image(image):
|
237 |
+
if type(image) == type(None):
|
238 |
+
return None
|
239 |
+
resized_img = scale_image(image)
|
240 |
+
image = paste_image(resized_img)
|
241 |
|
242 |
+
return image
|
243 |
|
244 |
block = gr.Blocks().queue()
|
245 |
|