BitCheck Codex commited on
Commit
8624fca
·
1 Parent(s): 662bc65

feat: add lightweight image forensic analysis

Browse files
app/services/forensic_analyzer.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from pathlib import Path
4
+ from typing import Any
5
+
6
+ import cv2
7
+ import numpy as np
8
+ from PIL import Image, ImageOps
9
+
10
+
11
+ def analyze_image_forensics(path: Path, verification_id: str, output_dir: Path) -> dict[str, Any]:
12
+ try:
13
+ with Image.open(path) as img:
14
+ rgb = np.array(ImageOps.exif_transpose(img).convert("RGB"))
15
+ gray = cv2.cvtColor(rgb, cv2.COLOR_RGB2GRAY)
16
+ sharpness = float(cv2.Laplacian(gray, cv2.CV_64F).var())
17
+
18
+ noise_values, boxes = _block_metric(gray, _noise_metric)
19
+ blur_values, _ = _block_metric(gray, _blur_metric)
20
+ edge_values, _ = _block_metric(gray, _edge_metric)
21
+ brightness_values, _ = _block_metric(gray, lambda p: float(np.mean(p)))
22
+ contrast_values, _ = _block_metric(gray, lambda p: float(np.std(p)))
23
+
24
+ noise_risk = _inconsistency(noise_values)
25
+ blur_risk = _inconsistency(blur_values)
26
+ edge_risk = _inconsistency(edge_values)
27
+ compression_risk = _compression_proxy(gray)
28
+ brightness_contrast_risk = max(_inconsistency(brightness_values), _inconsistency(contrast_values)) * 0.7
29
+
30
+ suspicious = _suspicious_regions(boxes, noise_values, blur_values, edge_values)
31
+ annotated_url = _save_annotation(rgb, suspicious, verification_id, output_dir) if suspicious else None
32
+ risk = 0.24 * noise_risk + 0.22 * compression_risk + 0.2 * blur_risk + 0.2 * edge_risk + 0.14 * brightness_contrast_risk
33
+
34
+ flags: list[str] = []
35
+ if sharpness < 45:
36
+ flags.append("Image has low global sharpness; this may reflect blur, resizing, or compression.")
37
+ if noise_risk > 0.55:
38
+ flags.append("Possible noise inconsistency across image regions.")
39
+ if compression_risk > 0.55:
40
+ flags.append("Possible compression artifact inconsistency.")
41
+ if blur_risk > 0.55:
42
+ flags.append("Possible local blur inconsistency.")
43
+ if edge_risk > 0.55:
44
+ flags.append("Possible edge density inconsistency.")
45
+ if brightness_contrast_risk > 0.55:
46
+ flags.append("Possible brightness or contrast anomaly.")
47
+
48
+ return {
49
+ "checked": True,
50
+ "risk_score": round(float(min(1.0, risk)), 3),
51
+ "sharpness_score": round(sharpness, 3),
52
+ "noise_inconsistency_risk": round(float(noise_risk), 3),
53
+ "compression_risk": round(float(compression_risk), 3),
54
+ "blur_inconsistency_risk": round(float(blur_risk), 3),
55
+ "edge_inconsistency_risk": round(float(edge_risk), 3),
56
+ "brightness_contrast_risk": round(float(brightness_contrast_risk), 3),
57
+ "suspicious_regions": suspicious,
58
+ "annotated_image_url": annotated_url,
59
+ "flags": flags,
60
+ "disclaimer": "Forensic indicators are risk signals, not definitive proof of manipulation.",
61
+ }
62
+ except Exception as exc:
63
+ return {
64
+ "checked": True,
65
+ "risk_score": 0.0,
66
+ "sharpness_score": 0.0,
67
+ "noise_inconsistency_risk": 0.0,
68
+ "compression_risk": 0.0,
69
+ "blur_inconsistency_risk": 0.0,
70
+ "edge_inconsistency_risk": 0.0,
71
+ "suspicious_regions": [],
72
+ "annotated_image_url": None,
73
+ "flags": ["Forensic analysis failed."],
74
+ "disclaimer": "Forensic indicators are risk signals, not definitive proof of manipulation.",
75
+ "error": str(exc),
76
+ }
77
+
78
+
79
+ def _block_metric(gray: np.ndarray, fn: Any, grid: int = 4) -> tuple[list[float], list[dict[str, int]]]:
80
+ h, w = gray.shape
81
+ values: list[float] = []
82
+ boxes: list[dict[str, int]] = []
83
+ ys = np.linspace(0, h, grid + 1, dtype=int)
84
+ xs = np.linspace(0, w, grid + 1, dtype=int)
85
+ for row in range(grid):
86
+ for col in range(grid):
87
+ y0, y1 = int(ys[row]), int(ys[row + 1])
88
+ x0, x1 = int(xs[col]), int(xs[col + 1])
89
+ patch = gray[y0:y1, x0:x1]
90
+ values.append(fn(patch))
91
+ boxes.append({"x": x0, "y": y0, "width": x1 - x0, "height": y1 - y0})
92
+ return values, boxes
93
+
94
+
95
+ def _noise_metric(patch: np.ndarray) -> float:
96
+ blur = cv2.GaussianBlur(patch, (3, 3), 0)
97
+ return float(np.std(patch.astype("float32") - blur.astype("float32")))
98
+
99
+
100
+ def _blur_metric(patch: np.ndarray) -> float:
101
+ return float(cv2.Laplacian(patch, cv2.CV_64F).var())
102
+
103
+
104
+ def _edge_metric(patch: np.ndarray) -> float:
105
+ edges = cv2.Canny(patch, 80, 180)
106
+ return float(np.mean(edges > 0))
107
+
108
+
109
+ def _inconsistency(values: list[float]) -> float:
110
+ arr = np.array(values, dtype="float32")
111
+ if arr.size == 0:
112
+ return 0.0
113
+ med = float(np.median(arr)) + 1e-6
114
+ mad = float(np.median(np.abs(arr - med))) + 1e-6
115
+ robust_cv = mad / (abs(med) + 1e-6)
116
+ return float(min(1.0, robust_cv / 0.65))
117
+
118
+
119
+ def _compression_proxy(gray: np.ndarray) -> float:
120
+ h, w = gray.shape
121
+ h8, w8 = h - (h % 8), w - (w % 8)
122
+ if h8 < 16 or w8 < 16:
123
+ return 0.0
124
+ crop = gray[:h8, :w8].astype("float32")
125
+ vertical = np.mean(np.abs(crop[:, 7::8] - crop[:, 8::8])) if crop[:, 8::8].size else 0.0
126
+ horizontal = np.mean(np.abs(crop[7::8, :] - crop[8::8, :])) if crop[8::8, :].size else 0.0
127
+ return float(min(1.0, (vertical + horizontal) / 36.0))
128
+
129
+
130
+ def _suspicious_regions(boxes: list[dict[str, int]], *metrics: list[float]) -> list[dict[str, Any]]:
131
+ scores = np.zeros(len(boxes), dtype="float32")
132
+ for values in metrics:
133
+ arr = np.array(values, dtype="float32")
134
+ med = np.median(arr)
135
+ std = np.std(arr) + 1e-6
136
+ scores += np.abs(arr - med) / std
137
+ regions = []
138
+ for box, score in sorted(zip(boxes, scores), key=lambda item: float(item[1]), reverse=True)[:5]:
139
+ normalized = float(min(1.0, score / 6.0))
140
+ if normalized >= 0.45:
141
+ regions.append({**box, "score": round(normalized, 3), "label": "possible visual inconsistency"})
142
+ return regions
143
+
144
+
145
+ def _save_annotation(rgb: np.ndarray, regions: list[dict[str, Any]], verification_id: str, output_dir: Path) -> str:
146
+ annotated = rgb.copy()
147
+ for region in regions:
148
+ x, y, w, h = region["x"], region["y"], region["width"], region["height"]
149
+ cv2.rectangle(annotated, (x, y), (x + w, y + h), (255, 188, 40), max(2, annotated.shape[1] // 300))
150
+ path = output_dir / f"{verification_id}_forensics.jpg"
151
+ Image.fromarray(annotated).save(path, quality=92)
152
+ return f"/outputs/{path.name}"
app/services/forensic_checker.py CHANGED
@@ -1,84 +1,9 @@
1
- from __future__ import annotations
2
-
3
- from io import BytesIO
4
  from pathlib import Path
5
  from typing import Any
6
 
7
- import cv2
8
- import numpy as np
9
- from PIL import Image
10
-
11
-
12
- def analyze_forensics(path: Path, metadata: dict[str, Any]) -> dict[str, Any]:
13
- try:
14
- with Image.open(path) as img:
15
- original_mode = img.mode
16
- fmt = img.format
17
- width, height = img.size
18
- rgb = np.array(img.convert("RGB"))
19
- gray = cv2.cvtColor(rgb, cv2.COLOR_RGB2GRAY)
20
- sharpness = float(cv2.Laplacian(gray, cv2.CV_64F).var())
21
-
22
- compression_risk = _jpeg_recompression_delta(rgb) if fmt == "JPEG" else 0.15
23
- noise_risk = _noise_inconsistency(gray)
24
- metadata_risk = 0.35 if not metadata.get("metadata_found") else 0.05
25
- resolution_risk = 0.0
26
- flags: list[str] = []
27
-
28
- if sharpness < 40:
29
- flags.append("Low sharpness may indicate blur, resizing, or heavy processing.")
30
- if compression_risk > 0.55:
31
- flags.append("Moderate compression artifacts detected.")
32
- if noise_risk > 0.55:
33
- flags.append("Noise inconsistency detected across image regions.")
34
- if metadata_risk > 0.2:
35
- flags.append("Image has unusually low metadata.")
36
- if width < 256 or height < 256 or width / max(height, 1) > 3.5 or height / max(width, 1) > 3.5:
37
- resolution_risk = 0.3
38
- flags.append("Suspicious image resolution or aspect ratio.")
39
- if "A" in original_mode:
40
- flags.append("Image contains an alpha channel or transparency.")
41
-
42
- manipulation = 0.25 * min(1.0, max(0.0, (80 - sharpness) / 80)) + 0.3 * compression_risk + 0.3 * noise_risk + 0.15 * max(metadata_risk, resolution_risk)
43
- return {
44
- "manipulation_risk_score": round(float(min(1.0, manipulation)), 3),
45
- "sharpness_score": round(sharpness, 3),
46
- "compression_risk": round(float(compression_risk), 3),
47
- "noise_inconsistency_risk": round(float(noise_risk), 3),
48
- "artifact_flags": flags,
49
- "forensic_summary": "Lightweight forensic checks completed. These are weak signals, not proof of editing.",
50
- }
51
- except Exception as exc:
52
- return {
53
- "manipulation_risk_score": 0.0,
54
- "sharpness_score": None,
55
- "compression_risk": 0.0,
56
- "noise_inconsistency_risk": 0.0,
57
- "artifact_flags": [],
58
- "forensic_summary": "Forensic checks failed.",
59
- "error": str(exc),
60
- }
61
-
62
-
63
- def _jpeg_recompression_delta(rgb: np.ndarray) -> float:
64
- image = Image.fromarray(rgb)
65
- buf = BytesIO()
66
- image.save(buf, format="JPEG", quality=75)
67
- buf.seek(0)
68
- recompressed = np.array(Image.open(buf).convert("RGB"))
69
- mad = np.mean(np.abs(rgb.astype("float32") - recompressed.astype("float32"))) / 255.0
70
- return float(min(1.0, mad * 8.0))
71
 
72
 
73
- def _noise_inconsistency(gray: np.ndarray) -> float:
74
- h, w = gray.shape
75
- grid = []
76
- for y in np.array_split(np.arange(h), 4):
77
- for x in np.array_split(np.arange(w), 4):
78
- patch = gray[y.min() : y.max() + 1, x.min() : x.max() + 1]
79
- blur = cv2.GaussianBlur(patch, (3, 3), 0)
80
- residual = patch.astype("float32") - blur.astype("float32")
81
- grid.append(float(residual.std()))
82
- mean = np.mean(grid) + 1e-6
83
- coeff = np.std(grid) / mean
84
- return float(min(1.0, coeff / 0.75))
 
 
 
 
1
  from pathlib import Path
2
  from typing import Any
3
 
4
+ from app.config import settings
5
+ from app.services.forensic_analyzer import analyze_image_forensics
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
 
8
+ def analyze_forensics(path: Path, metadata: dict[str, Any] | None = None, verification_id: str = "forensics") -> dict[str, Any]:
9
+ return analyze_image_forensics(path, verification_id, settings.outputs_dir)