Update app.py

#9
by linoyts HF Staff - opened
Files changed (1) hide show
  1. app.py +29 -4
app.py CHANGED
@@ -59,6 +59,7 @@ canny_processor = CannyDetector()
59
 
60
  current_dir = Path(__file__).parent
61
 
 
62
  def cleanup_session_files(request: gr.Request):
63
  """Clean up session-specific temporary files when user disconnects"""
64
  try:
@@ -133,7 +134,8 @@ def process_video_for_canny(video, width, height):
133
  """
134
  print("Processing video for canny control...")
135
  canny_video = []
136
- detect_resolution = max(video[0].size[0],video[0].size[1])
 
137
  image_resolution = max(width, height)
138
  for frame in video:
139
  canny_video.append(canny_processor(frame, low_threshold=50, high_threshold=200, detect_resolution=detect_resolution, image_resolution=image_resolution, output_type="np"))
@@ -145,12 +147,19 @@ def process_input_video(reference_video, width, height,
145
  """
146
  Process the input video for canny edges and return both processed video and preview.
147
  """
 
148
  if reference_video is None:
149
  return None
150
 
151
  try:
152
  # Load video into a list of PIL images
153
- video = load_video(reference_video)
 
 
 
 
 
 
154
 
155
  # Process video for canny edges
156
  processed_video = process_video_for_canny(video, width, height)
@@ -170,7 +179,13 @@ def process_input_video(reference_video, width, height,
170
 
171
  def process_video_for_control(reference_video, control_type, width, height):
172
  """Process video based on the selected control type - now only used for non-canny types"""
173
- video = load_video(reference_video)
 
 
 
 
 
 
174
 
175
  if control_type == "canny":
176
  # This should not be called for canny since it's pre-processed
@@ -232,6 +247,16 @@ def generate_video(
232
  # Convert to tensor
233
  processed_video = read_video(processed_video)
234
  print(type(processed_video))
 
 
 
 
 
 
 
 
 
 
235
 
236
 
237
  # Calculate downscaled dimensions
@@ -305,7 +330,7 @@ def generate_video(
305
  return None, seed
306
 
307
  # Create Gradio interface
308
- with gr.Blocks(theme=gr.themes.Ocean(font=[gr.themes.GoogleFont("Lexend Deca"), "sans-serif"]), delete_cache=(60, 3600)) as demo:
309
  gr.Markdown(
310
  """
311
  # Canny Control LTX Video Distilled
 
59
 
60
  current_dir = Path(__file__).parent
61
 
62
+
63
  def cleanup_session_files(request: gr.Request):
64
  """Clean up session-specific temporary files when user disconnects"""
65
  try:
 
134
  """
135
  print("Processing video for canny control...")
136
  canny_video = []
137
+ print(video[0].shape[0], video[0].shape[1])
138
+ detect_resolution = max(video[0].shape[0],video[0].shape[1])
139
  image_resolution = max(width, height)
140
  for frame in video:
141
  canny_video.append(canny_processor(frame, low_threshold=50, high_threshold=200, detect_resolution=detect_resolution, image_resolution=image_resolution, output_type="np"))
 
147
  """
148
  Process the input video for canny edges and return both processed video and preview.
149
  """
150
+ print(current_dir)
151
  if reference_video is None:
152
  return None
153
 
154
  try:
155
  # Load video into a list of PIL images
156
+ #video = load_video(reference_video)
157
+ video = []
158
+ with imageio.get_reader(reference_video) as reader:
159
+ # Read all frames
160
+ for frame in reader:
161
+ video.append(frame)
162
+ #video is a numpy array
163
 
164
  # Process video for canny edges
165
  processed_video = process_video_for_canny(video, width, height)
 
179
 
180
  def process_video_for_control(reference_video, control_type, width, height):
181
  """Process video based on the selected control type - now only used for non-canny types"""
182
+ #video = load_video(reference_video)
183
+
184
+ video = []
185
+ with imageio.get_reader(reference_video) as reader:
186
+ # Read all frames
187
+ for frame in reader:
188
+ video.append(frame)
189
 
190
  if control_type == "canny":
191
  # This should not be called for canny since it's pre-processed
 
247
  # Convert to tensor
248
  processed_video = read_video(processed_video)
249
  print(type(processed_video))
250
+
251
+ # Clean up example video if it's cached in tmp
252
+ if control_video and "/tmp" in control_video and any(x in control_video for x in ["vid_1_canny", "vid_2_canny", "vid_3_canny", "vid_4_canny"]):
253
+ if os.path.exists(control_video):
254
+ try:
255
+ os.remove(control_video)
256
+ print(f"βœ“ Deleted cached example video: {control_video}")
257
+ except OSError as e:
258
+ print(f"βœ— Failed to delete {control_video}: {e}")
259
+
260
 
261
 
262
  # Calculate downscaled dimensions
 
330
  return None, seed
331
 
332
  # Create Gradio interface
333
+ with gr.Blocks(theme=gr.themes.Ocean(font=[gr.themes.GoogleFont("Lexend Deca"), "sans-serif"]), delete_cache=(60, 1800)) as demo:
334
  gr.Markdown(
335
  """
336
  # Canny Control LTX Video Distilled