Update app.py
Browse files
app.py
CHANGED
|
@@ -143,50 +143,64 @@ def scenes_extraction(video_file: str, threshold: float, offset_frames: int, cro
|
|
| 143 |
# video_file es un str ya que aunque realmente el usuario subi贸 un archivo desde la UI, Gradio lo guarda temporalmente como ruta
|
| 144 |
|
| 145 |
try:
|
|
|
|
| 146 |
video_bytes = base64.b64decode(video_file)
|
| 147 |
-
|
| 148 |
# archivo temporal en /tmp
|
| 149 |
temp_video = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 150 |
temp_video.write(video_bytes)
|
| 151 |
temp_video.flush()
|
| 152 |
temp_video.close()
|
| 153 |
-
|
| 154 |
video_path = temp_video.name
|
|
|
|
| 155 |
# Detectamos las escenas
|
| 156 |
video_manager = VideoManager([video_path])
|
|
|
|
| 157 |
scene_manager = SceneManager()
|
|
|
|
| 158 |
scene_manager.add_detector(ContentDetector(threshold=threshold))
|
|
|
|
| 159 |
video_manager.start()
|
|
|
|
| 160 |
scene_manager.detect_scenes(video_manager)
|
|
|
|
| 161 |
scene_list = scene_manager.get_scene_list()
|
| 162 |
|
| 163 |
cap = cv2.VideoCapture(video_file)
|
|
|
|
| 164 |
images: List[Image.Image] = []
|
| 165 |
informacion_escenas: List[Dict] = []
|
| 166 |
|
| 167 |
for i, (start_time, end_time) in enumerate(scene_list):
|
|
|
|
| 168 |
frame_number = int(start_time.get_frames()) + offset_frames
|
|
|
|
| 169 |
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_number)
|
|
|
|
| 170 |
ret, frame = cap.read()
|
| 171 |
if ret:
|
|
|
|
| 172 |
h, w = frame.shape[:2]
|
| 173 |
|
| 174 |
# Ahora realizamos el recorte
|
|
|
|
| 175 |
ch, cw = int(h * crop_ratio), int(w * crop_ratio)
|
| 176 |
frame = frame[ch:h-ch, cw:w-cw]
|
| 177 |
-
|
| 178 |
# Guardamos la escena obtenida
|
| 179 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 180 |
img = Image.fromarray(frame_rgb)
|
|
|
|
| 181 |
images.append(img)
|
| 182 |
-
|
| 183 |
# Guardamos la informaci贸n de la escena
|
| 184 |
informacion_escenas.append({
|
| 185 |
"index": i+1,
|
| 186 |
"start": start_time.get_seconds(),
|
| 187 |
"end": end_time.get_seconds()
|
| 188 |
})
|
| 189 |
-
|
| 190 |
cap.release()
|
| 191 |
return images, informacion_escenas
|
| 192 |
|
|
|
|
| 143 |
# video_file es un str ya que aunque realmente el usuario subi贸 un archivo desde la UI, Gradio lo guarda temporalmente como ruta
|
| 144 |
|
| 145 |
try:
|
| 146 |
+
print("1")
|
| 147 |
video_bytes = base64.b64decode(video_file)
|
| 148 |
+
print("2")
|
| 149 |
# archivo temporal en /tmp
|
| 150 |
temp_video = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 151 |
temp_video.write(video_bytes)
|
| 152 |
temp_video.flush()
|
| 153 |
temp_video.close()
|
| 154 |
+
print("3")
|
| 155 |
video_path = temp_video.name
|
| 156 |
+
print("4")
|
| 157 |
# Detectamos las escenas
|
| 158 |
video_manager = VideoManager([video_path])
|
| 159 |
+
print("5")
|
| 160 |
scene_manager = SceneManager()
|
| 161 |
+
print("6")
|
| 162 |
scene_manager.add_detector(ContentDetector(threshold=threshold))
|
| 163 |
+
print("7")
|
| 164 |
video_manager.start()
|
| 165 |
+
print("8")
|
| 166 |
scene_manager.detect_scenes(video_manager)
|
| 167 |
+
print("9")
|
| 168 |
scene_list = scene_manager.get_scene_list()
|
| 169 |
|
| 170 |
cap = cv2.VideoCapture(video_file)
|
| 171 |
+
print("10")
|
| 172 |
images: List[Image.Image] = []
|
| 173 |
informacion_escenas: List[Dict] = []
|
| 174 |
|
| 175 |
for i, (start_time, end_time) in enumerate(scene_list):
|
| 176 |
+
print("11")
|
| 177 |
frame_number = int(start_time.get_frames()) + offset_frames
|
| 178 |
+
print("12")
|
| 179 |
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_number)
|
| 180 |
+
print("13")
|
| 181 |
ret, frame = cap.read()
|
| 182 |
if ret:
|
| 183 |
+
print("14")
|
| 184 |
h, w = frame.shape[:2]
|
| 185 |
|
| 186 |
# Ahora realizamos el recorte
|
| 187 |
+
print("15")
|
| 188 |
ch, cw = int(h * crop_ratio), int(w * crop_ratio)
|
| 189 |
frame = frame[ch:h-ch, cw:w-cw]
|
| 190 |
+
print("16")
|
| 191 |
# Guardamos la escena obtenida
|
| 192 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 193 |
img = Image.fromarray(frame_rgb)
|
| 194 |
+
print("17")
|
| 195 |
images.append(img)
|
| 196 |
+
print("18")
|
| 197 |
# Guardamos la informaci贸n de la escena
|
| 198 |
informacion_escenas.append({
|
| 199 |
"index": i+1,
|
| 200 |
"start": start_time.get_seconds(),
|
| 201 |
"end": end_time.get_seconds()
|
| 202 |
})
|
| 203 |
+
print("19")
|
| 204 |
cap.release()
|
| 205 |
return images, informacion_escenas
|
| 206 |
|