{"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=\"font-size:300%; color:blue;\"> 🦆 NFL Starter EDA 🦆 </h1>\n\n<h2> This is a starter EDA aimed at understanding literally the dataset itself. Hope this could be a guide to someone just entered the competition like me and having problems understanding the dataset.\nThere will be no fancy visualization or plot, just focusing on understanding each `csv` files.</h2>\n","metadata":{}},{"cell_type":"markdown","source":"This notebook is copied from [@Ducky](https://www.kaggle.com/illgamhoduck) [https://www.kaggle.com/code/illgamhoduck/nfl-starter-eda](https://www.kaggle.com/code/illgamhoduck/nfl-starter-eda) and I modified the EDA based on this year's competition dataset. if there are any mistakes, please contact me.","metadata":{}},{"cell_type":"markdown","source":"<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n  <h3 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" role=\"tab\" aria-controls=\"home\">Index</h3>\n  <a class=\"list-group-item list-group-item-action\" data-toggle=\"list\" href=\"#🚀-Environment-Setting\" role=\"tab\" aria-controls=\"profile\">🚀 Environment Setting<span class=\"badge badge-primary badge-pill\">1</span></a>\n  <a class=\"list-group-item list-group-item-action\" data-toggle=\"list\" href=\"#🚀-Helper-functions\" role=\"tab\" aria-controls=\"messages\">🚀 Helper functions<span class=\"badge badge-primary badge-pill\">2</span></a>\n  <a class=\"list-group-item list-group-item-action\"  data-toggle=\"list\" href=\"#🚀-EDA-(Exploratory-Data-Analysis)\" role=\"tab\" aria-controls=\"settings\">🚀EDA (Exploratory Data Analysis)<span class=\"badge badge-primary badge-pill\">3</span></a>\n","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:14:35.561248Z","start_time":"2021-09-09T02:14:35.551355Z"}}},{"cell_type":"markdown","source":"# 🚀 Environment Setting","metadata":{"heading_collapsed":true}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport cv2\nfrom PIL import Image, ImageDraw\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:12:30.362556Z","start_time":"2021-09-09T02:12:29.197345Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:12:52.346781Z","iopub.execute_input":"2023-01-08T04:12:52.347816Z","iopub.status.idle":"2023-01-08T04:12:52.352805Z","shell.execute_reply.started":"2023-01-08T04:12:52.347773Z","shell.execute_reply":"2023-01-08T04:12:52.351973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:23:19.581791Z","start_time":"2021-09-09T02:23:19.579227Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:12:52.354148Z","iopub.execute_input":"2023-01-08T04:12:52.354681Z","iopub.status.idle":"2023-01-08T04:12:52.366696Z","shell.execute_reply.started":"2023-01-08T04:12:52.354637Z","shell.execute_reply":"2023-01-08T04:12:52.365728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ENV_DIR = '../input'\nDATA_DIR = f'{ENV_DIR}/nfl-player-contact-detection'","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:12:30.36554Z","start_time":"2021-09-09T02:12:30.363653Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:12:52.368222Z","iopub.execute_input":"2023-01-08T04:12:52.369206Z","iopub.status.idle":"2023-01-08T04:12:52.378838Z","shell.execute_reply.started":"2023-01-08T04:12:52.369159Z","shell.execute_reply":"2023-01-08T04:12:52.377906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(DATA_DIR)","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:12:30.377602Z","start_time":"2021-09-09T02:12:30.366704Z"},"hidden":true,"scrolled":true,"execution":{"iopub.status.busy":"2023-01-08T04:12:52.380153Z","iopub.execute_input":"2023-01-08T04:12:52.380602Z","iopub.status.idle":"2023-01-08T04:12:52.399289Z","shell.execute_reply.started":"2023-01-08T04:12:52.380561Z","shell.execute_reply":"2023-01-08T04:12:52.398032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training data\n# -----------------------------------------------------------------------\n\n# Bounding Box\ntrain_df = pd.read_csv(f'{DATA_DIR}/train_labels.csv', parse_dates=[\"datetime\"])\n\n# Tracking Information using Sensor\ntrain_tracking_df = pd.read_csv(f'{DATA_DIR}/train_player_tracking.csv', parse_dates=[\"datetime\"])\ntest_tracking_df = pd.read_csv(f'{DATA_DIR}/test_player_tracking.csv', parse_dates=[\"datetime\"])\n\n# Video metadata\ntrain_metadata_df = pd.read_csv(f'{DATA_DIR}/train_video_metadata.csv', parse_dates=[\"start_time\", \"end_time\", \"snap_time\"])\ntest_metadata_df = pd.read_csv(f'{DATA_DIR}/test_video_metadata.csv', parse_dates=[\"start_time\", \"end_time\", \"snap_time\"])\n\n# Baseline Prediction - Trained by images inside folder images/\ntrain_predict_df = pd.read_csv(f'{DATA_DIR}/train_baseline_helmets.csv')\ntest_predict_df = pd.read_csv(f'{DATA_DIR}/test_baseline_helmets.csv')","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:12:32.86269Z","start_time":"2021-09-09T02:12:30.378645Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:12:52.401939Z","iopub.execute_input":"2023-01-08T04:12:52.402616Z","iopub.status.idle":"2023-01-08T04:13:29.696824Z","shell.execute_reply.started":"2023-01-08T04:12:52.402579Z","shell.execute_reply":"2023-01-08T04:13:29.695737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🚀 Helper functions\n- Get Image by frame\n- Draw Bounding Box\n- Video play\n- Football Animation\n\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n  <h3 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" role=\"tab\" aria-controls=\"home\">Dealing with image and video</h3>\n  <a class=\"list-group-item list-group-item-action\" data-toggle=\"list\" href=\"#Get-Image-by-frame\" role=\"tab\" aria-controls=\"profile\">Get Image by frame<span class=\"badge badge-primary badge-pill\">1</span></a>\n  <a class=\"list-group-item list-group-item-action\" data-toggle=\"list\" href=\"#Draw-Bounding-Box\" role=\"tab\" aria-controls=\"messages\">Draw Bounding Box<span class=\"badge badge-primary badge-pill\">2</span></a>\n  <a class=\"list-group-item list-group-item-action\"  data-toggle=\"list\" href=\"#Video-Play\" role=\"tab\" aria-controls=\"settings\">Video Play<span class=\"badge badge-primary badge-pill\">3</span></a>\n  <a class=\"list-group-item list-group-item-action\" data-toggle=\"list\" href=\"#Football-Animation\" role=\"tab\" aria-controls=\"settings\">Football Animation<span class=\"badge badge-primary badge-pill\">4</span></a> \n\n<div class=\"alert alert-info\" role=\"alert\">\n    <a data-toggle=\"list\" href=\"#Index\" role=\"tab\" aria-controls=\"settings\">☝🏻 Back to Index</a>\n</div>","metadata":{"heading_collapsed":true}},{"cell_type":"markdown","source":"## Get Image by frame\n> Get frame image from video\n\nCode is based on https://www.kaggle.com/coldfir3/eda-helmet-keypoint-tracking-data-comparison","metadata":{"heading_collapsed":true,"hidden":true}},{"cell_type":"code","source":"# reference : https://www.kaggle.com/coldfir3/eda-helmet-keypoint-tracking-data-comparison\ndef 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)\" \\\n        -vframes 1 \\\n        frame.png\n\n    img = Image.open('frame.png')\n    os.remove('frame.png')\n    return img","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:12:32.867359Z","start_time":"2021-09-09T02:12:32.863821Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:13:29.698296Z","iopub.execute_input":"2023-01-08T04:13:29.698655Z","iopub.status.idle":"2023-01-08T04:13:29.705712Z","shell.execute_reply.started":"2023-01-08T04:13:29.698622Z","shell.execute_reply":"2023-01-08T04:13:29.704237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_frame_from_video('58168_003392_Sideline.mp4', 320)","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:12:33.711782Z","start_time":"2021-09-09T02:12:32.868211Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:13:29.707357Z","iopub.execute_input":"2023-01-08T04:13:29.708008Z","iopub.status.idle":"2023-01-08T04:13:32.098432Z","shell.execute_reply.started":"2023-01-08T04:13:29.707933Z","shell.execute_reply":"2023-01-08T04:13:32.097082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Draw Bounding Box\n> Just a simple bounding box drawing without label for EDA purpose","metadata":{"heading_collapsed":true,"hidden":true}},{"cell_type":"code","source":"def draw_rect(image, bbox_df):\n    new_image = image.copy()\n    draw = ImageDraw.Draw(new_image)\n    for _, (left, width, top, height) in bbox_df[['left', 'width', 'top', 'height']].iterrows():\n        draw.rectangle(((left, top), (left + width, top + height)), outline=(0, 255, 0), width=2)\n    return new_image","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:12:33.71706Z","start_time":"2021-09-09T02:12:33.713153Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:13:32.100475Z","iopub.execute_input":"2023-01-08T04:13:32.101379Z","iopub.status.idle":"2023-01-08T04:13:32.109082Z","shell.execute_reply.started":"2023-01-08T04:13:32.101325Z","shell.execute_reply":"2023-01-08T04:13:32.107774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def frame_bbox(df, video_frame):\n    video_name = '_'.join(video_frame.split('_')[:3]) + '.mp4'\n    frame = int(video_frame.split('_')[-1])\n    \n    image = get_frame_from_video(video_name, frame)\n    bbox_df = df.query('video == @video_name and frame == @frame')\n    \n    bbox_image = draw_rect(image, bbox_df)\n    \n    return bbox_image","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:12:33.729098Z","start_time":"2021-09-09T02:12:33.718985Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:13:32.111454Z","iopub.execute_input":"2023-01-08T04:13:32.111914Z","iopub.status.idle":"2023-01-08T04:13:32.121927Z","shell.execute_reply.started":"2023-01-08T04:13:32.111875Z","shell.execute_reply":"2023-01-08T04:13:32.120738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frame_bbox(train_predict_df, '58168_003392_Endzone_290')","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:12:34.465174Z","start_time":"2021-09-09T02:12:33.730187Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:13:32.127221Z","iopub.execute_input":"2023-01-08T04:13:32.128289Z","iopub.status.idle":"2023-01-08T04:13:34.625996Z","shell.execute_reply.started":"2023-01-08T04:13:32.128247Z","shell.execute_reply":"2023-01-08T04:13:34.624969Z"},"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":{"heading_collapsed":true,"hidden":true}},{"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_Endzone.mp4')","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:12:34.469525Z","start_time":"2021-09-09T02:12:34.466172Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:13:34.627899Z","iopub.execute_input":"2023-01-08T04:13:34.628561Z","iopub.status.idle":"2023-01-08T04:13:34.777753Z","shell.execute_reply.started":"2023-01-08T04:13:34.628518Z","shell.execute_reply":"2023-01-08T04:13:34.776425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Football Animation\n\n> football animation made by `ammarnassanalhajali`\n\n**Reference**\n- https://www.kaggle.com/ammarnassanalhajali/nfl-big-data-bowl-2021-animating-players\n- https://www.kaggle.com/robikscube/nfl-helmet-assignment-getting-started-guide","metadata":{"heading_collapsed":true,"hidden":true}},{"cell_type":"code","source":"import plotly.express as px\nimport plotly.graph_objects as go\nimport plotly\n\n\ndef add_plotly_field(fig):\n    # Reference https://www.kaggle.com/ammarnassanalhajali/nfl-big-data-bowl-2021-animating-players\n    fig.update_traces(marker_size=20)\n    \n    fig.update_layout(paper_bgcolor='#29a500', plot_bgcolor='#29a500', font_color='white',\n        width = 800,\n        height = 600,\n        title = \"\",\n        \n        xaxis = dict(\n        nticks = 10,\n        title = \"\",\n        visible=False\n        ),\n        \n        yaxis = dict(\n        scaleanchor = \"x\",\n        title = \"Temp\",\n        visible=False\n        ),\n        showlegend= True,\n  \n        annotations=[\n       dict(\n            x=-5,\n            y=26.65,\n            xref=\"x\",\n            yref=\"y\",\n            text=\"ENDZONE\",\n            font=dict(size=16,color=\"#e9ece7\"),\n            align='center',\n            showarrow=False,\n            yanchor='middle',\n            textangle=-90\n        ),\n        dict(\n            x=105,\n            y=26.65,\n            xref=\"x\",\n            yref=\"y\",\n            text=\"ENDZONE\",\n            font=dict(size=16,color=\"#e9ece7\"),\n            align='center',\n            showarrow=False,\n            yanchor='middle',\n            textangle=90\n        )]  \n        ,\n        legend=dict(\n        traceorder=\"normal\",\n        font=dict(family=\"sans-serif\",size=12),\n        title = \"\",\n        orientation=\"h\",\n        yanchor=\"bottom\",\n        y=1.00,\n        xanchor=\"center\",\n        x=0.5\n        ),\n    )\n    ####################################################\n        \n    fig.add_shape(type=\"rect\", x0=-10, x1=0,  y0=0, y1=53.3,line=dict(color=\"#c8ddc0\",width=3),fillcolor=\"#217b00\" ,layer=\"below\")\n    fig.add_shape(type=\"rect\", x0=100, x1=110, y0=0, y1=53.3,line=dict(color=\"#c8ddc0\",width=3),fillcolor=\"#217b00\" ,layer=\"below\")\n    for x in range(0, 100, 10):\n        fig.add_shape(type=\"rect\", x0=x,   x1=x+10, y0=0, y1=53.3,line=dict(color=\"#c8ddc0\",width=3),fillcolor=\"#29a500\" ,layer=\"below\")\n    for x in range(0, 100, 1):\n        fig.add_shape(type=\"line\",x0=x, y0=1, x1=x, y1=2,line=dict(color=\"#c8ddc0\",width=2),layer=\"below\")\n    for x in range(0, 100, 1):\n        fig.add_shape(type=\"line\",x0=x, y0=51.3, x1=x, y1=52.3,line=dict(color=\"#c8ddc0\",width=2),layer=\"below\")\n    \n    for x in range(0, 100, 1):\n        fig.add_shape(type=\"line\",x0=x, y0=20.0, x1=x, y1=21,line=dict(color=\"#c8ddc0\",width=2),layer=\"below\")\n    for x in range(0, 100, 1):\n        fig.add_shape(type=\"line\",x0=x, y0=32.3, x1=x, y1=33.3,line=dict(color=\"#c8ddc0\",width=2),layer=\"below\")\n    \n    \n    fig.add_trace(go.Scatter(\n    x=[2,10,20,30,40,50,60,70,80,90,98], y=[5,5,5,5,5,5,5,5,5,5,5],\n    text=[\"G\",\"1 0\",\"2 0\",\"3 0\",\"4 0\",\"5 0\",\"4 0\",\"3 0\",\"2 0\",\"1 0\",\"G\"],\n    mode=\"text\",\n    textfont=dict(size=20,family=\"Arail\"),\n    showlegend=False,\n    ))\n    \n    fig.add_trace(go.Scatter(\n    x=[2,10,20,30,40,50,60,70,80,90,98], y=[48.3,48.3,48.3,48.3,48.3,48.3,48.3,48.3,48.3,48.3,48.3],\n    text=[\"G\",\"1 0\",\"2 0\",\"3 0\",\"4 0\",\"5 0\",\"4 0\",\"3 0\",\"2 0\",\"1 0\",\"G\"],\n    mode=\"text\",\n    textfont=dict(size=20,family=\"Arail\"),\n    showlegend=False,\n    ))\n    \n    return fig","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:12:35.384307Z","start_time":"2021-09-09T02:12:35.302588Z"},"code_folding":[5],"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:13:34.779326Z","iopub.execute_input":"2023-01-08T04:13:34.779700Z","iopub.status.idle":"2023-01-08T04:13:34.804162Z","shell.execute_reply.started":"2023-01-08T04:13:34.779666Z","shell.execute_reply":"2023-01-08T04:13:34.803273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def football_animation(game_play):\n    train_tracking_df[\"player\"] = train_tracking_df[\"team\"].str.upper().str[0]+train_tracking_df[\"jersey_number\"].astype(\"str\")\n   \n    train_tracking_df[\"track_time_count\"] = (\n        train_tracking_df.sort_values(\"datetime\")\n        .groupby(\"game_play\")[\"datetime\"]\n        .rank(method=\"dense\")\n        .astype(\"int\")\n    )\n    \n    \n    fig = px.scatter(\n        train_tracking_df.query(\"game_play == @game_play\")\n        .groupby([\"track_time_count\"])\n        .apply(lambda x: x.sort_values([\"player\"], ascending = False))# sort player number within the same tracking count (plotly scatter animation requirement),\n        .reset_index(drop=True),\n        \n        x=\"x_position\",\n        y=\"y_position\",\n        range_x=[-10, 110],\n        range_y=[-10, 53.3],\n        hover_data=[\"player\", \"speed\", \"acceleration\", \"direction\"],\n        color=\"team\",\n        animation_frame=\"track_time_count\",\n        text=\"player\",\n        title=f\"Animation of NGS data for game_play {game_play}\",\n    )\n\n    fig.update_traces(textfont_size=10)\n    fig = add_plotly_field(fig)\n    fig.show()","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:12:35.412482Z","start_time":"2021-09-09T02:12:35.409044Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:13:34.805486Z","iopub.execute_input":"2023-01-08T04:13:34.806681Z","iopub.status.idle":"2023-01-08T04:13:34.821549Z","shell.execute_reply.started":"2023-01-08T04:13:34.806643Z","shell.execute_reply":"2023-01-08T04:13:34.820644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"football_animation('58168_003392')","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:03.160018Z","start_time":"2021-09-09T02:12:35.413248Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:13:34.823026Z","iopub.execute_input":"2023-01-08T04:13:34.823805Z","iopub.status.idle":"2023-01-08T04:14:16.784620Z","shell.execute_reply.started":"2023-01-08T04:13:34.823766Z","shell.execute_reply":"2023-01-08T04:14:16.783547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🚀 EDA (Exploratory Data Analysis)\n\n> We will focus on understanding the videos and csv files\n\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n  <h3 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" role=\"tab\" aria-controls=\"home\">Videos and CSV files</h3>\n  <a class=\"list-group-item list-group-item-action\" data-toggle=\"list\" href=\"#🏈-[-Train-/-Test-]-video\" role=\"tab\" aria-controls=\"profile\">🏈 [ Train / Test ] video<span class=\"badge badge-primary badge-pill\">1</span></a>\n  <a class=\"list-group-item list-group-item-action\" data-toggle=\"list\" href=\"#🏈-[-Train-/-Test-]-video-metadata.csv\" role=\"tab\" aria-controls=\"messages\">🏈 [ Train / Test ] video metadata.csv<span class=\"badge badge-primary badge-pill\">2</span></a>\n  <a class=\"list-group-item list-group-item-action\" data-toggle=\"list\" href=\"#🏈-train_labels.csv\" role=\"tab\" aria-controls=\"messages\">🏈 train_labels.csv<span class=\"badge badge-primary badge-pill\">2</span></a>\n  <a class=\"list-group-item list-group-item-action\"  data-toggle=\"list\" href=\"#🏈-[-Train-/-Test-]-player-tracking.csv\" role=\"tab\" aria-controls=\"settings\">🏈 [ Train / Test ] player tracking.csv<span class=\"badge badge-primary badge-pill\">3</span></a>\n  <a class=\"list-group-item list-group-item-action\"  data-toggle=\"list\" href=\"#🏈-[-Train-/-Test-]-baseline-helmets.csv\" role=\"tab\" aria-controls=\"settings\">🏈 [ Train / Test ] baseline helmets.csv<span class=\"badge badge-primary badge-pill\">4</span></a>\n    \n<div class=\"alert alert-info\" role=\"alert\">\n    <a data-toggle=\"list\" href=\"#Index\" role=\"tab\" aria-controls=\"settings\">☝🏻 Back to Index</a>\n</div>","metadata":{}},{"cell_type":"markdown","source":"# 🏈 [ Train / Test ] video\n> Let's understand the vidoes inside `train` and `test` folder\n\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n  <h3 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" role=\"tab\" aria-controls=\"home\">Questions</h3>\n  <a class=\"list-group-item list-group-item-action\" data-toggle=\"list\" href=\"#Is-test-videos-a-subset-of-train-videos?\" role=\"tab\" aria-controls=\"profile\">Is test videos a subset of train videos?<span class=\"badge badge-primary badge-pill\">1</span></a>\n  <a class=\"list-group-item list-group-item-action\" data-toggle=\"list\" href=\"#How-many-train-videos-are-there?\" role=\"tab\" aria-controls=\"messages\">How many train videos are there?<span class=\"badge badge-primary badge-pill\">2</span></a>\n  <a class=\"list-group-item list-group-item-action\" data-toggle=\"list\" href=\"#How-many-frames-per-video-/-all-videos?\" role=\"tab\" aria-controls=\"messages\">How many frames per video / all videos?<span class=\"badge badge-primary badge-pill\">2</span></a>  \n  <a class=\"list-group-item list-group-item-action\"  data-toggle=\"list\" href=\"#Does-Sideline,-Endzone-Video-and-All29-has-same-frame?\" role=\"tab\" aria-controls=\"settings\">Does Sideline, Endzone Video and All29 has same frame?<span class=\"badge badge-primary badge-pill\">3</span></a>\n\n<div class=\"alert alert-info\" role=\"alert\">\n    <a data-toggle=\"list\" href=\"#🚀-EDA-(Exploratory-Data-Analysis)\" role=\"tab\" aria-controls=\"settings\">🚀 Back to EDA</a>\n</div>","metadata":{"heading_collapsed":true}},{"cell_type":"markdown","source":"## Is test videos a subset of train videos?\n> Yes!","metadata":{"heading_collapsed":true,"hidden":true}},{"cell_type":"code","source":"train_videos = os.listdir(f'{DATA_DIR}/train')\ntest_videos = os.listdir(f'{DATA_DIR}/test')\nlen(train_videos), len(test_videos)","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:03.172455Z","start_time":"2021-09-09T02:13:03.161112Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:14:16.786105Z","iopub.execute_input":"2023-01-08T04:14:16.786572Z","iopub.status.idle":"2023-01-08T04:14:16.797846Z","shell.execute_reply.started":"2023-01-08T04:14:16.786528Z","shell.execute_reply":"2023-01-08T04:14:16.796360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"set(test_videos).issubset(set(train_videos))","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:03.182579Z","start_time":"2021-09-09T02:13:03.173469Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:14:16.799738Z","iopub.execute_input":"2023-01-08T04:14:16.800211Z","iopub.status.idle":"2023-01-08T04:14:16.808925Z","shell.execute_reply.started":"2023-01-08T04:14:16.800152Z","shell.execute_reply":"2023-01-08T04:14:16.807763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## How many train videos are there?\n> Total 720! Sideline, Endzone and  video pair for every plays! This year, an additional view is provided, All29 which should include view of every player involved in the play. All29 video is not guaranteed to be time synced with the sideline and endzone.\n\n- train 720 videos\n    - Endzone 240\n    - Sideline 240\n    - All29 240\n    ","metadata":{"heading_collapsed":true,"hidden":true}},{"cell_type":"code","source":"end_count = 0\nside_count = 0\nall_count = 0\nendzone_list = []\nsideline_list = []\nall_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    elif view == \"Sideline\":\n        sideline_list.append('_'.join([video_id, play_id]))\n        side_count += 1\n        \n    else:\n        all_list.append('_'.join([video_id, play_id]))\n        all_count += 1\n\nprint(end_count, side_count, all_count)","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:03.19933Z","start_time":"2021-09-09T02:13:03.183838Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:14:16.810385Z","iopub.execute_input":"2023-01-08T04:14:16.810807Z","iopub.status.idle":"2023-01-08T04:14:16.822860Z","shell.execute_reply.started":"2023-01-08T04:14:16.810774Z","shell.execute_reply":"2023-01-08T04:14:16.821660Z"},"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":{"ExecuteTime":{"end_time":"2021-09-09T02:13:03.211534Z","start_time":"2021-09-09T02:13:03.200309Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:14:16.824422Z","iopub.execute_input":"2023-01-08T04:14:16.825245Z","iopub.status.idle":"2023-01-08T04:14:16.836088Z","shell.execute_reply.started":"2023-01-08T04:14:16.825207Z","shell.execute_reply":"2023-01-08T04:14:16.834766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# videos inside folder matches the video list inside the train_labels.csv\nset([i[:12] for i in train_videos]) == set(train_df.game_play.unique())","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:03.26335Z","start_time":"2021-09-09T02:13:03.21316Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:14:16.838367Z","iopub.execute_input":"2023-01-08T04:14:16.839209Z","iopub.status.idle":"2023-01-08T04:14:17.190617Z","shell.execute_reply.started":"2023-01-08T04:14:16.839170Z","shell.execute_reply":"2023-01-08T04:14:17.189512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(set([i[:12] for i in train_videos])), len(set(train_df.game_play.unique()))","metadata":{"execution":{"iopub.status.busy":"2023-01-08T04:14:17.191938Z","iopub.execute_input":"2023-01-08T04:14:17.192268Z","iopub.status.idle":"2023-01-08T04:14:17.535855Z","shell.execute_reply.started":"2023-01-08T04:14:17.192238Z","shell.execute_reply":"2023-01-08T04:14:17.534905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## How many frames per video / all videos?\n> - Most videos have 700~800 frames \n> - For 720 videos we have totally 559015 frames","metadata":{}},{"cell_type":"code","source":"def get_total_frame(video_path):\n    cap = cv2.VideoCapture(f\"{DATA_DIR}/train/{video_path}\")\n    property_id = int(cv2.CAP_PROP_FRAME_COUNT) \n    length = int(cv2.VideoCapture.get(cap, property_id))\n    return length\ndef add_view(group): # add all29 view to meta data dataframe\n    first_row = group.iloc[[0]]\n    first_row.iloc[:,1:] = np.nan\n    first_row['view'] = \"All29\"\n    return first_row.append(group)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T04:14:17.536910Z","iopub.execute_input":"2023-01-08T04:14:17.537761Z","iopub.status.idle":"2023-01-08T04:14:17.544734Z","shell.execute_reply.started":"2023-01-08T04:14:17.537726Z","shell.execute_reply":"2023-01-08T04:14:17.543485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_metadata_all_df = train_metadata_df.groupby(\"game_play\").apply(add_view).reset_index(drop=True)\ntrain_video = (train_metadata_all_df.game_play + \"_\" + train_metadata_all_df.view + \".mp4\").to_frame()\ntrain_video.columns = [\"video_name\"]\ntrain_video[\"frames\"] = train_video[\"video_name\"].apply(get_total_frame)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T04:14:17.546052Z","iopub.execute_input":"2023-01-08T04:14:17.546366Z","iopub.status.idle":"2023-01-08T04:14:34.635932Z","shell.execute_reply.started":"2023-01-08T04:14:17.546337Z","shell.execute_reply":"2023-01-08T04:14:34.634955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot(train_video.frames) ","metadata":{"execution":{"iopub.status.busy":"2023-01-08T04:14:34.637434Z","iopub.execute_input":"2023-01-08T04:14:34.637928Z","iopub.status.idle":"2023-01-08T04:14:34.935828Z","shell.execute_reply.started":"2023-01-08T04:14:34.637883Z","shell.execute_reply":"2023-01-08T04:14:34.934728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_video.frames.sum()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T04:14:34.942228Z","iopub.execute_input":"2023-01-08T04:14:34.942746Z","iopub.status.idle":"2023-01-08T04:14:34.950215Z","shell.execute_reply.started":"2023-01-08T04:14:34.942708Z","shell.execute_reply":"2023-01-08T04:14:34.949038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Does Sideline, Endzone Video and All29 has same frame?\n> 11 game plays out of 240 doesn't match and the minimum difference is 1 and the maximum difference is 24","metadata":{"heading_collapsed":true,"hidden":true}},{"cell_type":"code","source":"train_metadata_df.view.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T04:14:34.951830Z","iopub.execute_input":"2023-01-08T04:14:34.952619Z","iopub.status.idle":"2023-01-08T04:14:34.965726Z","shell.execute_reply.started":"2023-01-08T04:14:34.952569Z","shell.execute_reply":"2023-01-08T04:14:34.964688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"not_match_video = []\nno_match = 0\n\ntrain_video[\"game_play\"] = train_metadata_all_df.game_play\ntrain_video[\"view\"] = train_metadata_all_df.view\n\nfor play in train_predict_df.game_play.unique():\n    end_frame_n = train_video.query('game_play == @play and view == \"Endzone\"').frames.values[0]\n    side_frame_n = train_video.query('game_play == @play and view == \"Sideline\"').frames.values[0]\n    all_frame_n = train_video.query('game_play == @play and view == \"All29\"').frames.values[0]\n    \n    if end_frame_n != side_frame_n: # all29 is not guaranteed to be synced with end and side\n        no_match +=1\n        not_match_video.append(play)\n        print(f'Not same at playID {play} endzone [{end_frame_n}] sideline [{side_frame_n}] all29 [{all_frame_n}]  difference [{abs(end_frame_n - side_frame_n)}]')\nprint(\"\\n\")\nprint(f'{no_match} of {len(train_predict_df.game_play.unique())} videos are not matched ')","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:05.393033Z","start_time":"2021-09-09T02:13:03.264521Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:14:34.966935Z","iopub.execute_input":"2023-01-08T04:14:34.967247Z","iopub.status.idle":"2023-01-08T04:14:37.529029Z","shell.execute_reply.started":"2023-01-08T04:14:34.967219Z","shell.execute_reply":"2023-01-08T04:14:37.527887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🏈 [ Train / Test ] video metadata.csv\n> Metadata for each sideline and endzone video file including the play start/end/ball snap timestamp information to be used to sync with player tracking data.\n\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n  <h3 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" role=\"tab\" aria-controls=\"home\">Questions</h3>\n  <a class=\"list-group-item list-group-item-action\" data-toggle=\"list\" href=\"#How-many-games-and-plays?\" role=\"tab\" aria-controls=\"profile\">How many games and plays?<span class=\"badge badge-primary badge-pill\">1</span></a>\n  <a class=\"list-group-item list-group-item-action\" data-toggle=\"list\" href=\"#How-many-plays-per-game?\" role=\"tab\" aria-controls=\"messages\">How many plays per game?<span class=\"badge badge-primary badge-pill\">2</span></a>\n  <a class=\"list-group-item list-group-item-action\"  data-toggle=\"list\" href=\"#What-is-the-duration-of-each-play?\" role=\"tab\" aria-controls=\"settings\">What is the duration of each play?<span class=\"badge badge-primary badge-pill\">3</span></a>\n \n     \n<div class=\"alert alert-info\" role=\"alert\">\n    <a data-toggle=\"list\" href=\"#🚀-EDA-(Exploratory-Data-Analysis)\" role=\"tab\" aria-controls=\"settings\">🚀 Back to EDA</a>\n</div>","metadata":{}},{"cell_type":"markdown","source":"## How many games and plays?\n> `game_key` and `play_id` has unique ID\n- 149 games\n- 233 plays","metadata":{"heading_collapsed":true,"hidden":true}},{"cell_type":"code","source":"train_metadata_df.game_key.nunique()","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:07.620214Z","start_time":"2021-09-09T02:13:07.607839Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:14:37.530277Z","iopub.execute_input":"2023-01-08T04:14:37.530640Z","iopub.status.idle":"2023-01-08T04:14:37.537405Z","shell.execute_reply.started":"2023-01-08T04:14:37.530608Z","shell.execute_reply":"2023-01-08T04:14:37.536579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_metadata_df.play_id.nunique()","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:07.63579Z","start_time":"2021-09-09T02:13:07.621159Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:14:37.538791Z","iopub.execute_input":"2023-01-08T04:14:37.539774Z","iopub.status.idle":"2023-01-08T04:14:37.551842Z","shell.execute_reply.started":"2023-01-08T04:14:37.539731Z","shell.execute_reply":"2023-01-08T04:14:37.550430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_metadata_df.groupby('game_key')['play_id'].unique()","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:07.693044Z","start_time":"2021-09-09T02:13:07.636749Z"},"hidden":true,"scrolled":true,"execution":{"iopub.status.busy":"2023-01-08T04:14:37.553564Z","iopub.execute_input":"2023-01-08T04:14:37.554334Z","iopub.status.idle":"2023-01-08T04:14:37.576258Z","shell.execute_reply.started":"2023-01-08T04:14:37.554287Z","shell.execute_reply":"2023-01-08T04:14:37.574838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## How many plays per game?\n- 89 games has only 1 play\n- 42 games has 2 plays\n- 10 game has 3 plays\n- 5 game has 4 plays\n- 1 game has 5 plays\n- 2 game has 6 plays","metadata":{"heading_collapsed":true,"hidden":true}},{"cell_type":"code","source":"play_per_game = train_metadata_df.groupby('game_key')['play_id'].nunique().reset_index().groupby('play_id')['game_key'].unique().to_dict()\nfor key,value in play_per_game.items():\n    print(f\" {len(value)} games have {key} plays \")","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:08.194116Z","start_time":"2021-09-09T02:13:08.166464Z"},"hidden":true,"scrolled":true,"execution":{"iopub.status.busy":"2023-01-08T04:14:37.580349Z","iopub.execute_input":"2023-01-08T04:14:37.580800Z","iopub.status.idle":"2023-01-08T04:14:37.595846Z","shell.execute_reply.started":"2023-01-08T04:14:37.580761Z","shell.execute_reply":"2023-01-08T04:14:37.594623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## What is the duration of each play?\n> 8.6s ~ 22s per play, mostly 12s","metadata":{}},{"cell_type":"code","source":"train_metadata_df[\"cost_time\"] = (train_metadata_df[\"end_time\"]-train_metadata_df[\"start_time\"]).dt.total_seconds()\nsns.displot(train_metadata_df[\"cost_time\"])","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:07.80751Z","start_time":"2021-09-09T02:13:07.693965Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:14:37.597484Z","iopub.execute_input":"2023-01-08T04:14:37.598188Z","iopub.status.idle":"2023-01-08T04:14:37.977248Z","shell.execute_reply.started":"2023-01-08T04:14:37.598143Z","shell.execute_reply":"2023-01-08T04:14:37.975792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"round(train_metadata_df[\"cost_time\"] * 59.94).astype(int) #estimated frames","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-01-08T04:14:37.978996Z","iopub.execute_input":"2023-01-08T04:14:37.979803Z","iopub.status.idle":"2023-01-08T04:14:37.989622Z","shell.execute_reply.started":"2023-01-08T04:14:37.979754Z","shell.execute_reply":"2023-01-08T04:14:37.988313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🏈 train_labels.csv\n> Include contact information for each pair of players and ground in each timestamp.\n\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n  <h3 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" role=\"tab\" aria-controls=\"home\">Questions</h3>\n  <a class=\"list-group-item list-group-item-action\" data-toggle=\"list\" href=\"#Do-all-contacts-happen-after-the-ball-snap?\" role=\"tab\" aria-controls=\"profile\">Do all contacts happen after the ball snap?<span class=\"badge badge-primary badge-pill\">1</span></a>\n  <a class=\"list-group-item list-group-item-action\" data-toggle=\"list\" href=\"#Does-every-moment-have-every-combination-of-22-players-and-ground?\" role=\"tab\" aria-controls=\"profile\">Does every moment have every combination of 22 players and ground?<span class=\"badge badge-primary badge-pill\">2</span></a>\n\n     \n<div class=\"alert alert-info\" role=\"alert\">\n    <a data-toggle=\"list\" href=\"#🚀-EDA-(Exploratory-Data-Analysis)\" role=\"tab\" aria-controls=\"settings\">🚀 Back to EDA</a>\n</div>","metadata":{"ExecuteTime":{"end_time":"2021-09-07T08:04:10.500011Z","start_time":"2021-09-07T08:04:09.302224Z"},"heading_collapsed":true}},{"cell_type":"markdown","source":"## Do all contacts happen after the ball snap?\n> Yes, the contacts happen at or after the ball snap","metadata":{}},{"cell_type":"code","source":"sns.histplot((train_df.query(\"contact == 1\").step))","metadata":{"execution":{"iopub.status.busy":"2023-01-08T04:14:37.991330Z","iopub.execute_input":"2023-01-08T04:14:37.992130Z","iopub.status.idle":"2023-01-08T04:14:38.575679Z","shell.execute_reply.started":"2023-01-08T04:14:37.992082Z","shell.execute_reply":"2023-01-08T04:14:38.574528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.query(\"contact == 1\").step.value_counts().sort_index()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T04:14:38.576969Z","iopub.execute_input":"2023-01-08T04:14:38.577665Z","iopub.status.idle":"2023-01-08T04:14:38.642643Z","shell.execute_reply.started":"2023-01-08T04:14:38.577631Z","shell.execute_reply":"2023-01-08T04:14:38.641661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Does every moment have every combination of 22 players and ground?\n> Yes, each play contains every contact pairs of players and ground.","metadata":{}},{"cell_type":"code","source":"231+22 # combinations for 22 players and ground","metadata":{"execution":{"iopub.status.busy":"2023-01-08T04:14:38.644239Z","iopub.execute_input":"2023-01-08T04:14:38.644589Z","iopub.status.idle":"2023-01-08T04:14:38.651564Z","shell.execute_reply.started":"2023-01-08T04:14:38.644557Z","shell.execute_reply":"2023-01-08T04:14:38.650375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.groupby([\"game_play\", \"datetime\"]).contact_id.count().reset_index()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T04:14:38.653176Z","iopub.execute_input":"2023-01-08T04:14:38.654117Z","iopub.status.idle":"2023-01-08T04:14:39.572616Z","shell.execute_reply.started":"2023-01-08T04:14:38.654082Z","shell.execute_reply":"2023-01-08T04:14:39.571514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.groupby([\"game_play\", \"datetime\"]).contact_id.count().reset_index().query(\"contact_id!=253\").game_play.unique()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T04:14:39.574020Z","iopub.execute_input":"2023-01-08T04:14:39.574451Z","iopub.status.idle":"2023-01-08T04:14:40.409040Z","shell.execute_reply.started":"2023-01-08T04:14:39.574418Z","shell.execute_reply":"2023-01-08T04:14:40.407886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# the play only has 21 players on the filed\nvideo('58538_002774_Endzone.mp4')","metadata":{"execution":{"iopub.status.busy":"2023-01-08T04:14:40.410405Z","iopub.execute_input":"2023-01-08T04:14:40.410772Z","iopub.status.idle":"2023-01-08T04:14:40.572592Z","shell.execute_reply.started":"2023-01-08T04:14:40.410742Z","shell.execute_reply":"2023-01-08T04:14:40.571601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🏈 [ Train / Test ] player tracking.csv\n> Tracking information of the players that is used with videos to map the helmet label\n\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n  <h3 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" role=\"tab\" aria-controls=\"home\">Questions</h3>\n  <a class=\"list-group-item list-group-item-action\"  data-toggle=\"list\" href=\"#How-many-players-per-game?\" role=\"tab\" aria-controls=\"settings\">How many players per game?<span class=\"badge badge-primary badge-pill\">1</span></a>\n  <a class=\"list-group-item list-group-item-action\"  data-toggle=\"list\" href=\"#How-many-players-exist?\" role=\"tab\" aria-controls=\"settings\">How many players exist?<span class=\"badge badge-primary badge-pill\">2</span></a>   \n  <a class=\"list-group-item list-group-item-action\" data-toggle=\"list\" href=\"#Is-ball-snap-is-the-starting-point-of-the-game?\" role=\"tab\" aria-controls=\"messages\">Is ball snap is the starting point of the game?<span class=\"badge badge-primary badge-pill\">3</span></a>\n  <a class=\"list-group-item list-group-item-action\"  data-toggle=\"list\" href=\"#Does-track-information-always-track-22-players-every-moment?\" role=\"tab\" aria-controls=\"settings\">Does track information always track 22 players every moment?<span class=\"badge badge-primary badge-pill\">4</span></a>\n  <a class=\"list-group-item list-group-item-action\" data-toggle=\"list\" href=\"#Why-only-21-players-are-tracked?\" role=\"tab\" aria-controls=\"settings\">Why only 21 players are tracked?<span class=\"badge badge-primary badge-pill\">5</span></a>\n  <a class=\"list-group-item list-group-item-action\"  data-toggle=\"list\" href=\"#Does-tracking-information-recorded-longer-than-video?\" role=\"tab\" aria-controls=\"settings\">Does tracking information recorded longer than video?¶<span class=\"badge badge-primary badge-pill\">6</span></a>\n  <a class=\"list-group-item list-group-item-action\"  data-toggle=\"list\" href=\"#Does-all-trackings-recorded-before-the-ball-snap?\" role=\"tab\" aria-controls=\"settings\">Does all trackings recorded before the ball snap?<span class=\"badge badge-primary badge-pill\">7</span></a>\n  <a class=\"list-group-item list-group-item-action\"  data-toggle=\"list\" href=\"#Do-plays-have-consistent-player-numbers-while-beeing-tracked?\" role=\"tab\" aria-controls=\"settings\">Do plays have consistent player numbers while beeing tracked?<span class=\"badge badge-primary badge-pill\">8</span></a>\n\n    \n<div class=\"alert alert-info\" role=\"alert\">\n    <a data-toggle=\"list\" href=\"#🚀-EDA-(Exploratory-Data-Analysis)\" role=\"tab\" aria-controls=\"settings\">🚀 Back to EDA</a>\n</div>","metadata":{"heading_collapsed":true}},{"cell_type":"markdown","source":"## How many players per game?\n> Mostly 22 players per game. 1 game include 21 players","metadata":{"heading_collapsed":true,"hidden":true}},{"cell_type":"code","source":"# train_df.groupby(['gameKey', 'playID', 'view'])['label'].nunique().value_counts()\n\ntrain_tracking_df.groupby(['game_play', 'play_id'])['nfl_player_id'].nunique().value_counts()","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:08.333715Z","start_time":"2021-09-09T02:13:08.195096Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:14:40.574387Z","iopub.execute_input":"2023-01-08T04:14:40.575356Z","iopub.status.idle":"2023-01-08T04:14:40.877023Z","shell.execute_reply.started":"2023-01-08T04:14:40.575315Z","shell.execute_reply":"2023-01-08T04:14:40.875973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_tracking_df.query(\"game_play == '58580_001136'\").nfl_player_id.nunique()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T04:14:40.878725Z","iopub.execute_input":"2023-01-08T04:14:40.879189Z","iopub.status.idle":"2023-01-08T04:14:40.923933Z","shell.execute_reply.started":"2023-01-08T04:14:40.879147Z","shell.execute_reply":"2023-01-08T04:14:40.923162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check what game does only 21 players are running for?\ntrain_tracking_df.groupby(['game_play', 'play_id'])['nfl_player_id'].nunique().reset_index().query(\"nfl_player_id == 21\")","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:08.466502Z","start_time":"2021-09-09T02:13:08.334676Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:14:40.924961Z","iopub.execute_input":"2023-01-08T04:14:40.925674Z","iopub.status.idle":"2023-01-08T04:14:41.234814Z","shell.execute_reply.started":"2023-01-08T04:14:40.925640Z","shell.execute_reply":"2023-01-08T04:14:41.233700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# the play which has 21 players\nvideo('58538_002774_Endzone.mp4')","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:08.469772Z","start_time":"2021-09-09T02:13:08.467508Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:14:41.236699Z","iopub.execute_input":"2023-01-08T04:14:41.237146Z","iopub.status.idle":"2023-01-08T04:14:41.397357Z","shell.execute_reply.started":"2023-01-08T04:14:41.237105Z","shell.execute_reply":"2023-01-08T04:14:41.393203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"football_animation('58538_002774')","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:47:08.54485Z","start_time":"2021-09-09T02:46:43.337994Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:14:41.398859Z","iopub.execute_input":"2023-01-08T04:14:41.399224Z","iopub.status.idle":"2023-01-08T04:15:22.054248Z","shell.execute_reply.started":"2023-01-08T04:14:41.399190Z","shell.execute_reply":"2023-01-08T04:15:22.052977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## How many players exist?\n> 1687 players exist in 149 games. Most games have exact 22 players in plays.\n- Home and Away both have 99 numbers","metadata":{"heading_collapsed":true,"hidden":true}},{"cell_type":"code","source":"train_tracking_df.nfl_player_id.nunique()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T04:15:22.056056Z","iopub.execute_input":"2023-01-08T04:15:22.056479Z","iopub.status.idle":"2023-01-08T04:15:22.071997Z","shell.execute_reply.started":"2023-01-08T04:15:22.056412Z","shell.execute_reply":"2023-01-08T04:15:22.071166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# examine the player number in each game\ntrain_tracking_df.groupby(\"game_key\").nfl_player_id.nunique().value_counts()#.sum() 149","metadata":{"execution":{"iopub.status.busy":"2023-01-08T04:15:22.073351Z","iopub.execute_input":"2023-01-08T04:15:22.073762Z","iopub.status.idle":"2023-01-08T04:15:22.249072Z","shell.execute_reply.started":"2023-01-08T04:15:22.073713Z","shell.execute_reply":"2023-01-08T04:15:22.247979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"home_players = train_tracking_df.query('team == \"home\"').jersey_number.unique()\nvisitor_players = train_tracking_df.query('team == \"away\"').jersey_number.unique()\n\nhome_players.sort(), visitor_players.sort()\nhome_players, visitor_players","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:09.951375Z","start_time":"2021-09-09T02:13:09.811816Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:22.250404Z","iopub.execute_input":"2023-01-08T04:15:22.250788Z","iopub.status.idle":"2023-01-08T04:15:22.575813Z","shell.execute_reply.started":"2023-01-08T04:15:22.250756Z","shell.execute_reply":"2023-01-08T04:15:22.574508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(home_players), len(visitor_players)","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:09.955071Z","start_time":"2021-09-09T02:13:09.95254Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:22.576986Z","iopub.execute_input":"2023-01-08T04:15:22.577448Z","iopub.status.idle":"2023-01-08T04:15:22.585587Z","shell.execute_reply.started":"2023-01-08T04:15:22.577418Z","shell.execute_reply":"2023-01-08T04:15:22.584516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"set(home_players) - set(visitor_players)","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:09.966827Z","start_time":"2021-09-09T02:13:09.956055Z"},"hidden":true,"scrolled":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:22.587297Z","iopub.execute_input":"2023-01-08T04:15:22.588172Z","iopub.status.idle":"2023-01-08T04:15:22.597007Z","shell.execute_reply.started":"2023-01-08T04:15:22.588125Z","shell.execute_reply":"2023-01-08T04:15:22.595642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Is ball snap is the starting point of the game?\n> yes! It occurs only one time per play\n- Before snap players has small acceleration.\n- After snap they start to run!\n","metadata":{"heading_collapsed":true,"hidden":true}},{"cell_type":"code","source":"train_tracking_df","metadata":{"execution":{"iopub.status.busy":"2023-01-08T04:15:22.598376Z","iopub.execute_input":"2023-01-08T04:15:22.598770Z","iopub.status.idle":"2023-01-08T04:15:22.637652Z","shell.execute_reply.started":"2023-01-08T04:15:22.598723Z","shell.execute_reply":"2023-01-08T04:15:22.636244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# play starting time\nlen(train_tracking_df[train_tracking_df.step == 0].groupby(\"datetime\"))","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:25:22.302931Z","start_time":"2021-09-09T02:25:22.288998Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:22.639189Z","iopub.execute_input":"2023-01-08T04:15:22.639701Z","iopub.status.idle":"2023-01-08T04:15:22.657854Z","shell.execute_reply.started":"2023-01-08T04:15:22.639666Z","shell.execute_reply":"2023-01-08T04:15:22.656743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Wide Recivers are fast runners\n\n# before the ball snap the movement is Low\nplay = train_tracking_df.query('game_play == \"58580_001136\" and position == \"WR\"')\nsns.histplot(play[play.step < 0].acceleration)","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:25:22.841374Z","start_time":"2021-09-09T02:25:22.826726Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:22.659345Z","iopub.execute_input":"2023-01-08T04:15:22.659768Z","iopub.status.idle":"2023-01-08T04:15:23.006002Z","shell.execute_reply.started":"2023-01-08T04:15:22.659733Z","shell.execute_reply":"2023-01-08T04:15:23.004820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# after the ball snap the movement is High\nplay = train_tracking_df.query('game_play == \"58580_001136\" and position == \"WR\"')\nsns.histplot(play[play.step > 0].acceleration)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T04:15:23.007710Z","iopub.execute_input":"2023-01-08T04:15:23.008186Z","iopub.status.idle":"2023-01-08T04:15:23.336730Z","shell.execute_reply.started":"2023-01-08T04:15:23.008144Z","shell.execute_reply":"2023-01-08T04:15:23.335592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Does track information always track 22 players every moment?\n> No! Mostly 22 players are tracked but in one play less than 22 are tracked","metadata":{"heading_collapsed":true,"hidden":true}},{"cell_type":"code","source":"# Mostly 22 players are tracked but not all\ntrain_tracking_df.groupby(['play_id', 'datetime']).count()['nfl_player_id'].value_counts()","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:25:23.295443Z","start_time":"2021-09-09T02:25:23.219755Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:23.337846Z","iopub.execute_input":"2023-01-08T04:15:23.338142Z","iopub.status.idle":"2023-01-08T04:15:23.912562Z","shell.execute_reply.started":"2023-01-08T04:15:23.338114Z","shell.execute_reply":"2023-01-08T04:15:23.911220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Why only 21 players are tracked?\n> This happened in the play where only 21 players on the field","metadata":{"heading_collapsed":true,"hidden":true}},{"cell_type":"code","source":"player_n_df = train_tracking_df.groupby(['play_id', 'datetime']).count()['nfl_player_id'].reset_index()\nplayer_n_df","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:25:23.369509Z","start_time":"2021-09-09T02:25:23.296437Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:23.916073Z","iopub.execute_input":"2023-01-08T04:15:23.916427Z","iopub.status.idle":"2023-01-08T04:15:24.515070Z","shell.execute_reply.started":"2023-01-08T04:15:23.916395Z","shell.execute_reply":"2023-01-08T04:15:24.513895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"player_n_df.query('nfl_player_id == 21')","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:25:23.380523Z","start_time":"2021-09-09T02:25:23.370484Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:24.516179Z","iopub.execute_input":"2023-01-08T04:15:24.516544Z","iopub.status.idle":"2023-01-08T04:15:24.534510Z","shell.execute_reply.started":"2023-01-08T04:15:24.516504Z","shell.execute_reply":"2023-01-08T04:15:24.533680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"player_n_df.query('nfl_player_id == 21').play_id.nunique()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T04:15:24.535471Z","iopub.execute_input":"2023-01-08T04:15:24.535791Z","iopub.status.idle":"2023-01-08T04:15:24.547358Z","shell.execute_reply.started":"2023-01-08T04:15:24.535762Z","shell.execute_reply":"2023-01-08T04:15:24.546257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# There are 11 players at the last frame\ntrain_tracking_df.query('play_id == 2774')","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:25:23.405931Z","start_time":"2021-09-09T02:25:23.382729Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:24.548809Z","iopub.execute_input":"2023-01-08T04:15:24.549261Z","iopub.status.idle":"2023-01-08T04:15:24.593049Z","shell.execute_reply.started":"2023-01-08T04:15:24.549217Z","shell.execute_reply":"2023-01-08T04:15:24.591967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# this is the situation where 21 player on the court\nvideo(\"58538_002774_Endzone.mp4\")","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:25:23.645212Z","start_time":"2021-09-09T02:25:23.642768Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:24.594364Z","iopub.execute_input":"2023-01-08T04:15:24.594693Z","iopub.status.idle":"2023-01-08T04:15:24.752019Z","shell.execute_reply.started":"2023-01-08T04:15:24.594664Z","shell.execute_reply":"2023-01-08T04:15:24.750698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Does tracking information recorded longer than video?\n> The tracking time is usually longer than the relative video. ","metadata":{"heading_collapsed":true,"hidden":true}},{"cell_type":"code","source":"train_tracking_time = pd.concat([train_tracking_df.groupby(['game_play']).datetime.min(),train_tracking_df.groupby(['game_play']).datetime.max()],axis = 1).reset_index()\ntrain_tracking_time.columns = [\"game_play\",\"tracking_start_time\", \"tracking_end_time\"]\ntrain_video_time = train_metadata_df[train_metadata_df.view == \"Sideline\"][[\"game_play\",\"start_time\", \"end_time\", \"snap_time\"]].copy()\n\ntrain_video_time.columns = [\"game_play\",\"video_start_time\", \"video_end_time\", \"video_snap_time\"]\n\ntime_difference = pd.merge(train_tracking_time, train_video_time.iloc[:,:-1], on = \"game_play\")\ntime_difference[\"start_difference\"] = (time_difference[\"tracking_start_time\"]-time_difference[\"video_start_time\"]).dt.total_seconds()\ntime_difference[\"end_difference\"] = (time_difference[\"tracking_end_time\"]-time_difference[\"video_end_time\"]).dt.total_seconds()\ntime_difference[\"length_difference\"] = (time_difference[\"end_difference\"]-time_difference[\"start_difference\"])\n\nsns.histplot(time_difference.iloc[:,-3:])","metadata":{"execution":{"iopub.status.busy":"2023-01-08T04:15:24.753136Z","iopub.execute_input":"2023-01-08T04:15:24.754383Z","iopub.status.idle":"2023-01-08T04:15:25.546119Z","shell.execute_reply.started":"2023-01-08T04:15:24.754343Z","shell.execute_reply":"2023-01-08T04:15:25.544920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Does all trackings recorded before the ball snap?\n> Yes! All plays are recorded before the ball snap!","metadata":{"heading_collapsed":true,"hidden":true}},{"cell_type":"code","source":"# play starts at the 5s of train_metadata_df\n# every tracking starts before the play starts\n\ntime_difference[\"snap_offset\"] = (time_difference[\"tracking_start_time\"]-train_video_time[\"video_snap_time\"].reset_index(drop = True)).dt.total_seconds()\n\nlen(time_difference.query(\"snap_offset >= 0\"))","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:25:48.700984Z","start_time":"2021-09-09T02:25:48.670848Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:25.547589Z","iopub.execute_input":"2023-01-08T04:15:25.547948Z","iopub.status.idle":"2023-01-08T04:15:25.563670Z","shell.execute_reply.started":"2023-01-08T04:15:25.547915Z","shell.execute_reply":"2023-01-08T04:15:25.562383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Do plays have consistent player numbers while beeing tracked?\n> Yes! The plays have consistent player numbers","metadata":{"heading_collapsed":true,"hidden":true}},{"cell_type":"code","source":"player_df = train_tracking_df.groupby(['game_play', 'datetime']).count().nfl_player_id.reset_index()\nplayer_df","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:25:48.778046Z","start_time":"2021-09-09T02:25:48.701844Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:25.565864Z","iopub.execute_input":"2023-01-08T04:15:25.566224Z","iopub.status.idle":"2023-01-08T04:15:26.177565Z","shell.execute_reply.started":"2023-01-08T04:15:25.566192Z","shell.execute_reply":"2023-01-08T04:15:26.176463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_tracking_df.groupby(['game_play', 'datetime']).count().nfl_player_id.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T04:15:26.179060Z","iopub.execute_input":"2023-01-08T04:15:26.179406Z","iopub.status.idle":"2023-01-08T04:15:26.776147Z","shell.execute_reply.started":"2023-01-08T04:15:26.179375Z","shell.execute_reply":"2023-01-08T04:15:26.775160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This is the exact play 58538_002774 where only 21 players on the ground\nplayer_df.query(\"nfl_player_id == 21\").game_play.unique()\n","metadata":{"execution":{"iopub.status.busy":"2023-01-08T04:15:26.777372Z","iopub.execute_input":"2023-01-08T04:15:26.777827Z","iopub.status.idle":"2023-01-08T04:15:26.790841Z","shell.execute_reply.started":"2023-01-08T04:15:26.777796Z","shell.execute_reply":"2023-01-08T04:15:26.789468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_df = player_df.groupby('game_play').apply(lambda x: x['nfl_player_id'].values).reset_index()\nhistory_df = history_df.rename(columns={0: 'history'})\n# history_df.head()\nhistory_df","metadata":{"execution":{"iopub.status.busy":"2023-01-08T04:15:26.792740Z","iopub.execute_input":"2023-01-08T04:15:26.793202Z","iopub.status.idle":"2023-01-08T04:15:26.856007Z","shell.execute_reply.started":"2023-01-08T04:15:26.793158Z","shell.execute_reply":"2023-01-08T04:15:26.854641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"not_consistent = []\nfor _, (play_id, history) in history_df.iterrows():\n    start_player_n = history[0]\n    all_same = all(history == start_player_n)\n    if not all_same:\n        not_consistent.append(play_id)\nnot_consistent\n","metadata":{"execution":{"iopub.status.busy":"2023-01-08T04:15:26.857489Z","iopub.execute_input":"2023-01-08T04:15:26.857955Z","iopub.status.idle":"2023-01-08T04:15:26.885280Z","shell.execute_reply.started":"2023-01-08T04:15:26.857910Z","shell.execute_reply":"2023-01-08T04:15:26.884409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🏈 [ Train / Test ] baseline helmets.csv\n>The winning model from last year's competition was used to predict the bbox of train/test videos.\n\n\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n  <h3 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" role=\"tab\" aria-controls=\"home\">Questions</h3>\n  <a class=\"list-group-item list-group-item-action\" data-toggle=\"list\" href=\"#Does-frames-in-train_baseline_helmets.csv-match-with-the-frame-in-videos?\" role=\"tab\" aria-controls=\"profile\">Does frames in train_baseline_helmets.csv match with the frame in videos?<span class=\"badge badge-primary badge-pill\">1</span></a>\n  <a class=\"list-group-item list-group-item-action\" data-toggle=\"list\" href=\"#How-many-bbox-was-predicted-for-1-frame?\" role=\"tab\" aria-controls=\"profile\">How many bbox was predicted for 1 frame?<span class=\"badge badge-primary badge-pill\">2</span></a>\n  <a class=\"list-group-item list-group-item-action\" data-toggle=\"list\" href=\"#What-is-the-helmet-size-inside-the-video?\" role=\"tab\" aria-controls=\"profile\">What is the helmet size inside the video?<span class=\"badge badge-primary badge-pill\">3</span></a>\n<div class=\"alert alert-info\" role=\"alert\">\n    <a data-toggle=\"list\" href=\"#🚀-EDA-(Exploratory-Data-Analysis)\" role=\"tab\" aria-controls=\"settings\">🚀 Back to EDA</a>\n</div>","metadata":{"heading_collapsed":true}},{"cell_type":"code","source":"# Endzone2 should be ignored\n# See https://www.kaggle.com/competitions/nfl-player-contact-detection/discussion/370678#2059252\n\ntrain_predict_df = train_predict_df[train_predict_df.view != \"Endzone2\"]\ntrain_predict_df.view.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T04:15:26.894880Z","iopub.execute_input":"2023-01-08T04:15:26.895726Z","iopub.status.idle":"2023-01-08T04:15:27.875951Z","shell.execute_reply.started":"2023-01-08T04:15:26.895687Z","shell.execute_reply":"2023-01-08T04:15:27.874740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Does frames in train_baseline_helmets.csv match with the frame in videos?\n> The video frames are usually 1 or 2 more than those in train_baseline_helmets.csv (at the end of videos, the helmet detection boxes disapper)","metadata":{"heading_collapsed":true,"hidden":true}},{"cell_type":"code","source":"def get_total_frame(video_path):\n    cap = cv2.VideoCapture(f\"{DATA_DIR}/train/{video_path}\")\n    property_id = int(cv2.CAP_PROP_FRAME_COUNT) \n    length = int(cv2.VideoCapture.get(cap, property_id))\n    return length","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:05.407946Z","start_time":"2021-09-09T02:13:05.401019Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:27.877174Z","iopub.execute_input":"2023-01-08T04:15:27.877502Z","iopub.status.idle":"2023-01-08T04:15:27.882791Z","shell.execute_reply.started":"2023-01-08T04:15:27.877473Z","shell.execute_reply":"2023-01-08T04:15:27.882002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"play2frame = train_predict_df.groupby('video').frame.max().to_dict()","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:05.490166Z","start_time":"2021-09-09T02:13:05.410203Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:27.883682Z","iopub.execute_input":"2023-01-08T04:15:27.883977Z","iopub.status.idle":"2023-01-08T04:15:28.295225Z","shell.execute_reply.started":"2023-01-08T04:15:27.883949Z","shell.execute_reply":"2023-01-08T04:15:28.294007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for video_name, label_frame_n in play2frame.items():\n    video_frame_n = get_total_frame(video_name)\n    play2frame[video_name] = int(video_frame_n-label_frame_n)\nsns.histplot(play2frame.values()) ","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:07.432348Z","start_time":"2021-09-09T02:13:05.491407Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:28.296742Z","iopub.execute_input":"2023-01-08T04:15:28.297106Z","iopub.status.idle":"2023-01-08T04:15:36.617198Z","shell.execute_reply.started":"2023-01-08T04:15:28.297072Z","shell.execute_reply":"2023-01-08T04:15:36.615733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## How many bbox was predicted for 1 frame?\n> The model used for predicting the bounding box predicts sideline persons head as helmet too\n\n- 82 bbox was predicted in max for 1 frame\n- 2 bbox was predicted in min for 1 frame","metadata":{"heading_collapsed":true,"hidden":true}},{"cell_type":"code","source":"# 22 bbox was predicted in max for 1 frame\ntrain_predict_df.groupby(['video' ,'frame']).count()['left'].max()","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:13.491638Z","start_time":"2021-09-09T02:12:29.705Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:36.902398Z","iopub.execute_input":"2023-01-08T04:15:36.903526Z","iopub.status.idle":"2023-01-08T04:15:38.366257Z","shell.execute_reply.started":"2023-01-08T04:15:36.903487Z","shell.execute_reply":"2023-01-08T04:15:38.364573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 1 bbox was predicted in max for 1 frame\ntrain_predict_df.groupby(['video' ,'frame']).count()['left'].min()","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:13.492185Z","start_time":"2021-09-09T02:12:29.708Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:38.367786Z","iopub.execute_input":"2023-01-08T04:15:38.368237Z","iopub.status.idle":"2023-01-08T04:15:39.832924Z","shell.execute_reply.started":"2023-01-08T04:15:38.368200Z","shell.execute_reply":"2023-01-08T04:15:39.831429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# bbox number doesn't seem quite right\nsns.displot(train_predict_df.groupby(['video' ,'frame']).count()['left']);","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:13.492819Z","start_time":"2021-09-09T02:12:29.71Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:39.836624Z","iopub.execute_input":"2023-01-08T04:15:39.837057Z","iopub.status.idle":"2023-01-08T04:15:41.871382Z","shell.execute_reply.started":"2023-01-08T04:15:39.837019Z","shell.execute_reply":"2023-01-08T04:15:41.870281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_predict_df.groupby(['video' ,'frame']).count().query('left == 15')","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:13.493417Z","start_time":"2021-09-09T02:12:29.713Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:41.872994Z","iopub.execute_input":"2023-01-08T04:15:41.873448Z","iopub.status.idle":"2023-01-08T04:15:43.407562Z","shell.execute_reply.started":"2023-01-08T04:15:41.873405Z","shell.execute_reply":"2023-01-08T04:15:43.406395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frame_bbox(train_predict_df, '58168_003392_Endzone_493')","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:13.49402Z","start_time":"2021-09-09T02:12:29.716Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:43.409585Z","iopub.execute_input":"2023-01-08T04:15:43.410085Z","iopub.status.idle":"2023-01-08T04:15:46.208756Z","shell.execute_reply.started":"2023-01-08T04:15:43.410039Z","shell.execute_reply":"2023-01-08T04:15:46.207495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_predict_df.groupby(['video' ,'frame']).count().query('left == 1')","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:13.494634Z","start_time":"2021-09-09T02:12:29.719Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:46.211216Z","iopub.execute_input":"2023-01-08T04:15:46.211696Z","iopub.status.idle":"2023-01-08T04:15:47.710866Z","shell.execute_reply.started":"2023-01-08T04:15:46.211649Z","shell.execute_reply":"2023-01-08T04:15:47.709669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frame_bbox(train_predict_df, '58284_002768_Endzone_661')","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:13.495233Z","start_time":"2021-09-09T02:12:29.722Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:47.712041Z","iopub.execute_input":"2023-01-08T04:15:47.712368Z","iopub.status.idle":"2023-01-08T04:15:51.073245Z","shell.execute_reply.started":"2023-01-08T04:15:47.712338Z","shell.execute_reply":"2023-01-08T04:15:51.072142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## What is the helmet size inside the video?\n- 6786 is the biggest helmet size shown in the video and occure when the camera is zooming.\n- 10 is the smallest helmets size\n- Mostly the helmet size are around 90","metadata":{"heading_collapsed":true,"hidden":true}},{"cell_type":"code","source":"train_predict_df['helmet_size'] = train_predict_df.width * train_predict_df.height\ntrain_predict_df.helmet_size.hist(bins=100)","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:10.386276Z","start_time":"2021-09-09T02:13:10.217605Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:51.074955Z","iopub.execute_input":"2023-01-08T04:15:51.075511Z","iopub.status.idle":"2023-01-08T04:15:51.590743Z","shell.execute_reply.started":"2023-01-08T04:15:51.075470Z","shell.execute_reply":"2023-01-08T04:15:51.589646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Most of the helmets are around 100\ntrain_predict_df.helmet_size.value_counts()[:20]","metadata":{"execution":{"iopub.status.busy":"2023-01-08T04:15:51.594477Z","iopub.execute_input":"2023-01-08T04:15:51.594832Z","iopub.status.idle":"2023-01-08T04:15:51.644168Z","shell.execute_reply.started":"2023-01-08T04:15:51.594801Z","shell.execute_reply":"2023-01-08T04:15:51.643040Z"},"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":{"ExecuteTime":{"end_time":"2021-09-09T02:13:10.414141Z","start_time":"2021-09-09T02:13:10.402186Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:51.645818Z","iopub.execute_input":"2023-01-08T04:15:51.646162Z","iopub.status.idle":"2023-01-08T04:15:51.698688Z","shell.execute_reply.started":"2023-01-08T04:15:51.646131Z","shell.execute_reply":"2023-01-08T04:15:51.697443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The biggest size of helmet is 6786\ntrain_predict_df.helmet_size.max(), train_predict_df.helmet_size.min()","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:10.420818Z","start_time":"2021-09-09T02:13:10.415451Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:51.699974Z","iopub.execute_input":"2023-01-08T04:15:51.700423Z","iopub.status.idle":"2023-01-08T04:15:51.719141Z","shell.execute_reply.started":"2023-01-08T04:15:51.700390Z","shell.execute_reply":"2023-01-08T04:15:51.717682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_predict_df.query('helmet_size == 6786')","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:10.748942Z","start_time":"2021-09-09T02:13:10.422125Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:51.720845Z","iopub.execute_input":"2023-01-08T04:15:51.721184Z","iopub.status.idle":"2023-01-08T04:15:52.064195Z","shell.execute_reply.started":"2023-01-08T04:15:51.721154Z","shell.execute_reply":"2023-01-08T04:15:52.063073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# when does the helmet shown the biggest?\nframe_bbox(train_predict_df, '58218_003210_Sideline_678')","metadata":{"execution":{"iopub.status.busy":"2023-01-08T04:15:52.065993Z","iopub.execute_input":"2023-01-08T04:15:52.066493Z","iopub.status.idle":"2023-01-08T04:15:55.435799Z","shell.execute_reply.started":"2023-01-08T04:15:52.066432Z","shell.execute_reply":"2023-01-08T04:15:55.434565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check through the video\nvideo('58218_003210_Sideline.mp4')","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:11.737396Z","start_time":"2021-09-09T02:13:11.73501Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:55.437836Z","iopub.execute_input":"2023-01-08T04:15:55.439171Z","iopub.status.idle":"2023-01-08T04:15:55.582743Z","shell.execute_reply.started":"2023-01-08T04:15:55.439115Z","shell.execute_reply":"2023-01-08T04:15:55.581248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_predict_df.query('helmet_size == 10')","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:11.771461Z","start_time":"2021-09-09T02:13:11.738265Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:55.584119Z","iopub.execute_input":"2023-01-08T04:15:55.584453Z","iopub.status.idle":"2023-01-08T04:15:55.619676Z","shell.execute_reply.started":"2023-01-08T04:15:55.584423Z","shell.execute_reply":"2023-01-08T04:15:55.618512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# when does the helmet shown the smallest?\nframe_bbox(train_predict_df, '58581_000040_Endzone_426')","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:12.610202Z","start_time":"2021-09-09T02:13:11.772372Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:55.621077Z","iopub.execute_input":"2023-01-08T04:15:55.621428Z","iopub.status.idle":"2023-01-08T04:15:58.326802Z","shell.execute_reply.started":"2023-01-08T04:15:55.621390Z","shell.execute_reply":"2023-01-08T04:15:58.325756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check through the video\nvideo('58581_000040_Endzone.mp4')","metadata":{"ExecuteTime":{"end_time":"2021-09-09T02:13:12.61447Z","start_time":"2021-09-09T02:13:12.611674Z"},"hidden":true,"execution":{"iopub.status.busy":"2023-01-08T04:15:58.328415Z","iopub.execute_input":"2023-01-08T04:15:58.329393Z","iopub.status.idle":"2023-01-08T04:15:58.488826Z","shell.execute_reply.started":"2023-01-08T04:15:58.329348Z","shell.execute_reply":"2023-01-08T04:15:58.485797Z"},"trusted":true},"execution_count":null,"outputs":[]}]}