Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gst merge #17

Open
wants to merge 25 commits into
base: gstreamer
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
demo.gif
docs/
.git/
data/
frontend/build/
frontend/node_modules/
experiments/
tasks/
*.avi
.vscode/
venv/
*.ts
*.m3u8
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ __pycache__/
*.caffemodel
*.uff
*.avi
*.ts
*.m3u8
.vscode
venv/
*.tbz2
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ docker build -f x86-openvino.Dockerfile -t "neuralet/smart-social-distancing:lat
docker run -it -p HOST_PORT:8000 -v "$PWD/data":/repo/data neuralet/smart-social-distancing:latest-x86_64_openvino
```

**Run on x86 using DeepStream**

```
cd smart-social-distancing/
(sudo) ./deepstream.sh build
(sudo) ./deepstream.sh run
```

Make sure to set your hostname to your publicly accessable hostname in deepstream.ini
```
PublicUrl: http://your_hostname_here:8000
```

### Configurations
You can read and modify the configurations in `config-jetson.ini` file for Jetson Nano and `config-skeleton.ini` file for Coral.

Expand Down
80 changes: 80 additions & 0 deletions deepstream.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Copyright (c) 2020 Michael de Gans
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

ARG FRONTEND_BASE="build me with deesptream.sh"
ARG CUDA_PLUGIN_TAG="build me with deepstream.sh"
FROM ${FRONTEND_BASE} as frontend
FROM registry.hub.docker.com/mdegans/gstcudaplugin:${CUDA_PLUGIN_TAG}

# this can't be downloaded directly because a license needs to be accepted,
# (because those who abuse it will care so much about that) and a tarball
# extracted. This is very un-fun:
# https://developer.nvidia.com/deepstream-getting-started#python_bindings
ARG DS_SOURCES_ROOT='/opt/nvidia/deepstream/deepstream/sources'
ARG CONFIG_FILE="deepstream.ini"

# copy stuff we need at the start of the build
COPY requirements.txt /tmp/

# install pip, install requirements, remove pip and deps
RUN apt-get update && apt-get install -y --no-install-recommends \
python3-aiofiles \
python3-dev \
python3-gi \
python3-gst-1.0 \
python3-numpy \
python3-pil \
python3-pip \
python3-protobuf \
python3-scipy \
python3-setuptools \
graphviz \
&& pip3 install --require-hashes -r /tmp/requirements.txt \
&& apt-get purge -y --autoremove \
python3-pip \
python3-setuptools \
python3-dev \
&& rm -rf /var/lib/apt/cache/*

# TODO(mdegans) python3-opencv brings in a *ton* of dependencies so
# it's probably better off removed from the deepstream image

# NOTE(mdegans): these layers are here because docker's multi-line
# copy syntax is dumb and doesn't support copying folders in a sane way.
# one way of getting around this is to use a subdir for your
# project

WORKDIR /repo

# copy frontend
COPY --from=frontend /frontend/build /srv/frontend

# copy code
COPY neuralet-distancing.py README.md ${CONFIG_FILE} ./
COPY libs ./libs/
COPY ui ./ui/
COPY tools ./tools/
COPY logs ./logs/
COPY data ./data/

# entrypoint with deepstream.
EXPOSE 8000
ENTRYPOINT [ "/usr/bin/python3", "neuralet-distancing.py", "--config", "deepstream.ini" ]

41 changes: 41 additions & 0 deletions deepstream.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[App]
Host: 0.0.0.0
Port: 8000
Resolution: 640,480
; public uri without the trailing slash
PublicUrl: http://localhost:8000
Encoder: nvvideoconvert ! nvv4l2h264enc

[Source_0]
; VideoPath may be a uri supported by uridecodebin (rtsp, http, etc.)
; or a local file.
; TODO(mdegans): camera sources.
VideoPath: /opt/nvidia/deepstream/deepstream-5.0/samples/streams/sample_720p.h264

[Source_1]
VideoPath: /opt/nvidia/deepstream/deepstream-5.0/samples/streams/sample_720p.h264

[Detector]
; Supported devices: Deepstream
Device: DeepStream
; Detector's Name can be "resnet10" or "peoplenet"
Name: resnet10
;ImageSize is not needed since this is included in the deepstream config .ini
ClassID: 2
MinScore: 0.25

; TODO(mdegans): remove unused sections and keys from this file

[PostProcessor]
MaxTrackFrame: 5
NMSThreshold: 0.98
; distance threshold for smart distancing in (cm)
DistThreshold: 150
; ditance mesurement method, CenterPointsDistance: compare center of pedestrian boxes together, FourCornerPointsDistance: compare four corresponding points of pedestrian boxes and get the minimum of them.
DistMethod: CenterPointsDistance

[Logger]
; options: csv, json (default is csv if not set)
Name: csv
; optional log path (default to ~/.smart_distancing/logs/):
LogDirectory: /repo/data/web_gui/static/data
118 changes: 118 additions & 0 deletions deepstream.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/bin/bash
# Copyright (c) 2020 Michael de Gans
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

set -e

# change the default run port
readonly PORT="8000"
# change this to bump the version tag
readonly VERSION="0.1.0"
# change this to your docker hub user if you fork this and want to push it
readonly USER_NAME="neuralet"
# change this to override the arch (should never be necessary)
readonly ARCH="$(arch)"
# frontend dockerfile name
readonly FRONTEND_DOCKERFILE="frontend.Dockerfile"
# Dockerfile name
readonly DOCKERFILE="deepstream.Dockerfile"
# https://www.cyberciti.biz/faq/bash-get-basename-of-filename-or-directory-name/
readonly THIS_SCRIPT_BASENAME="${0##*/}"
# change this to use a newer gst-cuda-plugin version
readonly CUDA_PLUGIN_VER="0.3.4"
# https://stackoverflow.com/questions/4774054/reliable-way-for-a-bash-script-to-get-the-full-path-to-itself
readonly THIS_DIR="$( cd "$(dirname "$0")" > /dev/null 2>&1 ; pwd -P )"
# the primary group to use
if [[ $ARCH = "aarch64" ]]; then
# on tegra, a user must be in the video group to use the gpu
readonly GROUP_ID="$(cut -d: -f3 < <(getent group video))"
declare readonly GPU_ARGS=(
"--runtime"
"nvidia"
)
else
readonly GROUP_ID=$(id -g)
declare readonly GPU_ARGS=(
"--gpus"
"all"
)
fi
# the user id to use
if [[ -z "$SUDO_USER" ]]; then
readonly USER_ID=$UID
else
echo "sudo user: $SUDO_USER"
readonly USER_ID="$(id -u $SUDO_USER)"
fi

# this helps tag the image
GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
# get the docker tag suffix from the git branch
if [[ $GIT_BRANCH == "master" ]]; then
# if we're on master, just use "deepstream"
readonly GIT_BRANCH="latest"
else
readonly GIT_BRANCH=$GIT_BRANCH
fi

# change this to ovverride the image tag suffix
readonly TAG_SUFFIX1="deepstream-$VERSION-$ARCH"
readonly TAG_SUFFIX2="deepstream-$GIT_BRANCH-$ARCH"

function build() {
readonly local FRONTEND_TAG="$GIT_BRANCH-frontend"
set -x
docker build -f $FRONTEND_DOCKERFILE \
-t "$USER_NAME/smart-social-distancing:$FRONTEND_TAG" \
.
docker build -f $DOCKERFILE \
-t "$USER_NAME/smart-distancing:$TAG_SUFFIX1" \
-t "$USER_NAME/smart-distancing:$TAG_SUFFIX2" \
--build-arg CUDA_PLUGIN_TAG="$CUDA_PLUGIN_VER-$ARCH" \
--build-arg FRONTEND_BASE="$USER_NAME/smart-social-distancing:$FRONTEND_TAG" \
.
}

function run() {
set -x
exec docker run -it --rm --name smart_distancing \
"${GPU_ARGS[@]}" \
-v "$THIS_DIR/deepstream.ini:/repo/deepstream.ini" \
-v "$THIS_DIR/data:/repo/data" \
--user $USER_ID:$GROUP_ID \
-p "$PORT:8000" \
"$USER_NAME/smart-distancing:$TAG_SUFFIX1" \
--verbose
}

main() {
case "$1" in
build)
build
;;
run)
run
;;
*)
echo "Usage: $THIS_SCRIPT_BASENAME {build|run}"
esac
}

main "$1"
46 changes: 46 additions & 0 deletions libs/detectors/deepstream/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright (c) 2020 Michael de Gans
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""
The DeepStream detector module includes a DeepStream specific implementation
of the BaseDetector class and various utility classes and functions.
"""

# GStreamer needs to be imported before pyds or else there is crash on Gst.init
import gi
gi.require_version('Gst', '1.0')
gi.require_version('GLib', '2.0')
from gi.repository import (
Gst,
GLib,
)
from libs.detectors.deepstream._ds_utils import *
from libs.detectors.deepstream._ds_config import *
from libs.detectors.deepstream._gst_engine import *

__all__ = [
'bin_to_pdf', # _ds_utils.py
'DsConfig', # _ds_config.py
'ElemConfig', # _ds_config.py
'find_deepstream', # _ds_utils.py
'GstConfig', # _ds_config.py
'GstEngine', # _gst_engine.py
'link_many', # _gst_engine.py
]
Loading