Update app.py
Browse files
app.py
CHANGED
|
@@ -140,43 +140,48 @@ def face_image_embedding(image: Image.Image) -> List[float] | None:
|
|
| 140 |
def scenes_extraction(video_file: str, threshold: float, offset_frames: int, crop_ratio: float) -> Tuple[List[Image.Image], List[Dict]] | None:
|
| 141 |
# video_file es un str ya que aunque realmente el usuario subi贸 un archivo desde la UI, Gradio lo guarda temporalmente como ruta
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
# ----------------------------- UI & Endpoints --------------------------------
|
| 182 |
|
|
|
|
| 140 |
def scenes_extraction(video_file: str, threshold: float, offset_frames: int, crop_ratio: float) -> Tuple[List[Image.Image], List[Dict]] | None:
|
| 141 |
# video_file es un str ya que aunque realmente el usuario subi贸 un archivo desde la UI, Gradio lo guarda temporalmente como ruta
|
| 142 |
|
| 143 |
+
try:
|
| 144 |
+
# Detectamos las escenas
|
| 145 |
+
video_manager = VideoManager([video_file])
|
| 146 |
+
scene_manager = SceneManager()
|
| 147 |
+
scene_manager.add_detector(ContentDetector(threshold=threshold))
|
| 148 |
+
video_manager.start()
|
| 149 |
+
scene_manager.detect_scenes(video_manager)
|
| 150 |
+
scene_list = scene_manager.get_scene_list()
|
| 151 |
+
|
| 152 |
+
cap = cv2.VideoCapture(video_file)
|
| 153 |
+
images: List[Image.Image] = []
|
| 154 |
+
informacion_escenas: List[Dict] = []
|
| 155 |
+
|
| 156 |
+
for i, (start_time, end_time) in enumerate(scene_list):
|
| 157 |
+
frame_number = int(start_time.get_frames()) + offset_frames
|
| 158 |
+
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_number)
|
| 159 |
+
ret, frame = cap.read()
|
| 160 |
+
if ret:
|
| 161 |
+
h, w = frame.shape[:2]
|
| 162 |
+
|
| 163 |
+
# Ahora realizamos el recorte
|
| 164 |
+
ch, cw = int(h * crop_ratio), int(w * crop_ratio)
|
| 165 |
+
frame = frame[ch:h-ch, cw:w-cw]
|
| 166 |
+
|
| 167 |
+
# Guardamos la escena obtenida
|
| 168 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 169 |
+
img = Image.fromarray(frame_rgb)
|
| 170 |
+
images.append(img)
|
| 171 |
+
|
| 172 |
+
# Guardamos la informaci贸n de la escena
|
| 173 |
+
informacion_escenas.append({
|
| 174 |
+
"index": i+1,
|
| 175 |
+
"start": start_time.get_seconds(),
|
| 176 |
+
"end": end_time.get_seconds()
|
| 177 |
+
})
|
| 178 |
+
|
| 179 |
+
cap.release()
|
| 180 |
+
return images, informacion_escenas
|
| 181 |
+
|
| 182 |
+
except Exception as e:
|
| 183 |
+
print("Error en scenes_extraction:", e)
|
| 184 |
+
return [], []
|
| 185 |
|
| 186 |
# ----------------------------- UI & Endpoints --------------------------------
|
| 187 |
|