{"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":"code","source":"!pip install facenet-pytorch\n!pip install imutils\n!pip install matplotlib\n\nfrom facenet_pytorch import MTCNN\nfrom PIL import Image\nimport torch\nfrom imutils.video import FileVideoStream\nimport cv2\nimport time\nimport glob\nfrom tqdm.notebook import tqdm\n\ndevice = 'cuda' if torch.cuda.is_available() else 'cpu'\n\nfilenames = glob.glob('/kaggle/input/deepfake-detection-challenge/test_videos/*.mp4')[:5]\n\nclass FastMTCNN(object):\n\n    def __init__(self, stride, resize=1, *args, **kwargs):\n        self.stride = stride\n        self.resize = resize\n        self.mtcnn = MTCNN(*args, **kwargs)\n        \n    def get_coord(self, b, ind, w, h):\n        if (ind < 2):\n            return 0 if (b < 30) else b - 30\n        if (ind == 2):\n            return (h - 1) if (b + 30) >= h else b + 30\n        if (ind == 3):\n            return (w - 1) if (b + 30) >= w else b + 30\n            \n        \n    def __call__(self, frames):\n        if self.resize != 1:\n            frames = [\n                cv2.resize(f, (int(f.shape[1] * self.resize), int(f.shape[0] * self.resize)))\n                    for f in frames\n            ]\n                      \n        boxes, probs = self.mtcnn.detect(frames[::self.stride])\n\n        faces = []\n        rectangle_points = []\n        for i, frame in enumerate(frames):\n            box_ind = int(i / self.stride)\n            if boxes[box_ind] is None:\n                continue\n            for box in boxes[box_ind]:\n                box = [self.get_coord(int(b), ind, len(frame), len(frame[0])) for ind, b in enumerate(box)]\n                faces.append(frame[box[1]:box[3], box[0]:box[2]])\n                rectangle_points.append(box)\n        \n        return faces, rectangle_points\n    \nfast_mtcnn = FastMTCNN(\n    stride=4,\n    resize=0.5,\n    margin=25,\n    factor=0.6,\n    keep_all=True,\n    device=device\n)\n\ndef run_detection(fast_mtcnn, filenames):\n    frames = []\n    frames_processed = 0\n    faces_detected = 0\n    batch_size = 60\n    start = time.time()\n    res_faces = []\n    res_rectangle_points = [[] for y in range(len(filenames))] \n    index = 0\n    for filename in tqdm(filenames):\n        res_rectangle_points[index] = [filename]\n\n        v_cap = FileVideoStream(filename).start()\n        v_len = int(v_cap.stream.get(cv2.CAP_PROP_FRAME_COUNT))\n\n        for j in range(v_len):\n\n            frame = v_cap.read()\n            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n            frames.append(frame)\n\n            if len(frames) >= batch_size or j == v_len - 1:\n\n                faces, rectangle_points = fast_mtcnn(frames)\n\n                frames_processed += len(frames)\n                faces_detected += len(faces)\n                res_faces += faces\n                res_rectangle_points[index] += rectangle_points\n                frames = []\n\n                print(\n                    f'Frames per second: {frames_processed / (time.time() - start):.3f},',\n                    f'faces detected: {faces_detected}\\r',\n                    end=''\n                )\n        index += 1\n    v_cap.stop()\n    return res_faces, res_rectangle_points\n\nres_faces, res_boxes = run_detection(fast_mtcnn, filenames)\n\nfrom matplotlib import pyplot as plt\nplt.imshow(res_faces[90], interpolation='nearest')\nplt.show()\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-22T19:09:44.977410Z","iopub.execute_input":"2023-05-22T19:09:44.977763Z","iopub.status.idle":"2023-05-22T19:11:20.791704Z","shell.execute_reply.started":"2023-05-22T19:09:44.977710Z","shell.execute_reply":"2023-05-22T19:11:20.790606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(res_faces[430], interpolation='nearest')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-22T19:11:20.794254Z","iopub.execute_input":"2023-05-22T19:11:20.794909Z","iopub.status.idle":"2023-05-22T19:11:20.992126Z","shell.execute_reply.started":"2023-05-22T19:11:20.794840Z","shell.execute_reply":"2023-05-22T19:11:20.991073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy \nfor file in res_boxes:\n    if (len(file)):\n        res_masks = numpy.asarray(file[1:])\n        numpy.savetxt(file[0].split('/')[-1] + '.csv', res_masks, delimiter=\";\")\n","metadata":{"execution":{"iopub.status.busy":"2023-05-22T19:11:20.993867Z","iopub.execute_input":"2023-05-22T19:11:20.994477Z","iopub.status.idle":"2023-05-22T19:11:21.018982Z","shell.execute_reply.started":"2023-05-22T19:11:20.994409Z","shell.execute_reply":"2023-05-22T19:11:21.017815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\nres_gray_faces = []\nfor image in res_faces:\n    frame = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n    res_gray_faces.append(frame[0][:15])\ndf = pd.DataFrame(res_gray_faces)\ndf.corr()","metadata":{"execution":{"iopub.status.busy":"2023-05-22T19:17:36.714486Z","iopub.execute_input":"2023-05-22T19:17:36.714823Z","iopub.status.idle":"2023-05-22T19:17:36.850668Z","shell.execute_reply.started":"2023-05-22T19:17:36.714780Z","shell.execute_reply":"2023-05-22T19:17:36.849737Z"},"trusted":true},"execution_count":null,"outputs":[]}]}