Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,6 @@ import os
|
|
| 6 |
import zipfile
|
| 7 |
import hashlib
|
| 8 |
import matplotlib.pyplot as plt
|
| 9 |
-
import tempfile
|
| 10 |
import logging
|
| 11 |
from pathlib import Path
|
| 12 |
|
|
@@ -14,6 +13,11 @@ from pathlib import Path
|
|
| 14 |
logging.basicConfig(level=logging.INFO)
|
| 15 |
logger = logging.getLogger(__name__)
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def obtener_metadatos(imagen):
|
| 18 |
"""Obtiene los metadatos EXIF de la imagen."""
|
| 19 |
try:
|
|
@@ -69,8 +73,8 @@ def obtener_coordenadas(exif_data):
|
|
| 69 |
return None
|
| 70 |
|
| 71 |
def calcular_hash(imagen):
|
| 72 |
-
"""Calcula el hash
|
| 73 |
-
return hashlib.
|
| 74 |
|
| 75 |
def analizar_manipulacion(imagen, metadatos):
|
| 76 |
"""Analiza si la imagen ha sido manipulada."""
|
|
@@ -93,7 +97,8 @@ def analizar_manipulacion(imagen, metadatos):
|
|
| 93 |
manipulada = True
|
| 94 |
|
| 95 |
# Verificar hash (simulaci贸n)
|
| 96 |
-
|
|
|
|
| 97 |
hash_actual = calcular_hash(imagen)
|
| 98 |
if hash_actual != hash_conocido:
|
| 99 |
razones.append("El hash de la imagen no coincide con el hash conocido.")
|
|
@@ -109,7 +114,7 @@ def realizar_ela(imagen, quality=95, scale=100):
|
|
| 109 |
img_cv = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
|
| 110 |
|
| 111 |
# Guardar imagen comprimida
|
| 112 |
-
temp_path = "temp_image.jpg"
|
| 113 |
cv2.imwrite(temp_path, img_cv, [cv2.IMWRITE_JPEG_QUALITY, quality])
|
| 114 |
|
| 115 |
# Leer imagen comprimida
|
|
@@ -136,10 +141,6 @@ def realizar_ela(imagen, quality=95, scale=100):
|
|
| 136 |
|
| 137 |
def procesar_imagen(archivo_imagen):
|
| 138 |
"""Procesa la imagen completa y genera resultados."""
|
| 139 |
-
# Crear directorio temporal
|
| 140 |
-
temp_dir = tempfile.TemporaryDirectory()
|
| 141 |
-
logger.info(f"Directorio temporal creado: {temp_dir.name}")
|
| 142 |
-
|
| 143 |
try:
|
| 144 |
# Cargar imagen
|
| 145 |
img = Image.open(archivo_imagen)
|
|
@@ -189,12 +190,16 @@ def procesar_imagen(archivo_imagen):
|
|
| 189 |
# Generar texto de an谩lisis
|
| 190 |
analysis_text = f"{info_basica}\n{info_metadatos}\n{info_manipulacion}"
|
| 191 |
|
| 192 |
-
#
|
| 193 |
nombre = os.path.splitext(os.path.basename(archivo_imagen))[0]
|
| 194 |
-
original_path = os.path.join(temp_dir.name, f"{nombre}_original.jpg")
|
| 195 |
-
ela_path = os.path.join(temp_dir.name, f"{nombre}_ela.jpg")
|
| 196 |
-
text_path = os.path.join(temp_dir.name, f"{nombre}_analisis.txt")
|
| 197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
img.save(original_path)
|
| 199 |
cv2.imwrite(ela_path, realizar_ela(img))
|
| 200 |
|
|
@@ -202,13 +207,11 @@ def procesar_imagen(archivo_imagen):
|
|
| 202 |
f.write(analysis_text)
|
| 203 |
|
| 204 |
# Crear ZIP
|
| 205 |
-
zip_path = os.path.join(temp_dir.name, f"{nombre}_errorELA.zip")
|
| 206 |
with zipfile.ZipFile(zip_path, "w") as zipf:
|
| 207 |
zipf.write(original_path, os.path.basename(original_path))
|
| 208 |
zipf.write(ela_path, os.path.basename(ela_path))
|
| 209 |
zipf.write(text_path, os.path.basename(text_path))
|
| 210 |
|
| 211 |
-
# Limpiar archivos temporales (Gradio se encarga de esto autom谩ticamente)
|
| 212 |
logger.info(f"An谩lisis completado. Archivo ZIP: {zip_path}")
|
| 213 |
|
| 214 |
# Devolver el archivo ZIP como resultado
|
|
|
|
| 6 |
import zipfile
|
| 7 |
import hashlib
|
| 8 |
import matplotlib.pyplot as plt
|
|
|
|
| 9 |
import logging
|
| 10 |
from pathlib import Path
|
| 11 |
|
|
|
|
| 13 |
logging.basicConfig(level=logging.INFO)
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
+
# Crear directorio de evidencia permanente
|
| 17 |
+
evidence_dir = "/home/user/evidence"
|
| 18 |
+
os.makedirs(evidence_dir, exist_ok=True)
|
| 19 |
+
logger.info(f"Directorio de evidencia creado en: {evidence_dir}")
|
| 20 |
+
|
| 21 |
def obtener_metadatos(imagen):
|
| 22 |
"""Obtiene los metadatos EXIF de la imagen."""
|
| 23 |
try:
|
|
|
|
| 73 |
return None
|
| 74 |
|
| 75 |
def calcular_hash(imagen):
|
| 76 |
+
"""Calcula el hash SHA3-256 de la imagen (m谩s seguro que MD5)."""
|
| 77 |
+
return hashlib.sha3_256(imagen.tobytes()).hexdigest()
|
| 78 |
|
| 79 |
def analizar_manipulacion(imagen, metadatos):
|
| 80 |
"""Analiza si la imagen ha sido manipulada."""
|
|
|
|
| 97 |
manipulada = True
|
| 98 |
|
| 99 |
# Verificar hash (simulaci贸n)
|
| 100 |
+
# Hash SHA3-256 de ejemplo (64 caracteres hexadecimal)
|
| 101 |
+
hash_conocido = "d8e3d0e0d7a5e2b2c9d5f9d1c8e7a6f5b0d4e7c3f9d1a2b3c4d5e6f7a8b9c0d1"
|
| 102 |
hash_actual = calcular_hash(imagen)
|
| 103 |
if hash_actual != hash_conocido:
|
| 104 |
razones.append("El hash de la imagen no coincide con el hash conocido.")
|
|
|
|
| 114 |
img_cv = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
|
| 115 |
|
| 116 |
# Guardar imagen comprimida
|
| 117 |
+
temp_path = "/tmp/temp_image.jpg"
|
| 118 |
cv2.imwrite(temp_path, img_cv, [cv2.IMWRITE_JPEG_QUALITY, quality])
|
| 119 |
|
| 120 |
# Leer imagen comprimida
|
|
|
|
| 141 |
|
| 142 |
def procesar_imagen(archivo_imagen):
|
| 143 |
"""Procesa la imagen completa y genera resultados."""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
try:
|
| 145 |
# Cargar imagen
|
| 146 |
img = Image.open(archivo_imagen)
|
|
|
|
| 190 |
# Generar texto de an谩lisis
|
| 191 |
analysis_text = f"{info_basica}\n{info_metadatos}\n{info_manipulacion}"
|
| 192 |
|
| 193 |
+
# Obtener nombre de archivo sin extensi贸n
|
| 194 |
nombre = os.path.splitext(os.path.basename(archivo_imagen))[0]
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
+
# Rutas de archivos en directorio de evidencia
|
| 197 |
+
original_path = os.path.join(evidence_dir, f"{nombre}_original.jpg")
|
| 198 |
+
ela_path = os.path.join(evidence_dir, f"{nombre}_ela.jpg")
|
| 199 |
+
text_path = os.path.join(evidence_dir, f"{nombre}_analisis.txt")
|
| 200 |
+
zip_path = os.path.join(evidence_dir, f"{nombre}_errorELA.zip")
|
| 201 |
+
|
| 202 |
+
# Guardar resultados
|
| 203 |
img.save(original_path)
|
| 204 |
cv2.imwrite(ela_path, realizar_ela(img))
|
| 205 |
|
|
|
|
| 207 |
f.write(analysis_text)
|
| 208 |
|
| 209 |
# Crear ZIP
|
|
|
|
| 210 |
with zipfile.ZipFile(zip_path, "w") as zipf:
|
| 211 |
zipf.write(original_path, os.path.basename(original_path))
|
| 212 |
zipf.write(ela_path, os.path.basename(ela_path))
|
| 213 |
zipf.write(text_path, os.path.basename(text_path))
|
| 214 |
|
|
|
|
| 215 |
logger.info(f"An谩lisis completado. Archivo ZIP: {zip_path}")
|
| 216 |
|
| 217 |
# Devolver el archivo ZIP como resultado
|