Upload 3 files
Browse files- cnn_model.h5 +3 -0
- detector2.py +49 -0
- requirements.txt +10 -0
cnn_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2f54d9db020da33f99f861d41dc1334ec33adc14991ada4033a4ece790d0904e
|
| 3 |
+
size 312843624
|
detector2.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import numpy as np
|
| 3 |
+
from tensorflow.keras.preprocessing import image
|
| 4 |
+
from tensorflow.keras.models import load_model
|
| 5 |
+
import cv2
|
| 6 |
+
from mtcnn import MTCNN
|
| 7 |
+
|
| 8 |
+
# Load the trained model
|
| 9 |
+
model = load_model('cnn_model.h5')
|
| 10 |
+
|
| 11 |
+
# Function to detect and crop the face using MTCNN
|
| 12 |
+
def detect_and_crop_face(img_path):
|
| 13 |
+
img = cv2.imread(img_path)
|
| 14 |
+
detector = MTCNN()
|
| 15 |
+
results = detector.detect_faces(img)
|
| 16 |
+
|
| 17 |
+
if results:
|
| 18 |
+
bounding_box = results[0]['box']
|
| 19 |
+
x, y, width, height = bounding_box
|
| 20 |
+
face = img[y:y+height, x:x+width]
|
| 21 |
+
face = cv2.resize(face, (128, 128)) # Resize to the target size
|
| 22 |
+
return face
|
| 23 |
+
else:
|
| 24 |
+
# If no face is detected, return the original resized image
|
| 25 |
+
return cv2.resize(img, (128, 128))
|
| 26 |
+
|
| 27 |
+
# Function to preprocess the cropped face
|
| 28 |
+
def preprocess_face(face):
|
| 29 |
+
img_array = image.img_to_array(face)
|
| 30 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 31 |
+
img_array /= 255.0 # Normalize to [0, 1]
|
| 32 |
+
return img_array
|
| 33 |
+
|
| 34 |
+
# Main function to predict if an image is real or fake
|
| 35 |
+
def predict_real_or_fake(img_path):
|
| 36 |
+
# Detect and preprocess the face in the provided image
|
| 37 |
+
face = detect_and_crop_face(img_path)
|
| 38 |
+
processed_image = preprocess_face(face)
|
| 39 |
+
|
| 40 |
+
# Predict the class of the image
|
| 41 |
+
prediction = model.predict(processed_image)
|
| 42 |
+
|
| 43 |
+
# Print the result
|
| 44 |
+
result = 'Real' if prediction[0][0] < 0.5 else 'Fake'
|
| 45 |
+
print(f"Image: {img_path} - Predicted as: {result}")
|
| 46 |
+
|
| 47 |
+
# Example usage:
|
| 48 |
+
image_path = input("Enter your image path: ") # Replace with your image path
|
| 49 |
+
predict_real_or_fake(image_path)
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy
|
| 2 |
+
pandas
|
| 3 |
+
tensorflow
|
| 4 |
+
keras>=2.2.0
|
| 5 |
+
keras_applications >= 1.0.7
|
| 6 |
+
opencv-python>=4.1.0
|
| 7 |
+
mtcnn>=0.1.0
|
| 8 |
+
h5py
|
| 9 |
+
efficientnet
|
| 10 |
+
split_folders
|