Update app.py
Browse files
app.py
CHANGED
@@ -630,7 +630,7 @@ def process_bg_image(image, bg, fast_mode=False):
|
|
630 |
image = Image.composite(image, background, mask)
|
631 |
return image
|
632 |
|
633 |
-
def process_video_frame(frame, bg_type, bg, fast_mode,
|
634 |
"""๋น๋์ค ํ๋ ์ ์ฒ๋ฆฌ"""
|
635 |
try:
|
636 |
pil_image = Image.fromarray(frame)
|
@@ -639,16 +639,29 @@ def process_video_frame(frame, bg_type, bg, fast_mode, bg_frame_index, backgroun
|
|
639 |
elif bg_type == "์ด๋ฏธ์ง":
|
640 |
processed_image = process_bg_image(pil_image, bg, fast_mode)
|
641 |
elif bg_type == "๋น๋์ค":
|
642 |
-
|
643 |
-
|
644 |
-
|
645 |
-
|
|
|
|
|
|
|
|
|
|
|
646 |
else:
|
647 |
processed_image = pil_image
|
648 |
-
|
|
|
|
|
|
|
|
|
|
|
649 |
except Exception as e:
|
650 |
-
print(f"Error processing frame: {e}")
|
651 |
-
|
|
|
|
|
|
|
652 |
|
653 |
@spaces.GPU(duration=300)
|
654 |
def process_video_bg(vid, bg_type="์์", bg_image=None, bg_video=None, color="#00FF00",
|
@@ -672,44 +685,104 @@ def process_video_bg(vid, bg_type="์์", bg_image=None, bg_video=None, color=
|
|
672 |
audio = video.audio
|
673 |
frames = list(video.iter_frames(fps=fps))
|
674 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
675 |
processed_frames = []
|
676 |
yield gr.update(visible=True), gr.update(visible=False), f"์ฒ๋ฆฌ ์์... ๊ฒฝ๊ณผ ์๊ฐ: 0์ด"
|
677 |
|
678 |
-
|
|
|
|
|
679 |
background_video = VideoFileClip(bg_video)
|
680 |
-
|
681 |
-
|
682 |
-
|
683 |
-
|
684 |
-
|
685 |
-
|
686 |
-
|
687 |
-
|
688 |
-
|
689 |
-
background_video = concatenate_videoclips([background_video] *
|
|
|
|
|
|
|
|
|
|
|
|
|
690 |
background_frames = list(background_video.iter_frames(fps=fps))
|
691 |
-
|
692 |
-
|
693 |
-
|
694 |
-
|
695 |
-
|
696 |
-
bg_frame_index = 0
|
697 |
|
|
|
698 |
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
699 |
-
futures = [
|
700 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
701 |
for i, future in enumerate(futures):
|
702 |
-
|
703 |
-
|
704 |
-
|
705 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
706 |
|
|
|
707 |
processed_video = ImageSequenceClip(processed_frames, fps=fps)
|
708 |
-
processed_video = processed_video.with_audio(audio)
|
709 |
|
|
|
|
|
|
|
|
|
|
|
710 |
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as temp_file:
|
711 |
temp_filepath = temp_file.name
|
712 |
-
processed_video.write_videofile(temp_filepath, codec="libx264")
|
713 |
|
714 |
elapsed_time = time.time() - start_time
|
715 |
yield gr.update(visible=False), gr.update(visible=True), f"์ฒ๋ฆฌ ์๋ฃ! ๊ฒฝ๊ณผ ์๊ฐ: {elapsed_time:.2f}์ด"
|
@@ -717,6 +790,8 @@ def process_video_bg(vid, bg_type="์์", bg_image=None, bg_video=None, color=
|
|
717 |
|
718 |
except Exception as e:
|
719 |
print(f"Error: {e}")
|
|
|
|
|
720 |
elapsed_time = time.time() - start_time
|
721 |
yield gr.update(visible=False), gr.update(visible=True), f"๋น๋์ค ์ฒ๋ฆฌ ์ค๋ฅ: {e}. ๊ฒฝ๊ณผ ์๊ฐ: {elapsed_time:.2f}์ด"
|
722 |
yield None, None, f"๋น๋์ค ์ฒ๋ฆฌ ์ค๋ฅ: {e}. ๊ฒฝ๊ณผ ์๊ฐ: {elapsed_time:.2f}์ด"
|
|
|
630 |
image = Image.composite(image, background, mask)
|
631 |
return image
|
632 |
|
633 |
+
def process_video_frame(frame, bg_type, bg, fast_mode, frame_index, background_frames, color):
|
634 |
"""๋น๋์ค ํ๋ ์ ์ฒ๋ฆฌ"""
|
635 |
try:
|
636 |
pil_image = Image.fromarray(frame)
|
|
|
639 |
elif bg_type == "์ด๋ฏธ์ง":
|
640 |
processed_image = process_bg_image(pil_image, bg, fast_mode)
|
641 |
elif bg_type == "๋น๋์ค":
|
642 |
+
# ์ธ๋ฑ์ค ๋ฒ์ ํ์ธ
|
643 |
+
if background_frames and len(background_frames) > 0:
|
644 |
+
# ํ๋ ์ ์ธ๋ฑ์ค๋ฅผ ๋ฐฐ๊ฒฝ ๋น๋์ค ๊ธธ์ด๋ก ๋๋ ๋๋จธ์ง๋ฅผ ์ฌ์ฉ (๋ฃจํ ํจ๊ณผ)
|
645 |
+
bg_frame_index = frame_index % len(background_frames)
|
646 |
+
background_frame = background_frames[bg_frame_index]
|
647 |
+
background_image = Image.fromarray(background_frame)
|
648 |
+
processed_image = process_bg_image(pil_image, background_image, fast_mode)
|
649 |
+
else:
|
650 |
+
processed_image = pil_image
|
651 |
else:
|
652 |
processed_image = pil_image
|
653 |
+
|
654 |
+
# ์ฒ๋ฆฌ๋ ์ด๋ฏธ์ง๊ฐ numpy array๋ก ๋ฐํ๋๋์ง ํ์ธ
|
655 |
+
if isinstance(processed_image, Image.Image):
|
656 |
+
return np.array(processed_image)
|
657 |
+
return processed_image
|
658 |
+
|
659 |
except Exception as e:
|
660 |
+
print(f"Error processing frame {frame_index}: {e}")
|
661 |
+
# ์ค๋ฅ ๋ฐ์ ์ ์๋ณธ ํ๋ ์ ๋ฐํ
|
662 |
+
if isinstance(frame, np.ndarray):
|
663 |
+
return frame
|
664 |
+
return np.array(pil_image)
|
665 |
|
666 |
@spaces.GPU(duration=300)
|
667 |
def process_video_bg(vid, bg_type="์์", bg_image=None, bg_video=None, color="#00FF00",
|
|
|
685 |
audio = video.audio
|
686 |
frames = list(video.iter_frames(fps=fps))
|
687 |
|
688 |
+
# ํ๋ ์ ํฌ๊ธฐ ์ ์ฅ
|
689 |
+
if frames:
|
690 |
+
frame_height, frame_width = frames[0].shape[:2]
|
691 |
+
else:
|
692 |
+
yield gr.update(visible=False), gr.update(visible=True), "๋น๋์ค์ ํ๋ ์์ด ์์ต๋๋ค."
|
693 |
+
yield None, None, "๋น๋์ค์ ํ๋ ์์ด ์์ต๋๋ค."
|
694 |
+
return
|
695 |
+
|
696 |
processed_frames = []
|
697 |
yield gr.update(visible=True), gr.update(visible=False), f"์ฒ๋ฆฌ ์์... ๊ฒฝ๊ณผ ์๊ฐ: 0์ด"
|
698 |
|
699 |
+
# ๋ฐฐ๊ฒฝ ๋น๋์ค ์ฒ๋ฆฌ
|
700 |
+
background_frames = None
|
701 |
+
if bg_type == "๋น๋์ค" and bg_video:
|
702 |
background_video = VideoFileClip(bg_video)
|
703 |
+
|
704 |
+
# ๋ฐฐ๊ฒฝ ๋น๋์ค ๊ธธ์ด ์กฐ์
|
705 |
+
if video_handling == "slow_down" and background_video.duration < video.duration:
|
706 |
+
if speedx is not None:
|
707 |
+
factor = video.duration / background_video.duration
|
708 |
+
background_video = speedx(background_video, factor=factor)
|
709 |
+
else:
|
710 |
+
# speedx๊ฐ ์์ผ๋ฉด ๋ฐ๋ณต์ผ๋ก ๋์ฒด
|
711 |
+
loops = int(video.duration / background_video.duration) + 1
|
712 |
+
background_video = concatenate_videoclips([background_video] * loops)
|
713 |
+
elif video_handling == "loop" or background_video.duration < video.duration:
|
714 |
+
# ๋ฐ๋ณต ๋ชจ๋
|
715 |
+
loops = int(video.duration / background_video.duration) + 1
|
716 |
+
background_video = concatenate_videoclips([background_video] * loops)
|
717 |
+
|
718 |
+
# ๋ฐฐ๊ฒฝ ํ๋ ์ ์ถ์ถ
|
719 |
background_frames = list(background_video.iter_frames(fps=fps))
|
720 |
+
|
721 |
+
# ๋ฐฐ๏ฟฝ๏ฟฝ ๋น๋์ค๊ฐ ๋ ๊ธธ๋ฉด ์๋ผ๋
|
722 |
+
if len(background_frames) > len(frames):
|
723 |
+
background_frames = background_frames[:len(frames)]
|
|
|
|
|
724 |
|
725 |
+
# ๋ณ๋ ฌ ์ฒ๋ฆฌ
|
726 |
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
727 |
+
futures = []
|
728 |
+
for i in range(len(frames)):
|
729 |
+
future = executor.submit(
|
730 |
+
process_video_frame,
|
731 |
+
frames[i],
|
732 |
+
bg_type,
|
733 |
+
bg_image,
|
734 |
+
fast_mode,
|
735 |
+
i, # ํ๋ ์ ์ธ๋ฑ์ค ์ ๋ฌ
|
736 |
+
background_frames,
|
737 |
+
color
|
738 |
+
)
|
739 |
+
futures.append(future)
|
740 |
+
|
741 |
+
# ๊ฒฐ๊ณผ ์์ง
|
742 |
for i, future in enumerate(futures):
|
743 |
+
try:
|
744 |
+
result = future.result()
|
745 |
+
# ๊ฒฐ๊ณผ๊ฐ ์ฌ๋ฐ๋ฅธ ํฌ๊ธฐ์ธ์ง ํ์ธ
|
746 |
+
if result.shape[:2] != (frame_height, frame_width):
|
747 |
+
# ํฌ๊ธฐ๊ฐ ๋ค๋ฅด๋ฉด ๋ฆฌ์ฌ์ด์ฆ
|
748 |
+
pil_result = Image.fromarray(result)
|
749 |
+
pil_result = pil_result.resize((frame_width, frame_height), Image.LANCZOS)
|
750 |
+
result = np.array(pil_result)
|
751 |
+
|
752 |
+
processed_frames.append(result)
|
753 |
+
elapsed_time = time.time() - start_time
|
754 |
+
|
755 |
+
# 10ํ๋ ์๋ง๋ค ์ํ ์
๋ฐ์ดํธ
|
756 |
+
if i % 10 == 0:
|
757 |
+
yield result, None, f"ํ๋ ์ {i+1}/{len(frames)} ์ฒ๋ฆฌ ์ค... ๊ฒฝ๊ณผ ์๊ฐ: {elapsed_time:.2f}์ด"
|
758 |
+
except Exception as e:
|
759 |
+
print(f"Error getting result for frame {i}: {e}")
|
760 |
+
# ์ค๋ฅ ๋ฐ์ ์ ์๋ณธ ํ๋ ์ ์ฌ์ฉ
|
761 |
+
processed_frames.append(frames[i])
|
762 |
+
|
763 |
+
# ๋ชจ๋ ํ๋ ์์ด ๋์ผํ ํฌ๊ธฐ์ธ์ง ์ต์ข
ํ์ธ
|
764 |
+
frame_sizes = [frame.shape for frame in processed_frames]
|
765 |
+
if len(set(frame_sizes)) > 1:
|
766 |
+
print(f"Warning: Different frame sizes detected: {set(frame_sizes)}")
|
767 |
+
# ์ฒซ ๋ฒ์งธ ํ๋ ์ ํฌ๊ธฐ๋ก ๋ชจ๋ ํต์ผ
|
768 |
+
target_size = processed_frames[0].shape
|
769 |
+
for i in range(len(processed_frames)):
|
770 |
+
if processed_frames[i].shape != target_size:
|
771 |
+
pil_frame = Image.fromarray(processed_frames[i])
|
772 |
+
pil_frame = pil_frame.resize((target_size[1], target_size[0]), Image.LANCZOS)
|
773 |
+
processed_frames[i] = np.array(pil_frame)
|
774 |
|
775 |
+
# ๋น๋์ค ์์ฑ
|
776 |
processed_video = ImageSequenceClip(processed_frames, fps=fps)
|
|
|
777 |
|
778 |
+
# ์ค๋์ค ์ถ๊ฐ
|
779 |
+
if audio:
|
780 |
+
processed_video = processed_video.set_audio(audio)
|
781 |
+
|
782 |
+
# ์ ์ฅ
|
783 |
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as temp_file:
|
784 |
temp_filepath = temp_file.name
|
785 |
+
processed_video.write_videofile(temp_filepath, codec="libx264", audio_codec="aac")
|
786 |
|
787 |
elapsed_time = time.time() - start_time
|
788 |
yield gr.update(visible=False), gr.update(visible=True), f"์ฒ๋ฆฌ ์๋ฃ! ๊ฒฝ๊ณผ ์๊ฐ: {elapsed_time:.2f}์ด"
|
|
|
790 |
|
791 |
except Exception as e:
|
792 |
print(f"Error: {e}")
|
793 |
+
import traceback
|
794 |
+
traceback.print_exc()
|
795 |
elapsed_time = time.time() - start_time
|
796 |
yield gr.update(visible=False), gr.update(visible=True), f"๋น๋์ค ์ฒ๋ฆฌ ์ค๋ฅ: {e}. ๊ฒฝ๊ณผ ์๊ฐ: {elapsed_time:.2f}์ด"
|
797 |
yield None, None, f"๋น๋์ค ์ฒ๋ฆฌ ์ค๋ฅ: {e}. ๊ฒฝ๊ณผ ์๊ฐ: {elapsed_time:.2f}์ด"
|