Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,97 +4,114 @@ import cv2
|
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
from PIL import Image, ImageChops, ImageEnhance
|
| 6 |
from scipy import ndimage
|
| 7 |
-
import io,
|
| 8 |
|
| 9 |
warnings.filterwarnings("ignore")
|
| 10 |
|
| 11 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 12 |
-
# ๐ก๏ธ MEDIASHIELD
|
| 13 |
-
# Calibration spรฉciale : Archives & Mode Sombre
|
| 14 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 15 |
|
| 16 |
def apply_gamma_correction(img):
|
| 17 |
-
"""Prรฉpare l'image pour l'analyse si elle est trop sombre."""
|
| 18 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
| 19 |
brightness = np.mean(gray)
|
| 20 |
if brightness < 65:
|
| 21 |
-
# Correction pour rรฉvรฉler les artefacts de compression dans l'ombre
|
| 22 |
gamma = 0.6
|
| 23 |
invGamma = 1.0 / gamma
|
| 24 |
table = np.array([((i / 255.0) ** invGamma) * 255 for i in np.arange(0, 256)]).astype("uint8")
|
| 25 |
return cv2.LUT(img, table), f"๐ Mode Sombre (Lumiรจre: {brightness:.1f})"
|
| 26 |
return img, "โ๏ธ Lumiรจre Standard"
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
def analyze_forensics(img_input):
|
| 29 |
-
if img_input is None: return None, "โ Aucune image"
|
| 30 |
|
| 31 |
-
# 1.
|
| 32 |
img_cv = cv2.cvtColor(img_input, cv2.COLOR_RGBA2RGB) if img_input.shape[2] == 4 else img_input.copy()
|
| 33 |
img_ready, light_msg = apply_gamma_correction(img_cv)
|
| 34 |
pil_img = Image.fromarray(img_ready)
|
| 35 |
|
| 36 |
-
# 2. Analyse
|
|
|
|
|
|
|
|
|
|
| 37 |
ycrcb = cv2.cvtColor(img_ready, cv2.COLOR_RGB2YCrCb)
|
| 38 |
cr_var = ndimage.generic_filter(ycrcb[:,:,1], np.var, size=5)
|
| 39 |
-
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
f_shift = np.fft.fftshift(np.fft.fft2(
|
| 44 |
-
|
| 45 |
-
fft_vis = np.log(magnitude + 1)
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
quality = 90
|
| 49 |
buf = io.BytesIO()
|
| 50 |
-
pil_img.save(buf, format="JPEG", quality=
|
| 51 |
recomp = Image.open(io.BytesIO(buf.getvalue())).convert("RGB")
|
| 52 |
ela_diff = ImageChops.difference(pil_img, recomp)
|
| 53 |
ela_vis = ImageEnhance.Brightness(ela_diff).enhance(10)
|
| 54 |
|
| 55 |
-
#
|
| 56 |
score = 0
|
| 57 |
reasons = []
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
score += 30
|
| 63 |
-
reasons.append("
|
|
|
|
| 64 |
if np.mean(np.array(ela_diff)) < 1.2:
|
| 65 |
-
score +=
|
| 66 |
reasons.append("๐ Compression suspecte (ELA trop lisse)")
|
| 67 |
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
# Visualisation
|
| 71 |
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
|
| 72 |
axes[0].imshow(img_cv); axes[0].set_title(f'Original ({light_msg})')
|
| 73 |
-
axes[1].imshow(fft_vis, cmap='
|
| 74 |
-
axes[2].imshow(ela_vis); axes[2].set_title('Analyse ELA')
|
| 75 |
for ax in axes: ax.axis('off')
|
|
|
|
| 76 |
|
| 77 |
-
report = f"๐ก๏ธ MEDIASHIELD v3.0\nVERDICT: {
|
| 78 |
return fig, report
|
| 79 |
|
| 80 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 81 |
-
#
|
| 82 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 83 |
|
| 84 |
with gr.Blocks(title="MediaShield v3.0", theme=gr.themes.Soft()) as demo:
|
| 85 |
-
gr.Markdown("# ๐ก๏ธ MediaShield v3.0
|
| 86 |
-
gr.Markdown("
|
| 87 |
|
| 88 |
with gr.Row():
|
| 89 |
-
with gr.Column():
|
| 90 |
-
input_file = gr.Image(label="
|
| 91 |
-
run_btn = gr.Button("
|
| 92 |
-
with gr.Column():
|
| 93 |
-
output_plot = gr.Plot(label="
|
| 94 |
-
output_text = gr.Textbox(label="Rapport d'expertise
|
| 95 |
|
| 96 |
run_btn.click(analyze_forensics, inputs=input_file, outputs=[output_plot, output_text])
|
| 97 |
|
| 98 |
if __name__ == "__main__":
|
| 99 |
-
demo.launch()
|
| 100 |
-
|
|
|
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
from PIL import Image, ImageChops, ImageEnhance
|
| 6 |
from scipy import ndimage
|
| 7 |
+
import io, warnings
|
| 8 |
|
| 9 |
warnings.filterwarnings("ignore")
|
| 10 |
|
| 11 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 12 |
+
# ๐ก๏ธ MEDIASHIELD v3.0 โ ARCHIVE & FORENSIC SUITE
|
|
|
|
| 13 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 14 |
|
| 15 |
def apply_gamma_correction(img):
|
| 16 |
+
"""Prรฉpare l'image pour l'analyse si elle est trop sombre (Archives)."""
|
| 17 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
| 18 |
brightness = np.mean(gray)
|
| 19 |
if brightness < 65:
|
|
|
|
| 20 |
gamma = 0.6
|
| 21 |
invGamma = 1.0 / gamma
|
| 22 |
table = np.array([((i / 255.0) ** invGamma) * 255 for i in np.arange(0, 256)]).astype("uint8")
|
| 23 |
return cv2.LUT(img, table), f"๐ Mode Sombre (Lumiรจre: {brightness:.1f})"
|
| 24 |
return img, "โ๏ธ Lumiรจre Standard"
|
| 25 |
|
| 26 |
+
def verify_natural_grain(img):
|
| 27 |
+
"""Distingue le lissage IA du grain organique d'une photo rรฉelle."""
|
| 28 |
+
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
| 29 |
+
local_var = ndimage.generic_filter(gray, np.var, size=3)
|
| 30 |
+
avg_var = np.mean(local_var)
|
| 31 |
+
# Un score trรจs bas (< 0.8) indique une texture mathรฉmatiquement 'trop' parfaite (IA)
|
| 32 |
+
if avg_var < 0.8:
|
| 33 |
+
return True, avg_var
|
| 34 |
+
return False, avg_var
|
| 35 |
+
|
| 36 |
def analyze_forensics(img_input):
|
| 37 |
+
if img_input is None: return None, "โ Aucune image chargรฉe."
|
| 38 |
|
| 39 |
+
# 1. Nettoyage et Correction Gamma
|
| 40 |
img_cv = cv2.cvtColor(img_input, cv2.COLOR_RGBA2RGB) if img_input.shape[2] == 4 else img_input.copy()
|
| 41 |
img_ready, light_msg = apply_gamma_correction(img_cv)
|
| 42 |
pil_img = Image.fromarray(img_ready)
|
| 43 |
|
| 44 |
+
# 2. Analyse du Grain (Anti-Faux Positifs Archives)
|
| 45 |
+
is_too_smooth, grain_val = verify_natural_grain(img_ready)
|
| 46 |
+
|
| 47 |
+
# 3. Analyse de Chrominance (Signatures Couleurs IA)
|
| 48 |
ycrcb = cv2.cvtColor(img_ready, cv2.COLOR_RGB2YCrCb)
|
| 49 |
cr_var = ndimage.generic_filter(ycrcb[:,:,1], np.var, size=5)
|
| 50 |
+
chroma_uniformity = np.std(cr_var) / (np.mean(cr_var) + 1e-8)
|
| 51 |
|
| 52 |
+
# 4. FFT (Recherche de motifs de grille IA)
|
| 53 |
+
gray_f = cv2.cvtColor(img_ready, cv2.COLOR_RGB2GRAY).astype(np.float32)
|
| 54 |
+
f_shift = np.fft.fftshift(np.fft.fft2(gray_f))
|
| 55 |
+
fft_vis = np.log(np.abs(f_shift) + 1)
|
|
|
|
| 56 |
|
| 57 |
+
# 5. ELA (Error Level Analysis)
|
|
|
|
| 58 |
buf = io.BytesIO()
|
| 59 |
+
pil_img.save(buf, format="JPEG", quality=90)
|
| 60 |
recomp = Image.open(io.BytesIO(buf.getvalue())).convert("RGB")
|
| 61 |
ela_diff = ImageChops.difference(pil_img, recomp)
|
| 62 |
ela_vis = ImageEnhance.Brightness(ela_diff).enhance(10)
|
| 63 |
|
| 64 |
+
# 6. Systรจme de Score S2T Ariana
|
| 65 |
score = 0
|
| 66 |
reasons = []
|
| 67 |
+
|
| 68 |
+
if is_too_smooth:
|
| 69 |
+
score += 25
|
| 70 |
+
reasons.append(f"โก Texture suspecte (Grain trop faible: {grain_val:.2f})")
|
| 71 |
+
else:
|
| 72 |
+
score -= 10
|
| 73 |
+
reasons.append("โ
Grain organique dรฉtectรฉ (Photo rรฉelle)")
|
| 74 |
+
|
| 75 |
+
if chroma_uniformity < 0.9:
|
| 76 |
score += 30
|
| 77 |
+
reasons.append("โ ๏ธ Bruit chromatique uniforme (Signature IA)")
|
| 78 |
+
|
| 79 |
if np.mean(np.array(ela_diff)) < 1.2:
|
| 80 |
+
score += 20
|
| 81 |
reasons.append("๐ Compression suspecte (ELA trop lisse)")
|
| 82 |
|
| 83 |
+
# Bornage du score
|
| 84 |
+
score = max(0, min(100, score))
|
| 85 |
+
verdict = "๐ด IA DรTECTรE" if score > 55 else "๐ SUSPECT" if score > 35 else "๐ข AUTHENTIQUE"
|
| 86 |
|
| 87 |
+
# Visualisation des preuves
|
| 88 |
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
|
| 89 |
axes[0].imshow(img_cv); axes[0].set_title(f'Original ({light_msg})')
|
| 90 |
+
axes[1].imshow(fft_vis, cmap='magma'); axes[1].set_title('Spectre Frรฉquence (FFT)')
|
| 91 |
+
axes[2].imshow(ela_vis); axes[2].set_title('Analyse ELA (Compression)')
|
| 92 |
for ax in axes: ax.axis('off')
|
| 93 |
+
plt.tight_layout()
|
| 94 |
|
| 95 |
+
report = f"๐ก๏ธ RAPPORT MEDIASHIELD v3.0\nVERDICT : {verdict}\nIndice de confiance IA : {score}/100\n\nDiagnostic :\n" + "\n".join(reasons)
|
| 96 |
return fig, report
|
| 97 |
|
| 98 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 99 |
+
# INTERFACE UTILISATEUR (GRADIO)
|
| 100 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 101 |
|
| 102 |
with gr.Blocks(title="MediaShield v3.0", theme=gr.themes.Soft()) as demo:
|
| 103 |
+
gr.Markdown("# ๐ก๏ธ MediaShield v3.0")
|
| 104 |
+
gr.Markdown("**Expertise Forensic S2T** : Dรฉtection IA, analyse de grain et correction d'archives.")
|
| 105 |
|
| 106 |
with gr.Row():
|
| 107 |
+
with gr.Column(scale=1):
|
| 108 |
+
input_file = gr.Image(label="Charger une image (Photo ou Archive)", type="numpy")
|
| 109 |
+
run_btn = gr.Button("LANCER L'ANALYSE", variant="primary")
|
| 110 |
+
with gr.Column(scale=2):
|
| 111 |
+
output_plot = gr.Plot(label="Preuves visuelles")
|
| 112 |
+
output_text = gr.Textbox(label="Rapport d'expertise", lines=10)
|
| 113 |
|
| 114 |
run_btn.click(analyze_forensics, inputs=input_file, outputs=[output_plot, output_text])
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|
| 117 |
+
demo.launch()
|
|
|