openfree commited on
Commit
36eaa13
ยท
verified ยท
1 Parent(s): 065cff4

Update app-backup.py

Browse files
Files changed (1) hide show
  1. app-backup.py +108 -33
app-backup.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, bg_frame_index, background_frames, color):
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
- background_frame = background_frames[bg_frame_index]
643
- bg_frame_index += 1
644
- background_image = Image.fromarray(background_frame)
645
- processed_image = process_bg_image(pil_image, background_image, fast_mode)
 
 
 
 
 
646
  else:
647
  processed_image = pil_image
648
- return np.array(processed_image), bg_frame_index
 
 
 
 
 
649
  except Exception as e:
650
- print(f"Error processing frame: {e}")
651
- return frame, bg_frame_index
 
 
 
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
- if bg_type == "๋น„๋””์˜ค":
 
 
679
  background_video = VideoFileClip(bg_video)
680
- if background_video.duration < video.duration:
681
- if video_handling == "slow_down":
682
- # vfx.speedx ๋Œ€์‹  speedx ํ•จ์ˆ˜ ์ง์ ‘ ์‚ฌ์šฉ
683
- if speedx is not None:
684
- background_video = speedx(background_video, factor=video.duration / background_video.duration)
685
- else:
686
- # speedx๊ฐ€ ์—†์œผ๋ฉด ๋ฐ˜๋ณต์œผ๋กœ ๋Œ€์ฒด
687
- background_video = concatenate_videoclips([background_video] * int(video.duration / background_video.duration + 1))
688
- else: # video_handling == "loop"
689
- background_video = concatenate_videoclips([background_video] * int(video.duration / background_video.duration + 1))
 
 
 
 
 
 
690
  background_frames = list(background_video.iter_frames(fps=fps))
691
- else:
692
- background_frames = None
693
-
694
-
695
-
696
- bg_frame_index = 0
697
 
 
698
  with ThreadPoolExecutor(max_workers=max_workers) as executor:
699
- futures = [executor.submit(process_video_frame, frames[i], bg_type, bg_image, fast_mode,
700
- bg_frame_index + i, background_frames, color) for i in range(len(frames))]
 
 
 
 
 
 
 
 
 
 
 
 
 
701
  for i, future in enumerate(futures):
702
- result, _ = future.result()
703
- processed_frames.append(result)
704
- elapsed_time = time.time() - start_time
705
- yield result, None, f"ํ”„๋ ˆ์ž„ {i+1}/{len(frames)} ์ฒ˜๋ฆฌ ์ค‘... ๊ฒฝ๊ณผ ์‹œ๊ฐ„: {elapsed_time:.2f}์ดˆ"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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}์ดˆ"