{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Packages","execution_count":null},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \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 \"../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\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n\nimport matplotlib.pylab as plt\nimport cv2\nplt.style.use('ggplot')\nfrom IPython.display import Video\nfrom IPython.display import HTML\nfrom tqdm import tqdm\n\nfrom imblearn.under_sampling import RandomUnderSampler\nfrom sklearn.preprocessing import MinMaxScaler, Imputer\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import Imputer\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Importing stuff","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train_dir = '/kaggle/input/deepfake-detection-challenge/train_sample_videos/'\ntrain_video_files = [train_dir + x for x in os.listdir(train_dir)]\ntest_dir = '/kaggle/input/deepfake-detection-challenge/test_videos/' \ntest_video_files = [test_dir + x for x in os.listdir(test_dir)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train = pd.read_json('/kaggle/input/deepfake-detection-challenge/train_sample_videos/metadata.json').transpose()\ndf_train.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Looking at the data","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.groupby('label')['label'].count().plot(figsize=(15, 5), kind='bar', title='Distribution of Labels in the Training Set')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train['label'].value_counts(normalize=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"target = []\n\nfor i in df_train['label']:\n    if i == 'REAL':\n        target.append(1)\n    else:\n        target.append(0)\n        \ndf_train['Target'] = target","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Real == 1 Fake == 0\ndf_train.drop(['label', 'split'], axis=1, inplace=True)\ndf_train.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Getting the frames","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"Getting a single frame from a single video","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import cv2 as cv\n\ndef display_image_from_video(video_path):\n    '''\n    input: video_path - path for video\n    process:\n    1. perform a video capture from the video\n    2. read the image\n    3. display the image\n    '''\n    capture_image = cv.VideoCapture(video_path) \n    ret, frame = capture_image.read()\n    fig = plt.figure(figsize=(10,10))\n    ax = fig.add_subplot(111)\n    frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)\n    return    ax.imshow(frame)    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"display_image_from_video('/kaggle/input/deepfake-detection-challenge/train_sample_videos/ebebgmtlcu.mp4')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Trying to get several frames from the same video and failing :(","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def framecap(path):    \n    cam = cv2.VideoCapture(path) \n\n    try: \n\n        # creating a folder named data \n        if not os.path.exists('/kaggle/input/deepfake-detection-challenge/train_images'): \n            os.makedirs('/kaggle/input/deepfake-detection-challenge/train_images') \n\n    # if not created then raise error \n    except OSError: \n        print ('Error: Creating directory of data') \n\n    # frame \n    currentframe = 0\n\n    while(True): \n\n        # reading from frame \n        ret,frame = cam.read() \n\n        if ret: \n            # if video is still left continue creating images \n            name = './kaggle/input/deepfake-detection-challenge/train_images' + str(currentframe) + '.jpg'\n            print ('Creating...' + name) \n\n            # writing the extracted images \n            cv2.imwrite(name, frame) \n\n            # increasing counter so that it will \n            # show how many frames are created \n            currentframe += 1\n        else: \n            break\n\n    # Release all space and windows once done \n    cam.release() \n    cv2.destroyAllWindows() ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"framecap('/kaggle/input/deepfake-detection-challenge/train_sample_videos/ebebgmtlcu.mp4')","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":4}