| | import os
|
| | import numpy as np
|
| | from tensorflow.keras.preprocessing import image
|
| | from tensorflow.keras.models import load_model
|
| | import cv2
|
| | from mtcnn import MTCNN
|
| |
|
| |
|
| | model = load_model('cnn_model.h5')
|
| |
|
| |
|
| | def detect_and_crop_face(img_path):
|
| | img = cv2.imread(img_path)
|
| | detector = MTCNN()
|
| | results = detector.detect_faces(img)
|
| |
|
| | if results:
|
| | bounding_box = results[0]['box']
|
| | x, y, width, height = bounding_box
|
| | face = img[y:y+height, x:x+width]
|
| | face = cv2.resize(face, (128, 128))
|
| | return face
|
| | else:
|
| |
|
| | return cv2.resize(img, (128, 128))
|
| |
|
| |
|
| | def preprocess_face(face):
|
| | img_array = image.img_to_array(face)
|
| | img_array = np.expand_dims(img_array, axis=0)
|
| | img_array /= 255.0
|
| | return img_array
|
| |
|
| |
|
| | def predict_real_or_fake(img_path):
|
| |
|
| | face = detect_and_crop_face(img_path)
|
| | processed_image = preprocess_face(face)
|
| |
|
| |
|
| | prediction = model.predict(processed_image)
|
| |
|
| |
|
| | result = 'Real' if prediction[0][0] < 0.5 else 'Fake'
|
| | print(f"Image: {img_path} - Predicted as: {result}")
|
| |
|
| |
|
| | image_path = input("Enter your image path: ")
|
| | predict_real_or_fake(image_path)
|
| |
|