diff --git a/README.md b/README.md index 047c066..9651c0b 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ For the video analysis we used Nvidia's Tesla K80 GPU, the system analyzes the v - Download the [YOLOv7-POSE](https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-w6-pose.pt "YOLOv7-POSE") model into `Human-Fall-Detection` directory. - Install all the requirements with `pip install -r requirements.txt` - Run main.py file +[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/DevinduDh/Human-Fall-Detection/blob/master/colab-notebook.ipynb) ## Examples diff --git a/colab-notebook.ipynb b/colab-notebook.ipynb new file mode 100644 index 0000000..fe1b154 --- /dev/null +++ b/colab-notebook.ipynb @@ -0,0 +1,355 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "code", + "source": [ + "import urllib.request\n", + "import os" + ], + "metadata": { + "id": "E9JmkCtyYUVE" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "file_path = \"/content/Human-Fall-Detection/main.py\"" + ], + "metadata": { + "id": "xr1FKo8tY7dz" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "-ArdagHTXJTl", + "outputId": "303998f8-b7d6-4d8b-87b0-3ad9c52d46bf" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Cloning into 'Human-Fall-Detection'...\n", + "remote: Enumerating objects: 1377, done.\u001b[K\n", + "remote: Counting objects: 100% (134/134), done.\u001b[K\n", + "remote: Compressing objects: 100% (105/105), done.\u001b[K\n", + "remote: Total 1377 (delta 55), reused 90 (delta 26), pack-reused 1243\u001b[K\n", + "Receiving objects: 100% (1377/1377), 189.05 MiB | 33.99 MiB/s, done.\n", + "Resolving deltas: 100% (96/96), done.\n", + "Updating files: 100% (1136/1136), done.\n" + ] + } + ], + "source": [ + "!git clone https://github.com/DevinduDh/Human-Fall-Detection.git" + ] + }, + { + "cell_type": "code", + "source": [ + "pip install -r /content/Human-Fall-Detection/requirements.txt" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "XQrRNJ5eZLir", + "outputId": "23bd17e8-f87e-46c2-d9de-ab4b0d51bddd" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Requirement already satisfied: matplotlib>=3.2.2 in /usr/local/lib/python3.10/dist-packages (from -r /content/Human-Fall-Detection/requirements.txt (line 4)) (3.7.1)\n", + "Requirement already satisfied: numpy>=1.18.5 in /usr/local/lib/python3.10/dist-packages (from -r /content/Human-Fall-Detection/requirements.txt (line 5)) (1.22.4)\n", + "Requirement already satisfied: opencv-python>=4.1.1 in /usr/local/lib/python3.10/dist-packages (from -r /content/Human-Fall-Detection/requirements.txt (line 6)) (4.7.0.72)\n", + "Requirement already satisfied: Pillow>=7.1.2 in /usr/local/lib/python3.10/dist-packages (from -r /content/Human-Fall-Detection/requirements.txt (line 7)) (8.4.0)\n", + "Requirement already satisfied: PyYAML>=5.3.1 in /usr/local/lib/python3.10/dist-packages (from -r /content/Human-Fall-Detection/requirements.txt (line 8)) (6.0)\n", + "Requirement already satisfied: requests>=2.23.0 in /usr/local/lib/python3.10/dist-packages (from -r /content/Human-Fall-Detection/requirements.txt (line 9)) (2.27.1)\n", + "Requirement already satisfied: scipy>=1.4.1 in /usr/local/lib/python3.10/dist-packages (from -r /content/Human-Fall-Detection/requirements.txt (line 10)) (1.10.1)\n", + "Requirement already satisfied: torch!=1.12.0,>=1.7.0 in /usr/local/lib/python3.10/dist-packages (from -r /content/Human-Fall-Detection/requirements.txt (line 11)) (2.0.1+cu118)\n", + "Requirement already satisfied: torchvision!=0.13.0,>=0.8.1 in /usr/local/lib/python3.10/dist-packages (from -r /content/Human-Fall-Detection/requirements.txt (line 12)) (0.15.2+cu118)\n", + "Requirement already satisfied: tqdm>=4.41.0 in /usr/local/lib/python3.10/dist-packages (from -r /content/Human-Fall-Detection/requirements.txt (line 13)) (4.65.0)\n", + "Requirement already satisfied: protobuf<4.21.3 in /usr/local/lib/python3.10/dist-packages (from -r /content/Human-Fall-Detection/requirements.txt (line 14)) (3.20.3)\n", + "Requirement already satisfied: tensorboard>=2.4.1 in /usr/local/lib/python3.10/dist-packages (from -r /content/Human-Fall-Detection/requirements.txt (line 17)) (2.12.3)\n", + "Requirement already satisfied: pandas>=1.1.4 in /usr/local/lib/python3.10/dist-packages (from -r /content/Human-Fall-Detection/requirements.txt (line 21)) (1.5.3)\n", + "Requirement already satisfied: seaborn>=0.11.0 in /usr/local/lib/python3.10/dist-packages (from -r /content/Human-Fall-Detection/requirements.txt (line 22)) (0.12.2)\n", + "Requirement already satisfied: ipython in /usr/local/lib/python3.10/dist-packages (from -r /content/Human-Fall-Detection/requirements.txt (line 34)) (7.34.0)\n", + "Requirement already satisfied: psutil in /usr/local/lib/python3.10/dist-packages (from -r /content/Human-Fall-Detection/requirements.txt (line 35)) (5.9.5)\n", + "Collecting thop~=0.1.1.post2209072238 (from -r /content/Human-Fall-Detection/requirements.txt (line 36))\n", + " Downloading thop-0.1.1.post2209072238-py3-none-any.whl (15 kB)\n", + "Collecting telepot~=12.7 (from -r /content/Human-Fall-Detection/requirements.txt (line 41))\n", + " Downloading telepot-12.7.tar.gz (73 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m73.1/73.1 kB\u001b[0m \u001b[31m3.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", + "Collecting torchmetrics~=0.10.2 (from -r /content/Human-Fall-Detection/requirements.txt (line 42))\n", + " Downloading torchmetrics-0.10.3-py3-none-any.whl (529 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m529.7/529.7 kB\u001b[0m \u001b[31m14.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hRequirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.2.2->-r /content/Human-Fall-Detection/requirements.txt (line 4)) (1.1.0)\n", + "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.2.2->-r /content/Human-Fall-Detection/requirements.txt (line 4)) (0.11.0)\n", + "Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.2.2->-r /content/Human-Fall-Detection/requirements.txt (line 4)) (4.40.0)\n", + "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.2.2->-r /content/Human-Fall-Detection/requirements.txt (line 4)) (1.4.4)\n", + "Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.2.2->-r /content/Human-Fall-Detection/requirements.txt (line 4)) (23.1)\n", + "Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.2.2->-r /content/Human-Fall-Detection/requirements.txt (line 4)) (3.1.0)\n", + "Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.2.2->-r /content/Human-Fall-Detection/requirements.txt (line 4)) (2.8.2)\n", + "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.23.0->-r /content/Human-Fall-Detection/requirements.txt (line 9)) (1.26.16)\n", + "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.23.0->-r /content/Human-Fall-Detection/requirements.txt (line 9)) (2023.5.7)\n", + "Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.10/dist-packages (from requests>=2.23.0->-r /content/Human-Fall-Detection/requirements.txt (line 9)) (2.0.12)\n", + "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.23.0->-r /content/Human-Fall-Detection/requirements.txt (line 9)) (3.4)\n", + "Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from torch!=1.12.0,>=1.7.0->-r /content/Human-Fall-Detection/requirements.txt (line 11)) (3.12.2)\n", + "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.10/dist-packages (from torch!=1.12.0,>=1.7.0->-r /content/Human-Fall-Detection/requirements.txt (line 11)) (4.6.3)\n", + "Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from torch!=1.12.0,>=1.7.0->-r /content/Human-Fall-Detection/requirements.txt (line 11)) (1.11.1)\n", + "Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch!=1.12.0,>=1.7.0->-r /content/Human-Fall-Detection/requirements.txt (line 11)) (3.1)\n", + "Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch!=1.12.0,>=1.7.0->-r /content/Human-Fall-Detection/requirements.txt (line 11)) (3.1.2)\n", + "Requirement already satisfied: triton==2.0.0 in /usr/local/lib/python3.10/dist-packages (from torch!=1.12.0,>=1.7.0->-r /content/Human-Fall-Detection/requirements.txt (line 11)) (2.0.0)\n", + "Requirement already satisfied: cmake in /usr/local/lib/python3.10/dist-packages (from triton==2.0.0->torch!=1.12.0,>=1.7.0->-r /content/Human-Fall-Detection/requirements.txt (line 11)) (3.25.2)\n", + "Requirement already satisfied: lit in /usr/local/lib/python3.10/dist-packages (from triton==2.0.0->torch!=1.12.0,>=1.7.0->-r /content/Human-Fall-Detection/requirements.txt (line 11)) (16.0.6)\n", + "Requirement already satisfied: absl-py>=0.4 in /usr/local/lib/python3.10/dist-packages (from tensorboard>=2.4.1->-r /content/Human-Fall-Detection/requirements.txt (line 17)) (1.4.0)\n", + "Requirement already satisfied: grpcio>=1.48.2 in /usr/local/lib/python3.10/dist-packages (from tensorboard>=2.4.1->-r /content/Human-Fall-Detection/requirements.txt (line 17)) (1.56.0)\n", + "Requirement already satisfied: google-auth<3,>=1.6.3 in /usr/local/lib/python3.10/dist-packages (from tensorboard>=2.4.1->-r /content/Human-Fall-Detection/requirements.txt (line 17)) (2.17.3)\n", + "Requirement already satisfied: google-auth-oauthlib<1.1,>=0.5 in /usr/local/lib/python3.10/dist-packages (from tensorboard>=2.4.1->-r /content/Human-Fall-Detection/requirements.txt (line 17)) (1.0.0)\n", + "Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.10/dist-packages (from tensorboard>=2.4.1->-r /content/Human-Fall-Detection/requirements.txt (line 17)) (3.4.3)\n", + "Requirement already satisfied: setuptools>=41.0.0 in /usr/local/lib/python3.10/dist-packages (from tensorboard>=2.4.1->-r /content/Human-Fall-Detection/requirements.txt (line 17)) (67.7.2)\n", + "Requirement already satisfied: tensorboard-data-server<0.8.0,>=0.7.0 in /usr/local/lib/python3.10/dist-packages (from tensorboard>=2.4.1->-r /content/Human-Fall-Detection/requirements.txt (line 17)) (0.7.1)\n", + "Requirement already satisfied: werkzeug>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from tensorboard>=2.4.1->-r /content/Human-Fall-Detection/requirements.txt (line 17)) (2.3.6)\n", + "Requirement already satisfied: wheel>=0.26 in /usr/local/lib/python3.10/dist-packages (from tensorboard>=2.4.1->-r /content/Human-Fall-Detection/requirements.txt (line 17)) (0.40.0)\n", + "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas>=1.1.4->-r /content/Human-Fall-Detection/requirements.txt (line 21)) (2022.7.1)\n", + "Collecting jedi>=0.16 (from ipython->-r /content/Human-Fall-Detection/requirements.txt (line 34))\n", + " Downloading jedi-0.18.2-py2.py3-none-any.whl (1.6 MB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.6/1.6 MB\u001b[0m \u001b[31m47.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hRequirement already satisfied: decorator in /usr/local/lib/python3.10/dist-packages (from ipython->-r /content/Human-Fall-Detection/requirements.txt (line 34)) (4.4.2)\n", + "Requirement already satisfied: pickleshare in /usr/local/lib/python3.10/dist-packages (from ipython->-r /content/Human-Fall-Detection/requirements.txt (line 34)) (0.7.5)\n", + "Requirement already satisfied: traitlets>=4.2 in /usr/local/lib/python3.10/dist-packages (from ipython->-r /content/Human-Fall-Detection/requirements.txt (line 34)) (5.7.1)\n", + "Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from ipython->-r /content/Human-Fall-Detection/requirements.txt (line 34)) (3.0.38)\n", + "Requirement already satisfied: pygments in /usr/local/lib/python3.10/dist-packages (from ipython->-r /content/Human-Fall-Detection/requirements.txt (line 34)) (2.14.0)\n", + "Requirement already satisfied: backcall in /usr/local/lib/python3.10/dist-packages (from ipython->-r /content/Human-Fall-Detection/requirements.txt (line 34)) (0.2.0)\n", + "Requirement already satisfied: matplotlib-inline in /usr/local/lib/python3.10/dist-packages (from ipython->-r /content/Human-Fall-Detection/requirements.txt (line 34)) (0.1.6)\n", + "Requirement already satisfied: pexpect>4.3 in /usr/local/lib/python3.10/dist-packages (from ipython->-r /content/Human-Fall-Detection/requirements.txt (line 34)) (4.8.0)\n", + "Requirement already satisfied: aiohttp>=3.0.0 in /usr/local/lib/python3.10/dist-packages (from telepot~=12.7->-r /content/Human-Fall-Detection/requirements.txt (line 41)) (3.8.4)\n", + "Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp>=3.0.0->telepot~=12.7->-r /content/Human-Fall-Detection/requirements.txt (line 41)) (23.1.0)\n", + "Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.10/dist-packages (from aiohttp>=3.0.0->telepot~=12.7->-r /content/Human-Fall-Detection/requirements.txt (line 41)) (6.0.4)\n", + "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /usr/local/lib/python3.10/dist-packages (from aiohttp>=3.0.0->telepot~=12.7->-r /content/Human-Fall-Detection/requirements.txt (line 41)) (4.0.2)\n", + "Requirement already satisfied: yarl<2.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp>=3.0.0->telepot~=12.7->-r /content/Human-Fall-Detection/requirements.txt (line 41)) (1.9.2)\n", + "Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from aiohttp>=3.0.0->telepot~=12.7->-r /content/Human-Fall-Detection/requirements.txt (line 41)) (1.3.3)\n", + "Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/dist-packages (from aiohttp>=3.0.0->telepot~=12.7->-r /content/Human-Fall-Detection/requirements.txt (line 41)) (1.3.1)\n", + "Requirement already satisfied: cachetools<6.0,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.6.3->tensorboard>=2.4.1->-r /content/Human-Fall-Detection/requirements.txt (line 17)) (5.3.1)\n", + "Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.6.3->tensorboard>=2.4.1->-r /content/Human-Fall-Detection/requirements.txt (line 17)) (0.3.0)\n", + "Requirement already satisfied: six>=1.9.0 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.6.3->tensorboard>=2.4.1->-r /content/Human-Fall-Detection/requirements.txt (line 17)) (1.16.0)\n", + "Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.6.3->tensorboard>=2.4.1->-r /content/Human-Fall-Detection/requirements.txt (line 17)) (4.9)\n", + "Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.10/dist-packages (from google-auth-oauthlib<1.1,>=0.5->tensorboard>=2.4.1->-r /content/Human-Fall-Detection/requirements.txt (line 17)) (1.3.1)\n", + "Requirement already satisfied: parso<0.9.0,>=0.8.0 in /usr/local/lib/python3.10/dist-packages (from jedi>=0.16->ipython->-r /content/Human-Fall-Detection/requirements.txt (line 34)) (0.8.3)\n", + "Requirement already satisfied: ptyprocess>=0.5 in /usr/local/lib/python3.10/dist-packages (from pexpect>4.3->ipython->-r /content/Human-Fall-Detection/requirements.txt (line 34)) (0.7.0)\n", + "Requirement already satisfied: wcwidth in /usr/local/lib/python3.10/dist-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->ipython->-r /content/Human-Fall-Detection/requirements.txt (line 34)) (0.2.6)\n", + "Requirement already satisfied: MarkupSafe>=2.1.1 in /usr/local/lib/python3.10/dist-packages (from werkzeug>=1.0.1->tensorboard>=2.4.1->-r /content/Human-Fall-Detection/requirements.txt (line 17)) (2.1.3)\n", + "Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.10/dist-packages (from sympy->torch!=1.12.0,>=1.7.0->-r /content/Human-Fall-Detection/requirements.txt (line 11)) (1.3.0)\n", + "Requirement already satisfied: pyasn1<0.6.0,>=0.4.6 in /usr/local/lib/python3.10/dist-packages (from pyasn1-modules>=0.2.1->google-auth<3,>=1.6.3->tensorboard>=2.4.1->-r /content/Human-Fall-Detection/requirements.txt (line 17)) (0.5.0)\n", + "Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.10/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<1.1,>=0.5->tensorboard>=2.4.1->-r /content/Human-Fall-Detection/requirements.txt (line 17)) (3.2.2)\n", + "Building wheels for collected packages: telepot\n", + " Building wheel for telepot (setup.py) ... \u001b[?25l\u001b[?25hdone\n", + " Created wheel for telepot: filename=telepot-12.7-py3-none-any.whl size=57944 sha256=2576a3545e100d6db85de72942d64d96b72566cb430719ff723267db620c31e8\n", + " Stored in directory: /root/.cache/pip/wheels/9f/9a/92/2e34a093e40a09338847e54dc9fcaab4ce01a59ba17c784c6c\n", + "Successfully built telepot\n", + "Installing collected packages: jedi, telepot, torchmetrics, thop\n", + "Successfully installed jedi-0.18.2 telepot-12.7 thop-0.1.1.post2209072238 torchmetrics-0.10.3\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Define the URL of the model you want to download\n", + "model_url = \"https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-w6-pose.pt\"" + ], + "metadata": { + "id": "KDl0FN33YVc7" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "destination_folder = \"/content/Human-Fall-Detection\"" + ], + "metadata": { + "id": "HCvnb-7gYXeb" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "os.makedirs(destination_folder, exist_ok=True)" + ], + "metadata": { + "id": "TiLzTHYDYbik" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "file_path = os.path.join(destination_folder, \"yolov7-w6-pose.pt\")" + ], + "metadata": { + "id": "HDMUptU4Yd_b" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "urllib.request.urlretrieve(model_url, file_path)\n", + "print(\"Model downloaded successfully!\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "JKCAcNJHYgQj", + "outputId": "25f4430a-a396-41f4-f286-051e5a432650" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Model downloaded successfully!\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "%run /content/Human-Fall-Detection/main1.py" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "NOF0BuroZsKM", + "outputId": "9420894c-5c99-430d-ce58-557562743a49" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "device: cpu\n" + ] + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + "\r 0%| | 0/84 [00:00 left_foot_y - len_factor and left_body_y > left_foot_y - ( + len_factor / 2) and left_shoulder_y > left_body_y - (len_factor / 2) or ( + right_shoulder_y > right_foot_y - len_factor and right_body_y > right_foot_y - ( + len_factor / 2) and right_shoulder_y > right_body_y - (len_factor / 2)) \ + or difference < 0: + return True, (xmin, ymin, xmax, ymax) + return False, None + + +def falling_alarm(image, bbox): + x_min, y_min, x_max, y_max = bbox + cv2.rectangle(image, (int(x_min), int(y_min)), (int(x_max), int(y_max)), color=(0, 0, 255), + thickness=5, lineType=cv2.LINE_AA) + cv2.putText(image, 'Person Fell down', (11, 100), 0, 1, [0, 0, 2550], thickness=3, lineType=cv2.LINE_AA) + + +def get_pose_model(): + device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") + print("device: ", device) + weigths = torch.load('/content/Human-Fall-Detection/yolov7-w6-pose.pt', map_location=device) + model = weigths['model'] + _ = model.float().eval() + if torch.cuda.is_available(): + model = model.half().to(device) + return model, device + + +def get_pose(image, model, device): + image = letterbox(image, 960, stride=64, auto=True)[0] + image = transforms.ToTensor()(image) + image = torch.tensor(np.array([image.numpy()])) + if torch.cuda.is_available(): + image = image.half().to(device) + with torch.no_grad(): + output, _ = model(image) + output = non_max_suppression_kpt(output, 0.25, 0.65, nc=model.yaml['nc'], nkpt=model.yaml['nkpt'], + kpt_label=True) + with torch.no_grad(): + output = output_to_keypoint(output) + return image, output + + +def prepare_image(image): + _image = image[0].permute(1, 2, 0) * 255 + _image = _image.cpu().numpy().astype(np.uint8) + _image = cv2.cvtColor(_image, cv2.COLOR_RGB2BGR) + return _image + + +def prepare_vid_out(video_path, vid_cap): + vid_write_image = letterbox(vid_cap.read()[1], 960, stride=64, auto=True)[0] + resize_height, resize_width = vid_write_image.shape[:2] + out_video_name = f"{video_path.split('/')[-1].split('.')[0]}_keypoint.mp4" + out = cv2.VideoWriter(out_video_name, cv2.VideoWriter_fourcc(*'mp4v'), 30, (resize_width, resize_height)) + return out + + +def process_video(video_path): + vid_cap = cv2.VideoCapture(video_path) + + if not vid_cap.isOpened(): + print('Error while trying to read video. Please check path again') + return + + model, device = get_pose_model() + vid_out = prepare_vid_out(video_path, vid_cap) + + success, frame = vid_cap.read() + _frames = [] + while success: + _frames.append(frame) + success, frame = vid_cap.read() + + for image in tqdm(_frames): + image, output = get_pose(image, model, device) + _image = prepare_image(image) + is_fall, bbox = fall_detection(output) + if is_fall: + falling_alarm(_image, bbox) + vid_out.write(_image) + + vid_out.release() + vid_cap.release() + + +if __name__ == '__main__': + videos_path = '/content/Human-Fall-Detection/fall_dataset/videos' + for video in os.listdir(videos_path): + video_path = os.path.join(videos_path, video) + process_video(video_path) \ No newline at end of file