{"cells":[{"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# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# Importing the required libraries\n\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport os\nimport matplotlib.pyplot as plt\nimport glob\nimport sys\n%matplotlib inline\nimport pickle\nimport shutil\nimport time\nfrom keras.applications.inception_v3 import InceptionV3\nfrom keras.models import Model\nfrom keras.layers import Input\nfrom keras.preprocessing import image as im\nfrom keras.applications.inception_v3 import preprocess_input,decode_predictions\nfrom keras.models import load_model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import keras\nprint('Keras version:', keras.__version__)\nprint('OpenCV version:', cv2.__version__)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Initializing the paths\ninput_path = '/kaggle/input/deepfake-detection-challenge/'\noutput_path = '/kaggle/working/'\ntrain_dir = glob.glob(input_path + 'train_sample_videos/*.mp4')\ntest_dir = glob.glob(input_path + 'test_videos/*.mp4')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Reading the labels of training data\n\ndf_train = pd.read_json(input_path + 'train_sample_videos/metadata.json').transpose()\ndf_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Plotting the count of labels\n\ndf_train.label.value_counts().plot.bar()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Creating the dicrectory which would contain the video frames\n\n# shutil.rmtree(output_path + 'train_frames')\nos.mkdir(output_path + 'train_frames')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Creating frames of all the videos\ntrain_dir = [train_dir[0]]\nt = time.time()\ncount1 = 0\nfor v in train_dir:\n    t1 = time.time()\n    v_name = v.split(\"/\")[-1]\n    if not os.path.exists(output_path + \"train_frames/\" + v_name.split(\".\")[0]):\n        os.mkdir(output_path + \"train_frames/\" + v_name.split(\".\")[0])\n    count = 0\n    cap = cv2.VideoCapture(v)\n    while count < 100:\n        cap.set(cv2.CAP_PROP_POS_MSEC,(count * 100))   \n        ret,frame = cap.read()\n        image = frame\n        count = count + 1\n        cv2.imwrite(\"train_frames/\" + v_name.split(\".\")[0] + \"/frame\" + str(count) + \".jpg\",image)\n    count1 += 1\n    print('Elapsed: ', time.time() - t1, ' | ', count1, '/', len(train_dir), ' | ', v)\nprint('Total elapsed: ', time.time() - t)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(type(image))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Taking the base model as Inception V3 and initializing it's weight with imagenet\n\n# Enable internet on kernel settings\ninput_tensor = Input(shape = (229, 229, 3))\ncnn_model = InceptionV3(input_tensor = input_tensor, weights = 'imagenet', include_top = False, pooling = 'avg')\ncnn_model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Defining the frame files\n\nframe_files = glob.glob(\"train_frames/*/*.jpg\")\nprint('Total frames captured: ', len(frame_files))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Finding out the feature for each and every frame of all the videos\n\ncnn_output = {}\nt = time.time()\nfor name in frame_files:\n    t1 = time.time()\n    img = im.load_img(name, target_size = (229, 229, 3))\n    x = im.img_to_array(img)\n    x = np.expand_dims(x, axis=0)\n    x = preprocess_input(x)\n    folder_name = name.split(\"/\")[1]\n    if folder_name not in cnn_output.keys():\n        cnn_output[folder_name] = []\n    result = cnn_model.predict(x)\n    cnn_output[folder_name].append(result)\nprint('Elapsed: ', time.time() - t)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# # Example of CNN output\n# print(len(cnn_output['drsakwyvqv']), 'frames captured from eczrseixwq.mp4')\n# cnn_output['drsakwyvqv']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Saving the cnn_output\n\nos.mknod(output_path + 'cnn_output.txt')\nwith open('cnn_output.txt', 'wb') as f:\n    pickle.dump(cnn_output, f)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Retrieving the cnn_output\n\nwith open(output_path + 'cnn_output.txt', 'rb') as f:\n    cnn_output = pickle.load(f)","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":1}