Abs6187/Helmet-Detect-model
5
1{2 "cells": [3 {4 "cell_type": "markdown",5 "metadata": {6 "id": "view-in-github",7 "colab_type": "text"8 },9 "source": [10 "<a href=\"https://colab.research.google.com/github/Abs6187/Helmet-Detection/blob/main/Helmet_Detection_using_Yolo11.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"11 ]12 },13 {14 "cell_type": "markdown",15 "metadata": {16 "id": "KudphSMZLKdf"17 },18 "source": [19 "Data: https://universe.roboflow.com/yolo-do-it-yhopz/helmet-detector-9rzmg-bmd6q"20 ]21 },22 {23 "cell_type": "code",24 "execution_count": null,25 "metadata": {26 "colab": {27 "base_uri": "https://localhost:8080/"28 },29 "id": "iB8A5n5uRf-r",30 "outputId": "2782f406-b698-40cf-a236-2ca43933e93e"31 },32 "outputs": [],33 "source": [34 "!nvidia-smi"35 ]36 },37 {38 "cell_type": "markdown",39 "metadata": {40 "id": "gYrzZil_R9e8"41 },42 "source": [43 "**Step 01 # Install the Ultralytics Package**"44 ]45 },46 {47 "cell_type": "code",48 "execution_count": null,49 "metadata": {50 "colab": {51 "base_uri": "https://localhost:8080/"52 },53 "collapsed": true,54 "id": "Lrt8kAH2R5IH",55 "outputId": "14e9ee56-c651-4dbb-8666-63a0d9fc2c9e"56 },57 "outputs": [],58 "source": [59 "!pip install ultralytics"60 ]61 },62 {63 "cell_type": "markdown",64 "metadata": {65 "id": "7UavftMRSJhe"66 },67 "source": [68 "**Step 02 # Import All the Requried Libraries**"69 ]70 },71 {72 "cell_type": "code",73 "execution_count": null,74 "metadata": {75 "colab": {76 "base_uri": "https://localhost:8080/"77 },78 "id": "OKI6XspoSkdc",79 "outputId": "fd3025fa-a0fa-4c83-bc42-66c883ca954c"80 },81 "outputs": [],82 "source": [83 "import ultralytics\n",84 "ultralytics.checks()"85 ]86 },87 {88 "cell_type": "code",89 "execution_count": null,90 "metadata": {91 "id": "oHWcOSblSGg8"92 },93 "outputs": [],94 "source": [95 "from ultralytics import YOLO\n",96 "from IPython.display import Image"97 ]98 },99 {100 "cell_type": "markdown",101 "metadata": {102 "id": "x1Nv8H2mS41D"103 },104 "source": [105 "**Step # 03 Download Dataset from Roboflow**"106 ]107 },108 {109 "cell_type": "code",110 "execution_count": null,111 "metadata": {112 "colab": {113 "base_uri": "https://localhost:8080/"114 },115 "collapsed": true,116 "id": "E9wKYZvASY3U",117 "outputId": "13477f14-13f1-43e7-9181-df982850ef41"118 },119 "outputs": [],120 "source": [121 "!pip install roboflow\n",122 "\n",123 "from roboflow import Roboflow\n",124 "rf = Roboflow(api_key=\"ZkNMlNnyIa2y7w8zGWMS\")\n",125 "project = rf.workspace(\"yolo-do-it-yhopz\").project(\"helmet-detector-9rzmg-bmd6q\")\n",126 "version = project.version(1)\n",127 "dataset = version.download(\"yolov11\")\n",128 "\n"129 ]130 },131 {132 "cell_type": "code",133 "execution_count": null,134 "metadata": {135 "colab": {136 "base_uri": "https://localhost:8080/",137 "height": 36138 },139 "id": "mdZobxnHVhhf",140 "outputId": "f58637cc-a63c-4970-e321-86bd40d1673b"141 },142 "outputs": [],143 "source": [144 "dataset.location"145 ]146 },147 {148 "cell_type": "markdown",149 "metadata": {150 "id": "B_cfUufNYF3_"151 },152 "source": [153 "**Step # 04 Train YOLO11 Model on a Custom Dataset**"154 ]155 },156 {157 "cell_type": "code",158 "execution_count": null,159 "metadata": {160 "colab": {161 "base_uri": "https://localhost:8080/"162 },163 "id": "yxp_shPlYBAS",164 "outputId": "cdfe19bc-cfbb-4f20-fdad-8365c1c778d0"165 },166 "outputs": [],167 "source": [168 "!yolo task=detect mode=train data={dataset.location}/data.yaml model=\"yolo11n.pt\" epochs=50 imgsz=640"169 ]170 },171 {172 "cell_type": "markdown",173 "metadata": {174 "id": "gFLPh2gN04R_"175 },176 "source": [177 "**Step # 05 Examine Training Results**"178 ]179 },180 {181 "cell_type": "code",182 "execution_count": null,183 "metadata": {184 "colab": {185 "base_uri": "https://localhost:8080/",186 "height": 467187 },188 "id": "vHVXACym1VZk",189 "outputId": "96b69bc1-f73e-457c-dddc-2d5fbd403365"190 },191 "outputs": [],192 "source": [193 "Image(\"/content/runs/detect/train/confusion_matrix.png\", width=600)"194 ]195 },196 {197 "cell_type": "code",198 "execution_count": null,199 "metadata": {200 "colab": {201 "base_uri": "https://localhost:8080/",202 "height": 617203 },204 "id": "Hh2vau2HMuc_",205 "outputId": "ae25b81c-a0ba-40b1-a75e-4f6d15415e24"206 },207 "outputs": [],208 "source": [209 "Image(\"/content/runs/detect/train/labels.jpg\", width=600)"210 ]211 },212 {213 "cell_type": "code",214 "execution_count": null,215 "metadata": {216 "colab": {217 "base_uri": "https://localhost:8080/",218 "height": 317219 },220 "id": "PeSiFrwP1tbF",221 "outputId": "bc5c5a0f-6f05-48a9-f969-e67512174b09"222 },223 "outputs": [],224 "source": [225 "Image(\"/content/runs/detect/train/results.png\", width=600)"226 ]227 },228 {229 "cell_type": "code",230 "execution_count": null,231 "metadata": {232 "colab": {233 "base_uri": "https://localhost:8080/",234 "height": 617235 },236 "id": "RacyVWK3103E",237 "outputId": "6fd22301-d1d9-45f3-f408-0675a370197e"238 },239 "outputs": [],240 "source": [241 "Image(\"/content/runs/detect/train/train_batch0.jpg\", width=600)"242 ]243 },244 {245 "cell_type": "code",246 "execution_count": null,247 "metadata": {248 "colab": {249 "base_uri": "https://localhost:8080/",250 "height": 361251 },252 "id": "idaZDidT10_L",253 "outputId": "95b372b7-b8e4-4673-c96a-2421e4005e52"254 },255 "outputs": [],256 "source": [257 "Image(\"/content/runs/detect/train/val_batch0_pred.jpg\", width=600)"258 ]259 },260 {261 "cell_type": "code",262 "execution_count": null,263 "metadata": {264 "colab": {265 "base_uri": "https://localhost:8080/",266 "height": 361267 },268 "id": "VPEj-hRA11Pn",269 "outputId": "1c91d829-d1b7-4f96-f59a-32cbc19eaee6"270 },271 "outputs": [],272 "source": [273 "Image(\"/content/runs/detect/train/val_batch1_pred.jpg\", width=600)"274 ]275 },276 {277 "cell_type": "markdown",278 "metadata": {279 "id": "OOdt0GSA5ntm"280 },281 "source": [282 "**Step # 08 Inference with Custom Model on Images**"283 ]284 },285 {286 "cell_type": "code",287 "execution_count": null,288 "metadata": {289 "colab": {290 "base_uri": "https://localhost:8080/"291 },292 "id": "E1dU-fiz3sUr",293 "outputId": "bf94989c-6421-4bbf-c23a-07c8263a1d3a"294 },295 "outputs": [],296 "source": [297 "!yolo task=detect mode=predict model=\"/content/runs/detect/train/weights/best.pt\" conf=0.25 source={dataset.location}/test/images save=True"298 ]299 },300 {301 "cell_type": "code",302 "execution_count": null,303 "metadata": {304 "colab": {305 "base_uri": "https://localhost:8080/",306 "height": 1000307 },308 "id": "ovHNzaN76tNT",309 "outputId": "1491c314-bdc6-4930-b94f-793603639fd0"310 },311 "outputs": [],312 "source": [313 "import glob\n",314 "import os\n",315 "from IPython.display import Image as IPyImage, display\n",316 "\n",317 "latest_folder = max(glob.glob('/content/runs/detect/predict*/'), key=os.path.getmtime)\n",318 "for img in glob.glob(f'{latest_folder}/*.jpg')[1:4]:\n",319 " display(IPyImage(filename=img, width=600))\n",320 " print(\"\\n\")"321 ]322 },323 {324 "cell_type": "code",325 "execution_count": null,326 "metadata": {327 "colab": {328 "base_uri": "https://localhost:8080/"329 },330 "id": "RjjfKx_s7slc",331 "outputId": "ef915291-2991-4627-c819-aeb6c81d5f8e"332 },333 "outputs": [],334 "source": [335 "!yolo task=detect mode=predict model= \"/content/runs/detect/train/weights/best.pt\" conf=0.25 source=helmet.jpg save=True"336 ]337 },338 {339 "cell_type": "markdown",340 "source": [],341 "metadata": {342 "id": "3Fm1p8pogdnY"343 }344 },345 {346 "cell_type": "markdown",347 "metadata": {348 "id": "v8XHiRuh7gYJ"349 },350 "source": [351 "**Step # 08 Inference with Custom Model on Videos (if available)**"352 ]353 },354 {355 "cell_type": "code",356 "execution_count": null,357 "metadata": {358 "colab": {359 "base_uri": "https://localhost:8080/"360 },361 "id": "xmwUaudA8JFj",362 "outputId": "2dbb1af8-60b2-42d9-bad7-2f9a7317a5b9"363 },364 "outputs": [],365 "source": [366 "!yolo task=detect mode=predict model= \"/content/runs/detect/train/weights/best.pt\" conf=0.25 source=\"PPE_Part1.mp4\" save=True"367 ]368 },369 {370 "cell_type": "code",371 "execution_count": null,372 "metadata": {373 "colab": {374 "background_save": true375 },376 "id": "mqMDDLd28lGK"377 },378 "outputs": [],379 "source": [380 "from IPython.display import HTML\n",381 "from base64 import b64encode\n",382 "import os\n",383 "\n",384 "# Input video path\n",385 "save_path = '/content/runs/detect/predict3/PPE_Part1.avi'\n",386 "\n",387 "# Compressed video path\n",388 "compressed_path = \"/content/result_compressed.mp4\"\n",389 "\n",390 "os.system(f\"ffmpeg -i {save_path} -vcodec libx264 {compressed_path}\")\n",391 "\n",392 "# Show video\n",393 "mp4 = open(compressed_path,'rb').read()\n",394 "data_url = \"data:video/mp4;base64,\" + b64encode(mp4).decode()\n",395 "HTML(\"\"\"\n",396 "<video width=400 controls>\n",397 " <source src=\"%s\" type=\"video/mp4\">\n",398 "</video>\n",399 "\"\"\" % data_url)"400 ]401 },402 {403 "cell_type": "code",404 "execution_count": null,405 "metadata": {406 "id": "xWyRfCW79Mnp"407 },408 "outputs": [],409 "source": []410 }411 ],412 "metadata": {413 "accelerator": "GPU",414 "colab": {415 "gpuType": "T4",416 "provenance": [],417 "include_colab_link": true418 },419 "kernelspec": {420 "display_name": "Python 3",421 "name": "python3"422 },423 "language_info": {424 "name": "python"425 }426 },427 "nbformat": 4,428 "nbformat_minor": 0429}