{"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":"# <h1 style='background:#2cab6c; border:0; color:white'><center>Importing Libraries</center></h1>","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom glob import glob\nimport seaborn as sns\nimport plotly.express as px\nfrom base64 import b64encode\nimport matplotlib.pylab as plt\nfrom IPython.display import HTML\nimport plotly.graph_objects as go\n\nfrom sklearn.metrics import matthews_corrcoef","metadata":{"execution":{"iopub.status.busy":"2022-12-06T08:33:33.572440Z","iopub.execute_input":"2022-12-06T08:33:33.572900Z","iopub.status.idle":"2022-12-06T08:33:35.741510Z","shell.execute_reply.started":"2022-12-06T08:33:33.572814Z","shell.execute_reply":"2022-12-06T08:33:35.740709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style='background:#2cab6c; border:0; color:white'><center>Config</center></h1>","metadata":{}},{"cell_type":"code","source":"class CFG:\n    class path:\n        to_train_videos = \"/kaggle/input/nfl-player-contact-detection/train/*.mp4\"\n        to_train_labels = \"/kaggle/input/nfl-player-contact-detection/train_labels.csv\"\n        to_train_video_metadata = \"/kaggle/input/nfl-player-contact-detection/train_video_metadata.csv\"\n        to_train_player_tracking = \"/kaggle/input/nfl-player-contact-detection/train_player_tracking.csv\"\n        to_train_baseline_helmets = \"/kaggle/input/nfl-player-contact-detection/train_baseline_helmets.csv\"","metadata":{"execution":{"iopub.status.busy":"2022-12-06T08:33:35.744182Z","iopub.execute_input":"2022-12-06T08:33:35.744532Z","iopub.status.idle":"2022-12-06T08:33:35.750586Z","shell.execute_reply.started":"2022-12-06T08:33:35.744500Z","shell.execute_reply":"2022-12-06T08:33:35.749594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Goal of the Competition\nThe goal of this competition is to detect external contact experienced by players during an NFL football game. You will use video and player tracking data to identify moments with contact to help improve player safety.","metadata":{}},{"cell_type":"markdown","source":"# [train/test] video","metadata":{}},{"cell_type":"markdown","source":"<h1 style='background:#2cab6c; border:0; color:white'><center>Play video sample</center></h1>","metadata":{}},{"cell_type":"code","source":"video_gen = glob('/kaggle/input/nfl-player-contact-detection/train/*.mp4', recursive = True)\nlen(video_gen)","metadata":{"execution":{"iopub.status.busy":"2022-12-06T08:33:35.752010Z","iopub.execute_input":"2022-12-06T08:33:35.752517Z","iopub.status.idle":"2022-12-06T08:33:35.936388Z","shell.execute_reply.started":"2022-12-06T08:33:35.752487Z","shell.execute_reply":"2022-12-06T08:33:35.935710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def play(filename: str):\n    video = open(filename,'rb').read()\n    src = f'data:video/mp4;base64,{b64encode(video).decode()}'\n    html = f'<video width=500 controls autoplay loop><source src=\"{src}\" type=\"video/mp4\"></video>'\n\n    return HTML(html)\nplay(video_gen[0])","metadata":{"execution":{"iopub.status.busy":"2022-12-06T08:33:35.938276Z","iopub.execute_input":"2022-12-06T08:33:35.938773Z","iopub.status.idle":"2022-12-06T08:33:36.297095Z","shell.execute_reply.started":"2022-12-06T08:33:35.938746Z","shell.execute_reply":"2022-12-06T08:33:36.296114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style='background:#2cab6c; border:0; color:white'><center>Data Description</center></h1>","metadata":{}},{"cell_type":"markdown","source":"## [train/test] video_metadata.csv\nMetadata for each sideline and endzone video file including the timestamp information to be used to sync with player tracking data.\n\n- game_play: Unique game key and play id combination for the play.\n- game_key: the ID code for the game.\n- play_id: the ID code for the play.\n- view: The video view, either Sideline or Endzone\n- start_time: The timestamp of the video start.\n- end_time: The timestamp when the video ends.\n- snap_time: The timestamp when the play starts within the video. This is 5 seconds (300 frames) into the video.","metadata":{}},{"cell_type":"code","source":"video_metadata_df = pd.read_csv(CFG.path.to_train_video_metadata)\nvideo_metadata_df[\"end_time\"] = video_metadata_df.end_time.apply(pd.to_datetime)\nvideo_metadata_df[\"start_time\"] = video_metadata_df.start_time.apply(pd.to_datetime)\n\nvideo_metadata_df[\"duration\"] = (video_metadata_df[\"end_time\"] - video_metadata_df[\"start_time\"]).dt.total_seconds()\n\nprint(f\"Shape => {video_metadata_df.shape}\")\nvideo_metadata_df.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-12-06T08:33:36.298056Z","iopub.execute_input":"2022-12-06T08:33:36.298296Z","iopub.status.idle":"2022-12-06T08:33:36.425585Z","shell.execute_reply.started":"2022-12-06T08:33:36.298273Z","shell.execute_reply":"2022-12-06T08:33:36.424543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"video_metadata_df.game_play.nunique()","metadata":{"execution":{"iopub.status.busy":"2022-12-06T08:33:36.426836Z","iopub.execute_input":"2022-12-06T08:33:36.427122Z","iopub.status.idle":"2022-12-06T08:33:36.436613Z","shell.execute_reply.started":"2022-12-06T08:33:36.427095Z","shell.execute_reply":"2022-12-06T08:33:36.435564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"video_metadata_df.view.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-12-06T08:33:36.438316Z","iopub.execute_input":"2022-12-06T08:33:36.438724Z","iopub.status.idle":"2022-12-06T08:33:36.449608Z","shell.execute_reply.started":"2022-12-06T08:33:36.438695Z","shell.execute_reply":"2022-12-06T08:33:36.448767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"video_metadata_df.play_id.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-12-06T08:33:36.450901Z","iopub.execute_input":"2022-12-06T08:33:36.451148Z","iopub.status.idle":"2022-12-06T08:33:36.462441Z","shell.execute_reply.started":"2022-12-06T08:33:36.451125Z","shell.execute_reply":"2022-12-06T08:33:36.461622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.histogram(video_metadata_df, x='view', color='view')","metadata":{"execution":{"iopub.status.busy":"2022-12-06T08:33:36.463703Z","iopub.execute_input":"2022-12-06T08:33:36.464010Z","iopub.status.idle":"2022-12-06T08:33:37.603279Z","shell.execute_reply.started":"2022-12-06T08:33:36.463979Z","shell.execute_reply":"2022-12-06T08:33:37.602550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.box(x=video_metadata_df[\"duration\"],\n             template=\"simple_white\",\n             title= \"Distribution of duration\",\n             labels={'x': 'Time (in sec.)'},\n             \n             color_discrete_sequence=px.colors.sequential.Burg_r)\n\nfig.update_layout(coloraxis_showscale=False)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-06T08:33:37.605607Z","iopub.execute_input":"2022-12-06T08:33:37.606788Z","iopub.status.idle":"2022-12-06T08:33:37.744919Z","shell.execute_reply.started":"2022-12-06T08:33:37.606754Z","shell.execute_reply":"2022-12-06T08:33:37.743975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## train_labels.csv \nContains a row for every combination of players, and players with the ground for each 0.1 second timestamp in the play.\n\n- contact_id: A combination of the game_play, player_ids and step columns.\n- game_play: the unique ID for the game and play.\n- nfl_player_id_1 The lower numbered player id in the contact pair. If contact with ground then this is just the player id.\n- nfl_player_id_2: The larger number player id in the contact pair. If for contact with the ground, this will contain an uppercase \"G\"\n- step: A number representing each each timestep for each play, starting at 0 at the moment of the play starting, and incrementing by 1 every 0.1 seconds.\n- datetime: The timetamp of the contact, at 10Hz\n- contact: Whether contact occurred","metadata":{}},{"cell_type":"code","source":"labels_df = pd.read_csv(CFG.path.to_train_labels)\n\nprint(f\"Shape => {labels_df.shape}\")\nlabels_df.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-12-06T08:33:37.746094Z","iopub.execute_input":"2022-12-06T08:33:37.746400Z","iopub.status.idle":"2022-12-06T08:33:46.726550Z","shell.execute_reply.started":"2022-12-06T08:33:37.746372Z","shell.execute_reply":"2022-12-06T08:33:46.725524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.game_play.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-12-06T08:33:46.727839Z","iopub.execute_input":"2022-12-06T08:33:46.728183Z","iopub.status.idle":"2022-12-06T08:33:46.896452Z","shell.execute_reply.started":"2022-12-06T08:33:46.728151Z","shell.execute_reply":"2022-12-06T08:33:46.895165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## [train/test] baseline_helmets.csv\nContains imperfect baseline predictions for helmet boxes and player assignments for the Sideline and Endzone video view. The model used to create these predictions are from the winning solution from last year's competition and can we used to leverage your predictions.\n\n- game_play: Unique game key and play id combination for the play.\n- game_key: the ID code for the game.\n- play_id: the ID code for the play.\n- view: The video view, either Sideline or Endzone\n- video: The filename of the associated video.\n- frame: The associated frame within the video.\n- nfl_player_id: The imperfect predicted player id.\n- player_label: The player label. A combination of V/H (home or visiting team) and the player jersey number.","metadata":{}},{"cell_type":"code","source":"baseline_helmets_df = pd.read_csv(CFG.path.to_train_baseline_helmets)\n\nprint(f\"Shape => {baseline_helmets_df.shape}\")\nbaseline_helmets_df.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-12-06T08:33:46.899036Z","iopub.execute_input":"2022-12-06T08:33:46.900107Z","iopub.status.idle":"2022-12-06T08:33:53.300108Z","shell.execute_reply.started":"2022-12-06T08:33:46.900051Z","shell.execute_reply":"2022-12-06T08:33:53.297914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for column in  [\"view\"]:\n    fig = px.histogram(baseline_helmets_df, x=column, color=column)\n    fig.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-06T08:33:53.301461Z","iopub.execute_input":"2022-12-06T08:33:53.303116Z","iopub.status.idle":"2022-12-06T08:34:06.413961Z","shell.execute_reply.started":"2022-12-06T08:33:53.303084Z","shell.execute_reply":"2022-12-06T08:34:06.413257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## [train/test] player_tracking.csv \nEach player wears a sensor that allows us to locate them on the field; that information is reported in these two files.\n\n- game_play: Unique game key and play id combination for the play.\n- game_key: the ID code for the game.\n- play_id: the ID code for the play.\n- nfl_player_id: the player's ID code.\n- datetime: timestamp at 10 Hz.\n- step: timestep within play relative to the play start.\n- position: the football position of the player.\n- team: team of the player, either home or away.\n- jersey_number: Player jersey number\n- x_position: player position along the long axis of the field. See figure below.\n- y_position: player position along the short axis of the field. See figure below.\n- speed: speed in yards/second.\n- distance: distance traveled from prior time point, in yards.\n- orientation: orientation of player (deg).\n- direction: angle of player motion (deg).\n- event: game events like a snap, whistle, etc.\n- acceleration: magnitiude of the total acceleration in yards/second^2.\n- sa: Signed acceleration yards/second^2 in the direction the player is moving.","metadata":{}},{"cell_type":"code","source":"player_tracking_df = pd.read_csv(CFG.path.to_train_player_tracking)\n\nprint(f\"Shape => {player_tracking_df.shape}\")\nplayer_tracking_df.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-12-06T08:34:06.414814Z","iopub.execute_input":"2022-12-06T08:34:06.415200Z","iopub.status.idle":"2022-12-06T08:34:09.957714Z","shell.execute_reply.started":"2022-12-06T08:34:06.415177Z","shell.execute_reply":"2022-12-06T08:34:09.956847Z"},"trusted":true},"execution_count":null,"outputs":[]}]}