Spaces:
Configuration error
Configuration error
load_model
Browse files
app.py
CHANGED
|
@@ -13,15 +13,15 @@ import spaces
|
|
| 13 |
pipe = None
|
| 14 |
|
| 15 |
def load_model():
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
def convert_pil_to_opencv(pil_image):
|
| 24 |
-
|
| 25 |
|
| 26 |
def inv_func(y,
|
| 27 |
c = -712.380100,
|
|
@@ -30,103 +30,103 @@ def inv_func(y,
|
|
| 30 |
return (np.exp((y - c) / a) - np.exp(-c/a)) / 964.8468371292845
|
| 31 |
|
| 32 |
def create_point_cloud(img1, img2):
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
def point_cloud_to_glb(points, colors):
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
|
| 72 |
def visualize_3d(image1, image2):
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
|
| 84 |
-
|
| 85 |
|
| 86 |
def scale_image(original_image):
|
| 87 |
-
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
|
| 96 |
-
|
| 97 |
|
| 98 |
-
|
| 99 |
|
| 100 |
def get_edge_mode_color(img, edge_width=10):
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
|
| 113 |
-
|
| 114 |
|
| 115 |
def paste_image(resized_img):
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
|
| 125 |
-
|
| 126 |
|
| 127 |
def outpaint_image(image):
|
| 128 |
if type(image) == type(None):
|
| 129 |
-
|
| 130 |
resized_img = scale_image(image)
|
| 131 |
image = paste_image(resized_img)
|
| 132 |
|
|
@@ -134,29 +134,30 @@ def outpaint_image(image):
|
|
| 134 |
|
| 135 |
@spaces.GPU
|
| 136 |
def predict_image(cond_image, prompt, negative_prompt):
|
| 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 |
# Gradioアプリケーション
|
| 162 |
with gr.Blocks() as demo:
|
|
|
|
| 13 |
pipe = None
|
| 14 |
|
| 15 |
def load_model():
|
| 16 |
+
global pipe
|
| 17 |
+
pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
|
| 18 |
+
"yeq6x/animagine_position_map",
|
| 19 |
+
controlnet=ControlNetModel.from_pretrained("yeq6x/Image2PositionColor_v3"),
|
| 20 |
+
).to("cuda")
|
| 21 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
| 22 |
|
| 23 |
def convert_pil_to_opencv(pil_image):
|
| 24 |
+
return np.array(pil_image)
|
| 25 |
|
| 26 |
def inv_func(y,
|
| 27 |
c = -712.380100,
|
|
|
|
| 30 |
return (np.exp((y - c) / a) - np.exp(-c/a)) / 964.8468371292845
|
| 31 |
|
| 32 |
def create_point_cloud(img1, img2):
|
| 33 |
+
if img1.shape != img2.shape:
|
| 34 |
+
raise ValueError("Both images must have the same dimensions.")
|
| 35 |
+
|
| 36 |
+
h, w, _ = img1.shape
|
| 37 |
+
points = []
|
| 38 |
+
colors = []
|
| 39 |
+
for y in range(h):
|
| 40 |
+
for x in range(w):
|
| 41 |
+
# ピクセル位置 (x, y) のRGBをXYZとして取得
|
| 42 |
+
r, g, b = img1[y, x]
|
| 43 |
+
r = inv_func(r) * 0.9
|
| 44 |
+
g = inv_func(g) / 1.7 * 0.6
|
| 45 |
+
b = inv_func(b)
|
| 46 |
+
r *= 150
|
| 47 |
+
g *= 150
|
| 48 |
+
b *= 150
|
| 49 |
+
points.append([g, b, r]) # X, Y, Z
|
| 50 |
+
# 対応するピクセル位置の画像2の色を取得
|
| 51 |
+
colors.append(img2[y, x] / 255.0) # 色は0〜1にスケール
|
| 52 |
+
|
| 53 |
+
return np.array(points), np.array(colors)
|
| 54 |
|
| 55 |
def point_cloud_to_glb(points, colors):
|
| 56 |
+
# Open3Dでポイントクラウドを作成
|
| 57 |
+
pc = o3d.geometry.PointCloud()
|
| 58 |
+
pc.points = o3d.utility.Vector3dVector(points)
|
| 59 |
+
pc.colors = o3d.utility.Vector3dVector(colors)
|
| 60 |
+
|
| 61 |
+
# 一時的にPLY形式で保存
|
| 62 |
+
temp_ply_file = "temp_output.ply"
|
| 63 |
+
o3d.io.write_point_cloud(temp_ply_file, pc)
|
| 64 |
+
|
| 65 |
+
# PLYをGLBに変換
|
| 66 |
+
mesh = trimesh.load(temp_ply_file)
|
| 67 |
+
glb_file = "output.glb"
|
| 68 |
+
mesh.export(glb_file)
|
| 69 |
+
|
| 70 |
+
return glb_file
|
| 71 |
|
| 72 |
def visualize_3d(image1, image2):
|
| 73 |
+
print("Processing...")
|
| 74 |
+
# PIL画像をOpenCV形式に変換
|
| 75 |
+
img1 = convert_pil_to_opencv(image1)
|
| 76 |
+
img2 = convert_pil_to_opencv(image2)
|
| 77 |
|
| 78 |
+
# ポイントクラウド生成
|
| 79 |
+
points, colors = create_point_cloud(img1, img2)
|
| 80 |
|
| 81 |
+
# GLB形式に変換
|
| 82 |
+
glb_file = point_cloud_to_glb(points, colors)
|
| 83 |
|
| 84 |
+
return glb_file
|
| 85 |
|
| 86 |
def scale_image(original_image):
|
| 87 |
+
aspect_ratio = original_image.width / original_image.height
|
| 88 |
|
| 89 |
+
if original_image.width > original_image.height:
|
| 90 |
+
new_width = 1024
|
| 91 |
+
new_height = round(new_width / aspect_ratio)
|
| 92 |
+
else:
|
| 93 |
+
new_height = 1024
|
| 94 |
+
new_width = round(new_height * aspect_ratio)
|
| 95 |
|
| 96 |
+
resized_original = original_image.resize((new_width, new_height), Image.LANCZOS)
|
| 97 |
|
| 98 |
+
return resized_original
|
| 99 |
|
| 100 |
def get_edge_mode_color(img, edge_width=10):
|
| 101 |
+
# 外周の10ピクセル領域を取得
|
| 102 |
+
left = img.crop((0, 0, edge_width, img.height)) # 左端
|
| 103 |
+
right = img.crop((img.width - edge_width, 0, img.width, img.height)) # 右端
|
| 104 |
+
top = img.crop((0, 0, img.width, edge_width)) # 上端
|
| 105 |
+
bottom = img.crop((0, img.height - edge_width, img.width, img.height)) # 下端
|
| 106 |
|
| 107 |
+
# 各領域のピクセルデータを取得して結合
|
| 108 |
+
colors = list(left.getdata()) + list(right.getdata()) + list(top.getdata()) + list(bottom.getdata())
|
| 109 |
|
| 110 |
+
# 最頻値(mode)を計算
|
| 111 |
+
mode_color = Counter(colors).most_common(1)[0][0] # 最も頻繁に出現する色を取得
|
| 112 |
|
| 113 |
+
return mode_color
|
| 114 |
|
| 115 |
def paste_image(resized_img):
|
| 116 |
+
# 外周10pxの最頻値を背景色に設定
|
| 117 |
+
mode_color = get_edge_mode_color(resized_img, edge_width=10)
|
| 118 |
+
mode_background = Image.new("RGBA", (1024, 1024), mode_color)
|
| 119 |
+
mode_background = mode_background.convert('RGB')
|
| 120 |
|
| 121 |
+
x = (1024 - resized_img.width) // 2
|
| 122 |
+
y = (1024 - resized_img.height) // 2
|
| 123 |
+
mode_background.paste(resized_img, (x, y))
|
| 124 |
|
| 125 |
+
return mode_background
|
| 126 |
|
| 127 |
def outpaint_image(image):
|
| 128 |
if type(image) == type(None):
|
| 129 |
+
return None
|
| 130 |
resized_img = scale_image(image)
|
| 131 |
image = paste_image(resized_img)
|
| 132 |
|
|
|
|
| 134 |
|
| 135 |
@spaces.GPU
|
| 136 |
def predict_image(cond_image, prompt, negative_prompt):
|
| 137 |
+
if pipe is None:
|
| 138 |
+
load_model()
|
| 139 |
+
|
| 140 |
+
generator = torch.Generator()
|
| 141 |
+
generator.manual_seed(random.randint(0, 2147483647))
|
| 142 |
+
|
| 143 |
+
prompt = 'position map, 1girl, white background'
|
| 144 |
+
negative_prompt = "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"
|
| 145 |
+
|
| 146 |
+
image = pipe(
|
| 147 |
+
prompt,
|
| 148 |
+
prompt,
|
| 149 |
+
cond_image,
|
| 150 |
+
negative_prompt=negative_prompt,
|
| 151 |
+
width=1024,
|
| 152 |
+
height=1024,
|
| 153 |
+
guidance_scale=8,
|
| 154 |
+
num_inference_steps=20,
|
| 155 |
+
generator=generator,
|
| 156 |
+
guess_mode = True,
|
| 157 |
+
controlnet_conditioning_scale = 0.6,
|
| 158 |
+
).images[0]
|
| 159 |
|
| 160 |
+
return image
|
| 161 |
|
| 162 |
# Gradioアプリケーション
|
| 163 |
with gr.Blocks() as demo:
|