基于deepstream5.0 python api 实现yolov3视频检测目前全网和官网都没有提供详细案例,本作为python api实现yolov3开山篇,下面给出超级详细步骤,关注我的github,如果可以请给我star。
项目目前情况:实现了对象检测、置信度、边框信息获取,遗憾的是暂时没有实现怎么获取图像数据,这个等大佬来弄了。下面是详细安装和运行步骤:
本文项目地址(别忘了顺手star):https://github.com/futureflsl/deepstream-yolov3-python.git
本文提供了使用DeepStream开发基于python YOLOV3样例
目标检测算法:YOLO V3
视频源:视频文件
运行平台:Jetson NX(或者Jetson类似产品)
Ubuntu 18.04
DeepStream SDK 5.0 or later
Python 3.6
Gst Python v1.14.5
方法一:
假使这里已经有了一台安装了Jetpack 4.4的Jetson NX(以下简称为Nano)。
使用如下指令安装DeepStream 5.0:
sudo apt-get install DeepStream-5.0
一切顺利的话,DeepStream会自动安装到以下目录下:
/opt/nvidia/deepstream/deepstream-5.0
方法二:
1、安装必要的依赖
sudo apt install \
libssl1.0.0 \
libgstreamer1.0-0 \
gstreamer-1.0 \
gstreamer1.0-tools \
gstreamer1.0-plugins-good \
gstreamer1.0-plugins-bad \
gstreamer1.0-plugins-ugly \
gstreamer1.0-libav \
libgstrtspserver-1.0-0 \
libjansson4
sudo apt-get install libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev libgstrtspserver-1.0-dev libx11-dev libgstrtspserver-1.0-dev gstreamer1.0-rtsp
sudo apt-get install ffmpeg
2、安装librdkafka
git clone https://github.com/edenhill/librdkafka.git
cd librdkafka
git reset --hard 7101c2310341ab3f4675fc565f64f0967e135a6a
./configure
make -j8
sudo make install
sudo cp /usr/local/lib/librdkafka* /opt/nvidia/deepstream/deepstream-5.0/lib
3、deepstream安装
1)deb安装
https://developer.nvidia.com/deepstream-getting-started
下载deepstream5.0的.deb文件,进行安装。
sudo dpkg -i deepstream-5.0_5.0.0-1_amd64.deb
2)tar安装
下载deepstream5.0的.tar文件,进行安装。
1、解压到指定目录:
sudo tar -jxvf deepstream_sdk_v5.0.0_x86_64.tbz2 -C /
若需卸载之前应用,则使用下面方式:
cd /opt/nvidia/deepstream/deepstream-5.0
sudo vim uninstall.sh
//打开后,设置PREV_DS_VER=5.0
sudo ./uninstall.sh
2、安装deepstream:
cd /opt/nvidia/deepstream/deepstream-5.0/
sudo ./install.sh
sudo ldconfig
4、验证安装
至此deepstream5.0安装完成,输入deepstream-app --version-all 来查看安装的版本,得到输出如下:
deepstream-appversion 5.0.0
DeepStreamSDK 5.0.0
CUDADriverVersion: 10.2
CUDARuntimeVersion: 10.2
TensorRTVersion: 7.0
cuDNNVersion: 7.6
libNVWarp360Version: 2.0.1d3
如果运行时报错提示找不到一些库,如libnvdsgst_meta.so,则需要把deepstream-5.0/lib添加到系统lib路径中,如下:
sudo vi /etc/ld.so.conf
/opt/nvidia/deepstream/deepstream-5.0/lib/ //在文本后边添加该路径
sudo ldconfig //执行ldconfig立即生效
然后再执行就可以了。
方法三:docker安装,免去环境安装麻烦,不过需要安装docker环境。docker安装步骤省略。重点介绍怎么拉取镜像怎么进去:
拉取镜像:sudo docker pull nvcr.io/nvidia/deepstream:5.0.1-20.09-triton
获取IMAGE_ID:sudo docker images就可以找到镜像ID
启动容器:
sudo docker run -it -v ~/Downloads/:/tmp --shm-size=5g --name=deepstream5 -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=unix$DISPLAY -e GDK_SCALE -e GDK_DPI_SCALE IMAGE_ID bash
注意,如果你是GTX或者RTX显卡通用Ubuntu系统需要满足下面条件:
ubuntu18.04上deepstream5.0开发环境安装,依次安装即可:
1、ubuntu上cuda(CUDA10.2+CUDNN7.6.5)及显卡驱动(最新驱动即可,驱动CUDA API>=10.2)安装
2、ubuntu上tensorrt7.0安装(tensorrt7及其以上均可,测试用的是tensorrt7.0.0.11)
3、ubuntu上dseepstream5.0安装
系统环境:
ubuntu18.04、cuda10.2、driver440、cudnn7.6、tensorrt7.0、deepstream5.0。
依次安装cuda及显卡驱动、tensorrt7.0,再依据本文安装deepstream5.0即可。
安装DeepStream的Python绑定
先clone一下deepstream_python_apps:
git clone https://github.com/NVIDIA-AI-IOT/deepstream_python_apps.git
clone完毕后会在Nano的home目录下发现deepstrem_python_apps文件夹。
此时deepstrem_python_apps文件夹的文件如下所示:
├── apps
│ ├── common
│ ├── deepstream-imagedata-multistream
│ ├── deepstream-ssd-parser
│ ├── deepstream-test1
│ ├── deepstream-test1-rtsp-out
│ ├── deepstream-test1-usbcam
│ ├── deepstream-test2
│ ├── deepstream-test3
│ ├── deepstream-test4
│ └── README
├── FAQ.md
├── HOWTO.md
├── LICENSE
├── notebooks
│ ├── deepstream_test_1.ipynb
│ └── deepstream_test_4.ipynb
└── README.md
执行以下指令将DeepStream的Python Apps拷贝至DeepStream目录下:
cp -r deepstream_python_apps /opt/nvidia/deepstream/deepstream-5.0/sources
1
SDK MetaData库是用C / C ++开发的。Python绑定提供了从Python应用程序对MetaData的访问。绑定在已编译的模块中提供,可用于x86_64和Jetson平台。该模块pyds.so在DeepStream安装目录的lib中提供。
把pyds.so文件复制到deepstream_python_apps/apps下:
cp /opt/nvidia/deepstream/deepstream-5.0/lib/pyds.so /opt/nvidia/deepstream/deepstream-5.0/sources/deepstream_python_apps/apps
此时deepstrem_python_apps文件夹的目录结构如下所示:
.
├── apps
│ ├── common
│ ├── deepstream-imagedata-multistream
│ ├── deepstream-ssd-parser
│ ├── deepstream-test1
│ ├── deepstream-test1-rtsp-out
│ ├── deepstream-test1-usbcam
│ ├── deepstream-test2
│ ├── deepstream-test3
│ ├── deepstream-test4
│ ├── README
│ └── pyds.so
├── FAQ.md
├── HOWTO.md
├── LICENSE
├── notebooks
│ ├── deepstream_test_1.ipynb
│ └── deepstream_test_4.ipynb
└── README.md
可以看到apps目录下多了个pyds.so文件。
下载源码:
https://github.com/futureflsl/deepstream-yolov3-python.git
然后将源码文件夹放在:
/opt/nvidia/deepstream/deepstream/sources/deepstream_python_apps/apps/下面
下载yolov3.weights:https://pjreddie.com/media/files/yolov3.weights,然后放到deepstream-yolov3-python文件夹下面
把nvdsinfer_custom_impl_Yolo文件夹放到
/opt/nvidia/deepstream/deepstream/sources/objectDetector_Yolo或者直接使用 objectDetector_Yolo原来的均可,修改nvdsparsebbox_Yolo.cpp
static const int NUM_CLASSES_YOLO = 你的类别数;
然后
cd nvdsinfer_custom_impl_Yolo
export CUDA_VER=10.2
make
编译完成后复制
libnvdsinfer_custom_impl_Yolo.so
到/opt/nvidia/deepstream/deepstream/sources/deepstream_python_apps/apps/deepstream-yolov3-python/nvdsinfer_custom_impl_Yolo文件夹
修改config_infer_primary_yoloV3.txt,如果你自己训练的模型需要修改为对应参数:
[property]
gpu-id=0
net-scale-factor=0.0039215697906911373
#0=RGB, 1=BGR
model-color-format=0
custom-network-config=yolov3.cfg
model-file=yolov3.weights
model-engine-file=model_b1_gpu0_fp16.engine
labelfile-path=labels.txt
int8-calib-file=yolov3-calibration.table.trt7.0
## 0=FP32, 1=INT8, 2=FP16 mode
network-mode=2
num-detected-classes=80
gie-unique-id=1
network-type=0
is-classifier=0
## 0=Group Rectangles, 1=DBSCAN, 2=NMS, 3= DBSCAN+NMS Hybrid, 4 = None(No clustering)
cluster-mode=2
maintain-aspect-ratio=1
parse-bbox-func-name=NvDsInferParseCustomYoloV3
custom-lib-path=nvdsinfer_custom_impl_Yolo/libnvdsinfer_custom_impl_Yolo.so
engine-create-func-name=NvDsInferYoloCudaEngineGet
#scaling-filter=0
#scaling-compute-hw=0
[class-attrs-all]
nms-iou-threshold=0.3
threshold=0.7
运行(视频地址请根据自己的地址指定):
python deepstream_test_3.py file:///home/fut/test.mp4
错误写法:python deepstream_test_3.py /home/fut/test.mp4
本文参考教程:https://blog.csdn.net/lk007cx/article/details/110228243,再次特别鸣谢文章无私奉献