NOBODY204 commited on
Commit
d497487
Β·
verified Β·
1 Parent(s): e6a32a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -150
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 INFRAROUGE
8
  # ─────────────────────────────────────────────
9
-
10
- def infrared_thermal_analysis(img):
11
  ycrcb = cv2.cvtColor(img, cv2.COLOR_RGB2YCrCb)
12
  thermal = ycrcb[:, :, 1]
13
  infrared_map = cv2.applyColorMap(thermal, cv2.COLORMAP_JET)
14
- std_thermal = np.std(thermal)
15
- return infrared_map, std_thermal
16
-
17
-
18
- # ─────────────────────────────────────────────
19
- # πŸ“Š ANALYSE FRΓ‰QUENTIELLE
20
- # ─────────────────────────────────────────────
21
-
22
- def frequency_analysis(img):
23
  gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
24
- dft = np.fft.fft2(gray)
25
- dft_shift = np.fft.fftshift(dft)
26
- magnitude = 20 * np.log(np.abs(dft_shift) + 1)
27
- return np.mean(magnitude)
28
 
 
29
 
30
  # ─────────────────────────────────────────────
31
- # πŸ€– DΓ‰TECTION DIFFUSION (SD / MIDJOURNEY)
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
- periodicity = np.std(magnitude)
46
-
47
- # saturation
48
- hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
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 INTELLIGENT
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
- if img_input is None:
92
- return None, "Erreur : aucune image"
93
-
94
- img = img_input.copy()
95
-
96
- # analyses
97
- ir_map, thermal = infrared_thermal_analysis(img)
98
- freq = frequency_analysis(img)
99
- noise, periodicity, saturation, lap = detect_diffusion_artifacts(img)
100
-
101
- ai_score = compute_ai_score(freq, thermal, noise, periodicity, saturation, lap)
102
-
103
- # classification
104
- if ai_score > 75:
105
- status = "🚨 AI GENERATED (HIGH CONFIDENCE)"
106
- elif ai_score > 50:
107
- status = "⚠️ SUSPICIOUS IMAGE"
 
 
 
 
 
108
  else:
109
- status = "βœ… LIKELY REAL IMAGE"
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
- ax1.imshow(img)
123
- ax1.set_title("Original Image")
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
- STATUS: {status}
137
- AI SCORE: {ai_score:.2f}%
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
- MODEL DETECTION:
152
- β†’ {model_hint}
153
-
154
- FINAL VERDICT:
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 – Tunisia
159
- """
160
 
 
 
 
 
 
 
 
 
161
  return fig, report
162
 
163
-
164
  # ─────────────────────────────────────────────
165
- # 🎨 INTERFACE GRADIO
166
  # ─────────────────────────────────────────────
167
-
168
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
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
- input_img = gr.Image(label="Upload Image")
175
- output_plot = gr.Plot()
176
-
177
- output_text = gr.Textbox(label="Analysis Report", lines=15)
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()