{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-09T17:40:46.080779Z","iopub.execute_input":"2022-12-09T17:40:46.081378Z","iopub.status.idle":"2022-12-09T17:40:46.103648Z","shell.execute_reply.started":"2022-12-09T17:40:46.081328Z","shell.execute_reply":"2022-12-09T17:40:46.102550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Almost copy paste of this notebook https://www.kaggle.com/code/its7171/yolov7-demo-with-dfl/\nMy addintion is that I saved the coordinates in a text file and then in a frame I drew them myself to make sure I understand how the yolov7 algorithm gives out the coordinates. ","metadata":{"execution":{"iopub.status.busy":"2022-12-09T17:40:46.105478Z","iopub.execute_input":"2022-12-09T17:40:46.105816Z","iopub.status.idle":"2022-12-09T17:40:46.114720Z","shell.execute_reply.started":"2022-12-09T17:40:46.105786Z","shell.execute_reply":"2022-12-09T17:40:46.113346Z"}}},{"cell_type":"code","source":"from IPython.display import Video\nimport os\n\nwidth = 600\nsource = '/kaggle/input/nfl-player-contact-detection/train/58172_003247_Endzone.mp4'","metadata":{"execution":{"iopub.status.busy":"2022-12-09T17:40:46.116294Z","iopub.execute_input":"2022-12-09T17:40:46.117077Z","iopub.status.idle":"2022-12-09T17:40:46.124010Z","shell.execute_reply.started":"2022-12-09T17:40:46.117034Z","shell.execute_reply":"2022-12-09T17:40:46.123083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/WongKinYiu/yolov7 \n!cd yolov7 && wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt\n!cd yolov7 && wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-e6e.pt","metadata":{"execution":{"iopub.status.busy":"2022-12-09T17:40:46.127020Z","iopub.execute_input":"2022-12-09T17:40:46.127925Z","iopub.status.idle":"2022-12-09T17:41:03.174465Z","shell.execute_reply.started":"2022-12-09T17:40:46.127881Z","shell.execute_reply":"2022-12-09T17:41:03.173083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# helper tools\ndef convert_images(input_path, output_path):\n    # adding more codec\n    if os.path.exists(output_path):\n        os.remove(output_path)\n    !ffmpeg -i $input_path -crf 18 -preset veryfast -vcodec libx264 -hide_banner -loglevel error $output_path ","metadata":{"execution":{"iopub.status.busy":"2022-12-09T17:41:03.176811Z","iopub.execute_input":"2022-12-09T17:41:03.177319Z","iopub.status.idle":"2022-12-09T17:41:03.184710Z","shell.execute_reply.started":"2022-12-09T17:41:03.177270Z","shell.execute_reply":"2022-12-09T17:41:03.183842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I added the flags save-txt and save-conf to save the locations and the confidence of the boxes","metadata":{}},{"cell_type":"code","source":"!cd yolov7 && python detect.py --weights yolov7.pt --img-size 1280 --source $source --name base --save-txt --save-conf","metadata":{"execution":{"iopub.status.busy":"2022-12-09T17:41:03.185642Z","iopub.execute_input":"2022-12-09T17:41:03.186004Z","iopub.status.idle":"2022-12-09T18:34:40.397998Z","shell.execute_reply.started":"2022-12-09T17:41:03.185955Z","shell.execute_reply":"2022-12-09T18:34:40.396567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example_video = 'yolov7/runs/detect/base/58172_003247_Endzone.mp4'\nconvert_images(example_video, '/tmp/out.mp4')\nVideo(data='/tmp/out.mp4',\n              embed=True,\n              width=width)","metadata":{"execution":{"iopub.status.busy":"2022-12-09T18:34:40.399750Z","iopub.execute_input":"2022-12-09T18:34:40.400117Z","iopub.status.idle":"2022-12-09T18:34:57.736825Z","shell.execute_reply.started":"2022-12-09T18:34:40.400076Z","shell.execute_reply":"2022-12-09T18:34:57.735658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frac = 0.65  # scaling factor for display\ndisplay(\n    Video(data='/tmp/out.mp4', embed=True, height=int(720 * frac), width=int(1280 * frac))\n)","metadata":{"execution":{"iopub.status.busy":"2022-12-09T18:34:57.738481Z","iopub.execute_input":"2022-12-09T18:34:57.739025Z","iopub.status.idle":"2022-12-09T18:34:58.361070Z","shell.execute_reply.started":"2022-12-09T18:34:57.738976Z","shell.execute_reply":"2022-12-09T18:34:58.359383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pathnow = 'yolov7/runs/detect/base'","metadata":{"execution":{"iopub.status.busy":"2022-12-09T18:34:58.362824Z","iopub.execute_input":"2022-12-09T18:34:58.363424Z","iopub.status.idle":"2022-12-09T18:34:58.376577Z","shell.execute_reply.started":"2022-12-09T18:34:58.363362Z","shell.execute_reply":"2022-12-09T18:34:58.369303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files_list = []\nfor dirname, _, filenames in os.walk(pathnow):\n    for filename in filenames:\n        files_list.append(os.path.join(dirname, filename))\nprint('the number of files is ', len(files_list)-1)","metadata":{"execution":{"iopub.status.busy":"2022-12-09T18:34:58.384599Z","iopub.execute_input":"2022-12-09T18:34:58.384982Z","iopub.status.idle":"2022-12-09T18:34:58.407035Z","shell.execute_reply.started":"2022-12-09T18:34:58.384950Z","shell.execute_reply":"2022-12-09T18:34:58.405894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" f = open(files_list[1],'r')","metadata":{"execution":{"iopub.status.busy":"2022-12-09T18:34:58.408262Z","iopub.execute_input":"2022-12-09T18:34:58.408569Z","iopub.status.idle":"2022-12-09T18:34:58.413983Z","shell.execute_reply.started":"2022-12-09T18:34:58.408539Z","shell.execute_reply":"2022-12-09T18:34:58.412862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"listboxes = f.readlines()\nlen(listboxes)","metadata":{"execution":{"iopub.status.busy":"2022-12-09T18:34:58.415794Z","iopub.execute_input":"2022-12-09T18:34:58.416120Z","iopub.status.idle":"2022-12-09T18:34:58.426161Z","shell.execute_reply.started":"2022-12-09T18:34:58.416090Z","shell.execute_reply":"2022-12-09T18:34:58.425041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The boxes are given as [class number, xcm, ycm, width, height]","metadata":{}},{"cell_type":"code","source":"listboxes","metadata":{"execution":{"iopub.status.busy":"2022-12-09T18:34:58.427602Z","iopub.execute_input":"2022-12-09T18:34:58.427911Z","iopub.status.idle":"2022-12-09T18:34:58.437371Z","shell.execute_reply.started":"2022-12-09T18:34:58.427883Z","shell.execute_reply":"2022-12-09T18:34:58.436351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's draw the boxes ourselves now by using the txt files with the boxes info. Just to make sure we understand the output. ","metadata":{}},{"cell_type":"code","source":"import cv2\nfrom IPython.display import Video","metadata":{"execution":{"iopub.status.busy":"2022-12-09T18:54:18.663947Z","iopub.execute_input":"2022-12-09T18:54:18.664378Z","iopub.status.idle":"2022-12-09T18:54:18.692634Z","shell.execute_reply.started":"2022-12-09T18:54:18.664344Z","shell.execute_reply":"2022-12-09T18:54:18.691373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pylab as plt","metadata":{"execution":{"iopub.status.busy":"2022-12-09T18:58:28.377026Z","iopub.execute_input":"2022-12-09T18:58:28.377447Z","iopub.status.idle":"2022-12-09T18:58:28.386755Z","shell.execute_reply.started":"2022-12-09T18:58:28.377410Z","shell.execute_reply":"2022-12-09T18:58:28.385126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"source = example_video","metadata":{"execution":{"iopub.status.busy":"2022-12-09T18:54:20.589043Z","iopub.execute_input":"2022-12-09T18:54:20.589465Z","iopub.status.idle":"2022-12-09T18:54:20.594655Z","shell.execute_reply.started":"2022-12-09T18:54:20.589431Z","shell.execute_reply":"2022-12-09T18:54:20.593473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I choose a frame in the video","metadata":{}},{"cell_type":"code","source":"cap = cv2.VideoCapture(source)\ni = 1\nwhile cap.isOpened() and i < 520:\n    ret, frame = cap.read()\n    if not ret:\n        break\n     #resize image\n    #frame = cv2.resize(frame, (WIDHT, HEIGHT), interpolation=cv2.INTER_AREA)\n    #frame_path = os.path.join(frames_dir, f\"{basename}_{i}.jpg\")\n    #cv2.imwrite(frame_path, frame)\n    print(f\"{i} is the frame\", end=\"\\r\")\n    i += 1","metadata":{"execution":{"iopub.status.busy":"2022-12-09T18:55:11.319682Z","iopub.execute_input":"2022-12-09T18:55:11.320097Z","iopub.status.idle":"2022-12-09T18:55:12.349847Z","shell.execute_reply.started":"2022-12-09T18:55:11.320065Z","shell.execute_reply":"2022-12-09T18:55:12.348769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I get the text file with the info on the boxes","metadata":{}},{"cell_type":"code","source":"boxes_source = '/kaggle/working/yolov7/runs/detect/base/labels/58172_003247_Endzone_519.txt'\nf = open(boxes_source,'r')\nlistboxes = f.readlines()\nlen(listboxes)","metadata":{"execution":{"iopub.status.busy":"2022-12-09T18:57:55.696095Z","iopub.execute_input":"2022-12-09T18:57:55.696479Z","iopub.status.idle":"2022-12-09T18:57:55.705246Z","shell.execute_reply.started":"2022-12-09T18:57:55.696447Z","shell.execute_reply":"2022-12-09T18:57:55.703883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I add the boxes manually by converting the yolov7 coordinates into coordinates top, bottom, left right to be used \nwith cv2. ","metadata":{}},{"cell_type":"code","source":"def add_boxes(frame, listboxes):\n    height = frame.shape[0]\n    width = frame.shape[1]\n    for i in range(len(listboxes)):\n        if float(listboxes[i].split()[0]) == 0: \n            xc = float(listboxes[i].split()[1])\n            #print(xc)\n            yc = float(listboxes[i].split()[2])\n            w = float(listboxes[i].split()[3])\n            h = float(listboxes[i].split()[4])\n            width = float(frame.shape[1])\n            height = float(frame.shape[0])\n            x_left = int((xc-w/2)*width)\n            y_top = int((yc-h/2)*height)\n            x_right = int((xc+w/2)*width)\n            y_bottom = int((yc+h/2)*height)\n            print([x_left, y_top, x_right, y_bottom])\n            cv2.rectangle(frame, (x_left, y_top), (x_right,y_bottom), (0, 0, 255), 2)\n    return frame","metadata":{"execution":{"iopub.status.busy":"2022-12-09T18:58:04.660271Z","iopub.execute_input":"2022-12-09T18:58:04.661692Z","iopub.status.idle":"2022-12-09T18:58:04.672424Z","shell.execute_reply.started":"2022-12-09T18:58:04.661640Z","shell.execute_reply":"2022-12-09T18:58:04.671080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frameb = add_boxes(frame, listboxes) ","metadata":{"execution":{"iopub.status.busy":"2022-12-09T18:58:06.372207Z","iopub.execute_input":"2022-12-09T18:58:06.372575Z","iopub.status.idle":"2022-12-09T18:58:06.378940Z","shell.execute_reply.started":"2022-12-09T18:58:06.372544Z","shell.execute_reply":"2022-12-09T18:58:06.377791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.rcParams[\"figure.figsize\"] = [11, 11]\nplt.rcParams[\"figure.autolayout\"] = True","metadata":{"execution":{"iopub.status.busy":"2022-12-09T18:58:33.198152Z","iopub.execute_input":"2022-12-09T18:58:33.198594Z","iopub.status.idle":"2022-12-09T18:58:33.204070Z","shell.execute_reply.started":"2022-12-09T18:58:33.198559Z","shell.execute_reply":"2022-12-09T18:58:33.202771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We put the boxes ourselves so we understand the coordinates of the boxes. ","metadata":{}},{"cell_type":"code","source":"plt.imshow(frameb,interpolation='nearest')","metadata":{"execution":{"iopub.status.busy":"2022-12-09T18:58:34.581407Z","iopub.execute_input":"2022-12-09T18:58:34.581824Z","iopub.status.idle":"2022-12-09T18:58:35.139605Z","shell.execute_reply.started":"2022-12-09T18:58:34.581789Z","shell.execute_reply":"2022-12-09T18:58:35.136272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}