hysts HF Staff commited on
Commit
bb8d681
·
1 Parent(s): dd289a8

Use tempfile

Browse files
Files changed (1) hide show
  1. app.py +8 -7
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import colorsys
2
  import gc
 
3
  from collections.abc import Iterator
4
 
5
  import cv2
@@ -1116,14 +1117,14 @@ with gr.Blocks(title="SAM3", theme=Soft(primary_hue="blue", secondary_hue="rose"
1116
  frames_np.append(np.array(img)[:, :, ::-1])
1117
  if (idx + 1) % 60 == 0:
1118
  gc.collect()
1119
- out_path = "/tmp/sam3_playback.mp4"
1120
  try:
1121
- fourcc = cv2.VideoWriter_fourcc(*"mp4v")
1122
- writer = cv2.VideoWriter(out_path, fourcc, fps, (w, h))
1123
- for fr_bgr in frames_np:
1124
- writer.write(fr_bgr)
1125
- writer.release()
1126
- return out_path
 
1127
  except Exception as e:
1128
  print(f"Failed to render video with cv2: {e}")
1129
  raise gr.Error(f"Failed to render video: {e}")
 
1
  import colorsys
2
  import gc
3
+ import tempfile
4
  from collections.abc import Iterator
5
 
6
  import cv2
 
1117
  frames_np.append(np.array(img)[:, :, ::-1])
1118
  if (idx + 1) % 60 == 0:
1119
  gc.collect()
 
1120
  try:
1121
+ with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as out_path:
1122
+ fourcc = cv2.VideoWriter_fourcc(*"mp4v")
1123
+ writer = cv2.VideoWriter(out_path.name, fourcc, fps, (w, h))
1124
+ for fr_bgr in frames_np:
1125
+ writer.write(fr_bgr)
1126
+ writer.release()
1127
+ return out_path.name
1128
  except Exception as e:
1129
  print(f"Failed to render video with cv2: {e}")
1130
  raise gr.Error(f"Failed to render video: {e}")