{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"**deepfake video classification**\napprox 300 deepfake video and 300 real video\nfirst we pick up fake video and read each video frame by frame\nwe use mtcnn to detect the faces andposition of the faces through bounding box\n\nbounding box consist 4 thimngs\n1. top left corner (x,y)\n2. width and height","metadata":{}},{"cell_type":"code","source":"!wget https://lp-prod-resources.s3.amazonaws.com/other/detectingdeepfakes/VidTIMIT.zip","metadata":{"execution":{"iopub.status.busy":"2023-09-17T15:56:17.983529Z","iopub.execute_input":"2023-09-17T15:56:17.983969Z","iopub.status.idle":"2023-09-17T15:57:05.507626Z","shell.execute_reply.started":"2023-09-17T15:56:17.983896Z","shell.execute_reply":"2023-09-17T15:57:05.506350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget https://zenodo.org/record/4068245/files/DeepfakeTIMIT.tar.gz?download=1","metadata":{"execution":{"iopub.status.busy":"2023-09-17T15:57:05.510043Z","iopub.execute_input":"2023-09-17T15:57:05.510499Z","iopub.status.idle":"2023-09-17T15:57:28.603390Z","shell.execute_reply.started":"2023-09-17T15:57:05.510457Z","shell.execute_reply":"2023-09-17T15:57:28.602465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!tar -xvf /kaggle/working/DeepfakeTIMIT.tar.gz?download=1","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-09-17T15:57:28.604828Z","iopub.execute_input":"2023-09-17T15:57:28.605303Z","iopub.status.idle":"2023-09-17T15:57:30.135929Z","shell.execute_reply.started":"2023-09-17T15:57:28.605263Z","shell.execute_reply":"2023-09-17T15:57:30.134646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip /kaggle/working/VidTIMIT.zip","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-09-17T15:57:30.138771Z","iopub.execute_input":"2023-09-17T15:57:30.139142Z","iopub.status.idle":"2023-09-17T15:57:45.419980Z","shell.execute_reply.started":"2023-09-17T15:57:30.139112Z","shell.execute_reply":"2023-09-17T15:57:45.418731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install mtcnn","metadata":{"execution":{"iopub.status.busy":"2023-09-17T15:57:45.421569Z","iopub.execute_input":"2023-09-17T15:57:45.421896Z","iopub.status.idle":"2023-09-17T15:58:02.543627Z","shell.execute_reply.started":"2023-09-17T15:57:45.421868Z","shell.execute_reply":"2023-09-17T15:58:02.542577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\nfrom pathlib import Path\nimport numpy as np\nimport cv2\nfrom mtcnn import MTCNN\nimport time\nimport shutil\nimport tensorflow as tf\nfrom concurrent.futures import ProcessPoolExecutor","metadata":{"execution":{"iopub.status.busy":"2023-09-17T16:26:34.564562Z","iopub.execute_input":"2023-09-17T16:26:34.565092Z","iopub.status.idle":"2023-09-17T16:26:34.575658Z","shell.execute_reply.started":"2023-09-17T16:26:34.565058Z","shell.execute_reply":"2023-09-17T16:26:34.574743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# we have to seperate the data into real and fake catagory ","metadata":{}},{"cell_type":"code","source":"# making a folder where the fake and real videos of training data exists\n!mkdir -p ./Training\\ Videos/preproc_real_vids\n!mkdir -p ./Training\\ Videos/preproc_fake_vids\n\n\n# making a folder where the fake and real videos of testing data exists\n!mkdir -p ./Testing\\ Videos/preproc_real_vids\n!mkdir -p ./Testing\\ Videos/preproc_fake_vids\n","metadata":{"execution":{"iopub.status.busy":"2023-09-17T15:58:13.253790Z","iopub.execute_input":"2023-09-17T15:58:13.254445Z","iopub.status.idle":"2023-09-17T15:58:17.673956Z","shell.execute_reply.started":"2023-09-17T15:58:13.254412Z","shell.execute_reply":"2023-09-17T15:58:17.672254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# below folder contain the extra folders in VidTIMIT\n!mkdir ./Remaining\\ Real\\ Videos","metadata":{"execution":{"iopub.status.busy":"2023-09-17T16:14:27.317417Z","iopub.execute_input":"2023-09-17T16:14:27.317904Z","iopub.status.idle":"2023-09-17T16:14:28.454195Z","shell.execute_reply.started":"2023-09-17T16:14:27.317869Z","shell.execute_reply":"2023-09-17T16:14:28.452566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# we made a list containing the paths of the folder that itself contain the videos\nreal_fake_vids_paths = [\"./DeepfakeTIMIT/higher_quality\", \"./VidTIMIT\"]","metadata":{"execution":{"iopub.status.busy":"2023-09-17T15:58:17.676266Z","iopub.execute_input":"2023-09-17T15:58:17.676794Z","iopub.status.idle":"2023-09-17T15:58:17.684891Z","shell.execute_reply.started":"2023-09-17T15:58:17.676746Z","shell.execute_reply":"2023-09-17T15:58:17.683529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Bhai, 3 for loops waale code likhey huey hain. Pehla for loop to yeh dikhane ke liye hai ki deepfake and vidtimit waale folder mien kitney videos hain. Jiska answer deepfake waale ke liye 320 aayega and real ke liye 430 aayega.\n# \n# Uske baad second for loop woh hai jo real waale mien se 11 folders ko move karega to a new folder called Remaining Videos.\n# \n# And teesra for loop again dobara se yeh check karne ke liye hai ki kya move honey ke baad deepfake and vidtimit waale folders mien equal 320 videos bachey hain ki nahin.","metadata":{}},{"cell_type":"code","source":"# we will count the total number of videos that are available in these folders\n\n# this loop will iterate over folders\nfor base_vid_path in real_fake_vids_paths:\n    \n    vid_counter = 0\n    \n#   this loop will iterate over each video in each folder\n    for single_vid_path in Path(base_vid_path).glob(\"*/*.avi\"):\n        \n        vid_counter += 1\n    \n    print(f\" total number of videos are :: {format(vid_counter)}\")\n    \n    \n#      this line resets the vid_counter variable to zero before \n#      moving on to the next directory specified in the real_fake_vids_paths list.\n    vid_counter = 0","metadata":{"execution":{"iopub.status.busy":"2023-09-17T15:58:17.686731Z","iopub.execute_input":"2023-09-17T15:58:17.687152Z","iopub.status.idle":"2023-09-17T15:58:17.711652Z","shell.execute_reply.started":"2023-09-17T15:58:17.687116Z","shell.execute_reply":"2023-09-17T15:58:17.710366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vid_counter = 0\nfor single_vid_path in Path(real_fake_vids_paths[1]).glob(\"*/*.avi\"):\n    \n#     single_vid_path.parts[-2] is the name of the folder (mdld0, fdrd1, etc)\n    if str(single_vid_path.parts[-2]) in os.listdir(real_fake_vids_paths[0]):\n#         for loop me -> (DeepfakeTIMIT) iterate kr rhe h aur compare kr rhe h -> real_fake_vids_paths[0](VidTIMIT)\n        vid_counter+=1\n    else:\n#         agar Remaining Real Videos me agar \"single_vid_path.parts[-2]\" folder nhi h toh\n        if not os.path.isdir(\"./Remaining Real Videos/\"+single_vid_path.parts[-2]):\n#         toh bano do\n            os.mkdir(\"./Remaining Real Videos/\"+single_vid_path.parts[-2])\n        shutil.move(src= single_vid_path, dst=\"./Remaining Real Videos/\"+\"/\".join([single_vid_path.parts[-2], single_vid_path.parts[-1]]))\n        \n        \n            \nprint(\"Total Number of Videos are {}\".format(vid_counter))","metadata":{"execution":{"iopub.status.busy":"2023-09-17T16:40:42.848488Z","iopub.execute_input":"2023-09-17T16:40:42.849151Z","iopub.status.idle":"2023-09-17T16:40:42.889573Z","shell.execute_reply.started":"2023-09-17T16:40:42.849108Z","shell.execute_reply":"2023-09-17T16:40:42.888230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for base_vid_path in real_fake_vids_paths:\n    \n    vid_counter = 0\n    \n#   this loop will iterate over each video in each folder\n    for single_vid_path in Path(base_vid_path).glob(\"*/*.avi\"):\n        \n        vid_counter += 1\n    \n    print(f\" total number of videos are :: {format(vid_counter)}\")\n    ","metadata":{"execution":{"iopub.status.busy":"2023-09-17T16:40:47.715763Z","iopub.execute_input":"2023-09-17T16:40:47.717244Z","iopub.status.idle":"2023-09-17T16:40:47.730411Z","shell.execute_reply.started":"2023-09-17T16:40:47.717191Z","shell.execute_reply":"2023-09-17T16:40:47.729009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# we are done with some data filtering now we will crop face from the videos using mtcnn\n# fetching the right_eye, left_eye, etc if needed","metadata":{}},{"cell_type":"code","source":"# detecting faces\ndef detect_faces(frame):\n    face_detector=MTCNN()\n    face_attributes= face_detector.detect_faces(frame)\n    return face_attributes[0]['box'], face_attributes[0]['keypoints']['left_eye'], face_attributes[0]['keypoints']['right_eye'] \n    ","metadata":{"execution":{"iopub.status.busy":"2023-09-17T16:50:08.103574Z","iopub.execute_input":"2023-09-17T16:50:08.104355Z","iopub.status.idle":"2023-09-17T16:50:08.112099Z","shell.execute_reply.started":"2023-09-17T16:50:08.104306Z","shell.execute_reply":"2023-09-17T16:50:08.110625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preproc_single_vid(single_vid_path):\n    video=cv2.VideoCapture(str(single_vid_path))\n    while True:\n#         read() methods returns a tuple, first element is a bool \n#         and the second is frame\n        is_frame, frame = video.read()\n        \n        if not is_frame:\n            break\n        face_bbox, left_eye_kp, right_eye_kp=detect_faces(frame)\n        \n    print(\"Processed Video at {}\".format(single_vid_path))","metadata":{"execution":{"iopub.status.busy":"2023-09-17T16:58:19.119859Z","iopub.execute_input":"2023-09-17T16:58:19.121193Z","iopub.status.idle":"2023-09-17T16:58:19.129564Z","shell.execute_reply.started":"2023-09-17T16:58:19.121127Z","shell.execute_reply":"2023-09-17T16:58:19.128027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vid_counter = 0\nmb_number = 0\n\nreal_fake_preproc_vids_paths = [\"./Training Videos/preproc_fake_vids\", \"./Training Videos/preproc_real_vids\"]\n\nfor vids_path, preproc_vids_path in zip(real_fake_vids_paths, real_fake_preproc_vids_paths):\n    vids2preproc_path_list = list()\n    \n    for single_vid_path in pathlib.Path(vids_path).glob(\"*/*.avi\"):\n        \n        vids2preproc_path_list.append(single_vid_path)\n        vid_counter += 1\n        \n        if vid_counter % os.cpu_count() == 0:\n            start_time = time.time()\n            \n            with ProcessPoolExecutor(max_workers=os.cpu_count()) as pool:\n                pool.map(preproc_single_vid,vids2preproc_path_list)\n                \n            end_time = time.time()\n            \n            mb_number += 1\n            \n            elapsed_time = end_time - start_time\n            \n            print(\"\\nProcessed Mini Batch # {} of 64 Videos in {} seconds\\n\".format(mb_number,elapsed_time))\n            vids2preproc_path_list = list()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}