Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
@@ -1,148 +0,0 @@
|
|
1 |
-
|
2 |
-
import gradio as gr
|
3 |
-
import cv2
|
4 |
-
import os
|
5 |
-
import zipfile
|
6 |
-
from PIL import Image, ImageOps
|
7 |
-
from datetime import datetime
|
8 |
-
import hashlib
|
9 |
-
|
10 |
-
def procesar_video(video):
|
11 |
-
try:
|
12 |
-
original_name = os.path.basename(video)
|
13 |
-
allowed_extensions = ('.mp4', '.avi', '.mov', '.mkv')
|
14 |
-
if not original_name.lower().endswith(allowed_extensions):
|
15 |
-
raise gr.Error("Solo se permiten archivos de video (mp4, avi, mov, mkv)")
|
16 |
-
|
17 |
-
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
18 |
-
temp_dir = f"temp_{datetime.now().strftime('%Y%m%d%H%M%S')}"
|
19 |
-
os.makedirs(temp_dir, exist_ok=True)
|
20 |
-
|
21 |
-
cap = cv2.VideoCapture(video)
|
22 |
-
frame_count = 0
|
23 |
-
frame_paths = []
|
24 |
-
while True:
|
25 |
-
ret, frame = cap.read()
|
26 |
-
if not ret:
|
27 |
-
break
|
28 |
-
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
29 |
-
img = Image.fromarray(frame_rgb)
|
30 |
-
img_path = os.path.join(temp_dir, f"frame_{frame_count:04d}.jpg")
|
31 |
-
img.save(img_path)
|
32 |
-
frame_paths.append(img_path)
|
33 |
-
frame_count += 1
|
34 |
-
cap.release()
|
35 |
-
|
36 |
-
if frame_count == 0:
|
37 |
-
raise gr.Error("No se pudieron extraer fotogramas del video")
|
38 |
-
|
39 |
-
n_seleccion = 4
|
40 |
-
step = max(1, frame_count // (n_seleccion + 1))
|
41 |
-
selected_indices = [step * (i+1) for i in range(n_seleccion)]
|
42 |
-
selected_frames = [frame_paths[min(i, len(frame_paths)-1)] for i in selected_indices]
|
43 |
-
|
44 |
-
images = []
|
45 |
-
for img_path in selected_frames:
|
46 |
-
img = Image.open(img_path)
|
47 |
-
bordered_img = ImageOps.expand(img, border=2, fill='white')
|
48 |
-
images.append(bordered_img)
|
49 |
-
|
50 |
-
img_w, img_h = images[0].size
|
51 |
-
margin = 30
|
52 |
-
border_size = 20
|
53 |
-
shadow_offset = 5
|
54 |
-
|
55 |
-
collage_width = (img_w * 2) + margin + (border_size * 2)
|
56 |
-
collage_height = (img_h * 2) + margin + (border_size * 2)
|
57 |
-
|
58 |
-
collage = Image.new('RGB', (collage_width, collage_height), (230, 230, 230))
|
59 |
-
positions = [
|
60 |
-
(border_size, border_size),
|
61 |
-
(border_size + img_w + margin, border_size),
|
62 |
-
(border_size, border_size + img_h + margin),
|
63 |
-
(border_size + img_w + margin, border_size + img_h + margin)
|
64 |
-
]
|
65 |
-
|
66 |
-
for i, img in enumerate(images):
|
67 |
-
shadow = Image.new('RGBA', (img_w + shadow_offset, img_h + shadow_offset), (0,0,0,50))
|
68 |
-
collage.paste(shadow, (positions[i][0]+shadow_offset, positions[i][1]+shadow_offset), shadow)
|
69 |
-
collage.paste(img, positions[i])
|
70 |
-
|
71 |
-
collage_path = os.path.join(temp_dir, "collage_forense.jpg")
|
72 |
-
collage.save(collage_path, quality=95, dpi=(300, 300))
|
73 |
-
|
74 |
-
base_name = os.path.splitext(original_name)[0]
|
75 |
-
zip_filename = f"{base_name}_Fotogramas.zip"
|
76 |
-
final_zip_path = os.path.join(temp_dir, zip_filename)
|
77 |
-
|
78 |
-
with zipfile.ZipFile(final_zip_path, mode="w") as zipf:
|
79 |
-
for img_path in frame_paths:
|
80 |
-
zipf.write(img_path, os.path.basename(img_path))
|
81 |
-
|
82 |
-
with open(video, "rb") as f:
|
83 |
-
video_hash = hashlib.md5(f.read()).hexdigest()
|
84 |
-
|
85 |
-
chain_content = (
|
86 |
-
"=== CADENA DE CUSTODIA DIGITAL ===\r\n\r\n"
|
87 |
-
f"• Archivo original: {original_name}\r\n"
|
88 |
-
f"• Fecha de procesamiento: {timestamp}\r\n"
|
89 |
-
f"• Fotogramas totales: {frame_count}\r\n"
|
90 |
-
f"• Hash MD5 video: {video_hash}\r\n"
|
91 |
-
f"• Fotogramas muestra: {', '.join([f'#{i+1}' for i in selected_indices])}\r\n\r\n"
|
92 |
-
"Este documento certifica la integridad del proceso de extracción.\n"
|
93 |
-
"Sistema Certificado por Peritos Forenses Digitales de Guatemala. \n"
|
94 |
-
"www.forensedigital.gt"
|
95 |
-
)
|
96 |
-
zipf.writestr("00_CADENA_CUSTODIA.txt", chain_content)
|
97 |
-
|
98 |
-
return collage_path, final_zip_path
|
99 |
-
|
100 |
-
except Exception as e:
|
101 |
-
raise gr.Error(f"Error en procesamiento: {str(e)}")
|
102 |
-
|
103 |
-
with gr.Blocks(title="Extractor Forense de Fotogramas") as demo:
|
104 |
-
gr.Markdown("# 📷 Extractor Forense de Fotogramas de Videos")
|
105 |
-
gr.Markdown("""
|
106 |
-
**Herramienta certificada para extracción forense de fotogramas de videos**
|
107 |
-
(No se guarda ninguna información).
|
108 |
-
""")
|
109 |
-
gr.Markdown("Desarrollado por José R. Leonett para el Grupo de Peritos Forenses Digitales de Guatemala - [www.forensedigital.gt](https://www.forensedigital.gt)")
|
110 |
-
|
111 |
-
with gr.Row():
|
112 |
-
with gr.Column():
|
113 |
-
video_input = gr.File(
|
114 |
-
label="CARGAR VIDEO",
|
115 |
-
file_types=[".mp4", ".avi", ".mov", ".mkv"],
|
116 |
-
type="filepath",
|
117 |
-
interactive=True
|
118 |
-
)
|
119 |
-
procesar_btn = gr.Button("🔍 INICIAR ANÁLISIS", interactive=False)
|
120 |
-
with gr.Column():
|
121 |
-
gallery_output = gr.Image(label="COLLAGE DE REFERENCIA", height=400)
|
122 |
-
download_file = gr.File(label="DESCARGAR EVIDENCIAS", visible=True)
|
123 |
-
|
124 |
-
import os
|
125 |
-
def habilitar_procesado(video):
|
126 |
-
if video and os.path.exists(video) and video.lower().endswith(('.mp4', '.avi', '.mov', '.mkv')):
|
127 |
-
return gr.update(interactive=True)
|
128 |
-
return gr.update(interactive=False)
|
129 |
-
|
130 |
-
video_input.change(
|
131 |
-
fn=habilitar_procesado,
|
132 |
-
inputs=video_input,
|
133 |
-
outputs=procesar_btn,
|
134 |
-
queue=False
|
135 |
-
)
|
136 |
-
|
137 |
-
def procesar_y_mostrar(video):
|
138 |
-
collage, zip_path = procesar_video(video)
|
139 |
-
return collage, zip_path
|
140 |
-
|
141 |
-
procesar_btn.click(
|
142 |
-
fn=procesar_y_mostrar,
|
143 |
-
inputs=video_input,
|
144 |
-
outputs=[gallery_output, download_file]
|
145 |
-
)
|
146 |
-
|
147 |
-
if __name__ == "__main__":
|
148 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|