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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
import streamlit as st
from PIL import Image, ImageChops, ImageEnhance, ImageDraw, ImageFilter
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from scipy import ndimage
from skimage import feature, measure
import io
import cv2
import os
import cv2 as cv
from mtcnn import MTCNN
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image as keras_image
import keras

# Load models
@st.cache_resource
def load_image_forgery_model():
    return load_model("imageforgerydetection.h5")

@st.cache_resource
def load_deepfake_image_model():
    return load_model("deepfake_image_detection.h5")

@st.cache_resource
def load_video_forgery_model():
    return load_model("videoforgerydetection.keras")

# Constants
IMG_SIZE = 224
MAX_SEQ_LENGTH = 20
NUM_FEATURES = 2048

@st.cache_resource
def load_deepfake_model():
    return load_model('video_classifier_full_model.h5')

# Load pre-trained models and processor
deepfake_model = load_deepfake_model()
vocabulary2 = np.load('label_processor_vocabulary.npy', allow_pickle=True)
label_processor2 = keras.layers.StringLookup(num_oov_indices=0, vocabulary=vocabulary2.tolist())


# Helper functions
# Image Forgery Detection Functions
def convert_to_ela_image(image, quality=90):
    temp_filename = 'temp_file_name.jpg'
    ela_filename = 'temp_ela.png'
    
    if image.mode != 'RGB':
        image = image.convert('RGB')
    
    image.save(temp_filename, 'JPEG', quality=quality)
    temp_image = Image.open(temp_filename)
    
    ela_image = ImageChops.difference(image, temp_image)
    extrema = ela_image.getextrema()
    max_diff = max([ex[1] for ex in extrema])
    max_diff = max_diff if max_diff != 0 else 1
    scale = 255.0 / max_diff
    
    ela_image = ImageEnhance.Brightness(ela_image).enhance(scale)
    return ela_image

def prepare_image_for_forgery(image):
    ela_image = convert_to_ela_image(image, 90).resize((128, 128))
    return np.array(ela_image).flatten() / 255.0


# Individual Analysis Functions
def create_ela_analysis(image):
    """Create ELA analysis visualization"""
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6))
    fig.suptitle('Error Level Analysis (ELA)', fontsize=14, fontweight='bold')
    
    # Original image
    ax1.imshow(image)
    ax1.set_title('Original Image')
    ax1.axis('off')
    
    # ELA image
    ela_image = convert_to_ela_image(image, 90)
    ax2.imshow(ela_image)
    ax2.set_title('ELA Result (Bright areas indicate potential editing)')
    ax2.axis('off')
    
    plt.tight_layout()
    
    buffer = io.BytesIO()
    plt.savefig(buffer, format='png', dpi=150, bbox_inches='tight')
    buffer.seek(0)
    plt.close()
    
    return buffer


# Deepfake Image Detection
def predict_deepfake_image(image_path, model):
    img = keras_image.load_img(image_path, target_size=(256, 256))
    img_array = keras_image.img_to_array(img) / 255.0
    img_array = np.expand_dims(img_array, axis=0)
    prediction = model.predict(img_array)
    return 'Real' if prediction[0] > 0.5 else 'Fake'

# Video Forgery Detection
# Configuration
target_height, target_width = 240, 320
threshold = 30  # Threshold for freeze/duplicate detection

def predict_video_forgery_cnn(video_path, model):
    """CNN-based video forgery detection"""
    vid = []
    sumframes = 0
    cap = cv2.VideoCapture(video_path)

    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        
        # Resize frame to target dimensions
        frame = cv2.resize(frame, (target_width, target_height))
        sumframes += 1
        vid.append(frame)

    cap.release()
    
    if sumframes == 0:
        return False, 0, 0
    
    Xtest = np.array(vid)
    output = model.predict(Xtest)
    output = output.reshape((-1))

    # Check if any frame is predicted as forged
    forged_frames = sum(1 for i in output if i > 0.5)
    is_forged = any(i > 0.5 for i in output)
    
    return is_forged, forged_frames, sumframes

def analyze_video_tampering(video_path):
    """Frame difference analysis for tampering detection"""
    cap = cv2.VideoCapture(video_path)
    if not cap.isOpened():
        return False, [], []

    prev_frame = None
    frame_differences = []
    suspected_frames = []

    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        if prev_frame is not None:
            diff = cv2.absdiff(gray, prev_frame)
            non_zero = np.count_nonzero(diff)
            frame_differences.append(non_zero)

            if non_zero < threshold:
                current_frame = int(cap.get(cv2.CAP_PROP_POS_FRAMES))
                suspected_frames.append(current_frame)

        prev_frame = gray

    cap.release()
    
    # Simple rule: if any frame is suspected, flag as tampered
    is_tampered = len(suspected_frames) > 0
    
    return is_tampered, frame_differences, suspected_frames

def plot_frame_analysis(frame_differences):
    """Create a simple plot of frame differences"""
    plt.figure(figsize=(10, 4))
    plt.plot(frame_differences, color='blue', linewidth=1)
    plt.axhline(y=threshold, color='red', linestyle='--', label=f"Threshold ({threshold})")
    plt.xlabel("Frame Number")
    plt.ylabel("Pixel Differences")
    plt.title("Frame Difference Analysis")
    plt.legend()
    plt.grid(True, alpha=0.3)
    
    # Add statistics
    if frame_differences:
        mean_val = np.mean(frame_differences)
        std_val = np.std(frame_differences)
        plt.text(0.02, 0.98, f"Mean: {mean_val:.1f}\nStd: {std_val:.1f}", 
                transform=plt.gca().transAxes, verticalalignment='top',
                bbox=dict(boxstyle='round', facecolor='white', alpha=0.8))
    
    return plt

def combined_video_forgery_detection(video_path, model):
    """Combined detection using both CNN and frame analysis"""
    
    # Method 1: CNN-based detection
    cnn_forged, cnn_forged_frames, total_frames = predict_video_forgery_cnn(video_path, model)
    
    # Method 2: Frame analysis tampering detection
    frame_tampered, frame_differences, suspected_frames = analyze_video_tampering(video_path)
    
    # Results
    results = {
        'cnn_forged': cnn_forged,
        'cnn_forged_frames': cnn_forged_frames,
        'frame_tampered': frame_tampered,
        'suspected_frames': len(suspected_frames),
        'total_frames': total_frames,
        'frame_differences': frame_differences
    }
    
    # Simple decision logic
    if cnn_forged and frame_tampered:
        verdict = "FORGED - Detected by both CNN and Frame Analysis"
        confidence = "High"
    elif cnn_forged:
        verdict = "FORGED - Detected by CNN"
        confidence = "Medium"
    elif frame_tampered:
        verdict = "FORGED - Detected by Frame Analysis"
        confidence = "Medium"
    else:
        verdict = "NOT TAMPERED - No Forgery detected"
        confidence = "High"
    
    return verdict, confidence, results

# Deepfake Video Detection
def build_feature_extractor():
    feature_extractor = keras.applications.InceptionV3(
        weights="imagenet",
        include_top=False,
        pooling="avg",
        input_shape=(IMG_SIZE, IMG_SIZE, 3),
    )
    preprocess_input = keras.applications.inception_v3.preprocess_input

    inputs = keras.Input((IMG_SIZE, IMG_SIZE, 3))
    preprocessed = preprocess_input(inputs)
    outputs = feature_extractor(preprocessed)
    return keras.Model(inputs, outputs, name="feature_extractor")

feature_extractor = build_feature_extractor()
detector = MTCNN()

def load_video(path, max_frames=0, resize=(IMG_SIZE, IMG_SIZE), skip_frames=2):
    cap = cv.VideoCapture(path)
    frames = []
    frame_count = 0
    previous_box = None

    while True:
        ret, frame = cap.read()
        if not ret:
            break

        if frame_count % skip_frames == 0:
            frame, previous_box = get_face_region_first_frame(frame, previous_box)
            if frame is not None:
                frame = cv.resize(frame, resize)
                frame = frame[:, :, [2, 1, 0]]
                frames.append(frame)

            if len(frames) == max_frames:
                break
        frame_count += 1

    while len(frames) < max_frames and frames:
        frames.append(frames[-1])

    cap.release()
    return np.array(frames)

def get_face_region_first_frame(frame, previous_box=None):
    if previous_box is None:
        detections = detector.detect_faces(frame)
        if detections:
            x, y, width, height = detections[0]['box']
            previous_box = (x, y, width, height)
        else:
            return None, None
    else:
        x, y, width, height = previous_box

    face_region = frame[y:y+height, x:x+width]
    return face_region, previous_box

def prepare_single_video(frames):
    frames = frames[None, ...]
    frame_mask = np.zeros(shape=(1, MAX_SEQ_LENGTH,), dtype="bool")
    frame_features = np.zeros(shape=(1, MAX_SEQ_LENGTH, NUM_FEATURES), dtype="float32")

    for i, batch in enumerate(frames):
        video_length = batch.shape[0]
        length = min(MAX_SEQ_LENGTH, video_length)
        for j in range(length):
            frame_features[i, j, :] = feature_extractor.predict(batch[None, j, :])
        frame_mask[i, :length] = 1

    return frame_features, frame_mask

def sequence_prediction(video_path):
    class_vocab = label_processor2.get_vocabulary()
    frames = load_video(video_path)
    if len(frames) == 0:
        st.error("Could not process video. Please try another file.")
        return None

    frame_features, frame_mask = prepare_single_video(frames)
    probabilities = deepfake_model.predict([frame_features, frame_mask])[0]
    
    predictions = {class_vocab[i]: probabilities[i] * 100 for i in np.argsort(probabilities)[::-1]}
    return predictions


# Streamlit App
st.title("Fraudulent Image and Video Detection System")

# Sidebar for model selection
task = st.sidebar.selectbox("Choose a detection task:", [
    "Image Forgery Detection", 
    "Deepfake Image Detection", 
    "Video Forgery Detection", 
    "Deepfake Video Detection"
])

# Main Streamlit App
if task == "Image Forgery Detection":
    uploaded_file = st.file_uploader("Upload an image", type=['jpg', 'jpeg', 'png'])
    
    if uploaded_file:
        image = Image.open(uploaded_file)
        # Fixed size display - adjust width as needed (300-600 pixels work well)
        st.image(image, caption="Uploaded Image", width=400)
        
        # Original prediction
        prepared_image = prepare_image_for_forgery(image).reshape(-1, 128, 128, 3)
        model = load_image_forgery_model()
        prediction = model.predict(prepared_image)
        confidence_real = prediction[0][1] * 100
        confidence_fake = prediction[0][0] * 100
        
        if confidence_real > confidence_fake:
            st.success(f"Result: Real Image with {confidence_real:.2f}% confidence")
        else:
            st.error(f"Result: Forged Image with {confidence_fake:.2f}% confidence")
        
        # Add ELA analysis option
        st.markdown("---")
        st.subheader("πŸ” Additional Analysis")
        
        # Show ELA option checkbox
        show_ela = st.checkbox("View Error Level Analysis (ELA)", value=False)
        
        if show_ela:
            st.markdown("### Error Level Analysis")
            st.info("**ELA**: Reveals compression artifacts. Bright areas indicate potential editing or manipulation.")
            
            col1, col2 = st.columns([1, 3])
            
            with col1:
                analyze_button = st.button("Run ELA Analysis", type="primary", use_container_width=True)
            
            if analyze_button:
                with st.spinner("Running Error Level Analysis..."):
                    try:
                        analysis_buffer = create_ela_analysis(image)
                        
                        # Fixed size for analysis results
                        st.image(analysis_buffer, caption="ELA Analysis Results", width=500)
                        
                        # Download button
                        st.download_button(
                            label="Download ELA Results",
                            data=analysis_buffer.getvalue(),
                            file_name="ela_analysis.png",
                            mime="image/png",
                            use_container_width=True
                        )
                        
                    except Exception as e:
                        st.error(f"Error during ELA analysis: {str(e)}")
                        st.info("ELA analysis may not work with all image types. Try with a different image if needed.")
                    
elif task == "Deepfake Image Detection":
    uploaded_file = st.file_uploader("Upload an image", type=['jpg', 'jpeg', 'png'])
    if uploaded_file:
        with open("temp_image.jpg", "wb") as f:
            f.write(uploaded_file.getbuffer())

        # Fixed size display for deepfake detection
        st.image(uploaded_file, caption="Uploaded Image", width=400)
        model = load_deepfake_image_model()
        result = predict_deepfake_image("temp_image.jpg", model)

        if result == 'Real':
            st.success("Prediction: Real")
        else:
            st.error("Prediction: Fake")

        os.remove("temp_image.jpg")
        
if task == "Video Forgery Detection":
    uploaded_file = st.file_uploader("Upload a video", type=['mp4', 'avi', 'mov', 'mkv'])
    
    if uploaded_file:
        # Save uploaded file
        with open("temp_video.mp4", "wb") as f:
            f.write(uploaded_file.getbuffer())

        st.video("temp_video.mp4")
        st.write("Analyzing the video for forgery...")

        # Load model and run combined detection
        model = load_video_forgery_model()
        verdict, confidence, results = combined_video_forgery_detection("temp_video.mp4", model)

        # Display results
        if "FORGED" in verdict:
            st.error(f"🚨 {verdict}")
        else:
            st.success(f"βœ… {verdict}")
        
        st.write(f"**Confidence Level:** {confidence}")
        
        # Show detailed results
        col1, col2 = st.columns(2)
        
        with col1:
            st.write("**CNN Analysis:**")
            if results['cnn_forged']:
                st.write(f"- Status: Forged ❌")
                st.write(f"- Forged Frames: {results['cnn_forged_frames']}/{results['total_frames']}")
            else:
                st.write(f"- Status: Not Forged βœ…")
        
        with col2:
            st.write("**Frame Analysis:**")
            if results['frame_tampered']:
                st.write(f"- Status: Tampered ❌")
                st.write(f"- Suspected Frames: {results['suspected_frames']}")
            else:
                st.write(f"- Status: Not Tampered βœ…")
        
        # Plot frame differences if available
        if results['frame_differences']:
            st.write("**Frame Difference Analysis:**")
            fig = plot_frame_analysis(results['frame_differences'])
            st.pyplot(fig)
            plt.close()
        
        # Cleanup
        os.remove("temp_video.mp4")


elif task == "Deepfake Video Detection":
    uploaded_file = st.file_uploader("Upload a video", type=["mp4", "avi", "mov"])
    if uploaded_file is not None:
        with open("temp_video.mp4", "wb") as f:
            f.write(uploaded_file.read())

        st.video("temp_video.mp4")
        st.write("Analyzing the video...")

        frames = load_video("temp_video.mp4")
        if len(frames) == 0:
            st.error("Could not process video. Please try another file.")
        else:
            frame_features, frame_mask = prepare_single_video(frames)
            probabilities = deepfake_model.predict([frame_features, frame_mask])[0]
            
            predictions = {label_processor2.get_vocabulary()[i]: probabilities[i] * 100 for i in np.argsort(probabilities)[::-1]}

            if predictions:
                highest_label = max(predictions, key=predictions.get)
                highest_prob = predictions[highest_label]

                if highest_label.lower() == "real":
                    st.success(f"The video is real with a confidence of {highest_prob:.2f}%.")
                elif highest_label.lower() == "fake":
                    st.error(f"This video is a deepfake with a confidence of {highest_prob:.2f}%.")
                else:
                    st.warning(f"Uncertain prediction: {highest_label} with {highest_prob:.2f}% confidence.")