{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":16880,"databundleVersionId":858837,"sourceType":"competition"},{"sourceId":8661913,"sourceType":"datasetVersion","datasetId":5189844},{"sourceId":8664205,"sourceType":"datasetVersion","datasetId":5191601},{"sourceId":8679034,"sourceType":"datasetVersion","datasetId":5202615},{"sourceId":25026251,"sourceType":"kernelVersion"}],"dockerImageVersionId":29844,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n    #for filename in filenames:\n        #print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-15T05:20:20.919704Z","iopub.execute_input":"2024-06-15T05:20:20.920050Z","iopub.status.idle":"2024-06-15T05:20:21.495026Z","shell.execute_reply.started":"2024-06-15T05:20:20.919987Z","shell.execute_reply":"2024-06-15T05:20:21.494138Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\n\nif torch.cuda.is_available():\n    device = torch.device(\"cuda\")\n    print(\"GPU available\")","metadata":{"execution":{"iopub.status.busy":"2024-06-15T05:21:24.934366Z","iopub.execute_input":"2024-06-15T05:21:24.934788Z","iopub.status.idle":"2024-06-15T05:21:25.944161Z","shell.execute_reply.started":"2024-06-15T05:21:24.934716Z","shell.execute_reply":"2024-06-15T05:21:25.943164Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport math\nimport matplotlib.pyplot as plt\n%matplotlib inline ","metadata":{"execution":{"iopub.status.busy":"2024-06-15T05:21:26.937245Z","iopub.execute_input":"2024-06-15T05:21:26.937577Z","iopub.status.idle":"2024-06-15T05:21:27.161054Z","shell.execute_reply.started":"2024-06-15T05:21:26.937522Z","shell.execute_reply":"2024-06-15T05:21:27.160160Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#!pip install /kaggle/input/mtcnn1/mtcnn-0.1.0-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2024-06-15T05:21:28.130415Z","iopub.execute_input":"2024-06-15T05:21:28.130769Z","iopub.status.idle":"2024-06-15T05:21:28.134800Z","shell.execute_reply.started":"2024-06-15T05:21:28.130716Z","shell.execute_reply":"2024-06-15T05:21:28.133445Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import json\n\nwith open(\"/kaggle/input/deepfake-detection-challenge/train_sample_videos/metadata.json\",'r') as f:\n    file=json.load(f)\n\nvideo_name=[]\nlabel=[]\noriginal=[]\nfor key in file.keys():\n    video_name.append(key)\n    label.append(file[key][\"label\"])\n    \n\ndf=pd.DataFrame({\"video_name\":video_name,\"label\":label})","metadata":{"execution":{"iopub.status.busy":"2024-06-15T05:21:29.180878Z","iopub.execute_input":"2024-06-15T05:21:29.181234Z","iopub.status.idle":"2024-06-15T05:21:29.199464Z","shell.execute_reply.started":"2024-06-15T05:21:29.181171Z","shell.execute_reply":"2024-06-15T05:21:29.198632Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-15T05:21:30.069523Z","iopub.execute_input":"2024-06-15T05:21:30.069879Z","iopub.status.idle":"2024-06-15T05:21:30.096120Z","shell.execute_reply.started":"2024-06-15T05:21:30.069827Z","shell.execute_reply":"2024-06-15T05:21:30.094942Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df[\"label\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-06-15T05:21:30.700635Z","iopub.execute_input":"2024-06-15T05:21:30.700999Z","iopub.status.idle":"2024-06-15T05:21:30.711917Z","shell.execute_reply.started":"2024-06-15T05:21:30.700944Z","shell.execute_reply":"2024-06-15T05:21:30.710779Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"'''os.mkdir(os.path.join(\"/kaggle/working/\",\"video_images\"))\n\nreal=df[df[\"label\"]==\"REAL\"]\n\nfor ele in real[\"video_name\"][:5]:\n    print(ele)\n\nlen(real)\n\n#print(\"aagfhgtpmv.mp4\" in fake[\"video_name\"].tolist())\n\ndef get_frames(video_path):\n    cap = cv2.VideoCapture(video_path)\n    \n    frames = []\n    while(cap.isOpened()):\n        ret, frame = cap.read()\n        if ret==True:\n            frames.append(frame)\n            \n            if cv2.waitKey(1) & 0xFF == ord('q'):\n                break\n        else:\n            break\n    cap.release()\n    \n    facial_key=[]\n    for i in sorted([0,5,10,15,20,25,29,40,42,24,56,245,167,199,99,69,50,70,80,100,120,140,150,180,190,200,210,220,240,270,280,290,299,111,222,11]):      #  change the number for label real and fake\n        if i<len(frames):\n            frame=frames[i]\n            frame=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)\n            facial_key.append(frame)\n    return facial_key\n\n\n!pip install /kaggle/input/facenet/facenet_pytorch-2.2.9-py3-none-any.whl\n\nfrom facenet_pytorch import MTCNN\n\n\n\ndetector = MTCNN(margin=20, keep_all=True, post_process=False, device=device)\ndef detect_face(facial_key):\n    images = []\n    for key in facial_key:\n        faces, prob = detector.detect(key)\n        \n        # Ensure faces and prob are not None\n        if faces is not None and prob is not None:\n            for face, confidence_score in zip(faces, prob):\n                if face is not None and confidence_score >= 0.9:\n                    x1, y1, x2, y2 = face.astype(int)  # Convert to integer for bounding box coordinates\n                    # Adjust bounding box with margin\n                    x1 = max(0, x1)\n                    y1 = max(0, y1)\n                    x2 = min(key.shape[1], x2)\n                    y2 = min(key.shape[0], y2)\n                    images.append(key[y1:y2, x1:x2])\n    return images\n\ndef get_images(train_dir,final_dir,fake_df):\n    for video in os.listdir(train_dir):\n        if os.path.exists(os.path.join(final_dir,video)):\n            continue\n        if video not in fake_df:\n            continue\n        os.mkdir(os.path.join(final_dir,video))\n        \n        facial_key=get_frames(os.path.join(train_dir,video))\n        \n        faces=detect_face(facial_key)\n        \n        for i in range(len(faces)):\n            path=os.path.join(final_dir,video,str(i)+\".png\")\n            face=cv2.cvtColor(faces[i],cv2.COLOR_BGR2RGB)\n            cv2.imwrite(path, face, [cv2.IMWRITE_PNG_COMPRESSION, 0])\n            '''","metadata":{"execution":{"iopub.status.busy":"2024-06-15T05:21:31.358876Z","iopub.execute_input":"2024-06-15T05:21:31.359252Z","iopub.status.idle":"2024-06-15T05:21:31.366712Z","shell.execute_reply.started":"2024-06-15T05:21:31.359189Z","shell.execute_reply":"2024-06-15T05:21:31.365790Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#get_images(\"/kaggle/input/deepfake-detection-challenge/train_sample_videos\",\"/kaggle/working/video_images\",real[\"video_name\"].tolist())","metadata":{"execution":{"iopub.status.busy":"2024-06-15T05:21:31.955460Z","iopub.execute_input":"2024-06-15T05:21:31.955860Z","iopub.status.idle":"2024-06-15T05:21:31.959994Z","shell.execute_reply.started":"2024-06-15T05:21:31.955793Z","shell.execute_reply":"2024-06-15T05:21:31.958949Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#print(len(next(os.walk('/kaggle/working/video_images'))[1]))","metadata":{"execution":{"iopub.status.busy":"2024-06-15T05:21:32.398313Z","iopub.execute_input":"2024-06-15T05:21:32.398682Z","iopub.status.idle":"2024-06-15T05:21:32.402846Z","shell.execute_reply.started":"2024-06-15T05:21:32.398617Z","shell.execute_reply":"2024-06-15T05:21:32.401739Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"'''import os\nos.chdir(r'/kaggle/working')\n\n!tar -czf real.tar.gz video_images\n\nfrom IPython.display import FileLink\n\nFileLink(r'real.tar.gz')'''","metadata":{"execution":{"iopub.status.busy":"2024-06-15T05:21:32.811315Z","iopub.execute_input":"2024-06-15T05:21:32.811830Z","iopub.status.idle":"2024-06-15T05:21:32.817448Z","shell.execute_reply.started":"2024-06-15T05:21:32.811736Z","shell.execute_reply":"2024-06-15T05:21:32.816664Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"'''video_path=\"/kaggle/input/deepfake-detection-challenge/train_sample_videos/aagfhgtpmv.mp4\"\ncap = cv2.VideoCapture(video_path)\n#cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)\n#cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)\n#fps = cap.get(cv2.CAP_PROP_FPS)\n#frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))\n\nframes = []\nwhile(cap.isOpened()):\n    ret, frame = cap.read()\n    if ret==True:\n        frames.append(frame)\n        if cv2.waitKey(1) & 0xFF == ord('q'):\n            break\n    else:\n        break\ncap.release()\n\nlen(frames)\n\nfacial_key=[]\nfor i in [0,50,100,150,200,250]:\n    frame=frames[i]\n    frame=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)\n    facial_key.append(frame)'''","metadata":{"execution":{"iopub.status.busy":"2024-06-15T05:21:38.814853Z","iopub.execute_input":"2024-06-15T05:21:38.815176Z","iopub.status.idle":"2024-06-15T05:21:38.821498Z","shell.execute_reply.started":"2024-06-15T05:21:38.815129Z","shell.execute_reply":"2024-06-15T05:21:38.820456Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#imgplot = plt.imshow(facial_key[0])\n#plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-15T05:21:39.345463Z","iopub.execute_input":"2024-06-15T05:21:39.345831Z","iopub.status.idle":"2024-06-15T05:21:39.350017Z","shell.execute_reply.started":"2024-06-15T05:21:39.345774Z","shell.execute_reply":"2024-06-15T05:21:39.348940Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')","metadata":{"execution":{"iopub.status.busy":"2024-06-15T05:21:39.733956Z","iopub.execute_input":"2024-06-15T05:21:39.734286Z","iopub.status.idle":"2024-06-15T05:21:39.738565Z","shell.execute_reply.started":"2024-06-15T05:21:39.734236Z","shell.execute_reply":"2024-06-15T05:21:39.737516Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#locations = face_cascade.detectMultiScale(cv2.cvtColor(facial_key[0],cv2.COLOR_RGB2BGR))","metadata":{"execution":{"iopub.status.busy":"2024-06-15T05:21:40.197208Z","iopub.execute_input":"2024-06-15T05:21:40.197679Z","iopub.status.idle":"2024-06-15T05:21:40.202654Z","shell.execute_reply.started":"2024-06-15T05:21:40.197583Z","shell.execute_reply":"2024-06-15T05:21:40.201424Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"'''for face_location in locations:\n\n    # Print the location of each face in this image\n    x, y, w, h = face_location\n    print(\"A face is located at pixel location X: {}, Y: {}, Width: {}, Height: {}\".format(x, y, w, h))\n\n    # You can access the actual face itself like this:\n    face_image = facial_key[0][y:y+h, x:x+w]\n    \n    print(face_image.shape)\n    fig, ax = plt.subplots(1,1, figsize=(5, 5))\n    plt.grid(False)\n    ax.xaxis.set_visible(False)\n    ax.yaxis.set_visible(False)\n    ax.imshow(face_image)'''","metadata":{"execution":{"iopub.status.busy":"2024-06-15T05:21:40.445008Z","iopub.execute_input":"2024-06-15T05:21:40.445457Z","iopub.status.idle":"2024-06-15T05:21:40.453213Z","shell.execute_reply.started":"2024-06-15T05:21:40.445389Z","shell.execute_reply":"2024-06-15T05:21:40.451948Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"'''import mtcnn\nprint(mtcnn.__version__)\n\nfrom mtcnn.mtcnn import MTCNN\n\ndetector = MTCNN()\n\nfaces = detector.detect_faces(facial_key[3])\nfaces\n\nimage=[]\nprint(len(faces))\nfor face in faces:\n    x,y,w,h = face['box']\n\n    x_top=int(x-w/2)\n    y_top=int(y-h/2)\n    #rect = patches.Rectangle((x, y), w, h, linewidth=1, edgecolor='r', facecolor='none')\n    image.append(facial_key[3][y:y+h,x:x+w])\n    \n     \n    # Add the patch to the Axes\n    #ax.add_patch(rect)\n\nplt.imshow(image[1])'''\n\n\n#plt.imshow(image[1])","metadata":{"execution":{"iopub.status.busy":"2024-06-15T05:21:40.766268Z","iopub.execute_input":"2024-06-15T05:21:40.766840Z","iopub.status.idle":"2024-06-15T05:21:40.772615Z","shell.execute_reply.started":"2024-06-15T05:21:40.766541Z","shell.execute_reply":"2024-06-15T05:21:40.771745Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def isotropic_resize(image,size,resample=cv2.INTER_AREA):\n    \n    h,w=image.shape[:2]\n    if max(w,h)==size:\n        return image\n    if w>h:\n        h=h*size//w\n        w=size\n    else:\n        w=w*size//h\n        h=size\n    resized=cv2.resize(image,(w,h),interpolation=resample)\n    return resized\n\ndef make_square(image):\n    h,w=image.shape[:2]\n    size=max(h,w)\n    t=0\n    b=size-h\n    l=0\n    r=size-w\n    \n    return cv2.copyMakeBorder(image,t,b,l,r,cv2.BORDER_CONSTANT,value=0)","metadata":{"execution":{"iopub.status.busy":"2024-06-15T05:21:41.613674Z","iopub.execute_input":"2024-06-15T05:21:41.614058Z","iopub.status.idle":"2024-06-15T05:21:41.622660Z","shell.execute_reply.started":"2024-06-15T05:21:41.613993Z","shell.execute_reply":"2024-06-15T05:21:41.621727Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torchvision\nfrom torchvision import transforms\nimport torch\nfrom torch.utils.data import Dataset, DataLoader","metadata":{"execution":{"iopub.status.busy":"2024-06-15T05:21:42.597185Z","iopub.execute_input":"2024-06-15T05:21:42.597518Z","iopub.status.idle":"2024-06-15T05:21:42.745802Z","shell.execute_reply.started":"2024-06-15T05:21:42.597465Z","shell.execute_reply":"2024-06-15T05:21:42.744856Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nmean=[0.485,0.456,0.405]\nstd=[0.229,0.224,0.225]\nsize=224\ntransform=transforms.Compose([\n    transforms.Lambda(lambda img:cv2.cvtColor(img,cv2.COLOR_BGR2RGB)),\n    transforms.Lambda(lambda img:isotropic_resize(img,size)),\n    transforms.Lambda(lambda img:make_square(img)),\n    transforms.ToTensor(),\n    transforms.Normalize(mean=mean,std=std)\n])","metadata":{"execution":{"iopub.status.busy":"2024-06-15T05:21:43.494524Z","iopub.execute_input":"2024-06-15T05:21:43.494896Z","iopub.status.idle":"2024-06-15T05:21:43.501916Z","shell.execute_reply.started":"2024-06-15T05:21:43.494840Z","shell.execute_reply":"2024-06-15T05:21:43.500977Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_0_dir=os.path.join(\"/kaggle/input/deepfakeimages/Images\",\"real_images\")\nclass_1_dir=os.path.join(\"/kaggle/input/deepfakeimages/Images\",\"Fake_images\")    \nfake_images=[]\nfor subdir in os.listdir(class_1_dir):\n    subdir_path=os.path.join(class_1_dir,subdir)\n    fake_images.extend([os.path.join(subdir_path,img) for img in os.listdir(subdir_path)])\nreal_images=[]\nfor subdir in os.listdir(class_0_dir):\n    subdir_path=os.path.join(class_0_dir,subdir)\n    real_images.extend([os.path.join(subdir_path,img) for img in os.listdir(subdir_path)])\nimages=[(img,0) for img in real_images]+[(img,1) for img in fake_images]\n    ","metadata":{"execution":{"iopub.status.busy":"2024-06-15T05:21:44.373008Z","iopub.execute_input":"2024-06-15T05:21:44.373374Z","iopub.status.idle":"2024-06-15T05:21:46.157205Z","shell.execute_reply.started":"2024-06-15T05:21:44.373309Z","shell.execute_reply":"2024-06-15T05:21:46.156125Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(images)","metadata":{"execution":{"iopub.status.busy":"2024-06-15T05:21:46.159099Z","iopub.execute_input":"2024-06-15T05:21:46.159397Z","iopub.status.idle":"2024-06-15T05:21:46.164667Z","shell.execute_reply.started":"2024-06-15T05:21:46.159340Z","shell.execute_reply":"2024-06-15T05:21:46.163836Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class ImageDataset(Dataset):\n    \n    def __init__(self,images,transform=None):\n        \n        self.images=images\n        self.transform=transform\n    \n    def __len__(self):\n        return len(self.images)\n    \n    \n    def __getitem__(self,idx):\n        \n        image_path,label=self.images[idx]\n        img=cv2.imread(image_path)\n        if self.transform:\n            img=self.transform(img)\n        \n        return cv2.cvtColor(img,cv2.COLOR_BGR2RGB),label","metadata":{"execution":{"iopub.status.busy":"2024-06-15T05:22:40.288049Z","iopub.execute_input":"2024-06-15T05:22:40.288417Z","iopub.status.idle":"2024-06-15T05:22:40.296112Z","shell.execute_reply.started":"2024-06-15T05:22:40.288365Z","shell.execute_reply":"2024-06-15T05:22:40.295032Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import random\n\nrandom.seed(42)\n\nrandom.shuffle(images)\n\ntrain_size=int(0.8*len(images))\n\ntrain_images=images[:train_size]\n\nval_images=images[train_size:]","metadata":{"execution":{"iopub.status.busy":"2024-06-15T05:22:41.152097Z","iopub.execute_input":"2024-06-15T05:22:41.152452Z","iopub.status.idle":"2024-06-15T05:22:41.165971Z","shell.execute_reply.started":"2024-06-15T05:22:41.152385Z","shell.execute_reply":"2024-06-15T05:22:41.164876Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dataset=ImageDataset(train_images)\nval_datasset=ImageDataset(val_images,transform)","metadata":{"execution":{"iopub.status.busy":"2024-06-15T05:22:42.389724Z","iopub.execute_input":"2024-06-15T05:22:42.390059Z","iopub.status.idle":"2024-06-15T05:22:42.394920Z","shell.execute_reply.started":"2024-06-15T05:22:42.390009Z","shell.execute_reply":"2024-06-15T05:22:42.393686Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Get the image tensor from the dataset\nimg_tensor = train_dataset[0][0]\n\n# Convert the image tensor to a NumPy array\n#img_np = img_tensor.numpy()\n\n# Transpose the array to match the shape expected by matplotlib (C, H, W)\n#img_np = np.transpose(img_np, (1, 2, 0))\n\n# Display the image\nplt.imshow(img_tensor)\nplt.axis('off')  # Turn off axis\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-15T05:22:43.268268Z","iopub.execute_input":"2024-06-15T05:22:43.268658Z","iopub.status.idle":"2024-06-15T05:22:43.392489Z","shell.execute_reply.started":"2024-06-15T05:22:43.268578Z","shell.execute_reply":"2024-06-15T05:22:43.391457Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null}]}