{"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":"# YOLOv5 ball detection\n### References\n\n* YOLOv5 repository - https://github.com/ultralytics/yolov5\n* https://www.kaggle.com/code/eneszvo/yolov5-helmet-detection-train-and-inference","metadata":{}},{"cell_type":"code","source":"!pip install moviepy -q","metadata":{"execution":{"iopub.status.busy":"2022-08-08T02:50:01.871905Z","iopub.execute_input":"2022-08-08T02:50:01.872316Z","iopub.status.idle":"2022-08-08T02:50:23.589529Z","shell.execute_reply.started":"2022-08-08T02:50:01.872229Z","shell.execute_reply":"2022-08-08T02:50:23.588546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ../\n!mkdir tmp\n%cd tmp","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-08T02:50:23.592401Z","iopub.execute_input":"2022-08-08T02:50:23.592991Z","iopub.status.idle":"2022-08-08T02:50:24.570813Z","shell.execute_reply.started":"2022-08-08T02:50:23.592946Z","shell.execute_reply":"2022-08-08T02:50:24.569772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Download YOLOv5\n!git clone https://github.com/ultralytics/yolov5  # clone repo\n%cd yolov5\n# Install dependencies\n%pip install -qr requirements.txt  # install dependencies\n\n%cd ../\nimport torch\nprint(f\"Setup complete. Using torch {torch.__version__} ({torch.cuda.get_device_properties(0).name if torch.cuda.is_available() else 'CPU'})\")","metadata":{"execution":{"iopub.status.busy":"2022-08-08T02:50:24.572873Z","iopub.execute_input":"2022-08-08T02:50:24.573169Z","iopub.status.idle":"2022-08-08T02:50:37.304648Z","shell.execute_reply.started":"2022-08-08T02:50:24.573138Z","shell.execute_reply":"2022-08-08T02:50:37.303675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport gc\nimport cv2\nimport numpy as np\nimport pandas as pd\nfrom tqdm import tqdm\nfrom shutil import copyfile\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\n\nimport subprocess","metadata":{"execution":{"iopub.status.busy":"2022-08-08T02:50:37.306358Z","iopub.execute_input":"2022-08-08T02:50:37.306683Z","iopub.status.idle":"2022-08-08T02:50:38.249414Z","shell.execute_reply.started":"2022-08-08T02:50:37.306645Z","shell.execute_reply":"2022-08-08T02:50:38.248534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ../\n!cp -r input/dfl-yolov5l6-ball-detection/yolov5l6_trained_600images.pt tmp/yolov5/\n!cp -r input/dfl-bundesliga-data-shootout/clips/08fd33_0.mp4 tmp/yolov5/\n%cd tmp/yolov5/","metadata":{"execution":{"iopub.status.busy":"2022-08-08T02:50:38.250791Z","iopub.execute_input":"2022-08-08T02:50:38.251153Z","iopub.status.idle":"2022-08-08T02:50:42.368673Z","shell.execute_reply.started":"2022-08-08T02:50:38.251116Z","shell.execute_reply":"2022-08-08T02:50:42.36766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# YOLOv5 Inference with clip/08fd33_0.mp4","metadata":{}},{"cell_type":"code","source":"!python detect.py --img 1280 \\\n                  --weights yolov5l6_trained_600images.pt \\\n                  --source 08fd33_0.mp4 \\\n                  --project DFL","metadata":{"execution":{"iopub.status.busy":"2022-08-08T02:50:42.370418Z","iopub.execute_input":"2022-08-08T02:50:42.370797Z","iopub.status.idle":"2022-08-08T02:52:07.435082Z","shell.execute_reply.started":"2022-08-08T02:50:42.370752Z","shell.execute_reply":"2022-08-08T02:52:07.434068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r ./DFL/exp/ /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2022-08-08T02:52:07.438176Z","iopub.execute_input":"2022-08-08T02:52:07.438478Z","iopub.status.idle":"2022-08-08T02:52:08.475574Z","shell.execute_reply.started":"2022-08-08T02:52:07.438444Z","shell.execute_reply":"2022-08-08T02:52:08.474464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import subprocess\n\ncap = cv2.VideoCapture(\"./DFL/exp/08fd33_0.mp4\")\n\nframes = []\nwhile True:\n    ret, img = cap.read()\n    if ret == False:\n        break\n    frames.append(img)\n    width = img.shape[1]\n    height = img.shape[0]\ncap.release()\n\nvideo = cv2.VideoWriter(\"/kaggle/working/tmp.mp4\", cv2.VideoWriter_fourcc(*'MP4V'),\n                                25, (width,height))\nfor img in frames:\n    video.write(img)\n\nvideo.release()   \n\nsubprocess.run([\"ffmpeg\", \"-i\", \"/kaggle/working/tmp.mp4\", \"-crf\", \"18\", \"-preset\", \"veryfast\",\n                \"-vcodec\",\"libx264\", \"/kaggle/working/output.mp4\",])","metadata":{"execution":{"iopub.status.busy":"2022-08-08T02:52:08.478464Z","iopub.execute_input":"2022-08-08T02:52:08.478851Z","iopub.status.idle":"2022-08-08T02:53:03.173631Z","shell.execute_reply.started":"2022-08-08T02:52:08.478809Z","shell.execute_reply":"2022-08-08T02:53:03.172744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Show result","metadata":{}},{"cell_type":"code","source":"from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip\nfrom IPython.display import Video\n\nfilename = \"Output.mp4\"\nffmpeg_extract_subclip(\"/kaggle/working/output.mp4\", 0, 30, targetname=filename)\n    \nVideo(filename, width=800, embed=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T02:53:03.17549Z","iopub.execute_input":"2022-08-08T02:53:03.175875Z","iopub.status.idle":"2022-08-08T02:53:04.556285Z","shell.execute_reply.started":"2022-08-08T02:53:03.175835Z","shell.execute_reply":"2022-08-08T02:53:04.553111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}