{"cells":[{"metadata":{},"cell_type":"markdown","source":"### I just introduce the way to find the face by similarity approach.\n\nFaces can be detected easily \nif there are more than 2 images whose sources are the same."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport glob\nimport cv2\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"train_dir = \"/kaggle/input/deepfake-detection-challenge/train_sample_videos/\"\ntrain_video_files = glob.glob(train_dir+\"*.mp4\")\ntrain_metadata = pd.read_json('../input/deepfake-detection-challenge/train_sample_videos/metadata.json').T\ntrain_metadata.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(train_metadata[\"original\"].value_counts()[0:12])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_frequent=train_metadata[train_metadata[\"original\"]==\"qtnjyomzwo.mp4\"]\n\nframe_num=0\nvideo_1=list(train_frequent.index)[0]\nvideo_2=list(train_frequent.index)[1]\n\nfig, axes = plt.subplots(1,3, figsize=(30,10))\n\ncap = cv2.VideoCapture(train_dir+video_1)\ncap.set(cv2.CAP_PROP_POS_FRAMES, frame_num)\n_, image = cap.read()\nimage_1 = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\ncap.release()\naxes[0].imshow(image_1[0:500,850:1350,:])\naxes[0].title.set_text(f\"{video_1}\")\n\ncap = cv2.VideoCapture(train_dir+video_2)\ncap.set(cv2.CAP_PROP_POS_FRAMES, frame_num)\n_, image = cap.read()\nimage_2 = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\ncap.release()\naxes[1].imshow(image_2[0:500,850:1350,:])\naxes[1].title.set_text(f\"{video_2}\")\n\nimage_3=np.sum((image_1-image_2)**2,axis=2)\naxes[2].imshow(image_3[0:500,850:1350])\naxes[2].title.set_text(\"Difference(MSE)\")\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"First two images are two fake images and the right one is the RMS between them. You can see the large error appears in the face. Face area can be cropped with filter and/or clustering.(I don't show here)\nThis can be used to check if your detector is working well.\n\nThe following is other example."},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"train_frequent=train_metadata[train_metadata[\"original\"]==\"xngpzquyhs.mp4\"]\n\nframe_num=0\nvideo_1=list(train_frequent.index)[0]\nvideo_2=list(train_frequent.index)[1]\n\nfig, axes = plt.subplots(1,3, figsize=(30,10))\n\ncap = cv2.VideoCapture(train_dir+video_1)\ncap.set(cv2.CAP_PROP_POS_FRAMES, frame_num)\n_, image = cap.read()\nimage_1 = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\ncap.release()\naxes[0].imshow(image_1[0:400,800:1250,:])\naxes[0].title.set_text(f\"{video_1}\")\n\ncap = cv2.VideoCapture(train_dir+video_2)\ncap.set(cv2.CAP_PROP_POS_FRAMES, frame_num)\n_, image = cap.read()\nimage_2 = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\ncap.release()\naxes[1].imshow(image_2[0:400,800:1250,:])\naxes[1].title.set_text(f\"{video_2}\")\n\nimage_3=np.sum((image_1-image_2)**2,axis=2)\naxes[2].imshow(image_3[0:400,800:1250])\naxes[2].title.set_text(\"Difference(MSE>{})\")\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"I've also checked the similarity among all images."},{"metadata":{"trusted":true},"cell_type":"code","source":"frame_num=0\nall_first_image=[]\nfor i, file_name in tqdm(enumerate(list(train_metadata.index))):\n    cap = cv2.VideoCapture(train_dir+file_name)\n    cap.set(cv2.CAP_PROP_POS_FRAMES, frame_num)\n    _, image = cap.read()\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    cap.release()\n    image=cv2.resize(image,(96,128))\n    all_first_image.append(image)\nall_first_image=np.array(all_first_image)\nthresh=27#if MSE<thresh, regard pixels as the same\nsimilarity_matrix=np.sum(((all_first_image[:,np.newaxis,:,:]-all_first_image[np.newaxis,:,:,:])**2).reshape(400,400,-1)<thresh,axis=2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(similarity_matrix.shape)\nplt.pcolor(similarity_matrix[:20,:20])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"This shows the similarity among first 20 videos.\nFor example, you can see video no.3 and no.10 have the same source. (As you know, this is already provided information.)"}],"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}