{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"!pip install /kaggle/input/facenet-pytorch-vggface2/facenet_pytorch-2.2.7-py3-none-any.whl","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport keras\nimport cv2 as cv\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, img_to_array\nfrom tensorflow.keras.models import Sequential, load_model\nfrom facenet_pytorch import MTCNN\nimport time","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_videos = '../input/deepfake-detection-challenge/test_videos/'\ntest_movie_files = [test_videos + x for x in sorted(os.listdir(test_videos))]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = load_model('../input/models/MobilenetV2_third_-13-0.1018.h5')\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"detector = MTCNN(margin=50, keep_all=False, post_process=False, device='cuda:0',thresholds=[.9,.9,.9])\nvid_num = 0\nscores=[]\nfilenames = []\nstartTime = time.time()/60\n\nfor vid in test_movie_files:\n    predict_all=[]\n    count=0\n    file_name_mp4 = vid.split('/')[-1]\n    file_name = file_name_mp4.split('.')[0]\n    v_cap = cv.VideoCapture(vid)    \n    v_len = int(v_cap.get(cv.CAP_PROP_FRAME_COUNT))\n    for frm in range(v_len):    \n        success = v_cap.grab()\n        if frm % 7 == 0:\n            success, frame = v_cap.retrieve()\n            if not success:\n                continue\n            frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)\n            frame = detector(frame)\n            if frame is not None:\n                frame = np.transpose(frame, (1, 2, 0))\n                frame = np.array(cv.resize(np.array(frame),(160 ,160)))\n                frame = (frame.flatten() / 255.0).reshape(-1, 160, 160, 3)\n                count=count+frame.shape[0]\n                predict = model.predict(frame)\n                predict=1-predict[0][0]\n                predict_all.append(predict)\n            else:\n                continue\n        else:\n            continue\n\n    print('성공 :', file_name_mp4)\n    if (count>11):\n        predict_all.sort()\n        scores.append((sum(predict_all[5:-5])/(count-10)))\n    else:\n        scores.append(0.5)\n    filenames.append(file_name_mp4)\n\nv_cap.release()\nendTime = time.time()/60 - startTime\nprint('소요시간 :',endTime)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predict_df = pd.DataFrame({'filename':filenames, 'label':scores}) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predict_df.loc[predict_df['label']==1,'label'] = 0.99\npredict_df.loc[predict_df['label']==0,'label'] = 0.01\npredict_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predict_df.to_csv('submission.csv', index=False)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('치맥♥')","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}