{"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.\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#!pip install mtcnn","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nfrom keras.datasets import mnist\nfrom keras.layers import Input, Dense, Reshape, Flatten, Dropout\nfrom keras.layers import LSTM\nfrom keras.layers import TimeDistributed\nfrom keras.layers import BatchNormalization, Activation, ZeroPadding2D\nfrom keras.layers import LeakyReLU\nfrom keras.models import Sequential, Model\nfrom keras.optimizers import Adam\nfrom mtcnn import MTCNN\nfrom smart_open import smart_open\nimport matplotlib.pyplot as plt\nimport cv2\nfrom io import BytesIO\nimport numpy as np\nimport boto3\nimport pandas as pd\nfrom PIL import Image\nfrom tqdm.notebook import tqdm\nimport time\nfrom keras.models import model_from_json\n\nimport sys","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"from keras.models import model_from_json\njson_file = open('../input/models/model.json', 'r')\nloaded_model_json = json_file.read()\njson_file.close()\n\nloaded_model1 = model_from_json(loaded_model_json)\njson_file = open('../input/models/model2.json', 'r')\nloaded_model_json2 = json_file.read()\njson_file.close()\nloaded_model2 = model_from_json(loaded_model_json2)\n\njson_file = open('../input/models/model3.json', 'r')\nloaded_model_json3 = json_file.read()\njson_file.close()\nloaded_model3 = model_from_json(loaded_model_json3)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def detect_mtcnn(detector, images):\n    faces =[]\n    for image in images:\n        boxes = detector.detect_faces(image)\n        box = boxes[0]['box']\n        face = image[box[1]:box[3]+box[1], box[0]:box[2]+box[0]]\n        faces.append(face)\n\n    return faces","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def timer(detector, detect_fn, images, *args):\n                    start = time.time()\n                    faces = detect_fn(detector, images, *args)\n                    elapsed = time.time() - start\n                    return faces, elapsed  ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"DATA_FOLDER = '../input/deepfake-detection-challenge'\nTRAIN_SAMPLE_FOLDER = 'train_sample_videos'\nTEST_FOLDER = 'test_videos'\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    ax.imshow(frame)\ndisplay_image_from_video(os.path.join(DATA_FOLDER, TRAIN_SAMPLE_FOLDER, 'aettqgevhz.mp4'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"reader = cv2.VideoCapture('/kaggle/input/deepfake-detection-challenge/train_sample_videos/aettqgevhz.mp4')\nimages_540_960 = []\nfor i in tqdm(range(int(reader.get(cv2.CAP_PROP_FRAME_COUNT)))):\n    _, image = reader.read()\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    images_540_960.append(cv2.resize(image, (960, 540)))\nreader.release()\nimages_540_960 = np.stack(images_540_960)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from mtcnn import MTCNN\ndetector = MTCNN()\nfinal=[]\ntotal=299\nX_train=[]\nfaces, elapsed = timer(detector, detect_mtcnn, images_540_960)\nfinal.append(faces)\nfinal=[[cv2.resize(face, (16, 16)) for face in x] for x in final]\nfinal=[np.array(x) for x in final]\n\nif final[0].shape[0]<total:\n    length=total-final[0].shape[0]\n    np.append(final[0],np.zeros((length,16,16,3)))\nelse:\n    final[0]=final[0][:total,:,:,:]\n    print('coorected')\ntry:\n    X_train.extend(final)\n    print('tried')\nexcept:\n    X_train= final\nX_trainy=np.expand_dims(X_train[0], axis=0)\nloaded_model3.predict(X_trainy)","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}