{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":16880,"databundleVersionId":858837,"sourceType":"competition"},{"sourceId":854304,"sourceType":"datasetVersion","datasetId":452468}],"dockerImageVersionId":29844,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Load packages","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport matplotlib\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm_notebook\n%matplotlib inline \nimport cv2 as cv","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:18:53.516382Z","iopub.execute_input":"2023-12-23T19:18:53.517088Z","iopub.status.idle":"2023-12-23T19:18:56.362304Z","shell.execute_reply.started":"2023-12-23T19:18:53.517024Z","shell.execute_reply":"2023-12-23T19:18:56.361186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load data","metadata":{}},{"cell_type":"code","source":"DATA_FOLDER = '/kaggle/input/deepfake-detection-challenge'\nTRAIN_SAMPLE_FOLDER = '/kaggle/input/deepfake-detection-challenge/train_sample_videos'\nTEST_FOLDER = '/kaggle/input/deepfake-detection-challenge/test_videos'\n\nprint(f\"Train samples: {len(os.listdir(os.path.join(DATA_FOLDER, TRAIN_SAMPLE_FOLDER)))}\")\nprint(f\"Test samples: {len(os.listdir(os.path.join(DATA_FOLDER, TEST_FOLDER)))}\")","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:18:59.364617Z","iopub.execute_input":"2023-12-23T19:18:59.365036Z","iopub.status.idle":"2023-12-23T19:18:59.713006Z","shell.execute_reply.started":"2023-12-23T19:18:59.364949Z","shell.execute_reply":"2023-12-23T19:18:59.711812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FACE_DETECTION_FOLDER = '/kaggle/input/haar-cascades-for-face-detection'\nprint(f\"Face detection resources: {os.listdir(FACE_DETECTION_FOLDER)}\")","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:19:31.706443Z","iopub.execute_input":"2023-12-23T19:19:31.706835Z","iopub.status.idle":"2023-12-23T19:19:31.728265Z","shell.execute_reply.started":"2023-12-23T19:19:31.706782Z","shell.execute_reply":"2023-12-23T19:19:31.727183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Check files type\n\nHere we check the train data files extensions. Most of the files looks to have `mp4` extension, let's check if there is other extension as well.","metadata":{}},{"cell_type":"code","source":"train_list = list(os.listdir(os.path.join(DATA_FOLDER, TRAIN_SAMPLE_FOLDER)))\next_dict = []\nfor file in train_list:\n    file_ext = file.split('.')[1]\n    if (file_ext not in ext_dict):\n        ext_dict.append(file_ext)\nprint(f\"Extensions: {ext_dict}\")      ","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:19:38.250422Z","iopub.execute_input":"2023-12-23T19:19:38.251031Z","iopub.status.idle":"2023-12-23T19:19:38.263187Z","shell.execute_reply.started":"2023-12-23T19:19:38.250945Z","shell.execute_reply":"2023-12-23T19:19:38.261816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for file_ext in ext_dict:\n    print(f\"Files with extension `{file_ext}`: {len([file for file in train_list if  file.endswith(file_ext)])}\")","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:19:42.695048Z","iopub.execute_input":"2023-12-23T19:19:42.695736Z","iopub.status.idle":"2023-12-23T19:19:42.703273Z","shell.execute_reply.started":"2023-12-23T19:19:42.695367Z","shell.execute_reply":"2023-12-23T19:19:42.702080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_list = list(os.listdir(os.path.join(DATA_FOLDER, TEST_FOLDER)))\next_dict = []\nfor file in test_list:\n    file_ext = file.split('.')[1]\n    if (file_ext not in ext_dict):\n        ext_dict.append(file_ext)\nprint(f\"Extensions: {ext_dict}\")\nfor file_ext in ext_dict:\n    print(f\"Files with extension `{file_ext}`: {len([file for file in train_list if  file.endswith(file_ext)])}\")","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:19:48.994238Z","iopub.execute_input":"2023-12-23T19:19:48.994684Z","iopub.status.idle":"2023-12-23T19:19:49.006456Z","shell.execute_reply.started":"2023-12-23T19:19:48.994612Z","shell.execute_reply":"2023-12-23T19:19:49.005452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's check the `json` file first.","metadata":{}},{"cell_type":"code","source":"json_file = [file for file in train_list if  file.endswith('json')][0]\nprint(f\"JSON file: {json_file}\")","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:19:52.828815Z","iopub.execute_input":"2023-12-23T19:19:52.829412Z","iopub.status.idle":"2023-12-23T19:19:52.835950Z","shell.execute_reply.started":"2023-12-23T19:19:52.829359Z","shell.execute_reply":"2023-12-23T19:19:52.835126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_meta_from_json(path):\n    df = pd.read_json(os.path.join(DATA_FOLDER, path, json_file))\n    df = df.T\n    return df\n\nmeta_train_df = get_meta_from_json(TRAIN_SAMPLE_FOLDER)\nmeta_train_df.head()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:19:56.680297Z","iopub.execute_input":"2023-12-23T19:19:56.680800Z","iopub.status.idle":"2023-12-23T19:19:57.477889Z","shell.execute_reply.started":"2023-12-23T19:19:56.680754Z","shell.execute_reply":"2023-12-23T19:19:57.476955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n\n# Missing data\n\n","metadata":{}},{"cell_type":"code","source":"def missing_data(data):\n    total = data.isnull().sum()\n    percent = (data.isnull().sum()/data.isnull().count()*100)\n    tt = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])\n    types = []\n    for col in data.columns:\n        dtype = str(data[col].dtype)\n        types.append(dtype)\n    tt['Types'] = types\n    return(np.transpose(tt))","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:20:06.659942Z","iopub.execute_input":"2023-12-23T19:20:06.660321Z","iopub.status.idle":"2023-12-23T19:20:06.669335Z","shell.execute_reply.started":"2023-12-23T19:20:06.660269Z","shell.execute_reply":"2023-12-23T19:20:06.668503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_data(meta_train_df)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:20:08.411461Z","iopub.execute_input":"2023-12-23T19:20:08.412005Z","iopub.status.idle":"2023-12-23T19:20:08.545485Z","shell.execute_reply.started":"2023-12-23T19:20:08.411922Z","shell.execute_reply":"2023-12-23T19:20:08.544360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_data(meta_train_df.loc[meta_train_df.label=='REAL'])","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:20:14.223646Z","iopub.execute_input":"2023-12-23T19:20:14.224190Z","iopub.status.idle":"2023-12-23T19:20:14.243573Z","shell.execute_reply.started":"2023-12-23T19:20:14.223949Z","shell.execute_reply":"2023-12-23T19:20:14.242604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def unique_values(data):\n    total = data.count()\n    tt = pd.DataFrame(total)\n    tt.columns = ['Total']\n    uniques = []\n    for col in data.columns:\n        unique = data[col].nunique()\n        uniques.append(unique)\n    tt['Uniques'] = uniques\n    return(np.transpose(tt))","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:20:19.897471Z","iopub.execute_input":"2023-12-23T19:20:19.897832Z","iopub.status.idle":"2023-12-23T19:20:19.905304Z","shell.execute_reply.started":"2023-12-23T19:20:19.897772Z","shell.execute_reply":"2023-12-23T19:20:19.904097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_values(meta_train_df)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:20:22.144601Z","iopub.execute_input":"2023-12-23T19:20:22.145163Z","iopub.status.idle":"2023-12-23T19:20:22.161942Z","shell.execute_reply.started":"2023-12-23T19:20:22.144901Z","shell.execute_reply":"2023-12-23T19:20:22.160757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Most frequent originals\n","metadata":{}},{"cell_type":"code","source":"def most_frequent_values(data):\n    total = data.count()\n    tt = pd.DataFrame(total)\n    tt.columns = ['Total']\n    items = []\n    vals = []\n    for col in data.columns:\n        itm = data[col].value_counts().index[0]\n        val = data[col].value_counts().values[0]\n        items.append(itm)\n        vals.append(val)\n    tt['Most frequent item'] = items\n    tt['Frequence'] = vals\n    tt['Percent from total'] = np.round(vals / total * 100, 3)\n    return(np.transpose(tt))","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:20:29.717291Z","iopub.execute_input":"2023-12-23T19:20:29.717644Z","iopub.status.idle":"2023-12-23T19:20:29.727412Z","shell.execute_reply.started":"2023-12-23T19:20:29.717593Z","shell.execute_reply":"2023-12-23T19:20:29.726228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"most_frequent_values(meta_train_df)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:20:32.135646Z","iopub.execute_input":"2023-12-23T19:20:32.136031Z","iopub.status.idle":"2023-12-23T19:20:32.164859Z","shell.execute_reply.started":"2023-12-23T19:20:32.135959Z","shell.execute_reply":"2023-12-23T19:20:32.163871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_count(feature, title, df, size=1):\n \n    f, ax = plt.subplots(1,1, figsize=(4*size,4))\n    total = float(len(df))\n    g = sns.countplot(df[feature], order = df[feature].value_counts().index[:20], palette='Set3')\n    g.set_title(\"Number and percentage of {}\".format(title))\n    if(size > 2):\n        plt.xticks(rotation=90, size=8)\n    for p in ax.patches:\n        height = p.get_height()\n        ax.text(p.get_x()+p.get_width()/2.,\n                height + 3,\n                '{:1.2f}%'.format(100*height/total),\n                ha=\"center\") \n    plt.show()    ","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:20:39.200511Z","iopub.execute_input":"2023-12-23T19:20:39.200913Z","iopub.status.idle":"2023-12-23T19:20:39.212550Z","shell.execute_reply.started":"2023-12-23T19:20:39.200836Z","shell.execute_reply":"2023-12-23T19:20:39.211634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_count('split', 'split (train)', meta_train_df)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:20:42.983259Z","iopub.execute_input":"2023-12-23T19:20:42.983675Z","iopub.status.idle":"2023-12-23T19:20:43.243370Z","shell.execute_reply.started":"2023-12-23T19:20:42.983608Z","shell.execute_reply":"2023-12-23T19:20:43.242013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_count('label', 'label (train)', meta_train_df)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:20:45.301198Z","iopub.execute_input":"2023-12-23T19:20:45.301688Z","iopub.status.idle":"2023-12-23T19:20:45.547511Z","shell.execute_reply.started":"2023-12-23T19:20:45.301641Z","shell.execute_reply":"2023-12-23T19:20:45.546026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n\n# Missing video (or meta) data\n\n\n","metadata":{}},{"cell_type":"code","source":"meta = np.array(list(meta_train_df.index))\nstorage = np.array([file for file in train_list if  file.endswith('mp4')])\nprint(f\"Metadata: {meta.shape[0]}, Folder: {storage.shape[0]}\")\nprint(f\"Files in metadata and not in folder: {np.setdiff1d(meta,storage,assume_unique=False).shape[0]}\")\nprint(f\"Files in folder and not in metadata: {np.setdiff1d(storage,meta,assume_unique=False).shape[0]}\")","metadata":{"execution":{"iopub.status.busy":"2023-12-23T19:21:04.592218Z","iopub.execute_input":"2023-12-23T19:21:04.592726Z","iopub.status.idle":"2023-12-23T19:21:04.604261Z","shell.execute_reply.started":"2023-12-23T19:21:04.592677Z","shell.execute_reply":"2023-12-23T19:21:04.603256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Few fake videos","metadata":{}},{"cell_type":"code","source":"fake_train_sample_video = list(meta_train_df.loc[meta_train_df.label=='FAKE'].sample(3).index)\nfake_train_sample_video","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:21:21.261017Z","iopub.execute_input":"2023-12-23T19:21:21.261471Z","iopub.status.idle":"2023-12-23T19:21:21.274764Z","shell.execute_reply.started":"2023-12-23T19:21:21.261410Z","shell.execute_reply":"2023-12-23T19:21:21.273508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_image_from_video(video_path):\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    ax.imshow(frame)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:21:26.938967Z","iopub.execute_input":"2023-12-23T19:21:26.939332Z","iopub.status.idle":"2023-12-23T19:21:26.946835Z","shell.execute_reply.started":"2023-12-23T19:21:26.939281Z","shell.execute_reply":"2023-12-23T19:21:26.945878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for video_file in fake_train_sample_video:\n    display_image_from_video(os.path.join(DATA_FOLDER, TRAIN_SAMPLE_FOLDER, video_file))","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:21:28.694073Z","iopub.execute_input":"2023-12-23T19:21:28.694621Z","iopub.status.idle":"2023-12-23T19:21:30.863901Z","shell.execute_reply.started":"2023-12-23T19:21:28.694560Z","shell.execute_reply":"2023-12-23T19:21:30.863037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Few real videos","metadata":{}},{"cell_type":"code","source":"real_train_sample_video = list(meta_train_df.loc[meta_train_df.label=='REAL'].sample(3).index)\nreal_train_sample_video","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:21:40.347611Z","iopub.execute_input":"2023-12-23T19:21:40.347992Z","iopub.status.idle":"2023-12-23T19:21:40.357595Z","shell.execute_reply.started":"2023-12-23T19:21:40.347921Z","shell.execute_reply":"2023-12-23T19:21:40.356539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for video_file in real_train_sample_video:\n    display_image_from_video(os.path.join(DATA_FOLDER, TRAIN_SAMPLE_FOLDER, video_file))","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:21:44.322673Z","iopub.execute_input":"2023-12-23T19:21:44.323049Z","iopub.status.idle":"2023-12-23T19:21:46.255260Z","shell.execute_reply.started":"2023-12-23T19:21:44.322995Z","shell.execute_reply":"2023-12-23T19:21:46.254232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Videos with same original\n","metadata":{}},{"cell_type":"code","source":"meta_train_df['original'].value_counts()[0:5]","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:22:09.590799Z","iopub.execute_input":"2023-12-23T19:22:09.591409Z","iopub.status.idle":"2023-12-23T19:22:09.601020Z","shell.execute_reply.started":"2023-12-23T19:22:09.591357Z","shell.execute_reply":"2023-12-23T19:22:09.600029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_image_from_video_list(video_path_list, video_folder=TRAIN_SAMPLE_FOLDER):\n \n    plt.figure()\n    fig, ax = plt.subplots(2,3,figsize=(16,8))\n    # we only show images extracted from the first 6 videos\n    for i, video_file in enumerate(video_path_list[0:6]):\n        video_path = os.path.join(DATA_FOLDER, video_folder,video_file)\n        capture_image = cv.VideoCapture(video_path) \n        ret, frame = capture_image.read()\n        frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)\n        ax[i//3, i%3].imshow(frame)\n        ax[i//3, i%3].set_title(f\"Video: {video_file}\")\n        ax[i//3, i%3].axis('on')","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:22:11.873613Z","iopub.execute_input":"2023-12-23T19:22:11.873957Z","iopub.status.idle":"2023-12-23T19:22:11.885192Z","shell.execute_reply.started":"2023-12-23T19:22:11.873905Z","shell.execute_reply":"2023-12-23T19:22:11.883995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"same_original_fake_train_sample_video = list(meta_train_df.loc[meta_train_df.original=='meawmsgiti.mp4'].index)\ndisplay_image_from_video_list(same_original_fake_train_sample_video)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:22:13.794615Z","iopub.execute_input":"2023-12-23T19:22:13.795033Z","iopub.status.idle":"2023-12-23T19:22:16.662781Z","shell.execute_reply.started":"2023-12-23T19:22:13.794949Z","shell.execute_reply":"2023-12-23T19:22:16.662023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"same_original_fake_train_sample_video = list(meta_train_df.loc[meta_train_df.original=='atvmxvwyns.mp4'].index)\ndisplay_image_from_video_list(same_original_fake_train_sample_video)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:22:16.664234Z","iopub.execute_input":"2023-12-23T19:22:16.664644Z","iopub.status.idle":"2023-12-23T19:22:18.751782Z","shell.execute_reply.started":"2023-12-23T19:22:16.664599Z","shell.execute_reply":"2023-12-23T19:22:18.751022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"same_original_fake_train_sample_video = list(meta_train_df.loc[meta_train_df.original=='qeumxirsme.mp4'].index)\ndisplay_image_from_video_list(same_original_fake_train_sample_video)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:22:20.783453Z","iopub.execute_input":"2023-12-23T19:22:20.783986Z","iopub.status.idle":"2023-12-23T19:22:22.601371Z","shell.execute_reply.started":"2023-12-23T19:22:20.783925Z","shell.execute_reply":"2023-12-23T19:22:22.600081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"same_original_fake_train_sample_video = list(meta_train_df.loc[meta_train_df.original=='kgbkktcjxf.mp4'].index)\ndisplay_image_from_video_list(same_original_fake_train_sample_video)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:22:30.493270Z","iopub.execute_input":"2023-12-23T19:22:30.493658Z","iopub.status.idle":"2023-12-23T19:22:33.120647Z","shell.execute_reply.started":"2023-12-23T19:22:30.493601Z","shell.execute_reply":"2023-12-23T19:22:33.119506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test video files\n","metadata":{}},{"cell_type":"code","source":"test_videos = pd.DataFrame(list(os.listdir(os.path.join(DATA_FOLDER, TEST_FOLDER))), columns=['video'])","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:22:48.835792Z","iopub.execute_input":"2023-12-23T19:22:48.836262Z","iopub.status.idle":"2023-12-23T19:22:48.844181Z","shell.execute_reply.started":"2023-12-23T19:22:48.836181Z","shell.execute_reply":"2023-12-23T19:22:48.842732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_videos.head()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:22:54.848519Z","iopub.execute_input":"2023-12-23T19:22:54.848909Z","iopub.status.idle":"2023-12-23T19:22:54.860040Z","shell.execute_reply.started":"2023-12-23T19:22:54.848844Z","shell.execute_reply":"2023-12-23T19:22:54.858893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_image_from_video(os.path.join(DATA_FOLDER, TEST_FOLDER, test_videos.iloc[0].video))","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:23:04.007586Z","iopub.execute_input":"2023-12-23T19:23:04.008005Z","iopub.status.idle":"2023-12-23T19:23:04.702739Z","shell.execute_reply.started":"2023-12-23T19:23:04.007927Z","shell.execute_reply":"2023-12-23T19:23:04.701857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_image_from_video_list(test_videos.sample(6).video, TEST_FOLDER)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:23:09.562422Z","iopub.execute_input":"2023-12-23T19:23:09.563034Z","iopub.status.idle":"2023-12-23T19:23:12.981046Z","shell.execute_reply.started":"2023-12-23T19:23:09.562945Z","shell.execute_reply":"2023-12-23T19:23:12.980146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Face detection","metadata":{}},{"cell_type":"code","source":"class ObjectDetector():\n   \n    def __init__(self,object_cascade_path):\n      \n\n        self.objectCascade=cv.CascadeClassifier(object_cascade_path)\n\n\n    def detect(self, image, scale_factor=1.3,\n               min_neighbors=5,\n               min_size=(20,20)):\n       \n        rects=self.objectCascade.detectMultiScale(image,\n                                                scaleFactor=scale_factor,\n                                                minNeighbors=min_neighbors,\n                                                minSize=min_size)\n        return rects","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:23:23.583166Z","iopub.execute_input":"2023-12-23T19:23:23.583557Z","iopub.status.idle":"2023-12-23T19:23:23.592498Z","shell.execute_reply.started":"2023-12-23T19:23:23.583492Z","shell.execute_reply":"2023-12-23T19:23:23.591280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Frontal face, profile, eye and smile  haar cascade loaded\nfrontal_cascade_path= os.path.join(FACE_DETECTION_FOLDER,'haarcascade_frontalface_default.xml')\neye_cascade_path= os.path.join(FACE_DETECTION_FOLDER,'haarcascade_eye.xml')\nprofile_cascade_path= os.path.join(FACE_DETECTION_FOLDER,'haarcascade_profileface.xml')\nsmile_cascade_path= os.path.join(FACE_DETECTION_FOLDER,'haarcascade_smile.xml')\n\n#Detector object created\n# frontal face\nfd=ObjectDetector(frontal_cascade_path)\n# eye\ned=ObjectDetector(eye_cascade_path)\n# profile face\npd=ObjectDetector(profile_cascade_path)\n# smile\nsd=ObjectDetector(smile_cascade_path)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:23:35.669025Z","iopub.execute_input":"2023-12-23T19:23:35.669386Z","iopub.status.idle":"2023-12-23T19:23:35.805921Z","shell.execute_reply.started":"2023-12-23T19:23:35.669325Z","shell.execute_reply":"2023-12-23T19:23:35.804806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We also define a function for detection and display of all these specific objects.  \n\nThe function call the **detect** method of the **ObjectDetector** object. For each object we are using a different shape and color, as following:\n* Frontal face: green rectangle;  \n* Eye: red circle;  \n* Smile: red rectangle;  \n* Profile face: blue rectangle.  \n\nNote: due to a huge amount of false positive, we deactivate for now the smile detector.","metadata":{}},{"cell_type":"code","source":"def detect_objects(image, scale_factor, min_neighbors, min_size):\n\n    \n    image_gray=cv.cvtColor(image, cv.COLOR_BGR2GRAY)\n\n\n    eyes=ed.detect(image_gray,\n                   scale_factor=scale_factor,\n                   min_neighbors=min_neighbors,\n                   min_size=(int(min_size[0]/2), int(min_size[1]/2)))\n\n    for x, y, w, h in eyes:\n        #detected eyes shown in color image\n        cv.circle(image,(int(x+w/2),int(y+h/2)),(int((w + h)/4)),(0, 0,255),3)\n \n\n    profiles=pd.detect(image_gray,\n                   scale_factor=scale_factor,\n                   min_neighbors=min_neighbors,\n                   min_size=min_size)\n\n    for x, y, w, h in profiles:\n        #detected profiles shown in color image\n        cv.rectangle(image,(x,y),(x+w, y+h),(255, 0,0),3)\n\n    faces=fd.detect(image_gray,\n                   scale_factor=scale_factor,\n                   min_neighbors=min_neighbors,\n                   min_size=min_size)\n\n    for x, y, w, h in faces:\n        #detected faces shown in color image\n        cv.rectangle(image,(x,y),(x+w, y+h),(0, 255,0),3)\n\n    # image\n    fig = plt.figure(figsize=(10,10))\n    ax = fig.add_subplot(111)\n    image = cv.cvtColor(image, cv.COLOR_BGR2RGB)\n    ax.imshow(image)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:23:44.581668Z","iopub.execute_input":"2023-12-23T19:23:44.582074Z","iopub.status.idle":"2023-12-23T19:23:44.600464Z","shell.execute_reply.started":"2023-12-23T19:23:44.582002Z","shell.execute_reply":"2023-12-23T19:23:44.598816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def extract_image_objects(video_file, video_set_folder=TRAIN_SAMPLE_FOLDER):\n    video_path = os.path.join(DATA_FOLDER, video_set_folder,video_file)\n    capture_image = cv.VideoCapture(video_path) \n    ret, frame = capture_image.read()\n    #frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)\n    detect_objects(image=frame, \n            scale_factor=1.3, \n            min_neighbors=5, \n            min_size=(50, 50))  \n  ","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:23:48.874107Z","iopub.execute_input":"2023-12-23T19:23:48.874513Z","iopub.status.idle":"2023-12-23T19:23:48.882367Z","shell.execute_reply.started":"2023-12-23T19:23:48.874444Z","shell.execute_reply":"2023-12-23T19:23:48.880932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"same_original_fake_train_sample_video = list(meta_train_df.loc[meta_train_df.original=='kgbkktcjxf.mp4'].index)\nfor video_file in same_original_fake_train_sample_video[1:4]:\n    print(video_file)\n    extract_image_objects(video_file)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:23:51.637030Z","iopub.execute_input":"2023-12-23T19:23:51.637387Z","iopub.status.idle":"2023-12-23T19:23:54.121870Z","shell.execute_reply.started":"2023-12-23T19:23:51.637329Z","shell.execute_reply":"2023-12-23T19:23:54.120916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_subsample_video = list(meta_train_df.sample(3).index)\nfor video_file in train_subsample_video:\n    print(video_file)\n    extract_image_objects(video_file)","metadata":{"execution":{"iopub.status.busy":"2023-12-23T19:23:58.259263Z","iopub.execute_input":"2023-12-23T19:23:58.259744Z","iopub.status.idle":"2023-12-23T19:24:01.086133Z","shell.execute_reply.started":"2023-12-23T19:23:58.259667Z","shell.execute_reply":"2023-12-23T19:24:01.085145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subsample_test_videos = list(test_videos.sample(3).video)\nfor video_file in subsample_test_videos:\n    print(video_file)\n    extract_image_objects(video_file, TEST_FOLDER)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:24:18.773271Z","iopub.execute_input":"2023-12-23T19:24:18.773623Z","iopub.status.idle":"2023-12-23T19:24:21.484989Z","shell.execute_reply.started":"2023-12-23T19:24:18.773570Z","shell.execute_reply":"2023-12-23T19:24:21.483774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Play video files  \n","metadata":{}},{"cell_type":"code","source":"fake_videos = list(meta_train_df.loc[meta_train_df.label=='FAKE'].index)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:24:42.329692Z","iopub.execute_input":"2023-12-23T19:24:42.330105Z","iopub.status.idle":"2023-12-23T19:24:42.338356Z","shell.execute_reply.started":"2023-12-23T19:24:42.330049Z","shell.execute_reply":"2023-12-23T19:24:42.337144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import HTML\nfrom base64 import b64encode\n\ndef play_video(video_file, subset=TRAIN_SAMPLE_FOLDER):\n   \n    video_url = open(os.path.join(DATA_FOLDER, subset,video_file),'rb').read()\n    data_url = \"data:video/mp4;base64,\" + b64encode(video_url).decode()\n    return HTML(\"\"\"<video width=500 controls><source src=\"%s\" type=\"video/mp4\"></video>\"\"\" % data_url)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:24:56.917162Z","iopub.execute_input":"2023-12-23T19:24:56.917798Z","iopub.status.idle":"2023-12-23T19:24:56.925051Z","shell.execute_reply.started":"2023-12-23T19:24:56.917744Z","shell.execute_reply":"2023-12-23T19:24:56.924265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"play_video(fake_videos[0])","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:25:06.071923Z","iopub.execute_input":"2023-12-23T19:25:06.072590Z","iopub.status.idle":"2023-12-23T19:25:06.551279Z","shell.execute_reply.started":"2023-12-23T19:25:06.072534Z","shell.execute_reply":"2023-12-23T19:25:06.549595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"play_video(fake_videos[1])","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:25:07.227496Z","iopub.execute_input":"2023-12-23T19:25:07.227845Z","iopub.status.idle":"2023-12-23T19:25:07.408770Z","shell.execute_reply.started":"2023-12-23T19:25:07.227794Z","shell.execute_reply":"2023-12-23T19:25:07.407298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"play_video(fake_videos[2])","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-12-23T19:25:09.098162Z","iopub.execute_input":"2023-12-23T19:25:09.098675Z","iopub.status.idle":"2023-12-23T19:25:09.189887Z","shell.execute_reply.started":"2023-12-23T19:25:09.098592Z","shell.execute_reply":"2023-12-23T19:25:09.186115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"play_video(fake_videos[3])","metadata":{"execution":{"iopub.status.busy":"2023-12-23T19:31:43.513341Z","iopub.execute_input":"2023-12-23T19:31:43.513768Z","iopub.status.idle":"2023-12-23T19:31:43.591692Z","shell.execute_reply.started":"2023-12-23T19:31:43.513705Z","shell.execute_reply":"2023-12-23T19:31:43.590390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"play_video(fake_videos[4])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"play_video(fake_videos[5])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"play_video(fake_videos[10])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"play_video(fake_videos[12])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"play_video(fake_videos[15])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"play_video(fake_videos[18])","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}