Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,73 +2,81 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
import cv2
|
| 5 |
-
import tempfile
|
| 6 |
-
import os
|
| 7 |
from PIL import Image
|
| 8 |
|
| 9 |
# ─────────────────────────────────────────────────────────────
|
| 10 |
-
# FONCTIONS DE DÉTECTION
|
| 11 |
# ─────────────────────────────────────────────────────────────
|
| 12 |
|
| 13 |
-
def
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
def get_deepfake_score(img):
|
| 21 |
-
"""Calcule
|
| 22 |
-
# Analyse de la texture
|
| 23 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
| 24 |
dft = np.fft.fft2(gray)
|
| 25 |
dft_shift = np.fft.fftshift(dft)
|
| 26 |
magnitude_spectrum = 20 * np.log(np.abs(dft_shift) + 1)
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
score = 100 - (high_freq_mean * 0.8)
|
| 33 |
return min(max(score, 0), 100)
|
| 34 |
|
| 35 |
def analyze_image(img_input):
|
| 36 |
if img_input is None: return None, "Erreur"
|
| 37 |
|
| 38 |
-
# 1.
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
heatmap = cv2.applyColorMap(cv2.equalizeHist(gray), cv2.COLORMAP_JET)
|
| 44 |
|
| 45 |
-
# 3. Création du graphique de résultat
|
| 46 |
-
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6))
|
| 47 |
ax1.imshow(img_input)
|
| 48 |
-
ax1.set_title("
|
| 49 |
ax1.axis('off')
|
| 50 |
|
| 51 |
-
ax2.imshow(
|
| 52 |
-
ax2.set_title("
|
| 53 |
ax2.axis('off')
|
| 54 |
|
| 55 |
-
|
|
|
|
|
|
|
| 56 |
|
| 57 |
report = f"""
|
| 58 |
-
🔬 RAPPORT D'EXPERTISE MÉDIASHIELD PRO
|
| 59 |
--------------------------------------
|
| 60 |
STATUT : {status}
|
| 61 |
INDICE DE PROBABILITÉ IA : {df_score:.2f}%
|
| 62 |
|
| 63 |
-
ANALYSE
|
| 64 |
-
-
|
| 65 |
-
-
|
| 66 |
-
- Analyse
|
| 67 |
|
| 68 |
-
Conclusion :
|
| 69 |
-
typiques des outils de génération d'images par IA.
|
| 70 |
--------------------------------------
|
| 71 |
-
|
| 72 |
"""
|
| 73 |
return fig, report
|
| 74 |
|
|
@@ -76,18 +84,18 @@ Projet Trusted Sound 2026 - ACoNum
|
|
| 76 |
# INTERFACE
|
| 77 |
# ─────────────────────────────────────────────────────────────
|
| 78 |
|
| 79 |
-
with gr.Blocks(theme=gr.themes.
|
| 80 |
-
gr.Markdown("# 🛡️ ImageShield -
|
| 81 |
-
gr.Markdown("Analyseur de
|
| 82 |
|
| 83 |
with gr.Row():
|
| 84 |
input_img = gr.Image(label="Image à expertiser")
|
| 85 |
-
output_plot = gr.Plot(label="
|
| 86 |
|
| 87 |
with gr.Row():
|
| 88 |
output_text = gr.Textbox(label="Rapport de l'Agent", lines=10)
|
| 89 |
|
| 90 |
-
submit_btn = gr.Button("Lancer l'Analyse
|
| 91 |
submit_btn.click(fn=analyze_image, inputs=input_img, outputs=[output_plot, output_text])
|
| 92 |
|
| 93 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
import cv2
|
|
|
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
# ─────────────────────────────────────────────────────────────
|
| 8 |
+
# FONCTIONS DE DÉTECTION AVANCÉES
|
| 9 |
# ─────────────────────────────────────────────────────────────
|
| 10 |
|
| 11 |
+
def infrared_thermal_analysis(img):
|
| 12 |
+
"""
|
| 13 |
+
Simule une analyse infrarouge via l'espace colorimétrique YCrCb.
|
| 14 |
+
Détecte les anomalies de chaleur/couleur typiques des Deepfakes.
|
| 15 |
+
"""
|
| 16 |
+
# Conversion en YCrCb (Luminance + Chrominance Rouge/Bleu)
|
| 17 |
+
ycrcb = cv2.cvtColor(img, cv2.COLOR_RGB2YCrCb)
|
| 18 |
+
# On extrait le canal 'Cr' qui réagit comme un capteur thermique
|
| 19 |
+
thermal = ycrcb[:, :, 1]
|
| 20 |
+
# On applique une palette de couleur "JET" pour simuler l'infrarouge
|
| 21 |
+
infrared_map = cv2.applyColorMap(thermal, cv2.COLORMAP_JET)
|
| 22 |
+
|
| 23 |
+
# Calcul d'incohérence thermique
|
| 24 |
+
std_thermal = np.std(thermal)
|
| 25 |
+
return infrared_map, std_thermal
|
| 26 |
|
| 27 |
+
def get_deepfake_score(img, std_thermal):
|
| 28 |
+
"""Calcule le score final basé sur les fréquences et l'infra-rouge."""
|
|
|
|
| 29 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
| 30 |
dft = np.fft.fft2(gray)
|
| 31 |
dft_shift = np.fft.fftshift(dft)
|
| 32 |
magnitude_spectrum = 20 * np.log(np.abs(dft_shift) + 1)
|
| 33 |
|
| 34 |
+
# Score combiné : Fréquences + Incohérence thermique
|
| 35 |
+
freq_score = np.mean(magnitude_spectrum)
|
| 36 |
+
# Un score thermique trop bas (< 5) ou trop élevé (> 25) est suspect
|
| 37 |
+
thermal_flag = 1.5 if (std_thermal < 5 or std_thermal > 25) else 1.0
|
| 38 |
|
| 39 |
+
score = (100 - (freq_score * 0.8)) * thermal_flag
|
|
|
|
| 40 |
return min(max(score, 0), 100)
|
| 41 |
|
| 42 |
def analyze_image(img_input):
|
| 43 |
if img_input is None: return None, "Erreur"
|
| 44 |
|
| 45 |
+
# 1. Analyse Infrarouge / Thermique
|
| 46 |
+
ir_map, thermal_std = infrared_thermal_analysis(img_input)
|
| 47 |
+
|
| 48 |
+
# 2. Calcul du score
|
| 49 |
+
df_score = get_deepfake_score(img_input, thermal_std)
|
| 50 |
|
| 51 |
+
# 3. Graphique comparatif
|
| 52 |
+
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 7))
|
|
|
|
| 53 |
|
|
|
|
|
|
|
| 54 |
ax1.imshow(img_input)
|
| 55 |
+
ax1.set_title("Original Image")
|
| 56 |
ax1.axis('off')
|
| 57 |
|
| 58 |
+
ax2.imshow(ir_map)
|
| 59 |
+
ax2.set_title("Infrared / Thermal Forensic Map")
|
| 60 |
ax2.axis('off')
|
| 61 |
|
| 62 |
+
plt.tight_layout()
|
| 63 |
+
|
| 64 |
+
status = "🚨 ALERTE : DEEPFAKE DÉTECTÉ" if df_score > 70 else "✅ IMAGE AUTHENTIQUE"
|
| 65 |
|
| 66 |
report = f"""
|
| 67 |
+
🔬 RAPPORT D'EXPERTISE MÉDIASHIELD PRO (V2)
|
| 68 |
--------------------------------------
|
| 69 |
STATUT : {status}
|
| 70 |
INDICE DE PROBABILITÉ IA : {df_score:.2f}%
|
| 71 |
|
| 72 |
+
ANALYSE TECHNIQUE :
|
| 73 |
+
- Signature Infrarouge : { "Anormale (Chaleur artificielle)" if thermal_std < 5 else "Naturelle" }
|
| 74 |
+
- Spectre de Fréquence : { "Traces de Pixels Synthétiques" if df_score > 60 else "Structure Photo Réelle" }
|
| 75 |
+
- Analyse Thermique : {thermal_std:.2f} (Ecart-type de chrominance)
|
| 76 |
|
| 77 |
+
Conclusion : L'analyse infrarouge montre une distribution thermique { "incohérente avec un capteur CMOS/CCD" if df_score > 70 else "conforme" }.
|
|
|
|
| 78 |
--------------------------------------
|
| 79 |
+
Project Trusted Sound 2026 - ACoNum / Tunisia
|
| 80 |
"""
|
| 81 |
return fig, report
|
| 82 |
|
|
|
|
| 84 |
# INTERFACE
|
| 85 |
# ─────────────────────────────────────────────────────────────
|
| 86 |
|
| 87 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 88 |
+
gr.Markdown("# 🛡️ ImageShield - Infrared Forensic Mode")
|
| 89 |
+
gr.Markdown("Analyseur de chaleur chromatique et de fréquences pour la protection du patrimoine.")
|
| 90 |
|
| 91 |
with gr.Row():
|
| 92 |
input_img = gr.Image(label="Image à expertiser")
|
| 93 |
+
output_plot = gr.Plot(label="Scan Infrarouge & Source")
|
| 94 |
|
| 95 |
with gr.Row():
|
| 96 |
output_text = gr.Textbox(label="Rapport de l'Agent", lines=10)
|
| 97 |
|
| 98 |
+
submit_btn = gr.Button("🔥 Lancer l'Analyse Infrarouge", variant="primary")
|
| 99 |
submit_btn.click(fn=analyze_image, inputs=input_img, outputs=[output_plot, output_text])
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|