from classifiers import Meso4, MesoInception4 import numpy as np from PIL import Image # 1 - Load the model and its pretrained weights def load_mesonet(weight_path="models/weights/Meso4_DF.h5"): """ Builds the MesoNet architecture and loads pre-trained weights. """ model = Meso4() 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_mesonet(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