{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":16880,"databundleVersionId":858837,"sourceType":"competition"},{"sourceId":1307758,"sourceType":"datasetVersion","datasetId":757065}],"dockerImageVersionId":29844,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"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\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 read-only \"../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# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","scrolled":true,"execution":{"iopub.status.busy":"2024-07-09T18:33:21.580872Z","iopub.execute_input":"2024-07-09T18:33:21.581155Z","iopub.status.idle":"2024-07-09T18:33:22.352611Z","shell.execute_reply.started":"2024-07-09T18:33:21.581101Z","shell.execute_reply":"2024-07-09T18:33:22.351899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a=\"/kaggle/input/deepfake-detection-challenge/train_sample_videos\"\next=[]\nfor i in os.listdir(a):\n    i=i.split('.')[1]\n    if(i not in ext):\n        ext.append(i)\next","metadata":{"execution":{"iopub.status.busy":"2024-07-09T18:33:22.356676Z","iopub.execute_input":"2024-07-09T18:33:22.356962Z","iopub.status.idle":"2024-07-09T18:33:22.371128Z","shell.execute_reply.started":"2024-07-09T18:33:22.356918Z","shell.execute_reply":"2024-07-09T18:33:22.370406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"json_folder=[file for file in os.listdir(a) if file.endswith('json')][0]\njson_folder","metadata":{"execution":{"iopub.status.busy":"2024-07-09T18:33:22.372529Z","iopub.execute_input":"2024-07-09T18:33:22.372735Z","iopub.status.idle":"2024-07-09T18:33:22.383683Z","shell.execute_reply.started":"2024-07-09T18:33:22.372699Z","shell.execute_reply":"2024-07-09T18:33:22.382910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"json=os.path.join(a,json_folder)\ndf=pd.read_json(json)\ndf=df.T\ndf","metadata":{"execution":{"iopub.status.busy":"2024-07-09T18:33:22.384926Z","iopub.execute_input":"2024-07-09T18:33:22.385203Z","iopub.status.idle":"2024-07-09T18:33:23.036009Z","shell.execute_reply.started":"2024-07-09T18:33:22.385155Z","shell.execute_reply":"2024-07-09T18:33:23.035289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[df[\"label\"]=='FAKE'].count()","metadata":{"execution":{"iopub.status.busy":"2024-07-09T18:33:23.039007Z","iopub.execute_input":"2024-07-09T18:33:23.039258Z","iopub.status.idle":"2024-07-09T18:33:23.047542Z","shell.execute_reply.started":"2024-07-09T18:33:23.039210Z","shell.execute_reply":"2024-07-09T18:33:23.046735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=df.drop('split',axis=1)\ndf","metadata":{"execution":{"iopub.status.busy":"2024-07-09T18:33:23.048881Z","iopub.execute_input":"2024-07-09T18:33:23.049127Z","iopub.status.idle":"2024-07-09T18:33:23.067076Z","shell.execute_reply.started":"2024-07-09T18:33:23.049079Z","shell.execute_reply":"2024-07-09T18:33:23.066302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=df['label']\ndf=df.drop('label',axis=1)\nprint(df)\ny","metadata":{"execution":{"iopub.status.busy":"2024-07-09T18:33:23.068370Z","iopub.execute_input":"2024-07-09T18:33:23.068604Z","iopub.status.idle":"2024-07-09T18:33:23.083049Z","shell.execute_reply.started":"2024-07-09T18:33:23.068551Z","shell.execute_reply":"2024-07-09T18:33:23.082335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\ndef extract_frames(video,num_frames=120):\n    frame=[]\n    cap=cv2.VideoCapture(video)\n    while len(frame)<num_frames:\n        ret,fra=cap.read()\n        if not ret:\n            break\n        fra=cv2.resize(fra,(112,112))\n        frame.append(fra)\n    return frame","metadata":{"execution":{"iopub.status.busy":"2024-07-09T18:33:26.697965Z","iopub.execute_input":"2024-07-09T18:33:26.698261Z","iopub.status.idle":"2024-07-09T18:33:26.882347Z","shell.execute_reply.started":"2024-07-09T18:33:26.698206Z","shell.execute_reply":"2024-07-09T18:33:26.881616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def heatmap(image):\n    prev_gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)\n    heatmap=cv2.applyColorMap(prev_gray,cv2.COLORMAP_JET)\n    return heatmap","metadata":{"execution":{"iopub.status.busy":"2024-07-09T18:33:26.883542Z","iopub.execute_input":"2024-07-09T18:33:26.883839Z","iopub.status.idle":"2024-07-09T18:33:26.888179Z","shell.execute_reply.started":"2024-07-09T18:33:26.883746Z","shell.execute_reply":"2024-07-09T18:33:26.887409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage.metrics import structural_similarity as ssim\ndef compare_heatmaps(frames, ssim_threshold=0.5):\n    if len(frames) == 0:\n        print(\"Error: No frames to compare\")\n        return \"Unknown\", -1\n\n    prev_heatmap = heatmap(frames[0])\n    prev_gray = cv2.cvtColor(prev_heatmap, cv2.COLOR_BGR2GRAY)\n    inconsistencies = 0\n    for i in range(1, len(frames)):\n        next_heatmap = heatmap(frames[i])\n        next_gray = cv2.cvtColor(next_heatmap, cv2.COLOR_BGR2GRAY)\n        ssim_value = ssim(prev_gray, next_gray)        \n        if ssim_value < ssim_threshold:\n            inconsistencies += 1\n        prev_gray = next_gray\n        \n    if inconsistencies > (len(frames) * 0.1):  # More than 10% of frames show inconsistencies\n        return \"Real\"\n    else:\n        return \"Fake\"","metadata":{"execution":{"iopub.status.busy":"2024-07-09T18:33:26.889698Z","iopub.execute_input":"2024-07-09T18:33:26.890026Z","iopub.status.idle":"2024-07-09T18:33:27.005419Z","shell.execute_reply.started":"2024-07-09T18:33:26.889968Z","shell.execute_reply":"2024-07-09T18:33:27.004843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\ndef display_frames(frames, num_display=5):\n    plt.figure(figsize=(10, 5))\n    for i in range(min(num_display, len(frames))):\n        plt.subplot(2, num_display, i+1)\n        plt.imshow(cv2.cvtColor(frames[i], cv2.COLOR_BGR2RGB))\n        plt.title(f\"Frame {i+1}\")\n        plt.axis('off')\n        \n        heat = heatmap(frames[i])\n        plt.subplot(2, num_display, i + 1)\n        plt.imshow(cv2.cvtColor(heat, cv2.COLOR_BGR2RGB))\n        plt.title(f\"Heatmap {i+1}\")\n        plt.axis('off')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-09T18:33:27.006711Z","iopub.execute_input":"2024-07-09T18:33:27.007052Z","iopub.status.idle":"2024-07-09T18:33:27.015617Z","shell.execute_reply.started":"2024-07-09T18:33:27.006990Z","shell.execute_reply":"2024-07-09T18:33:27.014804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=[]\nfor i in df.index:\n    frames=extract_frames(os.path.join(a,i))\n    ans=compare_heatmaps(frames)\n    if(ans=='Fake'):\n        display_frames(frames)\n    y_pred.append(ans)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2024-07-09T18:33:27.016874Z","iopub.execute_input":"2024-07-09T18:33:27.017112Z","iopub.status.idle":"2024-07-09T18:41:40.617080Z","shell.execute_reply.started":"2024-07-09T18:33:27.017071Z","shell.execute_reply":"2024-07-09T18:41:40.616239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2024-07-09T18:41:40.618399Z","iopub.execute_input":"2024-07-09T18:41:40.618708Z","iopub.status.idle":"2024-07-09T18:41:40.630347Z","shell.execute_reply.started":"2024-07-09T18:41:40.618650Z","shell.execute_reply":"2024-07-09T18:41:40.629335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predict=[1 if i==\"Real\" else 0 for i in y_pred]\ny_true=[1 if i==\"REAL\" else 0 for i in y]","metadata":{"execution":{"iopub.status.busy":"2024-07-09T18:42:00.313811Z","iopub.execute_input":"2024-07-09T18:42:00.314139Z","iopub.status.idle":"2024-07-09T18:42:00.318867Z","shell.execute_reply.started":"2024-07-09T18:42:00.314076Z","shell.execute_reply":"2024-07-09T18:42:00.318068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_predict)\nprint(y_true)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2024-07-09T18:42:01.634633Z","iopub.execute_input":"2024-07-09T18:42:01.634982Z","iopub.status.idle":"2024-07-09T18:42:01.639350Z","shell.execute_reply.started":"2024-07-09T18:42:01.634924Z","shell.execute_reply":"2024-07-09T18:42:01.638607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix, classification_report\n\n\n# Calculate evaluation metrics\naccuracy = accuracy_score(y_true, y_predict)\nprecision = precision_score(y_true, y_predict)\nrecall = recall_score(y_true, y_predict)\nf1 = f1_score(y_true, y_predict)\nconf_matrix = confusion_matrix(y_true, y_predict)\nclass_report = classification_report(y_true, y_predict)\n\n# Print the results\nprint(f\"Accuracy: {accuracy:.2f}\")\nprint(f\"Precision: {precision:.2f}\")\nprint(f\"Recall: {recall:.2f}\")\nprint(f\"F1 Score: {f1:.2f}\")\nprint(\"Confusion Matrix:\")\nprint(conf_matrix)\nprint(\"Classification Report:\")\nprint(class_report)","metadata":{"execution":{"iopub.status.busy":"2024-07-09T18:42:02.707779Z","iopub.execute_input":"2024-07-09T18:42:02.708102Z","iopub.status.idle":"2024-07-09T18:42:03.991965Z","shell.execute_reply.started":"2024-07-09T18:42:02.708046Z","shell.execute_reply":"2024-07-09T18:42:03.991209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def test_video(video):\n    print(video)\n    frames=extract_frames(video)\n    ans=compare_heatmaps(frames)\n    display_frames(frames)\n    return ans","metadata":{"execution":{"iopub.status.busy":"2024-07-09T18:42:05.262041Z","iopub.execute_input":"2024-07-09T18:42:05.262341Z","iopub.status.idle":"2024-07-09T18:42:05.267000Z","shell.execute_reply.started":"2024-07-09T18:42:05.262298Z","shell.execute_reply":"2024-07-09T18:42:05.266238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"video='/kaggle/input/deepfake/fakevideo.mp4'\nres=test_video(video)\nprint(res)","metadata":{"execution":{"iopub.status.busy":"2024-07-09T18:42:06.959631Z","iopub.execute_input":"2024-07-09T18:42:06.959942Z","iopub.status.idle":"2024-07-09T18:42:07.844290Z","shell.execute_reply.started":"2024-07-09T18:42:06.959897Z","shell.execute_reply":"2024-07-09T18:42:07.843608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}