{"cells":[{"metadata":{},"cell_type":"markdown","source":"skript to compare real and fake face images from the first frame of the train videos\nwith inspiration from https://www.kaggle.com/robikscube/kaggle-deepfake-detection-introduction\nthanks rob mulla!"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"\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\nimport cv2\nfrom matplotlib import pyplot as plt\n\ntrain_dir = '/kaggle/input/deepfake-detection-challenge/train_sample_videos/'\nsample_submission = pd.read_csv(\"../input/deepfake-detection-challenge/sample_submission.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# download face_recognition\n! pip install face_recognition\nimport face_recognition as fr","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# find all fake videos\nmetadata = pd.read_json('/kaggle/input/deepfake-detection-challenge/train_sample_videos/metadata.json').T\nprint(metadata.head())\nprint(len(metadata))\n\nlist_fake_videos = list(metadata.loc[metadata['label']=='FAKE',:].index.values)\nprint(len(list_fake_videos))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#prepare plotting of ten videos and their first frame\nfig, axes = plt.subplots(10, 2, figsize=(15, 40))\naxes = np.array(axes)\naxes = axes.reshape(-1)\nax_ix = 0\nax_max = 20\n\n# choose from which videos to display the frames\nind0 = 0\nind1 = 100\npadding = 0\n\nfor vid in list_fake_videos[ind0:ind1]:\n    orig_vid = metadata.loc[vid,'original']\n    # check if original video exists in the directory, many do not exist\n    if not(os.path.isfile(train_dir + orig_vid) ):\n        #print(f'could not find real {orig_vid} for {vid}:')\n        continue\n    # image from fake video\n    cap = cv2.VideoCapture(train_dir + vid)\n    success, image = cap.read()\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    cap.release()\n    face_locations = fr.face_locations(image)\n    if len(face_locations) == 0:\n        print(f'Could not find face in {vid} FAKE')\n        continue\n    \n    top, right, bottom, left = face_locations[0] #first face only\n    image = image[top-padding:bottom+padding, left-padding:right+padding]\n    \n    # image from corresponding real video\n    cap = cv2.VideoCapture(train_dir + orig_vid)\n    success, orig_image = cap.read()\n    if not(success):\n        print(f'could capture in {orig_vid} for {vid}:')\n        continue\n    orig_image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB)\n    cap.release() \n    face_locations = fr.face_locations(orig_image)\n    if len(face_locations) == 0:\n        print(f'Could not find face in {orig_vid}')\n        continue\n    top, right, bottom, left = face_locations[0] #first face only\n    orig_image = orig_image[top-padding:bottom+padding, left-padding:right+padding]\n    # plot\n    \n    axes[ax_ix].imshow(image)\n    axes[ax_ix].xaxis.set_visible(False)\n    axes[ax_ix].yaxis.set_visible(False)\n    axes[ax_ix].set_title(f'{vid} FAKE')\n    ax_ix = ax_ix +1\n    \n    axes[ax_ix].imshow(orig_image)\n    axes[ax_ix].xaxis.set_visible(False)\n    axes[ax_ix].yaxis.set_visible(False)\n    axes[ax_ix].set_title(f'{orig_vid} REAL')\n    ax_ix = ax_ix +1\n    if ax_ix >=ax_max:\n        break\n\nplt.grid(False)\nplt.show()    ","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"","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}