Update app.py
Browse files
app.py
CHANGED
|
@@ -6,9 +6,9 @@ import os
|
|
| 6 |
import zipfile
|
| 7 |
import hashlib
|
| 8 |
import matplotlib.pyplot as plt
|
| 9 |
-
import
|
| 10 |
import logging
|
| 11 |
-
from
|
| 12 |
|
| 13 |
# Configuraci贸n de logs
|
| 14 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -23,8 +23,11 @@ def obtener_metadatos(imagen):
|
|
| 23 |
|
| 24 |
metadata = {}
|
| 25 |
for tag_id, value in exif_data.items():
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
return metadata
|
| 29 |
except Exception as e:
|
| 30 |
logger.error(f"Error al obtener metadatos: {str(e)}")
|
|
@@ -134,8 +137,8 @@ def realizar_ela(imagen, quality=95, scale=100):
|
|
| 134 |
def procesar_imagen(archivo_imagen):
|
| 135 |
"""Procesa la imagen completa y genera resultados."""
|
| 136 |
# Crear directorio temporal
|
| 137 |
-
temp_dir =
|
| 138 |
-
|
| 139 |
|
| 140 |
try:
|
| 141 |
# Cargar imagen
|
|
@@ -188,9 +191,9 @@ def procesar_imagen(archivo_imagen):
|
|
| 188 |
|
| 189 |
# Guardar resultados
|
| 190 |
nombre = os.path.splitext(os.path.basename(archivo_imagen))[0]
|
| 191 |
-
original_path = os.path.join(temp_dir, f"{nombre}_original.jpg")
|
| 192 |
-
ela_path = os.path.join(temp_dir, f"{nombre}_ela.jpg")
|
| 193 |
-
text_path = os.path.join(temp_dir, f"{nombre}_analisis.txt")
|
| 194 |
|
| 195 |
img.save(original_path)
|
| 196 |
cv2.imwrite(ela_path, realizar_ela(img))
|
|
@@ -199,23 +202,21 @@ def procesar_imagen(archivo_imagen):
|
|
| 199 |
f.write(analysis_text)
|
| 200 |
|
| 201 |
# Crear ZIP
|
| 202 |
-
zip_path = os.path.join(temp_dir, f"{nombre}_errorELA.zip")
|
| 203 |
with zipfile.ZipFile(zip_path, "w") as zipf:
|
| 204 |
zipf.write(original_path, os.path.basename(original_path))
|
| 205 |
zipf.write(ela_path, os.path.basename(ela_path))
|
| 206 |
zipf.write(text_path, os.path.basename(text_path))
|
| 207 |
|
| 208 |
-
# Limpiar archivos temporales
|
| 209 |
-
os.remove(original_path)
|
| 210 |
-
os.remove(ela_path)
|
| 211 |
-
os.remove(text_path)
|
| 212 |
-
|
| 213 |
logger.info(f"An谩lisis completado. Archivo ZIP: {zip_path}")
|
| 214 |
-
|
|
|
|
|
|
|
| 215 |
|
| 216 |
except Exception as e:
|
| 217 |
logger.error(f"Error en procesamiento: {str(e)}")
|
| 218 |
-
return f"Error: {str(e)}"
|
| 219 |
|
| 220 |
# Interfaz Gradio
|
| 221 |
with gr.Blocks(title="An谩lisis Forense de Im谩genes con ELA", theme=gr.themes.Soft()) as demo:
|
|
@@ -256,9 +257,11 @@ with gr.Blocks(title="An谩lisis Forense de Im谩genes con ELA", theme=gr.themes.S
|
|
| 256 |
|
| 257 |
# Configuraci贸n de ejecuci贸n
|
| 258 |
if __name__ == "__main__":
|
|
|
|
| 259 |
demo.launch(
|
| 260 |
server_name="0.0.0.0",
|
| 261 |
server_port=7860,
|
| 262 |
share=True,
|
| 263 |
-
inbrowser=True
|
|
|
|
| 264 |
)
|
|
|
|
| 6 |
import zipfile
|
| 7 |
import hashlib
|
| 8 |
import matplotlib.pyplot as plt
|
| 9 |
+
import tempfile
|
| 10 |
import logging
|
| 11 |
+
from pathlib import Path
|
| 12 |
|
| 13 |
# Configuraci贸n de logs
|
| 14 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 23 |
|
| 24 |
metadata = {}
|
| 25 |
for tag_id, value in exif_data.items():
|
| 26 |
+
try:
|
| 27 |
+
tag = TAGS.get(tag_id, tag_id)
|
| 28 |
+
metadata[tag] = value
|
| 29 |
+
except Exception as e:
|
| 30 |
+
logger.debug(f"Error al procesar etiqueta EXIF: {str(e)}")
|
| 31 |
return metadata
|
| 32 |
except Exception as e:
|
| 33 |
logger.error(f"Error al obtener metadatos: {str(e)}")
|
|
|
|
| 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
|
|
|
|
| 191 |
|
| 192 |
# Guardar resultados
|
| 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))
|
|
|
|
| 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
|
| 215 |
+
return zip_path, analysis_text
|
| 216 |
|
| 217 |
except Exception as e:
|
| 218 |
logger.error(f"Error en procesamiento: {str(e)}")
|
| 219 |
+
return f"Error: {str(e)}", f"Error al procesar la imagen: {str(e)}"
|
| 220 |
|
| 221 |
# Interfaz Gradio
|
| 222 |
with gr.Blocks(title="An谩lisis Forense de Im谩genes con ELA", theme=gr.themes.Soft()) as demo:
|
|
|
|
| 257 |
|
| 258 |
# Configuraci贸n de ejecuci贸n
|
| 259 |
if __name__ == "__main__":
|
| 260 |
+
# Configuraci贸n para Hugging Face Spaces
|
| 261 |
demo.launch(
|
| 262 |
server_name="0.0.0.0",
|
| 263 |
server_port=7860,
|
| 264 |
share=True,
|
| 265 |
+
inbrowser=True,
|
| 266 |
+
favicon_path="https://huggingface.co/datasets/huggingface/logo/resolve/main/hf-logo.png"
|
| 267 |
)
|