{"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":"# [\\[OriginalCodeLink\\]NFL - YOLOv8 Object Detection and Segmentation](https://www.kaggle.com/code/dariussingh/nfl-yolov8-object-detection-and-segmentation/notebook)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"!pip install ultralytics","metadata":{"execution":{"iopub.status.busy":"2023-03-30T07:51:59.260202Z","iopub.execute_input":"2023-03-30T07:51:59.260623Z","iopub.status.idle":"2023-03-30T07:52:15.584411Z","shell.execute_reply.started":"2023-03-30T07:51:59.260585Z","shell.execute_reply":"2023-03-30T07:52:15.583199Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import ultralytics\nultralytics.checks()","metadata":{"execution":{"iopub.status.busy":"2023-03-30T07:52:15.587326Z","iopub.execute_input":"2023-03-30T07:52:15.587723Z","iopub.status.idle":"2023-03-30T07:52:18.066149Z","shell.execute_reply.started":"2023-03-30T07:52:15.587680Z","shell.execute_reply":"2023-03-30T07:52:18.065095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport subprocess\nimport IPython\nfrom IPython.display import Video, display\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2023-03-30T07:52:18.069285Z","iopub.execute_input":"2023-03-30T07:52:18.070012Z","iopub.status.idle":"2023-03-30T07:52:18.076738Z","shell.execute_reply.started":"2023-03-30T07:52:18.069979Z","shell.execute_reply":"2023-03-30T07:52:18.075473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def play_video(video_path: str):\n    frac = 0.65 # scaling factor for display \n    display(\n        Video(data=video_path, embed=True, height=int(720*frac), width=int(1280*frac))\n    )","metadata":{"execution":{"iopub.status.busy":"2023-03-30T07:52:18.078734Z","iopub.execute_input":"2023-03-30T07:52:18.079420Z","iopub.status.idle":"2023-03-30T07:52:18.087469Z","shell.execute_reply.started":"2023-03-30T07:52:18.079362Z","shell.execute_reply":"2023-03-30T07:52:18.086154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def detect_and_segment_video(video_path:str) -> str:\n    \"\"\"\n    Annotates video with object detection bounding boxes and\n    segmentation masks using YOLOv8 model.\n    \"\"\"\n    \n    video_name = video_path.split('/')[-1]\n    folder_name = 'predict'\n    \n    # YOLOv8 Object Detection and Segmentation\n    # you can edit the model if you want to use a different yolo8 model\n    os.system(f\"yolo segment predict model=yolov8n-seg.pt source={video_path} save=True\")\n    \n    if not os.path.exists(\"/kaggle/working/runs/segment\"):\n        os.makedirs(\"/kaggle/working/runs/segment\")\n    # path to latest output \n    max = 0\n    for dir_name in os.listdir('/kaggle/working/runs/segment'):\n        if dir_name!='predict':\n            if int(dir_name[-1])>max:\n                max = int(dir_name[-1])\n    if max>0:\n        folder_name += str(max)\n    tmp_output_path = f'/kaggle/working/runs/segment/{folder_name}/{video_name}'\n    output_path = f'/kaggle/working/runs/segment/{folder_name}/final-{video_name}'\n    \n    if not os.path.exists(tmp_output_path):\n        os.makedirs(output_path)\n    \n    \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":"2023-03-30T07:58:33.655139Z","iopub.execute_input":"2023-03-30T07:58:33.655596Z","iopub.status.idle":"2023-03-30T07:58:33.674421Z","shell.execute_reply.started":"2023-03-30T07:58:33.655557Z","shell.execute_reply":"2023-03-30T07:58:33.673321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# change the video path to predict on a different video\nvideo_list = os.listdir('/kaggle/input/nfl-player-contact-detection/train/')\nvideo_path = f'/kaggle/input/nfl-player-contact-detection/train/{video_list[np.random.randint(0, len(video_list)-1)]}'\n# prediction\noutput_path = detect_and_segment_video(video_path)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-03-30T08:08:56.318126Z","iopub.execute_input":"2023-03-30T08:08:56.318852Z","iopub.status.idle":"2023-03-30T08:09:34.785323Z","shell.execute_reply.started":"2023-03-30T08:08:56.318813Z","shell.execute_reply":"2023-03-30T08:09:34.784021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"play_video(output_path)","metadata":{"execution":{"iopub.status.busy":"2023-03-30T08:09:34.789545Z","iopub.execute_input":"2023-03-30T08:09:34.789956Z","iopub.status.idle":"2023-03-30T08:09:34.988789Z","shell.execute_reply.started":"2023-03-30T08:09:34.789913Z","shell.execute_reply":"2023-03-30T08:09:34.987303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}