KingNish commited on
Commit
05b4e29
1 Parent(s): e87311d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -30
app.py CHANGED
@@ -34,27 +34,6 @@ transform_image = transforms.Compose(
34
  ]
35
  )
36
 
37
- # Function to delete files older than 10 minutes in the temp directory
38
- def cleanup_temp_files():
39
- while True:
40
- temp_dir = "temp"
41
- if os.path.exists(temp_dir):
42
- for filename in os.listdir(temp_dir):
43
- filepath = os.path.join(temp_dir, filename)
44
- if os.path.isfile(filepath):
45
- file_age = time.time() - os.path.getmtime(filepath)
46
- if file_age > 600: # 10 minutes in seconds
47
- try:
48
- os.remove(filepath)
49
- print(f"Deleted temporary file: {filepath}")
50
- except Exception as e:
51
- print(f"Error deleting file {filepath}: {e}")
52
- time.sleep(60) # Check every minute
53
-
54
- # Start the cleanup thread
55
- cleanup_thread = threading.Thread(target=cleanup_temp_files, daemon=True)
56
- cleanup_thread.start()
57
-
58
  # Function to process a single frame
59
  def process_frame(frame, bg_type, bg, fast_mode, bg_frame_index, background_frames, color):
60
  try:
@@ -111,13 +90,13 @@ def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", fps=
111
  bg_frame_index = 0 # Initialize background frame index
112
 
113
  # Use ThreadPoolExecutor for parallel processing with specified max_workers
114
- with ThreadPoolExecutor(max_workers=min(max_workers, 32)) as executor: # Limit max_workers to 32
115
  futures = [executor.submit(process_frame, frames[i], bg_type, bg_image, fast_mode, bg_frame_index, background_frames, color) for i in range(len(frames))]
116
  for future in futures:
117
  result, bg_frame_index = future.result()
118
  processed_frames.append(result)
119
- elapsed_time = time.time() - start_time
120
- yield result, None, f"Processing frame {len(processed_frames)}... Elapsed time: {elapsed_time:.2f} seconds"
121
 
122
  # Create a new video from the processed frames
123
  processed_video = mp.ImageSequenceClip(processed_frames, fps=fps)
@@ -125,12 +104,10 @@ def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", fps=
125
  # Add the original audio back to the processed video
126
  processed_video = processed_video.set_audio(audio)
127
 
128
- # Save the processed video to a temporary file
129
- temp_dir = "temp"
130
- os.makedirs(temp_dir, exist_ok=True)
131
- unique_filename = str(uuid.uuid4()) + ".mp4"
132
- temp_filepath = os.path.join(temp_dir, unique_filename)
133
- processed_video.write_videofile(temp_filepath, codec="libx264")
134
 
135
  elapsed_time = time.time() - start_time
136
  yield gr.update(visible=False), gr.update(visible=True), f"Processing complete! Elapsed time: {elapsed_time:.2f} seconds"
 
34
  ]
35
  )
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  # Function to process a single frame
38
  def process_frame(frame, bg_type, bg, fast_mode, bg_frame_index, background_frames, color):
39
  try:
 
90
  bg_frame_index = 0 # Initialize background frame index
91
 
92
  # Use ThreadPoolExecutor for parallel processing with specified max_workers
93
+ with ThreadPoolExecutor(max_workers=max_workers) as executor:
94
  futures = [executor.submit(process_frame, frames[i], bg_type, bg_image, fast_mode, bg_frame_index, background_frames, color) for i in range(len(frames))]
95
  for future in futures:
96
  result, bg_frame_index = future.result()
97
  processed_frames.append(result)
98
+ elapsed_time = time.time() - start_time
99
+ yield result, None, f"Processing frame {len(processed_frames)}... Elapsed time: {elapsed_time:.2f} seconds"
100
 
101
  # Create a new video from the processed frames
102
  processed_video = mp.ImageSequenceClip(processed_frames, fps=fps)
 
104
  # Add the original audio back to the processed video
105
  processed_video = processed_video.set_audio(audio)
106
 
107
+ # Save the processed video to a temporary file using tempfile
108
+ with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as temp_file:
109
+ temp_filepath = temp_file.name
110
+ processed_video.write_videofile(temp_filepath, codec="libx264")
 
 
111
 
112
  elapsed_time = time.time() - start_time
113
  yield gr.update(visible=False), gr.update(visible=True), f"Processing complete! Elapsed time: {elapsed_time:.2f} seconds"