{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"Frame_Extraction.ipynb","provenance":[],"authorship_tag":"ABX9TyMSlS7Is6EmAd3ZuV616ByC"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"code","metadata":{"id":"urjDI9Lw_4NV","executionInfo":{"status":"ok","timestamp":1638919357353,"user_tz":300,"elapsed":173,"user":{"displayName":"Siqiao Ruan","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GivK2G42DiY6v4AtE2BKdBplvdWBHnGBoiA4ngUig=s64","userId":"14688127135167048523"}}},"source":["import os\n","from os.path import join\n","import argparse\n","import subprocess\n","import cv2\n","from tqdm import tqdm\n","\n","\n","DATASET_PATHS = {\n"," 'original': 'original_sequences',\n"," 'Deepfakes': 'manipulated_sequences/Deepfakes',\n"," 'Face2Face': 'manipulated_sequences/Face2Face',\n"," 'FaceSwap': 'manipulated_sequences/FaceSwap',\n"," 'DeepFakeDetection': 'manipulated_sequences/DeepFakeDetection',\n"," 'DeepFakeDetection_original': 'original_sequences/actors'\n","}\n","COMPRESSION = ['c0', 'c23', 'c40']"],"execution_count":6,"outputs":[]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"PQzsfZcDVL-K","executionInfo":{"status":"ok","timestamp":1638919358359,"user_tz":300,"elapsed":827,"user":{"displayName":"Siqiao Ruan","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GivK2G42DiY6v4AtE2BKdBplvdWBHnGBoiA4ngUig=s64","userId":"14688127135167048523"}},"outputId":"bad6398b-b11c-426f-c183-e95de9388dc2"},"source":["# mount to drive\n","from google.colab import drive\n","drive.mount('/content/drive')"],"execution_count":7,"outputs":[{"output_type":"stream","name":"stdout","text":["Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n"]}]},{"cell_type":"code","metadata":{"id":"VvIfLxf2VQxR","executionInfo":{"status":"ok","timestamp":1638919377336,"user_tz":300,"elapsed":179,"user":{"displayName":"Siqiao Ruan","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GivK2G42DiY6v4AtE2BKdBplvdWBHnGBoiA4ngUig=s64","userId":"14688127135167048523"}}},"source":["# change working directory\n","os.chdir('/content/drive/MyDrive/AM205_FinalProject/')"],"execution_count":9,"outputs":[]},{"cell_type":"code","metadata":{"id":"KUGiPHUoVXZC","executionInfo":{"status":"ok","timestamp":1638919380897,"user_tz":300,"elapsed":159,"user":{"displayName":"Siqiao Ruan","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GivK2G42DiY6v4AtE2BKdBplvdWBHnGBoiA4ngUig=s64","userId":"14688127135167048523"}}},"source":["def extract_frames(data_path, output_path, method='cv2', interval=100):\n"," \"\"\"Method to extract frames, either with ffmpeg or opencv. FFmpeg won't\n"," start from 0 so we would have to rename if we want to keep the filenames\n"," coherent.\"\"\"\n"," os.makedirs(output_path, exist_ok=True)\n"," if method == 'ffmpeg':\n"," subprocess.check_output(\n"," 'ffmpeg -i {} {}'.format(\n"," data_path, join(output_path, '%04d.png')),\n"," shell=True, stderr=subprocess.STDOUT)\n"," elif method == 'cv2':\n"," reader = cv2.VideoCapture(data_path)\n"," frame_num = 0\n"," i = 0\n"," while reader.isOpened():\n"," success, image = reader.read()\n"," if not success:\n"," break\n"," if i % interval == 0:\n"," cv2.imwrite(join(output_path, '{:04d}.png'.format(frame_num)), image)\n"," frame_num += 1\n"," i = i + 1\n"," reader.release()\n"," else:\n"," raise Exception('Wrong extract frames method: {}'.format(method))\n","\n","\n","def extract_method_videos(data_path, dataset, compression):\n"," \"\"\"Extracts all videos of a specified method and compression in the\n"," FaceForensics++ file structure\"\"\"\n"," videos_path = join(data_path, DATASET_PATHS[dataset], compression, 'videos')\n"," images_path = join(data_path, DATASET_PATHS[dataset], compression, 'images')\n"," for video in tqdm(os.listdir(videos_path)):\n"," image_folder = video.split('.')[0]\n"," extract_frames(join(videos_path, video),\n"," join(images_path, image_folder))\n"],"execution_count":10,"outputs":[]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"MVZudT2kVcVb","outputId":"1fe0dcb0-f597-47b7-ea8e-4add2a01aa00"},"source":["from argparse import Namespace\n","p = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter )\n","p.add_argument('--data_path', type=str)\n","p.add_argument('--dataset', '-d', type=str,\n"," choices=list(DATASET_PATHS.keys()) + ['all'],\n"," default='all')\n","p.add_argument('--compression', '-c', type=str, choices=COMPRESSION,\n"," default='c0')\n","\n","#args = Namespace(data_path='/content/drive/My Drive/AM205_FinalProject/data_videos', dataset='DeepFakeDetection_original', compression='c40')\n","args = Namespace(data_path='/content/drive/My Drive/AM205_FinalProject/data_videos', dataset='DeepFakeDetection', compression='c40')\n","\n","if args.dataset == 'all':\n"," for dataset in DATASET_PATHS.keys():\n"," args.dataset = dataset\n"," extract_method_videos(**vars(args))\n","else:\n"," extract_method_videos(**vars(args))"],"execution_count":null,"outputs":[{"output_type":"stream","name":"stderr","text":[" 85%|████████▍ | 2606/3068 [5:42:00<1:00:04, 7.80s/it]"]}]},{"cell_type":"code","metadata":{"id":"b8vWbaycWY4k","executionInfo":{"status":"aborted","timestamp":1638919358553,"user_tz":300,"elapsed":201,"user":{"displayName":"Siqiao Ruan","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GivK2G42DiY6v4AtE2BKdBplvdWBHnGBoiA4ngUig=s64","userId":"14688127135167048523"}}},"source":[""],"execution_count":null,"outputs":[]}]}