Spaces:
Sleeping
Sleeping
File size: 3,813 Bytes
1157352 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
"""
Detection Orchestrator - Coordinates image and video analysis
"""
import sys
from pathlib import Path
# Add parent directory to path for imports
sys.path.append(str(Path(__file__).parent.parent))
from analyze.image_analyzer import ImageAnalyzer
from analyze.video_analyzer import VideoAnalyzer
from utils.media_io import MediaHandler
class DeepfakeDetector:
"""Main detector class that orchestrates the analysis"""
def __init__(self, api_key):
"""
Initialize detector with Gemini API key
Args:
api_key: Google Gemini API key
"""
self.api_key = api_key
self.image_analyzer = ImageAnalyzer(api_key)
self.video_analyzer = VideoAnalyzer(api_key)
self.media_handler = MediaHandler()
def analyze_image(self, image_path):
"""
Analyze an image for deepfake content
Args:
image_path: Path to the image file
Returns:
dict: Analysis results
"""
# Validate file
if not self.media_handler.validate_image(image_path):
return {
'is_deepfake': False,
'confidence_score': 0,
'analysis': 'Invalid image file',
'indicators': [],
'error': 'File validation failed'
}
# Perform analysis
results = self.image_analyzer.analyze(image_path)
# Add metadata
results['media_type'] = 'image'
results['file_path'] = image_path
return results
def analyze_video(self, video_path, max_frames=10):
"""
Analyze a video for deepfake content
Args:
video_path: Path to the video file
max_frames: Maximum number of frames to analyze
Returns:
dict: Analysis results
"""
# Validate file
if not self.media_handler.validate_video(video_path):
return {
'is_deepfake': False,
'confidence_score': 0,
'analysis': 'Invalid video file',
'indicators': [],
'frame_analysis': {'total_frames': 0, 'suspicious_frames': 0},
'error': 'File validation failed'
}
# Perform analysis
results = self.video_analyzer.analyze(video_path, max_frames)
# Add metadata
results['media_type'] = 'video'
results['file_path'] = video_path
return results
def batch_analyze(self, file_paths, media_type='auto'):
"""
Analyze multiple files
Args:
file_paths: List of file paths
media_type: 'image', 'video', or 'auto' (detect from extension)
Returns:
list: List of analysis results
"""
results = []
for file_path in file_paths:
if media_type == 'auto':
if self.media_handler.is_image(file_path):
result = self.analyze_image(file_path)
elif self.media_handler.is_video(file_path):
result = self.analyze_video(file_path)
else:
result = {
'error': 'Unknown file type',
'file_path': file_path
}
elif media_type == 'image':
result = self.analyze_image(file_path)
else:
result = self.analyze_video(file_path)
results.append(result)
return results |