{"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":"none","dataSources":[{"sourceId":16880,"databundleVersionId":858837,"sourceType":"competition"},{"sourceId":854304,"sourceType":"datasetVersion","datasetId":452468}],"dockerImageVersionId":29844,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-18T20:15:48.979064Z","iopub.execute_input":"2023-11-18T20:15:48.979610Z","iopub.status.idle":"2023-11-18T20:15:48.992693Z","shell.execute_reply.started":"2023-11-18T20:15:48.979510Z","shell.execute_reply":"2023-11-18T20:15:48.990752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_FOLDER = '../input/deepfake-detection-challenge'\nTRAIN_SAMPLE_FOLDER = 'train_sample_videos'\nTEST_FOLDER = '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":{"execution":{"iopub.status.busy":"2023-11-18T20:15:48.995318Z","iopub.execute_input":"2023-11-18T20:15:48.995794Z","iopub.status.idle":"2023-11-18T20:15:49.026126Z","shell.execute_reply.started":"2023-11-18T20:15:48.995708Z","shell.execute_reply":"2023-11-18T20:15:49.025258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FACE_DETECTION_FOLDER = '../input/haar-cascades-for-face-detection'\nprint(f\"Face detection resources: {os.listdir(FACE_DETECTION_FOLDER)}\")","metadata":{"execution":{"iopub.status.busy":"2023-11-18T20:15:49.028427Z","iopub.execute_input":"2023-11-18T20:15:49.029055Z","iopub.status.idle":"2023-11-18T20:15:49.036672Z","shell.execute_reply.started":"2023-11-18T20:15:49.028985Z","shell.execute_reply":"2023-11-18T20:15:49.034900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2023-11-18T20:15:49.038622Z","iopub.execute_input":"2023-11-18T20:15:49.039201Z","iopub.status.idle":"2023-11-18T20:15:49.050282Z","shell.execute_reply.started":"2023-11-18T20:15:49.039134Z","shell.execute_reply":"2023-11-18T20:15:49.049457Z"},"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":{"execution":{"iopub.status.busy":"2023-11-18T20:15:49.051676Z","iopub.execute_input":"2023-11-18T20:15:49.052105Z","iopub.status.idle":"2023-11-18T20:15:49.068522Z","shell.execute_reply.started":"2023-11-18T20:15:49.052062Z","shell.execute_reply":"2023-11-18T20:15:49.067094Z"},"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":{"execution":{"iopub.status.busy":"2023-11-18T20:15:49.070399Z","iopub.execute_input":"2023-11-18T20:15:49.071154Z","iopub.status.idle":"2023-11-18T20:15:49.086810Z","shell.execute_reply.started":"2023-11-18T20:15:49.070970Z","shell.execute_reply":"2023-11-18T20:15:49.085768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"json_file = [file for file in train_list if  file.endswith('json')][0]\nprint(f\"JSON file: {json_file}\")","metadata":{"execution":{"iopub.status.busy":"2023-11-18T20:15:49.089434Z","iopub.execute_input":"2023-11-18T20:15:49.090074Z","iopub.status.idle":"2023-11-18T20:15:49.105171Z","shell.execute_reply.started":"2023-11-18T20:15:49.090008Z","shell.execute_reply":"2023-11-18T20:15:49.103654Z"},"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":{"execution":{"iopub.status.busy":"2023-11-18T20:15:49.107785Z","iopub.execute_input":"2023-11-18T20:15:49.108273Z","iopub.status.idle":"2023-11-18T20:15:49.332037Z","shell.execute_reply.started":"2023-11-18T20:15:49.108188Z","shell.execute_reply":"2023-11-18T20:15:49.330773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2023-11-18T20:15:49.333959Z","iopub.execute_input":"2023-11-18T20:15:49.334292Z","iopub.status.idle":"2023-11-18T20:15:49.344073Z","shell.execute_reply.started":"2023-11-18T20:15:49.334238Z","shell.execute_reply":"2023-11-18T20:15:49.343112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_data(meta_train_df)","metadata":{"execution":{"iopub.status.busy":"2023-11-18T20:15:49.345842Z","iopub.execute_input":"2023-11-18T20:15:49.346410Z","iopub.status.idle":"2023-11-18T20:15:49.371509Z","shell.execute_reply.started":"2023-11-18T20:15:49.346351Z","shell.execute_reply":"2023-11-18T20:15:49.370271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_data(meta_train_df.loc[meta_train_df.label=='REAL'])","metadata":{"execution":{"iopub.status.busy":"2023-11-18T20:15:49.375253Z","iopub.execute_input":"2023-11-18T20:15:49.375763Z","iopub.status.idle":"2023-11-18T20:15:49.401160Z","shell.execute_reply.started":"2023-11-18T20:15:49.375690Z","shell.execute_reply":"2023-11-18T20:15:49.399752Z"},"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":{"execution":{"iopub.status.busy":"2023-11-18T20:15:49.404649Z","iopub.execute_input":"2023-11-18T20:15:49.405072Z","iopub.status.idle":"2023-11-18T20:15:49.413887Z","shell.execute_reply.started":"2023-11-18T20:15:49.404990Z","shell.execute_reply":"2023-11-18T20:15:49.412481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_values(meta_train_df)","metadata":{"execution":{"iopub.status.busy":"2023-11-18T20:15:49.415273Z","iopub.execute_input":"2023-11-18T20:15:49.415596Z","iopub.status.idle":"2023-11-18T20:15:49.439304Z","shell.execute_reply.started":"2023-11-18T20:15:49.415506Z","shell.execute_reply":"2023-11-18T20:15:49.437930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2023-11-18T20:15:49.440982Z","iopub.execute_input":"2023-11-18T20:15:49.441332Z","iopub.status.idle":"2023-11-18T20:15:49.451883Z","shell.execute_reply.started":"2023-11-18T20:15:49.441278Z","shell.execute_reply":"2023-11-18T20:15:49.450547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"most_frequent_values(meta_train_df)","metadata":{"execution":{"iopub.status.busy":"2023-11-18T20:15:49.454758Z","iopub.execute_input":"2023-11-18T20:15:49.455248Z","iopub.status.idle":"2023-11-18T20:15:49.490812Z","shell.execute_reply.started":"2023-11-18T20:15:49.455167Z","shell.execute_reply":"2023-11-18T20:15:49.489502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_count(feature, title, df, size=1):\n    '''\n    Plot count of classes / feature\n    param: feature - the feature to analyze\n    param: title - title to add to the graph\n    param: df - dataframe from which we plot feature's classes distribution \n    param: size - default 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":{"execution":{"iopub.status.busy":"2023-11-18T20:15:49.493387Z","iopub.execute_input":"2023-11-18T20:15:49.493873Z","iopub.status.idle":"2023-11-18T20:15:49.508451Z","shell.execute_reply.started":"2023-11-18T20:15:49.493791Z","shell.execute_reply":"2023-11-18T20:15:49.507015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_count('split', 'split (train)', meta_train_df)","metadata":{"execution":{"iopub.status.busy":"2023-11-18T20:15:49.510421Z","iopub.execute_input":"2023-11-18T20:15:49.510896Z","iopub.status.idle":"2023-11-18T20:15:49.796520Z","shell.execute_reply.started":"2023-11-18T20:15:49.510815Z","shell.execute_reply":"2023-11-18T20:15:49.795389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_count('label', 'label (train)', meta_train_df)","metadata":{"execution":{"iopub.status.busy":"2023-11-18T20:15:49.798371Z","iopub.execute_input":"2023-11-18T20:15:49.798989Z","iopub.status.idle":"2023-11-18T20:15:49.967956Z","shell.execute_reply.started":"2023-11-18T20:15:49.798922Z","shell.execute_reply":"2023-11-18T20:15:49.966694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-11-18T20:15:49.969650Z","iopub.execute_input":"2023-11-18T20:15:49.970273Z","iopub.status.idle":"2023-11-18T20:15:49.988092Z","shell.execute_reply.started":"2023-11-18T20:15:49.970204Z","shell.execute_reply":"2023-11-18T20:15:49.986734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2023-11-18T20:15:49.990460Z","iopub.execute_input":"2023-11-18T20:15:49.991381Z","iopub.status.idle":"2023-11-18T20:15:50.008031Z","shell.execute_reply.started":"2023-11-18T20:15:49.991300Z","shell.execute_reply":"2023-11-18T20:15:50.006002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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    ax.imshow(frame)","metadata":{"execution":{"iopub.status.busy":"2023-11-18T20:15:50.010369Z","iopub.execute_input":"2023-11-18T20:15:50.011262Z","iopub.status.idle":"2023-11-18T20:15:50.022692Z","shell.execute_reply.started":"2023-11-18T20:15:50.011177Z","shell.execute_reply":"2023-11-18T20:15:50.021544Z"},"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":{"execution":{"iopub.status.busy":"2023-11-18T20:15:50.024485Z","iopub.execute_input":"2023-11-18T20:15:50.025209Z","iopub.status.idle":"2023-11-18T20:15:51.826887Z","shell.execute_reply.started":"2023-11-18T20:15:50.025136Z","shell.execute_reply":"2023-11-18T20:15:51.825750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2023-11-18T20:15:51.829166Z","iopub.execute_input":"2023-11-18T20:15:51.829771Z","iopub.status.idle":"2023-11-18T20:15:51.840826Z","shell.execute_reply.started":"2023-11-18T20:15:51.829526Z","shell.execute_reply":"2023-11-18T20:15:51.839744Z"},"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":{"execution":{"iopub.status.busy":"2023-11-18T20:15:51.842602Z","iopub.execute_input":"2023-11-18T20:15:51.843200Z","iopub.status.idle":"2023-11-18T20:15:53.890627Z","shell.execute_reply.started":"2023-11-18T20:15:51.843134Z","shell.execute_reply":"2023-11-18T20:15:53.889653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meta_train_df['original'].value_counts()[0:5]","metadata":{"execution":{"iopub.status.busy":"2023-11-18T20:15:53.892242Z","iopub.execute_input":"2023-11-18T20:15:53.892908Z","iopub.status.idle":"2023-11-18T20:15:53.907678Z","shell.execute_reply.started":"2023-11-18T20:15:53.892845Z","shell.execute_reply":"2023-11-18T20:15:53.906129Z"},"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    input: video_path_list - path for video\n    process:\n    0. for each video in the video path list\n        1. perform a video capture from the video\n        2. read the image\n        3. display the image\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":{"execution":{"iopub.status.busy":"2023-11-18T20:15:53.909679Z","iopub.execute_input":"2023-11-18T20:15:53.910041Z","iopub.status.idle":"2023-11-18T20:15:53.923209Z","shell.execute_reply.started":"2023-11-18T20:15:53.909986Z","shell.execute_reply":"2023-11-18T20:15:53.921825Z"},"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":{"execution":{"iopub.status.busy":"2023-11-18T20:15:53.925160Z","iopub.execute_input":"2023-11-18T20:15:53.925517Z","iopub.status.idle":"2023-11-18T20:15:57.465495Z","shell.execute_reply.started":"2023-11-18T20:15:53.925457Z","shell.execute_reply":"2023-11-18T20:15:57.464475Z"},"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":{"execution":{"iopub.status.busy":"2023-11-18T20:15:57.467885Z","iopub.execute_input":"2023-11-18T20:15:57.468247Z","iopub.status.idle":"2023-11-18T20:16:00.886745Z","shell.execute_reply.started":"2023-11-18T20:15:57.468178Z","shell.execute_reply":"2023-11-18T20:16:00.885282Z"},"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":{"execution":{"iopub.status.busy":"2023-11-18T20:16:00.889252Z","iopub.execute_input":"2023-11-18T20:16:00.889747Z","iopub.status.idle":"2023-11-18T20:16:03.899053Z","shell.execute_reply.started":"2023-11-18T20:16:00.889674Z","shell.execute_reply":"2023-11-18T20:16:03.897951Z"},"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":{"execution":{"iopub.status.busy":"2023-11-18T20:16:03.901043Z","iopub.execute_input":"2023-11-18T20:16:03.901427Z","iopub.status.idle":"2023-11-18T20:16:07.048164Z","shell.execute_reply.started":"2023-11-18T20:16:03.901361Z","shell.execute_reply":"2023-11-18T20:16:07.046890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_videos = pd.DataFrame(list(os.listdir(os.path.join(DATA_FOLDER, TEST_FOLDER))), columns=['video'])","metadata":{"execution":{"iopub.status.busy":"2023-11-18T20:16:07.050072Z","iopub.execute_input":"2023-11-18T20:16:07.050466Z","iopub.status.idle":"2023-11-18T20:16:07.057986Z","shell.execute_reply.started":"2023-11-18T20:16:07.050406Z","shell.execute_reply":"2023-11-18T20:16:07.057096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_videos.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-18T20:16:07.063841Z","iopub.execute_input":"2023-11-18T20:16:07.064471Z","iopub.status.idle":"2023-11-18T20:16:07.082031Z","shell.execute_reply.started":"2023-11-18T20:16:07.064363Z","shell.execute_reply":"2023-11-18T20:16:07.080777Z"},"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":{"execution":{"iopub.status.busy":"2023-11-18T20:16:07.084308Z","iopub.execute_input":"2023-11-18T20:16:07.084770Z","iopub.status.idle":"2023-11-18T20:16:07.792749Z","shell.execute_reply.started":"2023-11-18T20:16:07.084665Z","shell.execute_reply":"2023-11-18T20:16:07.791213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_image_from_video_list(test_videos.sample(6).video, TEST_FOLDER)","metadata":{"execution":{"iopub.status.busy":"2023-11-18T20:16:07.795395Z","iopub.execute_input":"2023-11-18T20:16:07.795955Z","iopub.status.idle":"2023-11-18T20:16:11.333583Z","shell.execute_reply.started":"2023-11-18T20:16:07.795869Z","shell.execute_reply":"2023-11-18T20:16:11.332014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ObjectDetector():\n    '''\n    Class for Object Detection\n    '''\n    def __init__(self,object_cascade_path):\n        '''\n        param: object_cascade_path - path for the *.xml defining the parameters for {face, eye, smile, profile}\n        detection algorithm\n        source of the haarcascade resource is: https://github.com/opencv/opencv/tree/master/data/haarcascades\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        Function return rectangle coordinates of object for given image\n        param: image - image to process\n        param: scale_factor - scale factor used for object detection\n        param: min_neighbors - minimum number of parameters considered during object detection\n        param: min_size - minimum size of bounding box for object detected\n        '''\n        rects=self.objectCascade.detectMultiScale(image,\n                                                scaleFactor=scale_factor,\n                                                minNeighbors=min_neighbors,\n                                                minSize=min_size)\n        return rects","metadata":{"execution":{"iopub.status.busy":"2023-11-18T20:16:11.335580Z","iopub.execute_input":"2023-11-18T20:16:11.336002Z","iopub.status.idle":"2023-11-18T20:16:11.346027Z","shell.execute_reply.started":"2023-11-18T20:16:11.335928Z","shell.execute_reply":"2023-11-18T20:16:11.344853Z"},"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":{"execution":{"iopub.status.busy":"2023-11-18T20:16:11.348365Z","iopub.execute_input":"2023-11-18T20:16:11.348775Z","iopub.status.idle":"2023-11-18T20:16:11.488218Z","shell.execute_reply.started":"2023-11-18T20:16:11.348708Z","shell.execute_reply":"2023-11-18T20:16:11.486982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def detect_objects(image, scale_factor, min_neighbors, min_size):\n    '''\n    Objects detection function\n    Identify frontal face, eyes, smile and profile face and display the detected objects over the image\n    param: image - the image extracted from the video\n    param: scale_factor - scale factor parameter for `detect` function of ObjectDetector object\n    param: min_neighbors - min neighbors parameter for `detect` function of ObjectDetector object\n    param: min_size - minimum size parameter for f`detect` function of ObjectDetector object\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    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":{"execution":{"iopub.status.busy":"2023-11-18T20:16:11.490050Z","iopub.execute_input":"2023-11-18T20:16:11.490370Z","iopub.status.idle":"2023-11-18T20:16:11.508907Z","shell.execute_reply.started":"2023-11-18T20:16:11.490316Z","shell.execute_reply":"2023-11-18T20:16:11.507478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def extract_image_objects(video_file, video_set_folder=TRAIN_SAMPLE_FOLDER):\n    '''\n    Extract one image from the video and then perform face/eyes/smile/profile detection on the image\n    param: video_file - the video from which to extract the image from which we extract the face\n    '''\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))  ","metadata":{"execution":{"iopub.status.busy":"2023-11-18T20:16:11.510507Z","iopub.execute_input":"2023-11-18T20:16:11.510879Z","iopub.status.idle":"2023-11-18T20:16:11.527833Z","shell.execute_reply.started":"2023-11-18T20:16:11.510826Z","shell.execute_reply":"2023-11-18T20:16:11.525966Z"},"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":{"execution":{"iopub.status.busy":"2023-11-18T20:16:11.530163Z","iopub.execute_input":"2023-11-18T20:16:11.530683Z","iopub.status.idle":"2023-11-18T20:16:14.223742Z","shell.execute_reply.started":"2023-11-18T20:16:11.530612Z","shell.execute_reply":"2023-11-18T20:16:14.222517Z"},"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-11-18T20:16:14.225496Z","iopub.execute_input":"2023-11-18T20:16:14.225867Z","iopub.status.idle":"2023-11-18T20:16:17.619333Z","shell.execute_reply.started":"2023-11-18T20:16:14.225809Z","shell.execute_reply":"2023-11-18T20:16:17.617781Z"},"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":{"execution":{"iopub.status.busy":"2023-11-18T20:16:17.621615Z","iopub.execute_input":"2023-11-18T20:16:17.622135Z","iopub.status.idle":"2023-11-18T20:16:20.671644Z","shell.execute_reply.started":"2023-11-18T20:16:17.622034Z","shell.execute_reply":"2023-11-18T20:16:20.669809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fake_videos = list(meta_train_df.loc[meta_train_df.label=='FAKE'].index)","metadata":{"execution":{"iopub.status.busy":"2023-11-18T20:16:20.673859Z","iopub.execute_input":"2023-11-18T20:16:20.674342Z","iopub.status.idle":"2023-11-18T20:16:20.684047Z","shell.execute_reply.started":"2023-11-18T20:16:20.674240Z","shell.execute_reply":"2023-11-18T20:16:20.682374Z"},"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    Display video\n    param: video_file - the name of the video file to display\n    param: subset - the folder where the video file is located (can be TRAIN_SAMPLE_FOLDER or TEST_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":{"execution":{"iopub.status.busy":"2023-11-18T20:16:20.685764Z","iopub.execute_input":"2023-11-18T20:16:20.686211Z","iopub.status.idle":"2023-11-18T20:16:20.702853Z","shell.execute_reply.started":"2023-11-18T20:16:20.686134Z","shell.execute_reply":"2023-11-18T20:16:20.701451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"play_video(fake_videos[0])","metadata":{"execution":{"iopub.status.busy":"2023-11-18T20:16:20.704871Z","iopub.execute_input":"2023-11-18T20:16:20.705258Z","iopub.status.idle":"2023-11-18T20:16:21.289932Z","shell.execute_reply.started":"2023-11-18T20:16:20.705200Z","shell.execute_reply":"2023-11-18T20:16:21.288930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"play_video(fake_videos[1])","metadata":{"execution":{"iopub.status.busy":"2023-11-18T20:16:21.291060Z","iopub.execute_input":"2023-11-18T20:16:21.291321Z","iopub.status.idle":"2023-11-18T20:16:21.496922Z","shell.execute_reply.started":"2023-11-18T20:16:21.291280Z","shell.execute_reply":"2023-11-18T20:16:21.495595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"play_video(fake_videos[2])","metadata":{"execution":{"iopub.status.busy":"2023-11-18T20:16:21.499158Z","iopub.execute_input":"2023-11-18T20:16:21.499981Z","iopub.status.idle":"2023-11-18T20:16:21.581423Z","shell.execute_reply.started":"2023-11-18T20:16:21.499897Z","shell.execute_reply":"2023-11-18T20:16:21.579707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"play_video(fake_videos[3])","metadata":{"execution":{"iopub.status.busy":"2023-11-18T20:16:21.584074Z","iopub.execute_input":"2023-11-18T20:16:21.585057Z","iopub.status.idle":"2023-11-18T20:16:21.695926Z","shell.execute_reply.started":"2023-11-18T20:16:21.584954Z","shell.execute_reply":"2023-11-18T20:16:21.693911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"play_video(fake_videos[4])","metadata":{"execution":{"iopub.status.busy":"2023-11-18T20:16:21.698473Z","iopub.execute_input":"2023-11-18T20:16:21.698892Z","iopub.status.idle":"2023-11-18T20:16:21.912655Z","shell.execute_reply.started":"2023-11-18T20:16:21.698821Z","shell.execute_reply":"2023-11-18T20:16:21.910887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"play_video(fake_videos[5])","metadata":{"execution":{"iopub.status.busy":"2023-11-18T20:16:21.915245Z","iopub.execute_input":"2023-11-18T20:16:21.915901Z","iopub.status.idle":"2023-11-18T20:16:22.035426Z","shell.execute_reply.started":"2023-11-18T20:16:21.915795Z","shell.execute_reply":"2023-11-18T20:16:22.033611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"play_video(fake_videos[10])","metadata":{"execution":{"iopub.status.busy":"2023-11-18T20:16:22.036872Z","iopub.execute_input":"2023-11-18T20:16:22.037141Z","iopub.status.idle":"2023-11-18T20:16:22.353333Z","shell.execute_reply.started":"2023-11-18T20:16:22.037101Z","shell.execute_reply":"2023-11-18T20:16:22.350779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"play_video(fake_videos[12])","metadata":{"execution":{"iopub.status.busy":"2023-11-18T20:16:22.354670Z","iopub.execute_input":"2023-11-18T20:16:22.354972Z","iopub.status.idle":"2023-11-18T20:16:22.479221Z","shell.execute_reply.started":"2023-11-18T20:16:22.354930Z","shell.execute_reply":"2023-11-18T20:16:22.477721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"play_video(fake_videos[15])","metadata":{"execution":{"iopub.status.busy":"2023-11-18T20:16:22.481257Z","iopub.execute_input":"2023-11-18T20:16:22.481961Z","iopub.status.idle":"2023-11-18T20:16:22.671525Z","shell.execute_reply.started":"2023-11-18T20:16:22.481897Z","shell.execute_reply":"2023-11-18T20:16:22.669707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"play_video(fake_videos[18])","metadata":{"execution":{"iopub.status.busy":"2023-11-18T20:16:22.674066Z","iopub.execute_input":"2023-11-18T20:16:22.674965Z","iopub.status.idle":"2023-11-18T20:16:22.780769Z","shell.execute_reply.started":"2023-11-18T20:16:22.674886Z","shell.execute_reply":"2023-11-18T20:16:22.778384Z"},"trusted":true},"execution_count":null,"outputs":[]}]}