Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,7 @@ import hashlib
|
|
| 8 |
import matplotlib.pyplot as plt
|
| 9 |
import logging
|
| 10 |
from pathlib import Path
|
|
|
|
| 11 |
|
| 12 |
# Configuración de logs
|
| 13 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -28,7 +29,8 @@ def obtener_metadatos(imagen):
|
|
| 28 |
metadata = {}
|
| 29 |
for tag_id, value in exif_data.items():
|
| 30 |
try:
|
| 31 |
-
|
|
|
|
| 32 |
metadata[tag] = value
|
| 33 |
except Exception as e:
|
| 34 |
logger.debug(f"Error al procesar etiqueta EXIF: {str(e)}")
|
|
@@ -73,7 +75,7 @@ def obtener_coordenadas(exif_data):
|
|
| 73 |
return None
|
| 74 |
|
| 75 |
def calcular_hash(imagen):
|
| 76 |
-
"""Calcula el hash SHA3-256 de la imagen
|
| 77 |
return hashlib.sha3_256(imagen.tobytes()).hexdigest()
|
| 78 |
|
| 79 |
def analizar_manipulacion(imagen, metadatos):
|
|
@@ -157,7 +159,14 @@ def procesar_imagen(archivo_imagen):
|
|
| 157 |
|
| 158 |
# Guardar resultados
|
| 159 |
img.save(original_path)
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
# Generar texto de análisis
|
| 163 |
info_basica = f"Formato: {img.format}\nTamaño: {img.size} píxeles\nModo: {img.mode}\n"
|
|
@@ -189,6 +198,10 @@ def procesar_imagen(archivo_imagen):
|
|
| 189 |
else:
|
| 190 |
info_metadatos += "No se encontraron metadatos EXIF\n"
|
| 191 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
# Análisis de manipulación
|
| 193 |
manipulada, razones = analizar_manipulacion(img, metadatos)
|
| 194 |
info_manipulacion = "\nANÁLISIS DE MANIPULACIÓN:\n"
|
|
@@ -220,8 +233,8 @@ def procesar_imagen(archivo_imagen):
|
|
| 220 |
logger.error(f"Error en procesamiento: {str(e)}")
|
| 221 |
return f"Error: {str(e)}", f"Error al procesar la imagen: {str(e)}"
|
| 222 |
|
| 223 |
-
# Interfaz Gradio
|
| 224 |
-
with gr.Blocks(title="Análisis Forense de Imágenes con ELA", theme=gr.themes.
|
| 225 |
gr.Markdown("""
|
| 226 |
# 📸 Análisis Forense de Imágenes con Error Level Analysis (ELA)
|
| 227 |
Programa de computación forense para analizar imágenes en busca de evidencia de manipulación o edición.
|
|
@@ -229,18 +242,26 @@ with gr.Blocks(title="Análisis Forense de Imágenes con ELA", theme=gr.themes.S
|
|
| 229 |
|
| 230 |
with gr.Row():
|
| 231 |
with gr.Column():
|
|
|
|
| 232 |
input_image = gr.Image(label="Subir imagen (JPG/PNG)", type="filepath", height=400)
|
| 233 |
process_btn = gr.Button("Analizar imagen", variant="primary")
|
| 234 |
|
| 235 |
with gr.Column():
|
| 236 |
-
|
| 237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
# Evento de procesamiento
|
| 240 |
process_btn.click(
|
| 241 |
fn=procesar_imagen,
|
| 242 |
inputs=input_image,
|
| 243 |
-
outputs=[
|
| 244 |
api_name="analyze_image"
|
| 245 |
)
|
| 246 |
|
|
|
|
| 8 |
import matplotlib.pyplot as plt
|
| 9 |
import logging
|
| 10 |
from pathlib import Path
|
| 11 |
+
from PIL import ExifTags
|
| 12 |
|
| 13 |
# Configuración de logs
|
| 14 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 29 |
metadata = {}
|
| 30 |
for tag_id, value in exif_data.items():
|
| 31 |
try:
|
| 32 |
+
# Convertir etiquetas EXIF a nombres legibles
|
| 33 |
+
tag = ExifTags.TAGS.get(tag_id, tag_id)
|
| 34 |
metadata[tag] = value
|
| 35 |
except Exception as e:
|
| 36 |
logger.debug(f"Error al procesar etiqueta EXIF: {str(e)}")
|
|
|
|
| 75 |
return None
|
| 76 |
|
| 77 |
def calcular_hash(imagen):
|
| 78 |
+
"""Calcula el hash SHA3-256 de la imagen."""
|
| 79 |
return hashlib.sha3_256(imagen.tobytes()).hexdigest()
|
| 80 |
|
| 81 |
def analizar_manipulacion(imagen, metadatos):
|
|
|
|
| 159 |
|
| 160 |
# Guardar resultados
|
| 161 |
img.save(original_path)
|
| 162 |
+
ela_imagen = realizar_ela(img)
|
| 163 |
+
|
| 164 |
+
# Convertir ELA a imagen con colores para resaltar áreas modificadas
|
| 165 |
+
# Usamos un mapa de color para diferenciar las zonas con error
|
| 166 |
+
ela_color = cv2.applyColorMap(ela_imagen, cv2.COLORMAP_JET)
|
| 167 |
+
|
| 168 |
+
# Guardar imagen con ELA en color
|
| 169 |
+
cv2.imwrite(ela_path, ela_color)
|
| 170 |
|
| 171 |
# Generar texto de análisis
|
| 172 |
info_basica = f"Formato: {img.format}\nTamaño: {img.size} píxeles\nModo: {img.mode}\n"
|
|
|
|
| 198 |
else:
|
| 199 |
info_metadatos += "No se encontraron metadatos EXIF\n"
|
| 200 |
|
| 201 |
+
# Añadir SHA3-256 al análisis
|
| 202 |
+
sha3_hash = calcular_hash(img)
|
| 203 |
+
info_metadatos += f"\nSHA3-256: {sha3_hash}\n"
|
| 204 |
+
|
| 205 |
# Análisis de manipulación
|
| 206 |
manipulada, razones = analizar_manipulacion(img, metadatos)
|
| 207 |
info_manipulacion = "\nANÁLISIS DE MANIPULACIÓN:\n"
|
|
|
|
| 233 |
logger.error(f"Error en procesamiento: {str(e)}")
|
| 234 |
return f"Error: {str(e)}", f"Error al procesar la imagen: {str(e)}"
|
| 235 |
|
| 236 |
+
# Interfaz Gradio con tema oscuro
|
| 237 |
+
with gr.Blocks(title="Análisis Forense de Imágenes con ELA", theme=gr.themes.Dark()) as demo:
|
| 238 |
gr.Markdown("""
|
| 239 |
# 📸 Análisis Forense de Imágenes con Error Level Analysis (ELA)
|
| 240 |
Programa de computación forense para analizar imágenes en busca de evidencia de manipulación o edición.
|
|
|
|
| 242 |
|
| 243 |
with gr.Row():
|
| 244 |
with gr.Column():
|
| 245 |
+
# Eliminar opción de webcam
|
| 246 |
input_image = gr.Image(label="Subir imagen (JPG/PNG)", type="filepath", height=400)
|
| 247 |
process_btn = gr.Button("Analizar imagen", variant="primary")
|
| 248 |
|
| 249 |
with gr.Column():
|
| 250 |
+
# Sección de resultados
|
| 251 |
+
with gr.Accordion("Descargar resultados ZIP", open=True):
|
| 252 |
+
download_zip = gr.File(label="Descargar archivo ZIP", interactive=False)
|
| 253 |
+
metadata_text = gr.Textbox(label="Metadatos y SHA3-256", lines=10, max_lines=20)
|
| 254 |
+
|
| 255 |
+
# Sección de análisis detallado
|
| 256 |
+
with gr.Accordion("Análisis detallado", open=True):
|
| 257 |
+
ela_image = gr.Image(label="Imagen con Error Level Analysis (ELA)", type="numpy", elem_id="ela_image")
|
| 258 |
+
analysis_text = gr.Textbox(label="Análisis detallado", lines=15, max_lines=25)
|
| 259 |
|
| 260 |
# Evento de procesamiento
|
| 261 |
process_btn.click(
|
| 262 |
fn=procesar_imagen,
|
| 263 |
inputs=input_image,
|
| 264 |
+
outputs=[download_zip, metadata_text, ela_image, analysis_text],
|
| 265 |
api_name="analyze_image"
|
| 266 |
)
|
| 267 |
|