{"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 matplotlib.pyplot as plt\nimport seaborn as sns\nfrom IPython.display import Video","metadata":{"execution":{"iopub.status.busy":"2022-12-06T04:58:58.672473Z","iopub.execute_input":"2022-12-06T04:58:58.673127Z","iopub.status.idle":"2022-12-06T04:58:59.789840Z","shell.execute_reply.started":"2022-12-06T04:58:58.673014Z","shell.execute_reply":"2022-12-06T04:58:59.788755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_player = pd.read_csv(\"/kaggle/input/nfl-player-contact-detection/train_player_tracking.csv\")\ntest_player = pd.read_csv(\"/kaggle/input/nfl-player-contact-detection/test_player_tracking.csv\")\n\ntrain_baseline = pd.read_csv(\"/kaggle/input/nfl-player-contact-detection/train_baseline_helmets.csv\")\ntest_baseline = pd.read_csv(\"/kaggle/input/nfl-player-contact-detection/test_baseline_helmets.csv\")\n\ntrain_labels = pd.read_csv(\"/kaggle/input/nfl-player-contact-detection/train_labels.csv\")\n\ntrain_metadata = pd.read_csv(\"/kaggle/input/nfl-player-contact-detection/train_video_metadata.csv\")\ntest_metadata = pd.read_csv(\"/kaggle/input/nfl-player-contact-detection/test_video_metadata.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:04:13.351829Z","iopub.execute_input":"2022-12-06T05:04:13.352274Z","iopub.status.idle":"2022-12-06T05:04:34.501598Z","shell.execute_reply.started":"2022-12-06T05:04:13.352238Z","shell.execute_reply":"2022-12-06T05:04:34.500492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_player.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:05:27.786283Z","iopub.execute_input":"2022-12-06T05:05:27.786681Z","iopub.status.idle":"2022-12-06T05:05:27.817381Z","shell.execute_reply.started":"2022-12-06T05:05:27.786649Z","shell.execute_reply":"2022-12-06T05:05:27.816127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_player.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:06:30.167680Z","iopub.execute_input":"2022-12-06T05:06:30.168431Z","iopub.status.idle":"2022-12-06T05:06:30.209112Z","shell.execute_reply.started":"2022-12-06T05:06:30.168389Z","shell.execute_reply":"2022-12-06T05:06:30.207919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_player.columns","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:06:39.428943Z","iopub.execute_input":"2022-12-06T05:06:39.429360Z","iopub.status.idle":"2022-12-06T05:06:39.437313Z","shell.execute_reply.started":"2022-12-06T05:06:39.429326Z","shell.execute_reply":"2022-12-06T05:06:39.436050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_player.columns","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:06:42.262351Z","iopub.execute_input":"2022-12-06T05:06:42.262907Z","iopub.status.idle":"2022-12-06T05:06:42.277546Z","shell.execute_reply.started":"2022-12-06T05:06:42.262871Z","shell.execute_reply":"2022-12-06T05:06:42.269729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_player.info()","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:06:44.903238Z","iopub.execute_input":"2022-12-06T05:06:44.904023Z","iopub.status.idle":"2022-12-06T05:06:45.156635Z","shell.execute_reply.started":"2022-12-06T05:06:44.903982Z","shell.execute_reply":"2022-12-06T05:06:45.155352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_player.info()","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:06:48.055806Z","iopub.execute_input":"2022-12-06T05:06:48.056233Z","iopub.status.idle":"2022-12-06T05:06:48.077620Z","shell.execute_reply.started":"2022-12-06T05:06:48.056192Z","shell.execute_reply":"2022-12-06T05:06:48.076450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_player.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:07:13.827228Z","iopub.execute_input":"2022-12-06T05:07:13.828163Z","iopub.status.idle":"2022-12-06T05:07:13.835796Z","shell.execute_reply.started":"2022-12-06T05:07:13.828113Z","shell.execute_reply":"2022-12-06T05:07:13.834608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_player.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:07:22.385076Z","iopub.execute_input":"2022-12-06T05:07:22.385794Z","iopub.status.idle":"2022-12-06T05:07:22.392149Z","shell.execute_reply.started":"2022-12-06T05:07:22.385757Z","shell.execute_reply":"2022-12-06T05:07:22.391017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_player.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:09:58.947988Z","iopub.execute_input":"2022-12-06T05:09:58.948421Z","iopub.status.idle":"2022-12-06T05:09:59.180145Z","shell.execute_reply.started":"2022-12-06T05:09:58.948379Z","shell.execute_reply":"2022-12-06T05:09:59.178870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(train_player.isnull(),yticklabels=False,cbar=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:08:04.649280Z","iopub.execute_input":"2022-12-06T05:08:04.649664Z","iopub.status.idle":"2022-12-06T05:08:35.917119Z","shell.execute_reply.started":"2022-12-06T05:08:04.649631Z","shell.execute_reply":"2022-12-06T05:08:35.916081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_player.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:09:51.402862Z","iopub.execute_input":"2022-12-06T05:09:51.403298Z","iopub.status.idle":"2022-12-06T05:09:51.418586Z","shell.execute_reply.started":"2022-12-06T05:09:51.403264Z","shell.execute_reply":"2022-12-06T05:09:51.417208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(test_player.isnull(),yticklabels=False,cbar=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:09:00.395824Z","iopub.execute_input":"2022-12-06T05:09:00.396286Z","iopub.status.idle":"2022-12-06T05:09:01.044446Z","shell.execute_reply.started":"2022-12-06T05:09:00.396249Z","shell.execute_reply":"2022-12-06T05:09:01.043409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_player.describe()","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:08:54.361917Z","iopub.execute_input":"2022-12-06T05:08:54.362314Z","iopub.status.idle":"2022-12-06T05:08:54.994136Z","shell.execute_reply.started":"2022-12-06T05:08:54.362278Z","shell.execute_reply":"2022-12-06T05:08:54.992959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_player.describe()","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:08:43.161595Z","iopub.execute_input":"2022-12-06T05:08:43.161992Z","iopub.status.idle":"2022-12-06T05:08:43.226160Z","shell.execute_reply.started":"2022-12-06T05:08:43.161960Z","shell.execute_reply":"2022-12-06T05:08:43.225083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_player['sa'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:12:12.356675Z","iopub.execute_input":"2022-12-06T05:12:12.357079Z","iopub.status.idle":"2022-12-06T05:12:12.394326Z","shell.execute_reply.started":"2022-12-06T05:12:12.357045Z","shell.execute_reply":"2022-12-06T05:12:12.392868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_player['sa'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:12:22.940895Z","iopub.execute_input":"2022-12-06T05:12:22.941310Z","iopub.status.idle":"2022-12-06T05:12:22.954146Z","shell.execute_reply.started":"2022-12-06T05:12:22.941277Z","shell.execute_reply":"2022-12-06T05:12:22.952692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = [12,8])\nsns.countplot(x='nfl_player_id', data=train_player, palette=\"pastel\")","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:14:25.839740Z","iopub.execute_input":"2022-12-06T05:14:25.840109Z","iopub.status.idle":"2022-12-06T05:14:50.180253Z","shell.execute_reply.started":"2022-12-06T05:14:25.840078Z","shell.execute_reply":"2022-12-06T05:14:50.179150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = [12,8])\nsns.countplot(x='play_id', data=train_player, palette=\"pastel\")","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:15:33.743754Z","iopub.execute_input":"2022-12-06T05:15:33.744141Z","iopub.status.idle":"2022-12-06T05:15:36.851543Z","shell.execute_reply.started":"2022-12-06T05:15:33.744109Z","shell.execute_reply":"2022-12-06T05:15:36.850314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:23:12.642117Z","iopub.execute_input":"2022-12-06T05:23:12.642544Z","iopub.status.idle":"2022-12-06T05:23:12.656073Z","shell.execute_reply.started":"2022-12-06T05:23:12.642513Z","shell.execute_reply":"2022-12-06T05:23:12.654728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_metadata.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:25:50.114495Z","iopub.execute_input":"2022-12-06T05:25:50.114893Z","iopub.status.idle":"2022-12-06T05:25:50.128442Z","shell.execute_reply.started":"2022-12-06T05:25:50.114860Z","shell.execute_reply":"2022-12-06T05:25:50.127119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport subprocess\nfrom IPython.display import Video, display","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:27:11.425625Z","iopub.execute_input":"2022-12-06T05:27:11.426031Z","iopub.status.idle":"2022-12-06T05:27:11.631843Z","shell.execute_reply.started":"2022-12-06T05:27:11.425998Z","shell.execute_reply":"2022-12-06T05:27:11.630806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def video_with_helmets(\n    video_path: str, baseline_boxes: pd.DataFrame, verbose=True\n) -> str:\n    \"\"\"\n    Annotates a video with baseline model boxes and labels.\n    \"\"\"\n    VIDEO_CODEC = \"MP4V\"\n    HELMET_COLOR = (0, 0, 0)  # Black\n    video_name = os.path.basename(video_path)\n    if verbose:\n        print(f\"Running for {video_name}\")\n    baseline_boxes = baseline_boxes.copy()\n\n    vidcap = cv2.VideoCapture(video_path)\n    fps = vidcap.get(cv2.CAP_PROP_FPS)\n    width = int(vidcap.get(cv2.CAP_PROP_FRAME_WIDTH))\n    height = int(vidcap.get(cv2.CAP_PROP_FRAME_HEIGHT))\n    output_path = \"labeled_\" + video_name\n    tmp_output_path = \"tmp_\" + output_path\n    output_video = cv2.VideoWriter(\n        tmp_output_path, cv2.VideoWriter_fourcc(*VIDEO_CODEC), fps, (width, height)\n    )\n\n    frame = 0\n    while True:\n        it_worked, img = vidcap.read()\n        if not it_worked:\n            break\n        # We need to add 1 to the frame count to match the label frame index\n        # that starts at 1\n        frame += 1\n\n        # Let's add a frame index to the video so we can track where we are\n        img_name = video_name.replace(\".mp4\", \"\")\n        cv2.putText(\n            img,\n            img_name,\n            (10, 30),\n            cv2.FONT_HERSHEY_SIMPLEX,\n            1,\n            HELMET_COLOR,\n            thickness=1,\n        )\n\n        cv2.putText(\n            img,\n            str(frame),\n            (1280 - 90, 720 - 20),\n            cv2.FONT_HERSHEY_SIMPLEX,\n            1,\n            HELMET_COLOR,\n            thickness=1,\n        )\n\n        # Now, add the boxes\n        boxes = baseline_boxes.query(\"video == @video_name and frame == @frame\")\n        for box in boxes.itertuples(index=False):\n            cv2.rectangle(\n                img,\n                (box.left, box.top),\n                (box.left + box.width, box.top + box.height),\n                HELMET_COLOR,\n                thickness=1,\n            )\n            cv2.putText(\n                img,\n                box.player_label,\n                (box.left + 1, max(0, box.top - 20)),\n                cv2.FONT_HERSHEY_SIMPLEX,\n                0.5,\n                HELMET_COLOR,\n                thickness=1,\n            )\n\n        output_video.write(img)\n    output_video.release()\n    # Not all browsers support the codec, we will re-load the file at tmp_output_path\n    # and convert to a codec that is more broadly readable using ffmpeg\n    if os.path.exists(output_path):\n        os.remove(output_path)\n    subprocess.run(\n        [\n            \"ffmpeg\",\n            \"-i\",\n            tmp_output_path,\n            \"-crf\",\n            \"18\",\n            \"-preset\",\n            \"veryfast\",\n            \"-hide_banner\",\n            \"-loglevel\",\n            \"error\",\n            \"-vcodec\",\n            \"libx264\",\n            output_path,\n        ]\n    )\n    os.remove(tmp_output_path)\n\n    return output_path","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:29:10.263828Z","iopub.execute_input":"2022-12-06T05:29:10.264231Z","iopub.status.idle":"2022-12-06T05:29:10.281414Z","shell.execute_reply.started":"2022-12-06T05:29:10.264195Z","shell.execute_reply":"2022-12-06T05:29:10.280384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example_video = \"../input/nfl-player-contact-detection/train/58168_003392_Sideline.mp4\"\noutput_video = video_with_helmets(example_video, train_baseline)\n\nfrac = 0.65  # scaling factor for display\ndisplay(\n    Video(data=output_video, embed=True, height=int(720 * frac), width=int(1280 * frac))\n)","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:29:53.522073Z","iopub.execute_input":"2022-12-06T05:29:53.523047Z","iopub.status.idle":"2022-12-06T05:31:28.940474Z","shell.execute_reply.started":"2022-12-06T05:29:53.523008Z","shell.execute_reply":"2022-12-06T05:31:28.937391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_metadata.head() ","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:32:10.489358Z","iopub.execute_input":"2022-12-06T05:32:10.489786Z","iopub.status.idle":"2022-12-06T05:32:10.505337Z","shell.execute_reply.started":"2022-12-06T05:32:10.489750Z","shell.execute_reply":"2022-12-06T05:32:10.504103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = [15,8])\ntrain_metadata['view'].value_counts().plot(kind='barh')","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:33:38.305421Z","iopub.execute_input":"2022-12-06T05:33:38.305829Z","iopub.status.idle":"2022-12-06T05:33:38.550371Z","shell.execute_reply.started":"2022-12-06T05:33:38.305796Z","shell.execute_reply":"2022-12-06T05:33:38.549242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8,6), dpi=100)\nplt.title('Histogram of the Time Column')\nsns.histplot(train_metadata['view'], bins=30, kde=True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:37:00.612554Z","iopub.execute_input":"2022-12-06T05:37:00.613358Z","iopub.status.idle":"2022-12-06T05:37:00.876879Z","shell.execute_reply.started":"2022-12-06T05:37:00.613308Z","shell.execute_reply":"2022-12-06T05:37:00.875862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8,6), dpi=100)\nplt.title('Histogram of the Time Column')\nsns.histplot(train_metadata['game_key'], bins=30, kde=True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:37:25.725388Z","iopub.execute_input":"2022-12-06T05:37:25.725799Z","iopub.status.idle":"2022-12-06T05:37:26.033307Z","shell.execute_reply.started":"2022-12-06T05:37:25.725765Z","shell.execute_reply":"2022-12-06T05:37:26.032146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8,6), dpi=100)\nplt.title('Histogram of the Time Column')\nsns.histplot(train_metadata['play_id'], bins=30, kde=True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-06T05:37:46.555915Z","iopub.execute_input":"2022-12-06T05:37:46.556348Z","iopub.status.idle":"2022-12-06T05:37:46.859756Z","shell.execute_reply.started":"2022-12-06T05:37:46.556313Z","shell.execute_reply":"2022-12-06T05:37:46.858771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### More analysis on data and submission yet to come.......follow...","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}