{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install moviepy --quiet\nimport os\nimport json\nfrom glob import glob\nimport numpy as np\nfrom skimage.io import imsave\nfrom skimage.transform import resize\nfrom tqdm import tqdm_notebook\nfrom moviepy.editor import VideoFileClip\nfrom concurrent.futures import ProcessPoolExecutor\nimport warnings\nwarnings.filterwarnings(\"ignore\", category=FutureWarning)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"INPUT_DIR = '/kaggle/input/deepfake-detection-challenge'\nEXTRACT_EVERY = 5 # will save every 5th frame\nPROCESSED_DATA_DIR = '/kaggle/working/processed_data'\nH, W = 224, 224","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"if not os.path.exists(PROCESSED_DATA_DIR):\n    os.mkdir(PROCESSED_DATA_DIR)\n    os.mkdir(os.path.join(PROCESSED_DATA_DIR, 'real'))\n    os.mkdir(os.path.join(PROCESSED_DATA_DIR, 'fake'))\n\nwith open(INPUT_DIR + '/train_sample_videos/metadata.json', 'r') as f:\n    metadata = json.load(f)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def process_video_file(video_filepath):\n    with VideoFileClip(filename=video_filepath, audio=False) as clip:\n        fname = video_filepath.split('/')[-1]\n        label = metadata[fname]['label'].lower()\n        output_dir = os.path.join(PROCESSED_DATA_DIR, label, fname.split('.')[0])\n        os.mkdir(output_dir)\n        for i, frame in enumerate(clip.iter_frames()):\n            if (i+1) % EXTRACT_EVERY == 0:\n                output_path = os.path.join(output_dir, '{}.jpeg'.format(i+1))\n                resized_frame = np.uint8(resize(frame, [H, W]) * 255)\n                imsave(output_path, resized_frame, check_contrast=False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"> #### Processing 5 sample videos"},{"metadata":{"trusted":true},"cell_type":"code","source":"video_paths = glob(INPUT_DIR + '/train_sample_videos/*.mp4')[:5]\nwith ProcessPoolExecutor(max_workers=4) as ex:\n    processed = 0\n    for _ in tqdm_notebook(ex.map(process_video_file, video_paths), total=len(video_paths)):\n        print('Done processing {} videos'.format(processed))\n        processed += 1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"glob(\"/kaggle/working/processed_data/real/*/\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"glob(\"/kaggle/working/processed_data/fake/*/\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"glob(\"/kaggle/working/processed_data/fake/*/*\")[:20]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.8"},"widgets":{"application/vnd.jupyter.widget-state+json":{"state":{"02722b7b4e89427cbfe1f05c96cc254a":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"HTMLModel","state":{"layout":"IPY_MODEL_072ad62fa9dd45e49bdc3e02c90bc89d","style":"IPY_MODEL_279033a4dcfd4747ac10d9c563862ebd","value":" 0/401 [00:02&lt;?, ?it/s]"}},"072ad62fa9dd45e49bdc3e02c90bc89d":{"model_module":"@jupyter-widgets/base","model_module_version":"1.2.0","model_name":"LayoutModel","state":{}},"279033a4dcfd4747ac10d9c563862ebd":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"DescriptionStyleModel","state":{"description_width":""}},"2ff12562c99240998365a5de8f7f927c":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"IntProgressModel","state":{"bar_style":"success","description":"100%","layout":"IPY_MODEL_8340d25321564edb8be4f3aa04b67e3d","max":400,"style":"IPY_MODEL_e432ffe2efc14ebeb0e261fb02f66973","value":400}},"3b046664cb1c4c229f8fe4f7d7dc7fbb":{"model_module":"@jupyter-widgets/base","model_module_version":"1.2.0","model_name":"LayoutModel","state":{}},"4701a08adcc14c0193b44ebb23dc8f8a":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"HBoxModel","state":{"children":["IPY_MODEL_2ff12562c99240998365a5de8f7f927c","IPY_MODEL_6a450205a2de438a959ea7b595af159b"],"layout":"IPY_MODEL_b5879637364f42aea00b6ad1646a7e41"}},"620ae2c932bb46a382fad08e6c7da947":{"model_module":"@jupyter-widgets/base","model_module_version":"1.2.0","model_name":"LayoutModel","state":{}},"6a450205a2de438a959ea7b595af159b":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"HTMLModel","state":{"layout":"IPY_MODEL_3b046664cb1c4c229f8fe4f7d7dc7fbb","style":"IPY_MODEL_88a60cc5e5764702b6d51776222d1471","value":" 400/400 [09:36&lt;00:00,  1.44s/it]"}},"7219d489b05e43b8b233560029912c75":{"model_module":"@jupyter-widgets/base","model_module_version":"1.2.0","model_name":"LayoutModel","state":{}},"81733436d3424692885d600dbd7eea7e":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"ProgressStyleModel","state":{"description_width":"initial"}},"8340d25321564edb8be4f3aa04b67e3d":{"model_module":"@jupyter-widgets/base","model_module_version":"1.2.0","model_name":"LayoutModel","state":{}},"88a60cc5e5764702b6d51776222d1471":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"DescriptionStyleModel","state":{"description_width":""}},"a433d104f3a54d4ca0d5a22fb01a2602":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"IntProgressModel","state":{"bar_style":"danger","description":"  0%","layout":"IPY_MODEL_620ae2c932bb46a382fad08e6c7da947","max":401,"style":"IPY_MODEL_81733436d3424692885d600dbd7eea7e"}},"b5879637364f42aea00b6ad1646a7e41":{"model_module":"@jupyter-widgets/base","model_module_version":"1.2.0","model_name":"LayoutModel","state":{}},"cdcb056e4c984f5fb7bad14e0a8fff16":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"HBoxModel","state":{"children":["IPY_MODEL_a433d104f3a54d4ca0d5a22fb01a2602","IPY_MODEL_02722b7b4e89427cbfe1f05c96cc254a"],"layout":"IPY_MODEL_7219d489b05e43b8b233560029912c75"}},"e432ffe2efc14ebeb0e261fb02f66973":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"ProgressStyleModel","state":{"description_width":"initial"}}},"version_major":2,"version_minor":0}}},"nbformat":4,"nbformat_minor":1}