leonett commited on
Commit
57ef3c4
verified
1 Parent(s): 4c40bba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -10
app.py CHANGED
@@ -6,8 +6,16 @@ import zipfile
6
  from PIL import Image
7
  from datetime import datetime
8
 
9
- def procesar_video(video_path):
10
  try:
 
 
 
 
 
 
 
 
11
  # Configurar directorio temporal
12
  temp_dir = f"temp_{datetime.now().strftime('%Y%m%d%H%M%S')}"
13
  os.makedirs(temp_dir, exist_ok=True)
@@ -52,8 +60,10 @@ def procesar_video(video_path):
52
  collage_path = os.path.join(temp_dir, "collage.jpg")
53
  collage.save(collage_path)
54
 
55
- # Crear archivo ZIP con TODOS los frames
56
- zip_path = os.path.join(temp_dir, "frames.zip")
 
 
57
  with zipfile.ZipFile(zip_path, 'w') as zipf:
58
  for img in frame_paths:
59
  zipf.write(img, os.path.basename(img))
@@ -84,15 +94,20 @@ with gr.Blocks(title="Extracci贸n de Fotogramas Forenses") as demo:
84
  download_btn = gr.Button("DESCARGAR FOTOGRAMAS", interactive=False)
85
  download_file = gr.File(label="Archivo ZIP generado", visible=False)
86
 
 
87
  temp_dir_state = gr.State()
 
88
 
 
89
  def habilitar_procesar(video):
90
- return gr.Button(interactive=True) if video else gr.Button(interactive=False)
91
 
 
92
  def procesar_y_mostrar(video):
93
- limpiar_cache(temp_dir_state.value)
 
94
  collage_path, zip_path, temp_dir = procesar_video(video)
95
- return collage_path, zip_path, temp_dir, gr.Button(interactive=True)
96
 
97
  video_input.change(
98
  fn=habilitar_procesar,
@@ -104,14 +119,15 @@ with gr.Blocks(title="Extracci贸n de Fotogramas Forenses") as demo:
104
  procesar_btn.click(
105
  fn=procesar_y_mostrar,
106
  inputs=video_input,
107
- outputs=[gallery_output, download_file, temp_dir_state, download_btn],
108
  )
109
 
 
110
  download_btn.click(
111
- fn=lambda temp_dir: os.path.join(temp_dir, "frames.zip"),
112
- inputs=temp_dir_state,
113
  outputs=download_file,
114
  )
115
 
116
  if __name__ == "__main__":
117
- demo.launch()
 
6
  from PIL import Image
7
  from datetime import datetime
8
 
9
+ def procesar_video(video):
10
  try:
11
+ # Determinar la ruta del video y el nombre original
12
+ if isinstance(video, dict):
13
+ original_name = video.get("name", "video")
14
+ video_path = video.get("file", video.get("data"))
15
+ else:
16
+ original_name = os.path.basename(video)
17
+ video_path = video
18
+
19
  # Configurar directorio temporal
20
  temp_dir = f"temp_{datetime.now().strftime('%Y%m%d%H%M%S')}"
21
  os.makedirs(temp_dir, exist_ok=True)
 
60
  collage_path = os.path.join(temp_dir, "collage.jpg")
61
  collage.save(collage_path)
62
 
63
+ # Crear archivo ZIP con TODOS los frames usando el nombre original del video
64
+ base_name = os.path.splitext(original_name)[0]
65
+ zip_filename = f"{base_name}.zip"
66
+ zip_path = os.path.join(temp_dir, zip_filename)
67
  with zipfile.ZipFile(zip_path, 'w') as zipf:
68
  for img in frame_paths:
69
  zipf.write(img, os.path.basename(img))
 
94
  download_btn = gr.Button("DESCARGAR FOTOGRAMAS", interactive=False)
95
  download_file = gr.File(label="Archivo ZIP generado", visible=False)
96
 
97
+ # Estados para guardar el directorio temporal y la ruta del ZIP
98
  temp_dir_state = gr.State()
99
+ zip_path_state = gr.State()
100
 
101
+ # Habilita el bot贸n de procesar solo si se ha subido un video
102
  def habilitar_procesar(video):
103
+ return gr.Button.update(interactive=True) if video else gr.Button.update(interactive=False)
104
 
105
+ # Procesa el video, genera el collage y el ZIP y actualiza los estados
106
  def procesar_y_mostrar(video):
107
+ if temp_dir_state.value:
108
+ limpiar_cache(temp_dir_state.value)
109
  collage_path, zip_path, temp_dir = procesar_video(video)
110
+ return collage_path, zip_path, temp_dir, zip_path, gr.Button.update(interactive=True)
111
 
112
  video_input.change(
113
  fn=habilitar_procesar,
 
119
  procesar_btn.click(
120
  fn=procesar_y_mostrar,
121
  inputs=video_input,
122
+ outputs=[gallery_output, download_file, temp_dir_state, zip_path_state, download_btn],
123
  )
124
 
125
+ # Al pulsar "DESCARGAR FOTOGRAMAS", se env铆a la ruta del ZIP almacenada en zip_path_state
126
  download_btn.click(
127
+ fn=lambda zip_path: zip_path,
128
+ inputs=zip_path_state,
129
  outputs=download_file,
130
  )
131
 
132
  if __name__ == "__main__":
133
+ demo.launch()