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