Update app.py
Browse files
app.py
CHANGED
|
@@ -88,6 +88,34 @@ def generate_single_view(input_images, prompt, seed, num_inference_steps, true_g
|
|
| 88 |
|
| 89 |
return result[0]
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
# --- Main Inference Function ---
|
| 92 |
@spaces.GPU(duration=300)
|
| 93 |
def generate_turnaround(
|
|
@@ -100,6 +128,7 @@ def generate_turnaround(
|
|
| 100 |
):
|
| 101 |
"""
|
| 102 |
入力画像から4つの視点(正面、背面、左側面、右側面)の立ち絵を生成
|
|
|
|
| 103 |
"""
|
| 104 |
if randomize_seed:
|
| 105 |
seed = random.randint(0, MAX_SEED)
|
|
@@ -134,26 +163,11 @@ def generate_turnaround(
|
|
| 134 |
# 4. 正面立ち絵を入力として右側面を生成
|
| 135 |
progress(1.0, desc="右側面立ち絵を生成中...")
|
| 136 |
right_image = generate_single_view([front_image], PROMPTS["right"], seed+3, num_inference_steps, true_guidance_scale)
|
| 137 |
-
|
| 138 |
-
#
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
# 連結画像のサイズを計算
|
| 144 |
-
total_width = sum(widths)
|
| 145 |
-
max_height = max(heights)
|
| 146 |
-
|
| 147 |
-
# 新しい画像を作成
|
| 148 |
-
combined_image = Image.new('RGB', (total_width, max_height))
|
| 149 |
-
|
| 150 |
-
# 画像を左から順に貼り付け
|
| 151 |
-
x_offset = 0
|
| 152 |
-
for img in images:
|
| 153 |
-
combined_image.paste(img, (x_offset, 0))
|
| 154 |
-
x_offset += img.width
|
| 155 |
-
|
| 156 |
-
return front_image, back_image, left_image, right_image, combined_image, seed, "✅ 4視点の立ち絵生成が完了しました"
|
| 157 |
|
| 158 |
# --- UI Layout ---
|
| 159 |
css = """
|
|
@@ -177,7 +191,7 @@ css = """
|
|
| 177 |
with gr.Blocks(css=css) as demo:
|
| 178 |
gr.Markdown("# キャラクター4視点立ち絵自動生成")
|
| 179 |
gr.Markdown("キャラクター画像をアップロードすると、正面・背面・左側面・右側面の4つの立ち絵を自動生成します")
|
| 180 |
-
|
| 181 |
with gr.Column(elem_id="col-container"):
|
| 182 |
with gr.Row():
|
| 183 |
input_image = gr.Image(
|
|
@@ -203,9 +217,9 @@ with gr.Blocks(css=css) as demo:
|
|
| 203 |
result_left = gr.Image(label="左側面", type="pil", height=500, show_download_button=True, container=True, image_mode="RGB")
|
| 204 |
with gr.Column():
|
| 205 |
result_right = gr.Image(label="右側面", type="pil", height=500, show_download_button=True, container=True, image_mode="RGB")
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
|
| 210 |
with gr.Accordion("⚙️ 詳細設定", open=False):
|
| 211 |
seed = gr.Slider(
|
|
@@ -244,7 +258,8 @@ with gr.Blocks(css=css) as demo:
|
|
| 244 |
true_guidance_scale,
|
| 245 |
num_inference_steps,
|
| 246 |
],
|
| 247 |
-
|
|
|
|
| 248 |
)
|
| 249 |
|
| 250 |
if __name__ == "__main__":
|
|
|
|
| 88 |
|
| 89 |
return result[0]
|
| 90 |
|
| 91 |
+
# --- NEW: 横連結ユーティリティ ---
|
| 92 |
+
def concat_images_horizontally(images, bg_color=(255, 255, 255)):
|
| 93 |
+
"""
|
| 94 |
+
複数のPIL画像を横に連結して1枚のPIL画像を返す。
|
| 95 |
+
すべて同じ高さにスケールしてアスペクト比は維持。
|
| 96 |
+
"""
|
| 97 |
+
images = [img.convert("RGB") for img in images if img is not None]
|
| 98 |
+
if not images:
|
| 99 |
+
return None
|
| 100 |
+
|
| 101 |
+
# 連結の基準高さ(最大の高さ)に合わせて横幅を等比リサイズ
|
| 102 |
+
target_h = max(img.height for img in images)
|
| 103 |
+
resized = []
|
| 104 |
+
for img in images:
|
| 105 |
+
if img.height != target_h:
|
| 106 |
+
new_w = int(img.width * (target_h / img.height))
|
| 107 |
+
img = img.resize((new_w, target_h), Image.LANCZOS)
|
| 108 |
+
resized.append(img)
|
| 109 |
+
|
| 110 |
+
total_w = sum(img.width for img in resized)
|
| 111 |
+
canvas = Image.new("RGB", (total_w, target_h), bg_color)
|
| 112 |
+
|
| 113 |
+
x = 0
|
| 114 |
+
for img in resized:
|
| 115 |
+
canvas.paste(img, (x, 0))
|
| 116 |
+
x += img.width
|
| 117 |
+
return canvas
|
| 118 |
+
|
| 119 |
# --- Main Inference Function ---
|
| 120 |
@spaces.GPU(duration=300)
|
| 121 |
def generate_turnaround(
|
|
|
|
| 128 |
):
|
| 129 |
"""
|
| 130 |
入力画像から4つの視点(正面、背面、左側面、右側面)の立ち絵を生成
|
| 131 |
+
さらに「正面 → 右向き → 背面 → 左向き」を横並びで連結した画像も返す
|
| 132 |
"""
|
| 133 |
if randomize_seed:
|
| 134 |
seed = random.randint(0, MAX_SEED)
|
|
|
|
| 163 |
# 4. 正面立ち絵を入力として右側面を生成
|
| 164 |
progress(1.0, desc="右側面立ち絵を生成中...")
|
| 165 |
right_image = generate_single_view([front_image], PROMPTS["right"], seed+3, num_inference_steps, true_guidance_scale)
|
| 166 |
+
|
| 167 |
+
# --- NEW: 指定順で横連結(正面 → 右向き → 背面 → 左向き) ---
|
| 168 |
+
concatenated = concat_images_horizontally([front_image, right_image, back_image, left_image])
|
| 169 |
+
|
| 170 |
+
return front_image, back_image, left_image, right_image, concatenated, seed, "✅ 4視点の立ち絵生成と横連結画像の出力が完了しました"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
# --- UI Layout ---
|
| 173 |
css = """
|
|
|
|
| 191 |
with gr.Blocks(css=css) as demo:
|
| 192 |
gr.Markdown("# キャラクター4視点立ち絵自動生成")
|
| 193 |
gr.Markdown("キャラクター画像をアップロードすると、正面・背面・左側面・右側面の4つの立ち絵を自動生成します")
|
| 194 |
+
|
| 195 |
with gr.Column(elem_id="col-container"):
|
| 196 |
with gr.Row():
|
| 197 |
input_image = gr.Image(
|
|
|
|
| 217 |
result_left = gr.Image(label="左側面", type="pil", height=500, show_download_button=True, container=True, image_mode="RGB")
|
| 218 |
with gr.Column():
|
| 219 |
result_right = gr.Image(label="右側面", type="pil", height=500, show_download_button=True, container=True, image_mode="RGB")
|
| 220 |
+
|
| 221 |
+
# --- NEW: 横連結画像の出力欄 ---
|
| 222 |
+
result_concat = gr.Image(label="連結(正面 → 右向き → 背面 → 左向き)", type="pil", height=500, show_download_button=True, container=True, image_mode="RGB")
|
| 223 |
|
| 224 |
with gr.Accordion("⚙️ 詳細設定", open=False):
|
| 225 |
seed = gr.Slider(
|
|
|
|
| 258 |
true_guidance_scale,
|
| 259 |
num_inference_steps,
|
| 260 |
],
|
| 261 |
+
# --- NEW: 5つ目の出力として連結画像を追加 ---
|
| 262 |
+
outputs=[result_front, result_back, result_left, result_right, result_concat, seed, status_text],
|
| 263 |
)
|
| 264 |
|
| 265 |
if __name__ == "__main__":
|