LoufAn commited on
Commit
29f26e8
·
1 Parent(s): 91db62e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -4
app.py CHANGED
@@ -6,19 +6,21 @@ import tempfile
6
  import imageio
7
  from decord import VideoReader, cpu
8
  from transformers import pipeline
 
9
 
10
  hf_token = os.environ.get("HUGGINGFACE_TOKEN")
11
  model_id = "google/gemma-3-27b-it"
12
  NUM_FRAMES = 8
13
 
14
  # 从视频中采样 N 帧
15
- def sample_video_frames(video_path, num_frames=NUM_FRAMES):
16
  vr = VideoReader(video_path, ctx=cpu(0))
17
  total_frames = len(vr)
18
  indices = [int(i) for i in torch.linspace(0, total_frames - 1, steps=num_frames)]
19
- frames = [vr[i].asnumpy() for i in indices]
20
- pil_frames = [imageio.core.util.Array(frame) for frame in frames]
21
- return pil_frames
 
22
 
23
  # 推理函数:加载模型、采样视频帧、推理
24
  @spaces.GPU
 
6
  import imageio
7
  from decord import VideoReader, cpu
8
  from transformers import pipeline
9
+ from PIL import Image
10
 
11
  hf_token = os.environ.get("HUGGINGFACE_TOKEN")
12
  model_id = "google/gemma-3-27b-it"
13
  NUM_FRAMES = 8
14
 
15
  # 从视频中采样 N 帧
16
+ def sample_video_frames(video_path, num_frames=8):
17
  vr = VideoReader(video_path, ctx=cpu(0))
18
  total_frames = len(vr)
19
  indices = [int(i) for i in torch.linspace(0, total_frames - 1, steps=num_frames)]
20
+
21
+ # 关键点:强制转换为 PIL.Image
22
+ frames = [Image.fromarray(vr[i].asnumpy()) for i in indices]
23
+ return frames
24
 
25
  # 推理函数:加载模型、采样视频帧、推理
26
  @spaces.GPU