Update handler.py
Browse files- handler.py +10 -26
handler.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from typing import Dict,
|
2 |
from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
|
3 |
from modelscope import snapshot_download
|
4 |
from qwen_vl_utils import process_vision_info
|
@@ -7,9 +7,9 @@ import os
|
|
7 |
import base64
|
8 |
import io
|
9 |
from PIL import Image
|
10 |
-
import ffmpeg
|
11 |
import logging
|
12 |
import requests
|
|
|
13 |
|
14 |
class EndpointHandler():
|
15 |
def __init__(self, path=""):
|
@@ -98,31 +98,15 @@ class EndpointHandler():
|
|
98 |
return image
|
99 |
|
100 |
def _extract_video_frames(self, video_path, fps=1):
|
101 |
-
"""Extracts frames from a video at the specified FPS."""
|
102 |
try:
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
width = int(video_stream['width'])
|
110 |
-
height = int(video_stream['height'])
|
111 |
-
|
112 |
-
out, _ = (
|
113 |
-
ffmpeg
|
114 |
-
.input(video_path)
|
115 |
-
.filter('fps', fps=fps)
|
116 |
-
.output('pipe:', format='rawvideo', pix_fmt='rgb24')
|
117 |
-
.run(capture_stdout=True)
|
118 |
-
)
|
119 |
-
frames = []
|
120 |
-
for i in range(0, len(out), width * height * 3):
|
121 |
-
frame_data = out[i:i + width * height * 3]
|
122 |
-
frame = Image.frombytes('RGB', (width, height), frame_data)
|
123 |
-
frames.append(frame)
|
124 |
return frames
|
125 |
-
|
126 |
-
except ffmpeg.Error as e:
|
127 |
logging.error(f"Error extracting video frames: {e}")
|
128 |
return None
|
|
|
1 |
+
from typing import Dict, Any
|
2 |
from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
|
3 |
from modelscope import snapshot_download
|
4 |
from qwen_vl_utils import process_vision_info
|
|
|
7 |
import base64
|
8 |
import io
|
9 |
from PIL import Image
|
|
|
10 |
import logging
|
11 |
import requests
|
12 |
+
from moviepy.editor import VideoFileClip # For video frame extraction
|
13 |
|
14 |
class EndpointHandler():
|
15 |
def __init__(self, path=""):
|
|
|
98 |
return image
|
99 |
|
100 |
def _extract_video_frames(self, video_path, fps=1):
|
101 |
+
"""Extracts frames from a video at the specified FPS using MoviePy."""
|
102 |
try:
|
103 |
+
video = VideoFileClip(video_path)
|
104 |
+
frames = [
|
105 |
+
Image.fromarray(frame.astype('uint8'), 'RGB')
|
106 |
+
for frame in video.iter_frames(fps=fps)
|
107 |
+
]
|
108 |
+
video.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
return frames
|
110 |
+
except Exception as e:
|
|
|
111 |
logging.error(f"Error extracting video frames: {e}")
|
112 |
return None
|