├── LICENSE ├── README.md ├── deepfake_detection.py ├── face_detection.py ├── requirements.txt └── video_detection.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Shreyas S 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Realtime-Deepfake-Detection 2 | This project is a real-time deepfake detection system implemented in PyTorch. Deepfakes are manipulated videos or images that use artificial intelligence to swap faces or modify visual content, often with malicious intent. The goal of this project is to develop a system capable of detecting deepfake videos in real-time. 3 | # Requirements 4 | Python 3.x 5 | PyTorch 6 | OpenCV (cv2) 7 | facenet-pytorch 8 | pytorch-grad-cam 9 | # Installation 10 | Clone the repository to your local machine: 11 | ```bash 12 | 13 | git clone https://github.com/Zhreyu/Realtime-Deepfake-Detection.git 14 | ``` 15 | Change directory to the cloned repository: 16 | ```bash 17 | 18 | cd Realtime-Deepfake-Detection 19 | ``` 20 | Install the required Python packages using pip: 21 | ```bash 22 | 23 | pip install -r requirements.txt 24 | ``` 25 | Download the InceptionResnetV1 checkpoint file and place it in the root directory of the repository. 26 | 27 | 28 | # Usage 29 | To run the real-time deepfake detection on the video, execute the following command: 30 | 31 | ```bash 32 | python video_detection.py 33 | ``` 34 | The program will process each frame of the video, detecting faces and applying the deepfake detection algorithm. The results will be displayed in a window showing the original video with bounding boxes around the detected faces and labels indicating whether they are real or deepfake. Press 'q' to exit the program. 35 | 36 | # Additional Notes 37 | The deepfake detection model uses the InceptionResnetV1 architecture pretrained on the VGGFace2 dataset. You can experiment with different models or custom architectures by modifying the code in deepfake_detection.py. 38 | 39 | The face detection is performed using the Haar Cascade classifier and the MTCNN face detector. You can explore other face detection methods to see how they perform in this context. 40 | 41 | The project is meant for educational and experimental purposes and should not be used for production-grade applications without further fine-tuning and validation. 42 | -------------------------------------------------------------------------------- /deepfake_detection.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn.functional as F 3 | from facenet_pytorch import MTCNN, InceptionResnetV1 4 | from pytorch_grad_cam import GradCAM 5 | from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget 6 | from pytorch_grad_cam.utils.image import show_cam_on_image 7 | from PIL import ImageFont, ImageDraw, Image 8 | import numpy as np 9 | import cv2 10 | 11 | from face_detection import detect_bounding_box 12 | 13 | DEVICE = "cuda:0" if torch.cuda.is_available() else "cpu" 14 | 15 | mtcnn = MTCNN( 16 | select_largest=False, 17 | post_process=False, 18 | device=DEVICE 19 | ).to(DEVICE).eval() 20 | 21 | model = InceptionResnetV1( 22 | pretrained="vggface2", 23 | classify=True, 24 | num_classes=1, 25 | device=DEVICE 26 | ) 27 | checkpoint = torch.load("weights\\resnetinceptionv1_epoch_32.pth",map_location=torch.device('cpu')) 28 | model.load_state_dict(checkpoint["model_state_dict"]) 29 | model.to(DEVICE) 30 | model.eval() 31 | def predict(frame): 32 | """Predict the label of the input frame""" 33 | frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) 34 | input_image = Image.fromarray(frame_rgb) 35 | 36 | faces = detect_bounding_box(frame) # Detect faces 37 | 38 | for (x, y, w, h) in faces: 39 | face_region = frame[y:y + h, x:x + w] # Extract face region 40 | 41 | # Perform face recognition on the extracted face region 42 | input_face = Image.fromarray(cv2.cvtColor(face_region, cv2.COLOR_BGR2RGB)) 43 | input_face = mtcnn(input_face) 44 | if input_face is None: 45 | continue 46 | 47 | input_face = input_face.unsqueeze(0) # add the batch dimension 48 | input_face = F.interpolate(input_face, size=(256, 256), mode="bilinear", align_corners=False) 49 | input_face = input_face.to(DEVICE).to(torch.float32) / 255.0 50 | 51 | target_layers = [model.block8.branch1[-1]] 52 | use_cuda = True if torch.cuda.is_available() else False 53 | cam = GradCAM(model=model, target_layers=target_layers, use_cuda=use_cuda) 54 | targets = [ClassifierOutputTarget(0)] 55 | 56 | grayscale_cam = cam(input_tensor=input_face, targets=targets, eigen_smooth=True) 57 | grayscale_cam = grayscale_cam[0, :] 58 | visualization = show_cam_on_image( 59 | input_face.squeeze(0).permute(1, 2, 0).cpu().detach().numpy(), grayscale_cam, use_rgb=True 60 | ) 61 | 62 | with torch.no_grad(): 63 | output = torch.sigmoid(model(input_face).squeeze(0)) 64 | prediction = "Fake" if output.item() < 0.5 else "Real" 65 | 66 | if prediction == "Fake": 67 | print("Deepfake detected,confidence: ",output.item()*100) 68 | frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 4) 69 | frame = cv2.putText(frame, "Deep Fake Detected", (x, y - 10), 70 | cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2) 71 | else: 72 | print("real face, confidence: ",output.item()*100) 73 | frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 4) 74 | frame = cv2.putText(frame, "Real Face", (x, y - 10), 75 | cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2) 76 | 77 | 78 | return frame 79 | 80 | -------------------------------------------------------------------------------- /face_detection.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | 3 | face_classifier = cv2.CascadeClassifier( 4 | cv2.data.haarcascades + "haarcascade_frontalface_default.xml" 5 | ) 6 | 7 | def detect_bounding_box(vid): 8 | gray_image = cv2.cvtColor(vid, cv2.COLOR_BGR2GRAY) 9 | faces = face_classifier.detectMultiScale(gray_image, 1.1, 5, minSize=(40, 40)) 10 | for (x, y, w, h) in faces: 11 | cv2.rectangle(vid, (x, y), (x + w, y + h), (0, 255, 0), 4) 12 | return faces 13 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | torch>=1.9.0 2 | torchvision>=0.10.0 3 | facenet-pytorch>=2.5.2 4 | opencv-python>=4.5.3 5 | pytorch-grad-cam>=0.2.2 6 | -------------------------------------------------------------------------------- /video_detection.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | from face_detection import detect_bounding_box 3 | from deepfake_detection import predict 4 | 5 | def main(): 6 | video_capture = cv2.VideoCapture(0) 7 | while True: 8 | result, video_frame = video_capture.read() 9 | if result is False: 10 | break 11 | 12 | faces = detect_bounding_box(video_frame) 13 | video_frame = predict(video_frame) 14 | 15 | cv2.imshow("My Face Detection Project", video_frame) 16 | 17 | if cv2.waitKey(1) & 0xFF == ord("q"): 18 | break 19 | 20 | video_capture.release() 21 | cv2.destroyAllWindows() 22 | 23 | if __name__ == "__main__": 24 | main() 25 | --------------------------------------------------------------------------------