{"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":"# NFL YOLOv8 Player Position Detection\nhttps://github.com/ultralytics/ultralytics<br/>","metadata":{"papermill":{"duration":0.006075,"end_time":"2023-01-22T03:21:00.951142","exception":false,"start_time":"2023-01-22T03:21:00.945067","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"![](https://raw.githubusercontent.com/ultralytics/assets/main/yolov8/banner-yolov8.png)","metadata":{}},{"cell_type":"markdown","source":"Ultralytics YOLOv8, developed by Ultralytics, is a cutting-edge, state-of-the-art (SOTA) model that builds upon the success of previous YOLO versions and introduces new features and improvements to further boost performance and flexibility. YOLOv8 is designed to be fast, accurate, and easy to use, making it an excellent choice for a wide range of object detection, image segmentation and image classification tasks.","metadata":{}},{"cell_type":"markdown","source":"![](https://raw.githubusercontent.com/ultralytics/assets/main/yolov8/yolo-comparison-plots.png)","metadata":{}},{"cell_type":"code","source":"!rm -rf","metadata":{"papermill":{"duration":0.962851,"end_time":"2023-01-22T03:21:01.919039","exception":false,"start_time":"2023-01-22T03:21:00.956188","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-22T11:02:05.166917Z","iopub.execute_input":"2023-01-22T11:02:05.167901Z","iopub.status.idle":"2023-01-22T11:02:06.192951Z","shell.execute_reply.started":"2023-01-22T11:02:05.167799Z","shell.execute_reply":"2023-01-22T11:02:06.191433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install ultralytics &> /dev/null","metadata":{"papermill":{"duration":11.427562,"end_time":"2023-01-22T03:21:13.351928","exception":false,"start_time":"2023-01-22T03:21:01.924366","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-22T11:02:06.199493Z","iopub.execute_input":"2023-01-22T11:02:06.201540Z","iopub.status.idle":"2023-01-22T11:02:18.946872Z","shell.execute_reply.started":"2023-01-22T11:02:06.201493Z","shell.execute_reply":"2023-01-22T11:02:18.945592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ultralytics import YOLO\nimport os\nimport cv2\nimport shutil\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image\nfrom tqdm import tqdm\nfrom IPython.display import HTML, Video, Image, clear_output\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom matplotlib import animation, rc\nrc('animation', html='jshtml')","metadata":{"papermill":{"duration":2.929272,"end_time":"2023-01-22T03:21:16.286215","exception":false,"start_time":"2023-01-22T03:21:13.356943","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-22T11:02:18.948597Z","iopub.execute_input":"2023-01-22T11:02:18.948993Z","iopub.status.idle":"2023-01-22T11:02:22.254406Z","shell.execute_reply.started":"2023-01-22T11:02:18.948948Z","shell.execute_reply":"2023-01-22T11:02:22.253357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Copy Source Movie","metadata":{"papermill":{"duration":0.004505,"end_time":"2023-01-22T03:21:16.295705","exception":false,"start_time":"2023-01-22T03:21:16.291200","status":"completed"},"tags":[]}},{"cell_type":"code","source":"!mkdir -p /kaggle/working/sample1    ### source mp4\n!mkdir -p /kaggle/working/sample2    ### frame images\n!mkdir -p /kaggle/working/sample3    ### fig","metadata":{"papermill":{"duration":1.897872,"end_time":"2023-01-22T03:21:18.198285","exception":false,"start_time":"2023-01-22T03:21:16.300413","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-22T11:02:22.257006Z","iopub.execute_input":"2023-01-22T11:02:22.258626Z","iopub.status.idle":"2023-01-22T11:02:25.225145Z","shell.execute_reply.started":"2023-01-22T11:02:22.258582Z","shell.execute_reply":"2023-01-22T11:02:25.223716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"58168_003392_Sideline.mp4","metadata":{}},{"cell_type":"code","source":"path_mp0='/kaggle/input/nfl-player-contact-detection/train/58168_003392_Sideline.mp4'\npath_mp1='/kaggle/working/sample1/sample.mp4'","metadata":{"papermill":{"duration":0.013351,"end_time":"2023-01-22T03:21:18.217927","exception":false,"start_time":"2023-01-22T03:21:18.204576","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-22T11:02:25.227413Z","iopub.execute_input":"2023-01-22T11:02:25.227865Z","iopub.status.idle":"2023-01-22T11:02:25.233785Z","shell.execute_reply.started":"2023-01-22T11:02:25.227821Z","shell.execute_reply":"2023-01-22T11:02:25.232378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.copy(path_mp0,path_mp1)","metadata":{"papermill":{"duration":0.236828,"end_time":"2023-01-22T03:21:18.459561","exception":false,"start_time":"2023-01-22T03:21:18.222733","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-22T11:02:25.235801Z","iopub.execute_input":"2023-01-22T11:02:25.236326Z","iopub.status.idle":"2023-01-22T11:02:25.323583Z","shell.execute_reply.started":"2023-01-22T11:02:25.236264Z","shell.execute_reply":"2023-01-22T11:02:25.322684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir('/kaggle/working'))\nprint(os.listdir('/kaggle/working/sample1'))","metadata":{"papermill":{"duration":0.015571,"end_time":"2023-01-22T03:21:18.481552","exception":false,"start_time":"2023-01-22T03:21:18.465981","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-22T11:02:25.325178Z","iopub.execute_input":"2023-01-22T11:02:25.325570Z","iopub.status.idle":"2023-01-22T11:02:25.331543Z","shell.execute_reply.started":"2023-01-22T11:02:25.325532Z","shell.execute_reply":"2023-01-22T11:02:25.330554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# YOLOv8","metadata":{"papermill":{"duration":0.004826,"end_time":"2023-01-22T03:21:18.492276","exception":false,"start_time":"2023-01-22T03:21:18.487450","status":"completed"},"tags":[]}},{"cell_type":"code","source":"model = YOLO(\"yolov8x.pt\") ","metadata":{"papermill":{"duration":2.22776,"end_time":"2023-01-22T03:21:20.725225","exception":false,"start_time":"2023-01-22T03:21:18.497465","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-22T11:02:25.333513Z","iopub.execute_input":"2023-01-22T11:02:25.334275Z","iopub.status.idle":"2023-01-22T11:02:36.044956Z","shell.execute_reply.started":"2023-01-22T11:02:25.334238Z","shell.execute_reply":"2023-01-22T11:02:36.043923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"save=True is required as of 2023 march","metadata":{}},{"cell_type":"code","source":"!yolo task=detect mode=predict model=yolov8x.pt conf=0.001 source={path_mp1} save=True","metadata":{"papermill":{"duration":61.655456,"end_time":"2023-01-22T03:22:22.386372","exception":false,"start_time":"2023-01-22T03:21:20.730916","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-22T11:02:36.046366Z","iopub.execute_input":"2023-01-22T11:02:36.046811Z","iopub.status.idle":"2023-01-22T11:03:33.125444Z","shell.execute_reply.started":"2023-01-22T11:02:36.046764Z","shell.execute_reply":"2023-01-22T11:03:33.124171Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls runs/detect/predict","metadata":{"papermill":{"duration":0.990921,"end_time":"2023-01-22T03:22:23.419294","exception":false,"start_time":"2023-01-22T03:22:22.428373","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-22T11:03:33.131647Z","iopub.execute_input":"2023-01-22T11:03:33.131997Z","iopub.status.idle":"2023-01-22T11:03:34.080046Z","shell.execute_reply.started":"2023-01-22T11:03:33.131964Z","shell.execute_reply":"2023-01-22T11:03:34.078799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_mp2=\"/kaggle/working/runs/detect/predict/sample.mp4\"","metadata":{"papermill":{"duration":0.050229,"end_time":"2023-01-22T03:22:23.511493","exception":false,"start_time":"2023-01-22T03:22:23.461264","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-22T11:03:34.081920Z","iopub.execute_input":"2023-01-22T11:03:34.083658Z","iopub.status.idle":"2023-01-22T11:03:34.088898Z","shell.execute_reply.started":"2023-01-22T11:03:34.083599Z","shell.execute_reply":"2023-01-22T11:03:34.087865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Video(path_mp2, width=600, height=400, embed=True)","metadata":{"papermill":{"duration":0.048402,"end_time":"2023-01-22T03:22:23.601257","exception":false,"start_time":"2023-01-22T03:22:23.552855","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-22T11:03:34.092257Z","iopub.execute_input":"2023-01-22T11:03:34.092576Z","iopub.status.idle":"2023-01-22T11:03:34.099753Z","shell.execute_reply.started":"2023-01-22T11:03:34.092549Z","shell.execute_reply":"2023-01-22T11:03:34.098726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def video2frames(video_file=path_mp2, image_dir='/kaggle/working/sample2/', image_file='img_%s.png'):\n    i = 0\n    cap = cv2.VideoCapture(video_file)\n    while(cap.isOpened()):\n        flag, frame = cap.read()\n        if flag == False:\n            break\n        cv2.imwrite(image_dir+image_file % str(i).zfill(5), frame) \n        i += 1\n    cap.release()\n    \nvideo2frames()","metadata":{"papermill":{"duration":80.562651,"end_time":"2023-01-22T03:23:44.205340","exception":false,"start_time":"2023-01-22T03:22:23.642689","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-22T11:03:34.103031Z","iopub.execute_input":"2023-01-22T11:03:34.103922Z","iopub.status.idle":"2023-01-22T11:04:53.765634Z","shell.execute_reply.started":"2023-01-22T11:03:34.103885Z","shell.execute_reply":"2023-01-22T11:04:53.764482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paths0=[]\nfor dirname, _, filenames in os.walk('/kaggle/working/sample2/'):\n    for filename in filenames:\n        if filename[-4:]=='.png':\n            paths0+=[(os.path.join(dirname, filename))]\npaths0=sorted(paths0)            \nimages0=[]\nfor i in tqdm(range(len(paths0))):\n    images0+=[cv2.imread(paths0[i])]","metadata":{"papermill":{"duration":16.662056,"end_time":"2023-01-22T03:24:00.909102","exception":false,"start_time":"2023-01-22T03:23:44.247046","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-22T11:04:53.767007Z","iopub.execute_input":"2023-01-22T11:04:53.767421Z","iopub.status.idle":"2023-01-22T11:05:11.033027Z","shell.execute_reply.started":"2023-01-22T11:04:53.767385Z","shell.execute_reply":"2023-01-22T11:05:11.031852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_animation(ims):\n    fig=plt.figure(figsize=(12,8))\n    im=plt.imshow(cv2.cvtColor(ims[0],cv2.COLOR_BGR2RGB))\n    plt.axis('off')\n    plt.close()\n\n    def animate_func(i):\n        im.set_array(cv2.cvtColor(ims[i],cv2.COLOR_BGR2RGB))\n        return [im]    \n    \n    return animation.FuncAnimation(fig, animate_func, frames=len(ims), interval=1000//10)\n\ncreate_animation(np.array(images0))","metadata":{"papermill":{"duration":60.538302,"end_time":"2023-01-22T03:25:01.495291","exception":false,"start_time":"2023-01-22T03:24:00.956989","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-22T11:05:11.034654Z","iopub.execute_input":"2023-01-22T11:05:11.035550Z","iopub.status.idle":"2023-01-22T11:06:07.693329Z","shell.execute_reply.started":"2023-01-22T11:05:11.035509Z","shell.execute_reply":"2023-01-22T11:06:07.691942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Obtain rectangle information","metadata":{"papermill":{"duration":0.594134,"end_time":"2023-01-22T03:25:02.686456","exception":false,"start_time":"2023-01-22T03:25:02.092322","status":"completed"},"tags":[]}},{"cell_type":"code","source":"results = model.predict(path_mp1,conf=0.001)\nprint(len(results))","metadata":{"papermill":{"duration":35.966895,"end_time":"2023-01-22T03:25:39.240041","exception":false,"start_time":"2023-01-22T03:25:03.273146","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-22T11:06:07.694965Z","iopub.execute_input":"2023-01-22T11:06:07.695686Z","iopub.status.idle":"2023-01-22T11:06:40.698556Z","shell.execute_reply.started":"2023-01-22T11:06:07.695607Z","shell.execute_reply":"2023-01-22T11:06:40.697517Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BOX=pd.DataFrame(columns=range(6))\nfor i in range(len(results)):\n    arri=pd.DataFrame(results[i].boxes.boxes).astype(float)\n    arri['i']=i\n    BOX=pd.concat([BOX,arri],axis=0)\nBOX.columns=['x','y','x2','y2','confidence','class','i']\ndisplay(BOX)","metadata":{"papermill":{"duration":3.033841,"end_time":"2023-01-22T03:25:42.855432","exception":false,"start_time":"2023-01-22T03:25:39.821591","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-22T11:06:40.700026Z","iopub.execute_input":"2023-01-22T11:06:40.700528Z","iopub.status.idle":"2023-01-22T11:06:41.956318Z","shell.execute_reply.started":"2023-01-22T11:06:40.700489Z","shell.execute_reply":"2023-01-22T11:06:41.955343Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_map = {0: u'__background__', 1: u'person', 2: u'bicycle',3: u'car', 4: u'motorcycle', 5: u'airplane', 6: u'bus', 7: u'train', 8: u'truck', 9: u'boat', 10: u'traffic light', 11: u'fire hydrant', 12: u'stop sign', 13: u'parking meter', 14: u'bench', 15: u'bird', 16: u'cat', 17: u'dog', 18: u'horse', 19: u'sheep', 20: u'cow', 21: u'elephant', 22: u'bear', 23: u'zebra', 24: u'giraffe', 25: u'backpack', 26: u'umbrella', 27: u'handbag', 28: u'tie', 29: u'suitcase', 30: u'frisbee', 31: u'skis', 32: u'snowboard', 33: u'sports ball', 34: u'kite', 35: u'baseball bat', 36: u'baseball glove', 37: u'skateboard', 38: u'surfboard', 39: u'tennis racket', 40: u'bottle', 41: u'wine glass', 42: u'cup', 43: u'fork', 44: u'knife', 45: u'spoon', 46: u'bowl', 47: u'banana', 48: u'apple', 49: u'sandwich', 50: u'orange', 51: u'broccoli', 52: u'carrot', 53: u'hot dog', 54: u'pizza', 55: u'donut', 56: u'cake', 57: u'chair', 58: u'couch', 59: u'potted plant', 60: u'bed', 61: u'dining table', 62: u'toilet', 63: u'tv', 64: u'laptop', 65: u'mouse', 66: u'remote', 67: u'keyboard', 68: u'cell phone', 69: u'microwave', 70: u'oven', 71: u'toaster', 72: u'sink', 73: u'refrigerator', 74: u'book', 75: u'clock', 76: u'vase', 77: u'scissors', 78: u'teddy bear', 79: u'hair drier', 80: u'toothbrush'}","metadata":{"papermill":{"duration":0.607278,"end_time":"2023-01-22T03:25:44.049923","exception":false,"start_time":"2023-01-22T03:25:43.442645","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-22T11:06:41.957825Z","iopub.execute_input":"2023-01-22T11:06:41.958221Z","iopub.status.idle":"2023-01-22T11:06:41.968510Z","shell.execute_reply.started":"2023-01-22T11:06:41.958183Z","shell.execute_reply":"2023-01-22T11:06:41.967451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BOX['class']=BOX['class'].apply(lambda x: class_map[int(x)+1])\nBOX=BOX.reset_index(drop=True)\ndisplay(BOX)\ndisplay(BOX['class'].value_counts())","metadata":{"papermill":{"duration":0.640078,"end_time":"2023-01-22T03:25:45.280192","exception":false,"start_time":"2023-01-22T03:25:44.640114","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-22T11:06:41.970143Z","iopub.execute_input":"2023-01-22T11:06:41.970933Z","iopub.status.idle":"2023-01-22T11:06:42.001286Z","shell.execute_reply.started":"2023-01-22T11:06:41.970896Z","shell.execute_reply":"2023-01-22T11:06:42.000291Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data0=BOX[BOX['class']=='person'] #[BOX['confidence']>0.1]\ndata0['i']=data0['i'].apply(lambda x: int(x))\ndata0['y']=data0['y'].apply(lambda y: -y)\ndata0=data0.reset_index(drop=True)\ndata0['j']=data0.index.tolist()\ndisplay(data0)\nprint(len(data0))\ndata0.to_csv('data0.csv',index=False)","metadata":{"papermill":{"duration":0.637652,"end_time":"2023-01-22T03:25:46.498900","exception":false,"start_time":"2023-01-22T03:25:45.861248","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-22T11:08:44.180190Z","iopub.execute_input":"2023-01-22T11:08:44.180939Z","iopub.status.idle":"2023-01-22T11:08:44.212923Z","shell.execute_reply.started":"2023-01-22T11:08:44.180897Z","shell.execute_reply":"2023-01-22T11:08:44.212005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport plotly.express as px\nimport plotly.graph_objects as go\nfrom plotly.subplots import make_subplots","metadata":{"papermill":{"duration":3.225343,"end_time":"2023-01-22T03:25:50.340798","exception":false,"start_time":"2023-01-22T03:25:47.115455","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-22T11:08:48.435456Z","iopub.execute_input":"2023-01-22T11:08:48.435873Z","iopub.status.idle":"2023-01-22T11:08:48.441930Z","shell.execute_reply.started":"2023-01-22T11:08:48.435837Z","shell.execute_reply":"2023-01-22T11:08:48.440567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(data0['x'].min(),data0['x'].max())\nprint(data0['y'].min(),data0['y'].max())","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:08:55.598448Z","iopub.execute_input":"2023-01-22T11:08:55.598858Z","iopub.status.idle":"2023-01-22T11:08:55.606531Z","shell.execute_reply.started":"2023-01-22T11:08:55.598826Z","shell.execute_reply":"2023-01-22T11:08:55.605365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(results)):\n    ax=sns.scatterplot(data=data0[data0['i']==i], x=\"x\", y=\"y\", palette=\"Paired\") \n    plt.title(\"Player Positions \"+str(i).zfill(5))\n    ax.set_xlim([0, 1200])\n    ax.set_ylim([-600, 0])\n    plt.savefig('./sample3/fig_'+str(i).zfill(5)+'.png')\n    plt.close()","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:15:00.187367Z","iopub.execute_input":"2023-01-22T11:15:00.187776Z","iopub.status.idle":"2023-01-22T11:15:04.992037Z","shell.execute_reply.started":"2023-01-22T11:15:00.187743Z","shell.execute_reply":"2023-01-22T11:15:04.990843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paths3=[]\nfor dirname, _, filenames in os.walk('/kaggle/working/sample3/'):\n    for filename in filenames:\n        if filename[-4:]=='.png':\n            paths3+=[(os.path.join(dirname, filename))]\npaths3=sorted(paths3)\nprint(paths3[100:103])\nimages1=[]\nfor i in tqdm(range(0,len(paths3),2)):\n    images1+=[cv2.imread(paths3[i])]","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:06:43.845523Z","iopub.status.idle":"2023-01-22T11:06:43.846538Z","shell.execute_reply.started":"2023-01-22T11:06:43.846251Z","shell.execute_reply":"2023-01-22T11:06:43.846278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_animation(np.array(images1))","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:06:43.847920Z","iopub.status.idle":"2023-01-22T11:06:43.848865Z","shell.execute_reply.started":"2023-01-22T11:06:43.848590Z","shell.execute_reply":"2023-01-22T11:06:43.848617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.574092,"end_time":"2023-01-22T03:25:54.154549","exception":false,"start_time":"2023-01-22T03:25:53.580457","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}