File size: 3,571 Bytes
d7f12b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from classifiers import Meso4, MesoInception4
import numpy as np
from PIL import Image

# 1 - Load the model and its pretrained weights

def load_mesonetInception(weight_path="models/weights/MesoInception_DF.h5"):
    """
    Builds the MesoNet architecture and loads pre-trained weights.
    """
    model = MesoInception4()
    model.load(weight_path)
    # Load weights by name
    return model

def preprocess_image(image):
    """
    Preprocesses an input image for prediction.
    
    Args:
        image (PIL.Image): The input image.
    
    Returns:
        np.array: The preprocessed image ready for prediction.
    """
    image = image.resize((256, 256))  # Resize to model input size
    image = np.array(image) / 255.0  # Normalize the image
    image = np.expand_dims(image, axis=0)  # Add batch dimension
    return image

def predict_mesonetInception(model, image):
    """
    Predicts DeepFake probability using MesoNet (TensorFlow/Keras).
    """
    """
    Predicts the DeepFake probability using the MesoNet model.
    
    Args:
        model: The MesoNet model (Meso4 or MesoInception4).
        image (PIL.Image): The input image to predict.
    
    Returns:
        float: The predicted probability of the image being a DeepFake.
    """
    # Preprocess the image
    preprocessed_image = preprocess_image(image)

    # Perform prediction
    predictions = model.predict(preprocessed_image)


    fake_prob = predictions[0][0]
    # print('Predicted :', model.predict(X), '\nReal class :', y)
    return fake_prob*100




    
# from tensorflow.keras.models import Model
# from tensorflow.keras.layers import Input, Conv2D, BatchNormalization, ReLU, Flatten, Dense, MaxPooling2D, Dropout
# import numpy as np

# def build_mesonet():
#     """
#     Builds the MesoNet architecture to match the weight file.
#     """
#     input_layer = Input(shape=(256, 256, 3), name="input_2")

#     # Block 1
#     x = Conv2D(8, (3, 3), padding="same", name="conv2d_5")(input_layer)
#     x = BatchNormalization(name="batch_normalization_5")(x)
#     x = ReLU()(x)
#     x = MaxPooling2D(pool_size=(2, 2), padding="same", name="max_pooling2d_5")(x)

#     # Block 2
#     x = Conv2D(8, (5, 5), padding="same", name="conv2d_6")(x)
#     x = BatchNormalization(name="batch_normalization_6")(x)
#     x = ReLU()(x)
#     x = MaxPooling2D(pool_size=(2, 2), padding="same", name="max_pooling2d_6")(x)

#     # Block 3
#     x = Conv2D(16, (5, 5), padding="same", name="conv2d_7")(x)
#     x = BatchNormalization(name="batch_normalization_7")(x)
#     x = ReLU()(x)
#     x = MaxPooling2D(pool_size=(2, 2), padding="same", name="max_pooling2d_7")(x)

#     # Block 4
#     x = Conv2D(16, (5, 5), padding="same", name="conv2d_8")(x)
#     x = BatchNormalization(name="batch_normalization_8")(x)
#     x = ReLU()(x)
#     x = MaxPooling2D(pool_size=(4, 4), padding="same", name="max_pooling2d_8")(x)

#     # Fully connected layers
#     x = Flatten(name="flatten_2")(x)  # Output should now match 1024
#     x = Dense(16, activation="relu", name="dense_3")(x)
#     x = Dropout(0.5, name="dropout_3")(x)
#     x = Dense(1, activation="sigmoid", name="dense_4")(x)  # Single output for binary classification

#     model = Model(inputs=input_layer, outputs=x)
#     return model


# # def load_mesonet(weight_path="models/weights/Meso4_DF.h5"):
#     """
#     Builds the MesoNet architecture and loads pre-trained weights.
#     """
#     model = build_mesonet()
#     model.load_weights(weight_path, by_name=True)  # Load weights by name
#     return model