Update app.py
Browse files
app.py
CHANGED
|
@@ -412,6 +412,43 @@ def get_models():
|
|
| 412 |
return models
|
| 413 |
|
| 414 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 415 |
stream = AsyncStream()
|
| 416 |
|
| 417 |
|
|
@@ -515,14 +552,24 @@ def worker(input_image, prompt, n_prompt, seed, total_second_length, latent_wind
|
|
| 515 |
|
| 516 |
try:
|
| 517 |
H, W, C = input_image.shape
|
| 518 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 519 |
|
| 520 |
# CPUモードの場合、処理サイズを小さくする
|
| 521 |
if cpu_fallback_mode:
|
| 522 |
-
|
| 523 |
-
|
|
|
|
| 524 |
|
| 525 |
-
|
|
|
|
|
|
|
|
|
|
| 526 |
|
| 527 |
Image.fromarray(input_image_np).save(os.path.join(outputs_folder, f'{job_id}.png'))
|
| 528 |
|
|
@@ -1489,10 +1536,10 @@ with block:
|
|
| 1489 |
# タッチ操作を最適化するためにslider-containerクラスを追加
|
| 1490 |
with gr.Group(elem_classes="slider-container"):
|
| 1491 |
total_second_length = gr.Slider(
|
| 1492 |
-
label="動画の長さ(最大
|
| 1493 |
-
minimum=
|
| 1494 |
-
maximum=
|
| 1495 |
-
value=
|
| 1496 |
step=0.1
|
| 1497 |
)
|
| 1498 |
|
|
@@ -1654,4 +1701,5 @@ def create_error_html(error_msg, is_timeout=False):
|
|
| 1654 |
}}
|
| 1655 |
}})();
|
| 1656 |
</script>
|
| 1657 |
-
"""
|
|
|
|
|
|
| 412 |
return models
|
| 413 |
|
| 414 |
|
| 415 |
+
# 事前定義された解像度リスト(グローバル変数として追加)
|
| 416 |
+
PREDEFINED_RESOLUTIONS = [
|
| 417 |
+
(416, 960), (448, 864), (480, 832), (512, 768), (544, 704),
|
| 418 |
+
(576, 672), (608, 640), (640, 608), (672, 576), (704, 544),
|
| 419 |
+
(768, 512), (832, 480), (864, 448), (960, 416)
|
| 420 |
+
]
|
| 421 |
+
|
| 422 |
+
# 最も近いアスペクト比を見つける関数
|
| 423 |
+
def find_closest_aspect_ratio(width, height, target_resolutions):
|
| 424 |
+
"""
|
| 425 |
+
事前定義された解像度リストから、元の画像のアスペクト比に最も近い解像度を見つける
|
| 426 |
+
|
| 427 |
+
引数:
|
| 428 |
+
width: 元の画像の幅
|
| 429 |
+
height: 元の画像の高さ
|
| 430 |
+
target_resolutions: 目標解像度のリスト(幅, 高さ)のタプル
|
| 431 |
+
|
| 432 |
+
戻り値:
|
| 433 |
+
tuple: 最も近いアスペクト比の (target_width, target_height)
|
| 434 |
+
"""
|
| 435 |
+
original_aspect = width / height
|
| 436 |
+
|
| 437 |
+
# 各目標解像度に対してアスペクト比の差を計算
|
| 438 |
+
min_diff = float('inf')
|
| 439 |
+
closest_resolution = None
|
| 440 |
+
|
| 441 |
+
for target_width, target_height in target_resolutions:
|
| 442 |
+
target_aspect = target_width / target_height
|
| 443 |
+
diff = abs(original_aspect - target_aspect)
|
| 444 |
+
|
| 445 |
+
if diff < min_diff:
|
| 446 |
+
min_diff = diff
|
| 447 |
+
closest_resolution = (target_width, target_height)
|
| 448 |
+
|
| 449 |
+
return closest_resolution
|
| 450 |
+
|
| 451 |
+
|
| 452 |
stream = AsyncStream()
|
| 453 |
|
| 454 |
|
|
|
|
| 552 |
|
| 553 |
try:
|
| 554 |
H, W, C = input_image.shape
|
| 555 |
+
|
| 556 |
+
# 事前定義された解像度から最も近いアスペクト比を見つける
|
| 557 |
+
target_width, target_height = find_closest_aspect_ratio(W, H, PREDEFINED_RESOLUTIONS)
|
| 558 |
+
|
| 559 |
+
# リサイズメッセージを表示
|
| 560 |
+
resize_message = f'画像を最適なアスペクト比にリサイズしています: {target_width}x{target_height}'
|
| 561 |
+
stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, resize_message))))
|
| 562 |
|
| 563 |
# CPUモードの場合、処理サイズを小さくする
|
| 564 |
if cpu_fallback_mode:
|
| 565 |
+
scale_factor = min(320 / target_height, 320 / target_width)
|
| 566 |
+
target_height = int(target_height * scale_factor)
|
| 567 |
+
target_width = int(target_width * scale_factor)
|
| 568 |
|
| 569 |
+
print(f'元の画像サイズ: {W}x{H}, リサイズ先: {target_width}x{target_height}')
|
| 570 |
+
|
| 571 |
+
# 選択された解像度にリサイズ
|
| 572 |
+
input_image_np = resize_and_center_crop(input_image, target_width=target_width, target_height=target_height)
|
| 573 |
|
| 574 |
Image.fromarray(input_image_np).save(os.path.join(outputs_folder, f'{job_id}.png'))
|
| 575 |
|
|
|
|
| 1536 |
# タッチ操作を最適化するためにslider-containerクラスを追加
|
| 1537 |
with gr.Group(elem_classes="slider-container"):
|
| 1538 |
total_second_length = gr.Slider(
|
| 1539 |
+
label="動画の長さ(最大1秒) / Video Length (max 1 seconds)",
|
| 1540 |
+
minimum=0.5,
|
| 1541 |
+
maximum=1,
|
| 1542 |
+
value=1,
|
| 1543 |
step=0.1
|
| 1544 |
)
|
| 1545 |
|
|
|
|
| 1701 |
}}
|
| 1702 |
}})();
|
| 1703 |
</script>
|
| 1704 |
+
"""
|
| 1705 |
+
|