{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Deepfake\n\nSometimes to solve a problem, you need a change of perspective.\n\nWe can flip the video, and beat deepfake.","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport matplotlib\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm_notebook\n%matplotlib inline \nimport cv2 as cv\nimport base64\nimport json\nimport requests\nfrom tqdm import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-12T09:43:34.214420Z","iopub.execute_input":"2022-09-12T09:43:34.214872Z","iopub.status.idle":"2022-09-12T09:43:34.251286Z","shell.execute_reply.started":"2022-09-12T09:43:34.214835Z","shell.execute_reply":"2022-09-12T09:43:34.249922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filepath = '../input/ai-village-ctf/deepfake/sample.mp4'\nvid = cv.VideoCapture(filepath) \ntotal_frames = last_frame_number =  vid.get(cv.CAP_PROP_FRAME_COUNT)\nfourcc = cv.VideoWriter_fourcc(*'mp4v')\nfps = vid.get(cv.CAP_PROP_FPS)\nwidth  = vid.get(cv.CAP_PROP_FRAME_WIDTH)\nheight = vid.get(cv.CAP_PROP_FRAME_HEIGHT)\nprint(width, height)\nwriter = cv.VideoWriter(\"output.mp4\", fourcc=fourcc, fps=fps, frameSize=(int(width), int(height)))\n\nframe_number = -1\n\nfor frame_number in tqdm(range(int(last_frame_number))):\n    vid.set(1,frame_number)\n    ret, frame = vid.read()\n    \n    frame = cv.flip(frame, -1)\n    \n    writer.write(frame)\n\nvid.release()\nwriter.release()","metadata":{"execution":{"iopub.status.busy":"2022-09-12T09:43:36.876866Z","iopub.execute_input":"2022-09-12T09:43:36.877509Z","iopub.status.idle":"2022-09-12T09:46:22.005349Z","shell.execute_reply.started":"2022-09-12T09:43:36.877474Z","shell.execute_reply":"2022-09-12T09:46:22.004240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pylab as pl\npl.imshow(frame)\npl.axis('off')\npl.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-09-12T09:46:46.247150Z","iopub.execute_input":"2022-09-12T09:46:46.247712Z","iopub.status.idle":"2022-09-12T09:46:46.882059Z","shell.execute_reply.started":"2022-09-12T09:46:46.247645Z","shell.execute_reply":"2022-09-12T09:46:46.880963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(f\"/kaggle/working/output.mp4\", \"rb\") as f:\n    data = f.read()\nb64_data = base64.urlsafe_b64encode(data).decode()\nin_data = json.dumps({\"input\": b64_data})\nr = requests.post(\"https://deepfake.fly.dev/score\", data=in_data)\nprint(r.text)","metadata":{"execution":{"iopub.status.busy":"2022-09-12T03:10:08.105595Z","iopub.execute_input":"2022-09-12T03:10:08.106017Z","iopub.status.idle":"2022-09-12T03:10:14.335155Z","shell.execute_reply.started":"2022-09-12T03:10:08.105980Z","shell.execute_reply":"2022-09-12T03:10:14.334123Z"},"trusted":true},"execution_count":null,"outputs":[]}]}