Image Forensics
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The Image Forensics system in TrueLens is a multi-signal analysis module designed to detect image manipulations, pixel-level splicing, and AI-generated content. Within the broader context of the project, it serves as a critical component of the content verification suite, providing technical evidence to distinguish authentic photography from fraudulent or synthetic media.
The system operates by combining traditional forensic techniques, such as Error Level Analysis (ELA) and EXIF metadata extraction, with modern frequency-domain analysis for GAN (Generative Adversarial Network) fingerprinting and Large Language Model (LLM) visual inspection.
Sources: truelens-backend/app/services/image_analysis.py:1-16, truelens-app/src/app/page.tsx:238-242
Core Analysis Techniques
The forensics engine utilizes four primary methods to evaluate the authenticity of an image file. These methods are executed concurrently to produce a set of signals that contribute to the final trust score.
1. Error Level Analysis (ELA)
ELA identifies areas within an image that are at different compression levels. In a standard JPEG, the entire image should reach a similar level of error after re-compression. If a portion of the image has been spliced or modified, that specific region will show a significantly higher error level compared to the rest of the image.
- Logic: The system re-saves the image at a known quality (default 90) and calculates the pixel-level difference between the original and the re-compressed version.
- Metrics: It calculates the average pixel difference and standard deviation. High variance in error levels typically indicates pixel-level manipulation.
Sources: truelens-backend/app/services/image_analysis.py:25-78
2. EXIF Metadata Analysis
This module parses the metadata embedded in image files to find discrepancies or "smoking guns" of editing.
- Software Detection: It checks against a list of known editing tools (e.g., Photoshop, GIMP) and AI generation tools (e.g., Midjourney, DALL-E).
- Consistency Checks: It flags instances where the modification date differs from the original capture date or where EXIF dimensions do not match actual pixel dimensions, suggesting cropping or resizing.
- Integrity: The absence of EXIF data is treated as a suspicious signal, as it may indicate that metadata was intentionally stripped.
Sources: truelens-backend/app/services/image_analysis.py:92-181
3. GAN Fingerprint Detection
AI-generated images often contain periodic spectral artifacts caused by upsampling layers in GAN architectures.
- Spectral Analysis: Using Fast Fourier Transform (FFT), the system analyzes the image in the frequency domain. It looks for "peaks" in high-frequency bands that are characteristic of synthetic generation.
- Checkerboard Detection: It specifically looks for corner energy ratios in the magnitude spectrum to identify checkerboard artifacts common in early GAN models.
Sources: truelens-backend/app/services/image_analysis.py:187-285
4. AI Vision Forensics (Gemini)
The system leverages Gemini 1.5 Flash to act as a digital forensics expert. It inspects images for visual anomalies that are difficult to detect via pure pixel mathematics, such as:
- Anomalous anatomical details (e.g., extra fingers).
- Impossible lighting or shadows.
- "Melting" textures or nonsensical background text.
Sources: truelens-backend/app/services/image_analysis.py:321-375
System Architecture and Data Flow
The Image Forensics service is integrated into an asynchronous pipeline. When a user submits content, the backend orchestrates several services via a Celery task.
Processing Pipeline
The following diagram illustrates the flow from image submission to signal generation:
This diagram shows the routing of image data through the specialized analysis functions to produce a consolidated verdict.
Sources: truelens-backend/app/tasks/ml_pipeline.py:168-204, truelens-backend/app/services/image_analysis.py:382-428
Signal Classification and Weighting
Each analysis method produces a "Signal" object. These signals are weighted by the ensemble engine to determine the final trust score.
| Signal Type | Label | Weight | Key Evidence |
|---|---|---|---|
image | Error Level Analysis | 0.20 | avg_pixel_difference, difference_stddev |
metadata | EXIF Metadata Analysis | 0.25 | software, camera_make, date_mismatch |
image | GAN Fingerprint Detection | 0.20 | spectral_peaks, high_freq_ratio |
image | AI Vision Forensics | 0.20 | Visual anomalies (Gemini highlights) |
Sources: truelens-backend/app/tasks/ml_pipeline.py:39-46, truelens-backend/app/services/image_analysis.py:389-428, truelens-app/src/lib/analysis.ts:167-173
Scoring Logic
The final trust score is calculated using a confidence-adjusted weighted average. A score of 0-100 is generated for each signal, where a higher score indicates a higher probability of the content being "fake" or manipulated.
# Simplified Logic from ml_pipeline.py
effective_weight = signal_weight * confidence
weighted_sum += signal_score * effective_weight
trust_score = 100 - (weighted_sum / total_weight)
Sources: truelens-backend/app/tasks/ml_pipeline.py:53-78
Scanned Document Forensics
Image forensics are also applied to scanned documents (PNG, JPG, PDF-embedded images). In these cases, the document_analysis service integrates OCR with forensic checks.
- OCR Integration: Uses
pytesseractto extract text from image-based documents. - Arithmetic Validation: For invoices, it validates that line items sum correctly to the total, penalizing the trust score if inconsistencies suggest tampering.
- Font Consistency: In PDFs, detecting more than 5 distinct fonts is flagged as a high-severity indicator of content splicing.
Sources: truelens-backend/app/services/document_analysis.py:95-135, truelens-backend/app/services/document_analysis.py:230-245, truelens-backend/app/services/document_analysis.py:352-385
Conclusion
The Image Forensics module provides a multi-layered defense against visual misinformation. By combining mathematical error analysis (ELA), structural metadata inspection, frequency-domain GAN detection, and LLM-powered visual reasoning, TrueLens generates a comprehensive "Trust Score" that helps users identify manipulated and AI-generated imagery with high confidence.
Sources: truelens-app/src/app/page.tsx:288-300, truelens-backend/app/tasks/ml_pipeline.py:221-226