{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"!pip install '/kaggle/input/dfdc-packages/dlib-19.19.0/dlib-19.19.0' \n!pip install '/kaggle/input/dfdc-packages/face_recognition_models-0.3.0/face_recognition_models-0.3.0'\n!pip install '/kaggle/input/dfdc-packages/face_recognition-1.2.3-py2.py3-none-any.whl'","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"#########################################################################################################################\n## Starting Script\n#########################################################################################################################\n\n## Imports\nimport time\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport face_recognition\nimport dlib\nimport os\nimport cv2\nimport multiprocessing as mp\nimport gc ## important for controlling memory usage\nfrom sklearn.model_selection import train_test_split\n\n## Confirm GPU compilation\nprint(dlib.DLIB_USE_CUDA)\nprint(dlib.cuda.get_num_devices())\n\n## Procedures\ndef read_images(vf):\n    cap = cv2.VideoCapture(vf)\n    print(\"## Reading frames\")\n    frames = []\n    while cap.isOpened():\n        success, image = cap.read()\n\n        if not success:\n            break\n\n        image = image[:, :, ::-1]\n        frames.append(image)\n\n    return frames\n\n\nif __name__ == \"__main__\":\n    data_folder = \"/kaggle/input/deepfake-detection-challenge\"\n    train_folder = os.path.join(data_folder, \"train_sample_videos\")\n    meta_file = os.path.join(train_folder, \"metadata.json\")\n    \n    print(\"## Reading meta data\")\n    meta_df = pd.read_json(meta_file).T\n    \n    ## Take only 2 files\n    file_list = meta_df.index.tolist()[:2]\n\n    for f in file_list:\n        print(\"## Starting on {}\".format(f))\n        fp = os.path.join(train_folder, f)\n\n        t0 = time.time()\n        frames = read_images(fp)\n        print(\"## Reading {:d} frames took: {:f}\".format(len(frames), time.time() - t0))\n\n        gc.collect()\n        bsz = 30\n        t0 = time.time()\n        all_face_locations = []\n        for i in range(0,len(frames), bsz):\n            batch_frames = frames[i:i+bsz]\n            batch_of_face_locations = face_recognition.batch_face_locations(batch_frames, number_of_times_to_upsample=0)\n            all_face_locations.extend(batch_of_face_locations)\n        print(\"## GPU Getting faces frames {:d} from frames took: {:f}\".format(len(all_face_locations), time.time() - t0))\n        del all_face_locations\n        del frames\n        gc.collect()\n        print(\"## Done {}\".format(f))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}