Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,186 +2,109 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
import cv2
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 7 |
-
# π¬ ANALYSE
|
| 8 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 9 |
-
|
| 10 |
-
|
| 11 |
ycrcb = cv2.cvtColor(img, cv2.COLOR_RGB2YCrCb)
|
| 12 |
thermal = ycrcb[:, :, 1]
|
| 13 |
infrared_map = cv2.applyColorMap(thermal, cv2.COLORMAP_JET)
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 19 |
-
# π ANALYSE FRΓQUENTIELLE
|
| 20 |
-
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 21 |
-
|
| 22 |
-
def frequency_analysis(img):
|
| 23 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
|
|
|
|
| 29 |
|
| 30 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 31 |
-
#
|
| 32 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 33 |
-
|
| 34 |
-
def detect_diffusion_artifacts(img):
|
| 35 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
| 36 |
-
|
| 37 |
-
# bruit
|
| 38 |
-
blur = cv2.GaussianBlur(gray, (5, 5), 0)
|
| 39 |
-
noise = np.mean(np.abs(gray - blur))
|
| 40 |
-
|
| 41 |
-
# frΓ©quence rΓ©pΓ©titive
|
| 42 |
f = np.fft.fft2(gray)
|
| 43 |
fshift = np.fft.fftshift(f)
|
| 44 |
-
magnitude = np.log(np.abs(fshift) + 1)
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
saturation = np.mean(hsv[:, :, 1])
|
| 50 |
-
|
| 51 |
-
# texture (lissage)
|
| 52 |
-
lap = cv2.Laplacian(gray, cv2.CV_64F).var()
|
| 53 |
-
|
| 54 |
-
return noise, periodicity, saturation, lap
|
| 55 |
-
|
| 56 |
|
| 57 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 58 |
-
# π§ SCORE GLOBAL
|
| 59 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 60 |
-
|
| 61 |
-
def compute_ai_score(freq, thermal, noise, periodicity, saturation, lap):
|
| 62 |
-
score = 0
|
| 63 |
-
|
| 64 |
-
# frΓ©quence
|
| 65 |
-
if freq < 60:
|
| 66 |
-
score += 20
|
| 67 |
-
|
| 68 |
-
# thermique
|
| 69 |
-
if thermal < 5 or thermal > 25:
|
| 70 |
-
score += 20
|
| 71 |
-
|
| 72 |
-
# diffusion patterns
|
| 73 |
-
if noise < 8:
|
| 74 |
-
score += 15
|
| 75 |
-
if periodicity < 40:
|
| 76 |
-
score += 15
|
| 77 |
-
if saturation > 150:
|
| 78 |
-
score += 10
|
| 79 |
-
if lap < 20:
|
| 80 |
-
score += 20
|
| 81 |
-
|
| 82 |
-
return min(score, 100)
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 86 |
-
# π§ͺ ANALYSE PRINCIPALE
|
| 87 |
-
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 88 |
-
|
| 89 |
def analyze_image(img_input):
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
#
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
ai_score =
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
else:
|
| 109 |
-
status = "β
|
| 110 |
-
|
| 111 |
-
# type IA estimΓ©
|
| 112 |
-
if noise < 8 and lap < 20:
|
| 113 |
-
model_hint = "Midjourney-like generation"
|
| 114 |
-
elif periodicity < 40:
|
| 115 |
-
model_hint = "Stable Diffusion-like pattern"
|
| 116 |
-
else:
|
| 117 |
-
model_hint = "No clear AI signature"
|
| 118 |
-
|
| 119 |
-
# affichage
|
| 120 |
-
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 7))
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
ax1.axis("off")
|
| 125 |
-
|
| 126 |
-
ax2.imshow(ir_map)
|
| 127 |
-
ax2.set_title("Infrared Forensic Map")
|
| 128 |
-
ax2.axis("off")
|
| 129 |
-
|
| 130 |
-
plt.tight_layout()
|
| 131 |
-
|
| 132 |
-
# rapport
|
| 133 |
-
report = f"""
|
| 134 |
-
π¬ MEDIASHIELD PRO β AI DETECTION REPORT (2026)
|
| 135 |
--------------------------------------
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
THERMAL:
|
| 140 |
-
- Std deviation: {thermal:.2f}
|
| 141 |
-
|
| 142 |
-
FREQUENCY:
|
| 143 |
-
- Spectrum score: {freq:.2f}
|
| 144 |
-
|
| 145 |
-
AI ARTIFACTS:
|
| 146 |
-
- Noise: {noise:.2f}
|
| 147 |
-
- Periodicity: {periodicity:.2f}
|
| 148 |
-
- Saturation: {saturation:.2f}
|
| 149 |
-
- Texture (Sharpness): {lap:.2f}
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
This image is {'likely AI-generated' if ai_score > 75 else 'potentially manipulated' if ai_score > 50 else 'likely authentic'}.
|
| 156 |
|
|
|
|
|
|
|
|
|
|
| 157 |
--------------------------------------
|
| 158 |
-
Trusted Sound 2026
|
| 159 |
-
"""
|
| 160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
return fig, report
|
| 162 |
|
| 163 |
-
|
| 164 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 165 |
-
#
|
| 166 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
gr.Markdown("# π‘οΈ ImageShield PRO β AI Detection 2026")
|
| 171 |
-
gr.Markdown("Analyse avancΓ©e : Deepfake / Stable Diffusion / Midjourney")
|
| 172 |
-
|
| 173 |
with gr.Row():
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
btn = gr.Button("π Analyze Image", variant="primary")
|
| 180 |
-
|
| 181 |
-
btn.click(fn=analyze_image, inputs=input_img, outputs=[output_plot, output_text])
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 185 |
|
| 186 |
if __name__ == "__main__":
|
| 187 |
demo.launch()
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
import cv2
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from skimage import feature # Ajout pour l'analyse de texture profonde
|
| 7 |
|
| 8 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 9 |
+
# π¬ ANALYSE THERMIQUE ET TEXTURE (LBP)
|
| 10 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 11 |
+
def advanced_forensic_analysis(img):
|
| 12 |
+
# 1. Analyse Infrarouge (YCrCb)
|
| 13 |
ycrcb = cv2.cvtColor(img, cv2.COLOR_RGB2YCrCb)
|
| 14 |
thermal = ycrcb[:, :, 1]
|
| 15 |
infrared_map = cv2.applyColorMap(thermal, cv2.COLORMAP_JET)
|
| 16 |
+
|
| 17 |
+
# 2. Analyse LBP (Local Binary Pattern) - DΓ©tecte les faux pixels
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
| 19 |
+
lbp = feature.local_binary_pattern(gray, P=8, R=1, method="uniform")
|
| 20 |
+
# Si l'image est gΓ©nΓ©rΓ©e, le LBP est souvent "trop parfait"
|
| 21 |
+
lbp_hist, _ = np.histogram(lbp.ravel(), bins=np.arange(0, 12), density=True)
|
| 22 |
+
lbp_consistency = np.max(lbp_hist)
|
| 23 |
|
| 24 |
+
return infrared_map, np.std(thermal), lbp_consistency
|
| 25 |
|
| 26 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 27 |
+
# π ANALYSE FRΓQUENTIELLE (FFT)
|
| 28 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 29 |
+
def frequency_analysis(img):
|
|
|
|
| 30 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
f = np.fft.fft2(gray)
|
| 32 |
fshift = np.fft.fftshift(f)
|
| 33 |
+
magnitude = 20 * np.log(np.abs(fshift) + 1)
|
| 34 |
+
# Les IA laissent des traces dans les hautes frΓ©quences (les coins du spectre)
|
| 35 |
+
h, w = magnitude.shape
|
| 36 |
+
center_void = np.mean(magnitude[0:10, 0:10]) # Analyse des coins
|
| 37 |
+
return np.mean(magnitude), center_void
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 40 |
+
# π§ SCORE GLOBAL "ANTI-TRICHE"
|
| 41 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
def analyze_image(img_input):
|
| 43 |
+
if img_input is None: return None, "Erreur"
|
| 44 |
+
|
| 45 |
+
# ExΓ©cution des analyses
|
| 46 |
+
ir_map, thermal_std, lbp_score = advanced_forensic_analysis(img_input)
|
| 47 |
+
freq_mean, corner_freq = frequency_analysis(img_input)
|
| 48 |
+
|
| 49 |
+
# CALCUL DU SCORE DURCI
|
| 50 |
+
# On pΓ©nalise si l'image est "trop lisse" (LBP Γ©levΓ©) ou "thermique plate"
|
| 51 |
+
ai_score = 0
|
| 52 |
+
if lbp_score > 0.25: ai_score += 35 # Texture trop rΓ©pΓ©titive (IA)
|
| 53 |
+
if thermal_std < 6: ai_score += 25 # Chaleur trop uniforme
|
| 54 |
+
if corner_freq > 15: ai_score += 20 # Artefacts de grille IA
|
| 55 |
+
if freq_mean < 55: ai_score += 20 # Manque de dΓ©tails naturels
|
| 56 |
+
|
| 57 |
+
ai_score = min(ai_score, 100)
|
| 58 |
+
|
| 59 |
+
# Verdict
|
| 60 |
+
if ai_score > 65:
|
| 61 |
+
status = "π¨ DEEPFAKE DΓTECTΓ (High Probability)"
|
| 62 |
+
color = "red"
|
| 63 |
+
elif ai_score > 40:
|
| 64 |
+
status = "β οΈ IMAGE SUSPECTE (Manipulation probable)"
|
| 65 |
+
color = "orange"
|
| 66 |
else:
|
| 67 |
+
status = "β
IMAGE AUTHENTIQUE"
|
| 68 |
+
color = "green"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
# Rapport technique pour Mauro Barni
|
| 71 |
+
report = f"""π¬ MEDIASHIELD PRO - FORENSIC REPORT v3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
--------------------------------------
|
| 73 |
+
VERDICT FINAL : {status}
|
| 74 |
+
INDICE DE FAUX : {ai_score:.2f}%
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
+
DΓTAILS TECHNIQUES :
|
| 77 |
+
- CohΓ©rence LBP (Texture) : {lbp_score:.4f} {"(Anomalie IA)" if lbp_score > 0.25 else "(OK)"}
|
| 78 |
+
- DΓ©viation Thermique : {thermal_std:.2f} {"(Trop stable/IA)" if thermal_std < 6 else "(OK)"}
|
| 79 |
+
- FrΓ©quences de grille : {corner_freq:.2f} {"(Signal synthΓ©tique)" if corner_freq > 15 else "(OK)"}
|
|
|
|
| 80 |
|
| 81 |
+
CONCLUSION :
|
| 82 |
+
L'analyse des micro-textures (LBP) montre des motifs de rΓ©pΓ©tition non-organiques.
|
| 83 |
+
La signature thermique confirme l'absence de bruit de capteur physique.
|
| 84 |
--------------------------------------
|
| 85 |
+
Trusted Sound 2026 - ACoNum Tunisia"""
|
|
|
|
| 86 |
|
| 87 |
+
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 7))
|
| 88 |
+
ax1.imshow(img_input)
|
| 89 |
+
ax1.set_title("Image Source")
|
| 90 |
+
ax1.axis('off')
|
| 91 |
+
ax2.imshow(ir_map)
|
| 92 |
+
ax2.set_title("Analyse Infrarouge & Texture")
|
| 93 |
+
ax2.axis('off')
|
| 94 |
+
|
| 95 |
return fig, report
|
| 96 |
|
|
|
|
| 97 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 98 |
+
# INTERFACE
|
| 99 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 100 |
+
with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
| 101 |
+
gr.Markdown("# π‘οΈ ImageShield PRO - Forensic Suite")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
with gr.Row():
|
| 103 |
+
in_img = gr.Image(label="Upload image")
|
| 104 |
+
out_plot = gr.Plot()
|
| 105 |
+
out_txt = gr.Textbox(label="Expert Report", lines=12)
|
| 106 |
+
btn = gr.Button("π ANALYSER MAINTENANT", variant="primary")
|
| 107 |
+
btn.click(analyze_image, in_img, [out_plot, out_txt])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
if __name__ == "__main__":
|
| 110 |
demo.launch()
|