{"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":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nfrom IPython.display import Video, display\nfrom scipy.optimize import minimize\nimport cv2\nfrom glob import glob\nfrom tqdm import tqdm\nfrom PIL import Image, ImageDraw","metadata":{"execution":{"iopub.status.busy":"2022-12-14T06:36:30.683024Z","iopub.execute_input":"2022-12-14T06:36:30.683414Z","iopub.status.idle":"2022-12-14T06:36:31.189759Z","shell.execute_reply.started":"2022-12-14T06:36:30.683383Z","shell.execute_reply":"2022-12-14T06:36:31.188615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nimport os\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2022-12-14T06:46:59.814748Z","iopub.execute_input":"2022-12-14T06:46:59.815231Z","iopub.status.idle":"2022-12-14T06:46:59.820468Z","shell.execute_reply.started":"2022-12-14T06:46:59.815165Z","shell.execute_reply":"2022-12-14T06:46:59.819493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ENV_DIR = '../input'\nDATA_DIR = f'{ENV_DIR}/nfl-player-contact-detection'","metadata":{"execution":{"iopub.status.busy":"2022-12-14T06:47:02.527011Z","iopub.execute_input":"2022-12-14T06:47:02.527710Z","iopub.status.idle":"2022-12-14T06:47:02.532474Z","shell.execute_reply.started":"2022-12-14T06:47:02.527674Z","shell.execute_reply":"2022-12-14T06:47:02.531270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(DATA_DIR)","metadata":{"execution":{"iopub.status.busy":"2022-12-14T06:47:03.990412Z","iopub.execute_input":"2022-12-14T06:47:03.991290Z","iopub.status.idle":"2022-12-14T06:47:04.000540Z","shell.execute_reply.started":"2022-12-14T06:47:03.991243Z","shell.execute_reply":"2022-12-14T06:47:03.999384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(f'{DATA_DIR}/train_labels.csv')\n\n# Tracking Information using Sensor\ntrain_tracking_df = pd.read_csv(f'{DATA_DIR}/train_player_tracking.csv')\ntest_tracking_df = pd.read_csv(f'{DATA_DIR}/test_player_tracking.csv')\n\n# Baseline Prediction - Trained by images inside folder images/\ntrain_predict_df = pd.read_csv(f'{DATA_DIR}/train_baseline_helmets.csv')\n\ntest_predict_df = pd.read_csv(f'{DATA_DIR}/test_baseline_helmets.csv')","metadata":{"execution":{"iopub.status.busy":"2022-12-14T06:49:05.052944Z","iopub.execute_input":"2022-12-14T06:49:05.053381Z","iopub.status.idle":"2022-12-14T06:49:16.488038Z","shell.execute_reply.started":"2022-12-14T06:49:05.053343Z","shell.execute_reply":"2022-12-14T06:49:16.486794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## convert vedio into frame","metadata":{}},{"cell_type":"code","source":"import cv2\nimport numpy as np\nvidObj = cv2.VideoCapture('/kaggle/input/nfl-player-contact-detection/train/58172_003247_All29.mp4')\ncount = 0\n\nwhile True: \n      \n    success, image = vidObj.read() \n    \n    if success:\n        cv2.imwrite(f\"frame{count}.jpg\", image) \n    else: \n        break\n        \n    count += 1","metadata":{"execution":{"iopub.status.busy":"2022-12-14T07:31:54.741444Z","iopub.execute_input":"2022-12-14T07:31:54.742142Z","iopub.status.idle":"2022-12-14T07:32:39.115783Z","shell.execute_reply.started":"2022-12-14T07:31:54.742105Z","shell.execute_reply":"2022-12-14T07:32:39.114533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n\nfor i in range(0, 1300, 100):\n    \n    imgplot = plt.imshow(img)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-14T07:37:05.119222Z","iopub.execute_input":"2022-12-14T07:37:05.119989Z","iopub.status.idle":"2022-12-14T07:37:10.813016Z","shell.execute_reply.started":"2022-12-14T07:37:05.119950Z","shell.execute_reply":"2022-12-14T07:37:10.811963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Get Image by frame","metadata":{}},{"cell_type":"code","source":"def get_frame_from_video(video_path, frame):\n    video_path = f\"{DATA_DIR}/train/{video_path}\"\n    frame = frame - 1\n    \n    !ffmpeg \\\n        -hide_banner \\\n        -loglevel fatal \\\n        -nostats \\\n        -i $video_path -vf \"select=eq(n\\,$frame)\" -vframes 1 frame.png\n    \n    img = Image.open('frame.png')\n    os.remove('frame.png')\n    return img","metadata":{"execution":{"iopub.status.busy":"2022-12-14T06:49:41.692850Z","iopub.execute_input":"2022-12-14T06:49:41.693234Z","iopub.status.idle":"2022-12-14T06:49:41.700275Z","shell.execute_reply.started":"2022-12-14T06:49:41.693203Z","shell.execute_reply":"2022-12-14T06:49:41.699246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_frame_from_video('58168_003392_All29.mp4', 1)","metadata":{"execution":{"iopub.status.busy":"2022-12-14T06:50:08.688395Z","iopub.execute_input":"2022-12-14T06:50:08.689358Z","iopub.status.idle":"2022-12-14T06:50:11.012749Z","shell.execute_reply.started":"2022-12-14T06:50:08.689289Z","shell.execute_reply":"2022-12-14T06:50:11.011228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Video Play\n* Let's enjoy the video and see whats the game looks like.","metadata":{}},{"cell_type":"code","source":"from IPython.display import Video, display\n\ndef video(video_path, ratio=0.7):\n    nfl_video = Video(f\"{DATA_DIR}/train/{video_path}\",\n                      embed=True,\n                      height=int(720 * ratio),\n                      width=int(1280 * ratio))\n    return nfl_video\n    \nvideo('58168_003392_All29.mp4')","metadata":{"execution":{"iopub.status.busy":"2022-12-14T06:51:09.554689Z","iopub.execute_input":"2022-12-14T06:51:09.555195Z","iopub.status.idle":"2022-12-14T06:51:09.816904Z","shell.execute_reply.started":"2022-12-14T06:51:09.555153Z","shell.execute_reply":"2022-12-14T06:51:09.815387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_tracking_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-14T06:52:53.629382Z","iopub.execute_input":"2022-12-14T06:52:53.629769Z","iopub.status.idle":"2022-12-14T06:52:53.653932Z","shell.execute_reply.started":"2022-12-14T06:52:53.629731Z","shell.execute_reply":"2022-12-14T06:52:53.652786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Is test videos subset of train videos?\n yes","metadata":{}},{"cell_type":"code","source":"train_videos = os.listdir(f'{DATA_DIR}/train')\ntest_videos = os.listdir(f'{DATA_DIR}/test')\n\nlen(train_videos), len(test_videos)","metadata":{"execution":{"iopub.status.busy":"2022-12-14T06:53:58.444602Z","iopub.execute_input":"2022-12-14T06:53:58.444984Z","iopub.status.idle":"2022-12-14T06:53:58.781403Z","shell.execute_reply.started":"2022-12-14T06:53:58.444953Z","shell.execute_reply":"2022-12-14T06:53:58.780361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"set(test_videos).issubset(set(train_videos))","metadata":{"execution":{"iopub.status.busy":"2022-12-14T06:54:07.518176Z","iopub.execute_input":"2022-12-14T06:54:07.518881Z","iopub.status.idle":"2022-12-14T06:54:07.526174Z","shell.execute_reply.started":"2022-12-14T06:54:07.518845Z","shell.execute_reply":"2022-12-14T06:54:07.525169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## How many train videos are there?\n* Total 720! Sideline and Endzone video pair for every plays!\n\n* train 720 videos\n > Endzone 240\n\n > Sideline 480","metadata":{}},{"cell_type":"code","source":"end_count = 0\nside_count = 0\nendzone_list = []\nsideline_list = []\nfor train_video in train_videos:\n    name = train_video.split('.')[0]\n    video_id, play_id, view = name.split('_')\n    \n    if view == \"Endzone\":\n        endzone_list.append('_'.join([video_id, play_id]))\n        end_count += 1\n    else:\n        sideline_list.append('_'.join([video_id, play_id]))\n        side_count += 1\n\nprint(end_count, side_count)","metadata":{"execution":{"iopub.status.busy":"2022-12-14T06:54:28.533726Z","iopub.execute_input":"2022-12-14T06:54:28.534091Z","iopub.status.idle":"2022-12-14T06:54:28.542326Z","shell.execute_reply.started":"2022-12-14T06:54:28.534052Z","shell.execute_reply":"2022-12-14T06:54:28.541364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# One game video includes endzone and sideline view\nlen(set(endzone_list)), len(set(sideline_list)), set(endzone_list) == set(sideline_list)","metadata":{"execution":{"iopub.status.busy":"2022-12-14T06:56:16.302673Z","iopub.execute_input":"2022-12-14T06:56:16.303075Z","iopub.status.idle":"2022-12-14T06:56:16.310366Z","shell.execute_reply.started":"2022-12-14T06:56:16.303044Z","shell.execute_reply":"2022-12-14T06:56:16.309041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-12-14T06:59:58.208729Z","iopub.execute_input":"2022-12-14T06:59:58.209137Z","iopub.status.idle":"2022-12-14T06:59:58.227311Z","shell.execute_reply.started":"2022-12-14T06:59:58.209105Z","shell.execute_reply":"2022-12-14T06:59:58.226398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_frame_from_video('58172_003247_Sideline.mp4', 149)","metadata":{"execution":{"iopub.status.busy":"2022-12-14T07:02:41.662680Z","iopub.execute_input":"2022-12-14T07:02:41.663043Z","iopub.status.idle":"2022-12-14T07:02:43.762098Z","shell.execute_reply.started":"2022-12-14T07:02:41.663015Z","shell.execute_reply":"2022-12-14T07:02:43.760773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"video('58172_003247_Sideline.mp4')","metadata":{"execution":{"iopub.status.busy":"2022-12-14T07:02:59.729327Z","iopub.execute_input":"2022-12-14T07:02:59.730431Z","iopub.status.idle":"2022-12-14T07:03:00.297822Z","shell.execute_reply.started":"2022-12-14T07:02:59.730366Z","shell.execute_reply":"2022-12-14T07:03:00.293391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_tracking_df","metadata":{"execution":{"iopub.status.busy":"2022-12-14T07:04:38.320829Z","iopub.execute_input":"2022-12-14T07:04:38.321190Z","iopub.status.idle":"2022-12-14T07:04:38.356571Z","shell.execute_reply.started":"2022-12-14T07:04:38.321159Z","shell.execute_reply":"2022-12-14T07:04:38.355615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_tracking_df.game_key.nunique()","metadata":{"execution":{"iopub.status.busy":"2022-12-14T07:03:57.530099Z","iopub.execute_input":"2022-12-14T07:03:57.530571Z","iopub.status.idle":"2022-12-14T07:03:57.546841Z","shell.execute_reply.started":"2022-12-14T07:03:57.530537Z","shell.execute_reply":"2022-12-14T07:03:57.545804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_tracking_df.play_id.nunique()","metadata":{"execution":{"iopub.status.busy":"2022-12-14T07:04:28.891696Z","iopub.execute_input":"2022-12-14T07:04:28.892044Z","iopub.status.idle":"2022-12-14T07:04:28.908475Z","shell.execute_reply.started":"2022-12-14T07:04:28.892016Z","shell.execute_reply":"2022-12-14T07:04:28.907517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 97 ~ 398 images per play\nframe_df = train_tracking_df.groupby(['game_key', 'play_id'])['datetime'].nunique().reset_index()\nframe_df","metadata":{"execution":{"iopub.status.busy":"2022-12-14T07:05:35.953283Z","iopub.execute_input":"2022-12-14T07:05:35.953996Z","iopub.status.idle":"2022-12-14T07:05:36.185994Z","shell.execute_reply.started":"2022-12-14T07:05:35.953961Z","shell.execute_reply":"2022-12-14T07:05:36.184781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(frame_df.datetime);","metadata":{"execution":{"iopub.status.busy":"2022-12-14T07:10:35.921624Z","iopub.execute_input":"2022-12-14T07:10:35.922156Z","iopub.status.idle":"2022-12-14T07:10:36.325359Z","shell.execute_reply.started":"2022-12-14T07:10:35.922124Z","shell.execute_reply":"2022-12-14T07:10:36.324258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frame_df.datetime.sum()","metadata":{"execution":{"iopub.status.busy":"2022-12-14T07:10:49.435459Z","iopub.execute_input":"2022-12-14T07:10:49.435925Z","iopub.status.idle":"2022-12-14T07:10:49.451314Z","shell.execute_reply.started":"2022-12-14T07:10:49.435893Z","shell.execute_reply":"2022-12-14T07:10:49.450130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frame_df.datetime.min(), frame_df.datetime.max()","metadata":{"execution":{"iopub.status.busy":"2022-12-14T07:11:11.207845Z","iopub.execute_input":"2022-12-14T07:11:11.208278Z","iopub.status.idle":"2022-12-14T07:11:11.218276Z","shell.execute_reply.started":"2022-12-14T07:11:11.208239Z","shell.execute_reply":"2022-12-14T07:11:11.217177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_predict_df","metadata":{"execution":{"iopub.status.busy":"2022-12-14T07:20:36.926594Z","iopub.execute_input":"2022-12-14T07:20:36.926967Z","iopub.status.idle":"2022-12-14T07:20:36.952139Z","shell.execute_reply.started":"2022-12-14T07:20:36.926920Z","shell.execute_reply":"2022-12-14T07:20:36.951073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_predict_df['helmet_size'] = train_predict_df.width * train_predict_df.height\ntrain_predict_df.helmet_size.hist();","metadata":{"execution":{"iopub.status.busy":"2022-12-14T07:18:02.884433Z","iopub.execute_input":"2022-12-14T07:18:02.884828Z","iopub.status.idle":"2022-12-14T07:18:03.240253Z","shell.execute_reply.started":"2022-12-14T07:18:02.884796Z","shell.execute_reply":"2022-12-14T07:18:03.239238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_predict_df.helmet_size.value_counts()[:10]","metadata":{"execution":{"iopub.status.busy":"2022-12-14T07:18:28.516713Z","iopub.execute_input":"2022-12-14T07:18:28.517272Z","iopub.status.idle":"2022-12-14T07:18:28.564141Z","shell.execute_reply.started":"2022-12-14T07:18:28.517231Z","shell.execute_reply":"2022-12-14T07:18:28.562085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Few helmets shown large at videos\ntrain_predict_df.helmet_size.value_counts()[-10:]","metadata":{"execution":{"iopub.status.busy":"2022-12-14T07:18:45.882505Z","iopub.execute_input":"2022-12-14T07:18:45.882858Z","iopub.status.idle":"2022-12-14T07:18:45.923839Z","shell.execute_reply.started":"2022-12-14T07:18:45.882830Z","shell.execute_reply":"2022-12-14T07:18:45.922861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The biggest size of helmet is 5244\ntrain_predict_df.helmet_size.max(), train_predict_df.helmet_size.min()","metadata":{"execution":{"iopub.status.busy":"2022-12-14T07:23:30.997064Z","iopub.execute_input":"2022-12-14T07:23:30.997455Z","iopub.status.idle":"2022-12-14T07:23:31.015128Z","shell.execute_reply.started":"2022-12-14T07:23:30.997424Z","shell.execute_reply":"2022-12-14T07:23:31.014242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_predict_df.query('helmet_size == 5244')","metadata":{"execution":{"iopub.status.busy":"2022-12-14T07:23:33.440026Z","iopub.execute_input":"2022-12-14T07:23:33.440407Z","iopub.status.idle":"2022-12-14T07:23:33.486844Z","shell.execute_reply.started":"2022-12-14T07:23:33.440377Z","shell.execute_reply":"2022-12-14T07:23:33.485684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_frame_from_video('58204_002864_Endzone.mp4', 624)","metadata":{"execution":{"iopub.status.busy":"2022-12-14T07:23:58.045350Z","iopub.execute_input":"2022-12-14T07:23:58.046048Z","iopub.status.idle":"2022-12-14T07:24:01.677444Z","shell.execute_reply.started":"2022-12-14T07:23:58.046012Z","shell.execute_reply":"2022-12-14T07:24:01.676370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check through the video\nvideo('57686_002546_Endzone.mp4')","metadata":{"execution":{"iopub.status.busy":"2022-12-14T07:24:05.960138Z","iopub.execute_input":"2022-12-14T07:24:05.960908Z","iopub.status.idle":"2022-12-14T07:24:05.969184Z","shell.execute_reply.started":"2022-12-14T07:24:05.960840Z","shell.execute_reply":"2022-12-14T07:24:05.968107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_predict_df.query('helmet_size == 850')","metadata":{"execution":{"iopub.status.busy":"2022-12-14T07:24:31.361924Z","iopub.execute_input":"2022-12-14T07:24:31.362288Z","iopub.status.idle":"2022-12-14T07:24:31.402765Z","shell.execute_reply.started":"2022-12-14T07:24:31.362256Z","shell.execute_reply":"2022-12-14T07:24:31.401708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# when does the helmet shown the smallest?\nget_frame_from_video('58168_003392_Endzone.mp4', 290)","metadata":{"execution":{"iopub.status.busy":"2022-12-14T07:25:35.695778Z","iopub.execute_input":"2022-12-14T07:25:35.696166Z","iopub.status.idle":"2022-12-14T07:25:38.154699Z","shell.execute_reply.started":"2022-12-14T07:25:35.696134Z","shell.execute_reply":"2022-12-14T07:25:38.153548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check through the video\nvideo('58168_003392_Endzone.mp4')","metadata":{"execution":{"iopub.status.busy":"2022-12-14T07:26:25.777500Z","iopub.execute_input":"2022-12-14T07:26:25.778898Z","iopub.status.idle":"2022-12-14T07:26:26.180385Z","shell.execute_reply.started":"2022-12-14T07:26:25.778853Z","shell.execute_reply":"2022-12-14T07:26:26.178886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## More analysis on test data coming soon","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}