diff --git a/contrib/ActionRecognition/README.md b/contrib/ActionRecognition/README.md new file mode 100644 index 0000000000000000000000000000000000000000..3b4248d4c2643b5a329786a5f6bdf226053330df --- /dev/null +++ b/contrib/ActionRecognition/README.md @@ -0,0 +1,372 @@ +# ActionRecgnition + +## 1 介绍 + +本开发样例演示动作识别系统 ActionRecgnition,供用户参考。 +本系统基于mxVision SDK进行开发,以昇腾Atlas300卡为主要的硬件平台,主要应用于单人独处、逗留超时、快速移动、剧烈运动、离床检测、攀高检测六种应用场景。 + +1. 单人独处:识别出单个人独处场景后报警。 +2. 逗留超时:识别出单人或多人在区域内长时间逗留的情况并发出警报。 +3. 快速移动:检测出视频中单人或多人行进速度大于阈值的情况,并发出警报。 +4. 剧烈运动:检测到视频流中有剧烈运动并进行报警。 +5. 离床检测:检测出视频中行人离开指定区域的情况并报警。 +6. 攀高检测:检测出行人中心点向上移动的情况,并发出警报。 + +## 2 环境依赖 + +* 支持的硬件形态和操作系统版本 + + | 硬件形态 | 操作系统版本 | + | ------------------------------------- | -------------- | + | x86_64+Atlas 300I 推理卡(型号3010) | Ubuntu 18.04.1 | + | x86_64+Atlas 300I 推理卡 (型号3010) | CentOS 7.6 | + | ARM+Atlas 300I 推理卡 (型号3000) | Ubuntu 18.04.1 | + | ARM+Atlas 300I 推理卡 (型号3000) | CentOS 7.6 | + +* 软件依赖 + + | 软件名称 | 版本 | + | -------- | ----- | + | cmake | 3.5.+ | + | mxVision | 2.0.2 | + | Python | 3.7.5 | + | OpenCV | 3.4.0 | + | gcc | 7.5.0 | + | ffmpeg | 4.3.2 | + +## 3 代码主要目录介绍 + +本Sample工程名称为Actionrecognition,工程目录如下图所示: + +``` +. +├── data +│ ├── roi +│   │   ├── Climbup +│   │   └── ... +│   └── roi +│   │   ├── Alone +│   │   └── ... +├── models +│   ├── ECONet +│   │   └── ... +│   └── yolov3 +│   │   └── ... +├── pipeline +│   ├── plugin_all.pipeline +│   ├── plugin_alone.pipeline +│   ├── plugin_climb.pipeline +│   ├── plugin_outofbed.pipeline +│   ├── plugin_overspeed.pipeline +│   ├── plugin_overstay.pipeline +│   └── plugin_violentaction.pipeline +├── plugins +│   ├── MxpiStackFrame // 堆帧插件 +│   │   ├── CMakeLists.txt +│   │   ├── MxpiStackFrame.cpp +│   │   ├── MxpiStackFrame.h +│   │   ├── BlockingMap.cpp +│   │   └── BlockingMap.h +│   ├── PluginAlone // 单人独处插件 +│   │   ├── CMakeLists.txt +│   │   ├── PluginAlone.cpp +│   │   └── PluginAlone.h +│   ├── PluginClimb // 攀高检测插件 +│   │   ├── CMakeLists.txt +│   │   ├── PluginClimb.cpp +│   │   └── PluginClimb.h +│   ├── PluginOutOfBed // 离床检测插件 +│   │   ├── CMakeLists.txt +│   │   ├── PluginOutOfBed.cpp +│   │   └── PluginOutOfBede.h +│   ├── PluginOverSpeed // 快速移动插件 +│   │   ├── CMakeLists.txt +│   │   ├── PluginOverSpeed.cpp +│   │   └── PluginOverSpeed.h +│   ├── PluginOverStay // 逗留超时插件 +│   │   ├── CMakeLists.txt +│   │   ├── PluginOverStay.cpp +│   │   └── PluginOverStay.h +│   ├── PluginCounter // 计时插件 +│   │   ├── CMakeLists.txt +│   │   ├── PluginCounter.cpp +│   │   └── PluginCounter.h +│   ├── PluginViolentAction // 剧烈运动插件 +│   │   ├── CMakeLists.txt +│   │   ├── PluginViolentAction.cpp +│   │   └── PluginViolentAction.h +├── main.py +├── README.md +└── run.sh +``` + +## 4 软件方案介绍 + +为了完成上述六种应用场景中的行为识别,系统需要检测出同一目标短时间内状态的变化以及是否存在剧烈运动,因此系统中需要包含目标检测、目标跟踪、动作识别与逻辑后处理。其中目标检测模块选取Yolov3,得到行人候选框;目标跟踪模块使用IOU匹配,关联连续帧中的同一目标。将同一目标在连续帧的区域抠图组成视频序列,输入动作识别模块ECONet,模型输出动作类别,判断是否为剧烈运动。逻辑后处理通过判断同一目标在连续帧内的空间位置变化判断难以被定义为运动的其余五种应用场景。系统方案中各模块功能如表1.1 所示。 + +表1.1 系统方案中个模块功能: + +| 序号 | 子系统 | 功能描述 | +| ---- | -------------- | ------------------------------------------------------------ | +| 1 | 初始化配置 | 主要用于初始化资源,如线程数量、共享内存等。 | +| 2 | 视频流 | 从多路IPC相机拉流,并传输入Device进行计算。 | +| 3 | 视频解码 | 通过硬件(DVPP)对视频进行解码,转换为YUV数据进行后续处理。 | +| 4 | 图像预处理 | 在进行基于深度神经网络的图像处理之前,需要将图像缩放到固定的尺寸和格式。 | +| 5 | 目标检测 | 基于深度学习的目标检测算法是该系统的核心模块之一,本方案选用基于Yolov3的目标检测。 | +| 6 | 目标跟踪 | 基于卡尔曼滤波与匈牙利算法的目标跟踪算法是该系统的核心模块之一,本方案选用IOU匹配。 | +| 7 | 图像抠图 | 将同一目标在连续帧所在区域抠图,并组成图像序列,输入动作识别模块。 | +| 8 | 动作识别 | 基于深度学习的动作识别算法是该系统的核心模块之一,本方案选用基于ECOLite的动作识别。 | +| 9 | 单人独处后处理 | 当连续m帧只出现一个目标ID时,则判断为单人独处并报警,如果单人独处报警之前n帧内已经报警过,则不重复报警。 | +| 10 | 快速移动后处理 | 当同一目标在连续m帧中心点平均位移高于阈值v,则判断为快速移动,如果快速移动报警之前n帧内已经报警过,则不重复报警。 | +| 11 | 逗留超时后处理 | 当同一目标在连续m帧中心点平均位移低于阈值v,则判断为快速移动,如果快速移动报警之前n帧内已经报警过,则不重复报警。 | +| 12 | 离床检测后处理 | 当同一目标在连续m帧内从给定区域roi内离开,则判断为离床,如果离床报警之前n帧内已经报警过,则不重复报警。 | +| 13 | 攀高检测后处理 | 当同一目标在连续m帧内从给定区域roi内中心点上升,并且中心点位移大于阈值h,则判断为离床,如果离床报警之前n帧内已经报警过,则不重复报警。 | +| 14 | 剧烈运动后处理 | 动作识别模块输出类别为关注的动作类别时,则判断为剧烈运动,如果剧烈运动之前n帧内已经报警过,则不重复报警。 | + +## 5 准备 + +**步骤1:** 参考安装教程《mxVision 用户指南》安装 mxVision SDK。 + +**步骤2:** 配置 mxVision SDK 环境变量。 + +`export MX_SDK_HOME=${安装路径}/mxVision ` + +注:本例中mxVision SDK安装路径为 /root/work/MindX_SDK/mxVision。 + +**步骤3:** 在${MX_SDK_HOME}/samples下创建ActionRecognition根目录,在项目根目录下创建目录models `mkdir models`,分别为yolov3和ECONet创建一个文件夹,将两个离线模型及各自的配置文件放入文件夹下。下载地址:[Baiduyun](https://pan.baidu.com/s/1IEWS0IjOFaQrXFdt-4kSRg)(passwd: rdpu)。创建完成后models文件夹下的目录结构如下: + +``` +.models +├── ECONet +│   ├── eco_aipp.cfg // 模型转换aipp配置文件 +│   ├── eco_post.cfg // 模型后处理配置文件 +│   ├── ECONet.om // 离线模型 +│   ├── ucf101.names // label文件 +│   ├── ucf101_best.pb // 冻结pb模型 +│   └── trans_pb2om.sh // 模型转换脚本 +├── yolov3 +│   ├── coco.names // label文件 +│   ├── yolov3_tf_bs1_fp16.om // 离线模型 +│   └── yolov3_tf_bs1_fp16.cfg // 模型后处理配置文件 +``` + +**步骤4:** 编译程序前提需要先交叉编译好第三方依赖库。 + +**步骤5:** 配置环境变量MX_SDK_HOME: + +```cmake +export MX_SDK_HOME=/MindX_SDK/mxVision/ +# 此处MX_SDK_HOME请使用MindX_SDK的实际路径 +``` + +**步骤6**:在插件代码目录下创建build文件夹,使用cmake命令进行编译,生成.so文件。下面以单人独处插件的编译过程作为范例: + +```bash +## 进入目录 /plugins/plugin_Alone +## 创建build目录 +mkdir build +## 使用cmake命令进行编译 +cmake .. +make -j +``` + +编译好的插件会自动存放到SDK的插件库中,可以直接在pipiline中使用。 + +**步骤7:** 配置pipeline + +1. 插件参数介绍 + + * MxpiStackFrame + + | 参数名称 | 参数解释 | + | :----------- | -------------------- | + | visionSource | 抠图插件名称 | + | trackSource | 跟踪插件名称 | + | frameNum | 跳帧间隔(为1不跳) | + | timeOut | 某个目标堆帧超时时间 | + | sleepTime | 检查线程休眠时间 | + + * PluginAlone + + | 参数名称 | 参数解释 | + | :-------------- | ------------ | + | dataSourceTrack | 跟踪插件名称 | + | detectThresh | 检测帧数 | + | detectRatio | 警报帧阈值 | + | detectSleep | 警报间隔 | + + * PluginClimb + + | 参数名称 | 参数解释 | + | :------------------ | ---------------------- | + | dataSourceTrack | 跟踪插件名称 | + | dataSourceDetection | 目标检测后处理插件名称 | + | detectRatio | 警报帧阈值 | + | filePath | ROI配置txt文件 | + | detectSleep | 警报间隔 | + | bufferLength | 检测帧数窗口大小 | + | highThresh | 高度阈值 | + + * PluginOutOfBed + + | 参数名称 | 参数解释 | + | :------------------ | ---------------------- | + | dataSourceTrack | 跟踪插件名称 | + | dataSourceDetection | 目标检测后处理插件名称 | + | configPath | ROI配置txt文件 | + | detectThresh | 检测帧数窗口大小 | + | detectSleep | 警报间隔 | + | detectRatio | 警报帧阈值 | + + * PluginOverSpeed + + | 参数名称 | 参数解释 | + | :------------------ | ---------------------- | + | dataSourceTrack | 跟踪插件名称 | + | dataSourceDetection | 目标检测后处理插件名称 | + | speedThresh | 速度阈值 | + | frames | 检测帧数窗口大小 | + | detectSleep | 警报间隔 | + + * PluginOverStay + + | 参数名称 | 参数解释 | + | :------------------ | ---------------------- | + | dataSourceTrack | 跟踪插件名称 | + | dataSourceDetection | 目标检测后处理插件名称 | + | stayThresh | 逗留时间阈值 | + | frames | 检测间隔帧数 | + | distanceThresh | 逗留范围 | + | detectRatio | 警报帧阈值 | + | detectSleep | 警报间隔 | + + * PluginViolentAction + + | 参数名称 | 参数解释 | + | :---------- | --------------------- | + | classSource | 分类后处理插件名称 | + | filePath | 感兴趣动作类别txt文件 | + | detectSleep | 警报间隔 | + +2. 配置范例 + + ```bash + ## PluginAlone + "mxpi_pluginclimb0": { + "props": { + "dataSourceTrack": "mxpi_motsimplesort0", + "dataSourceDetection": "mxpi_objectpostprocessor0", + "detectRatio": "0.6", + "filePath": "../data/roi/Climbup/*.txt", + "detectSleep": "30", + "bufferLength": "8", + "highThresh": "10" + }, + "factory": "mxpi_pluginclimb", + "next": "mxpi_dataserialize0" + } + ``` + + 根据所需场景,配置pipeline文件,调整路径参数以及插件阈值参数。例如"filePath"字段替换为roi/Climb目录下的感兴趣区域txt文件。 + +3. 将pipeline中“rtspUrl”字段值替换为可用的 rtsp 流源地址(需要自行准备可用的视频流,视频流格式为H264)。 + +**步骤8:** 在main.py中,更改pipeline路径。 + +## 6 运行 + +直接运行 + +```bash +chmod +x run.sh +./run.sh +``` + +## 7 常见问题 +### 7.1 未配置ROI + +#### 问题描述: + +攀高检测与离床检测出现如下报错: +```bash +terminate called after throwing an instance of 'cv::Exception' + what(): OpenCV(4.2.0) /usr1/workspace/MindX_SDK_Multi_DailyBuild/opensource/opensource-scl7/opencv/modules/imgproc/src/geometry.cpp:103: error: (-215:Assertion failed) total >= 0 && (depth == CV_32S || depth == CV_32F) in function 'pointPolygonTest' +``` +#### 解决方案: +在pipeline处没有配置roi区域的txt文件,进行配置即可。 + +示例: + +``` +"mxpi_pluginoutofbed0": { + "props": { + "dataSourceTrack": "mxpi_motsimplesort0", + "dataSourceDetect": "mxpi_objectpostprocessor0", + "configPath": "../data/roi/OutofBed/*.txt", + "detectThresh": "8", + "detectSleep": "15", + "detectRatio": "0.25" + }, + "factory": "mxpi_pluginoutofbed", + "next": "mxpi_dataserialize0" +} +``` + +### 7.2 模型路径配置 + +#### 问题描述: + +检测过程中用到的模型以及模型后处理插件需配置路径属性。 + +#### 后处理插件配置范例: + +``` +"mxpi_objectpostprocessor0": { + "props": { + "dataSource": "mxpi_tensorinfer0", + "funcLanguage":"c++", + "postProcessConfigPath": "../models/yolov3/yolov3_tf_bs1_fp16.cfg", + "labelPath": "../models/yolov3/coco.names", + "postProcessLibPath": "../../../lib/modelpostprocessors/libyolov3postprocess.so" + }, + "factory": "mxpi_objectpostprocessor", + "next": "mxpi_motsimplesort0" +} +``` + +### 7.3 数据下载与RTSP + +H264视频文件及ROI文件:[Baiduyun](https://pan.baidu.com/s/1Xz378sHh0WeeeYIjnZ-pPQ) ,提取码:as01 + +RTSP去留地址(可以从可以从网络摄像机获取,也可通过Live555等工具将本地视频文 件转换为rtsp流)。自主搭建RTSP拉流教程:[live555链接](https://bbs.huaweicloud.com/forum/thread-68720-1-1.html) + +RTSP视频拉流插件配置范例: + +``` +"mxpi_rtspsrc0": { + "props": { + "rtspUrl": "rtsp_Url" + }, + "factory": "mxpi_rtspsrc", + "next": "mxpi_videodecoder0" +} +``` + +其中rtsp_Url的格式是 rtsp:://host:port/Data + +## 8 模型转换 + +本项目中用到的模型有:ECONet,yolov3 + +yolov3模型转换及下载参考华为昇腾社区[ModelZoo](https://www.hiascend.com/zh/software/modelzoo/detail/C/210261e64adc42d2b3d84c447844e4c7); + +ECONet离线模型转换参考 [昇腾Gitee](https://gitee.com/ascend/modelzoo/tree/master/contrib/TensorFlow/Research/cv/econet/ECONet_tf_paper99):下载冻结pb模型ucf101_best.pb,编辑trans_pb2om.sh文件,将--model 配置为ECONet模型所在目录,--output配置为模型输出路径,--insert_op_conf配置为aipp文件路径,在命令行输入 + +``` +chmod +x trans_pb2om.sh +./trans_pb2om.sh +``` + +完成ECONet模型转换。 + +模型下载或转换完成后,按照目录结构放置模型。 \ No newline at end of file diff --git a/contrib/ActionRecognition/data/.keep b/contrib/ActionRecognition/data/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/contrib/ActionRecognition/data/roi/.keep b/contrib/ActionRecognition/data/roi/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/contrib/ActionRecognition/data/video/.keep b/contrib/ActionRecognition/data/video/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/contrib/ActionRecognition/main.py b/contrib/ActionRecognition/main.py new file mode 100644 index 0000000000000000000000000000000000000000..29b8a342692c486f5d4be515b33ab2c1f63c7b4e --- /dev/null +++ b/contrib/ActionRecognition/main.py @@ -0,0 +1,57 @@ +# +# Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from StreamManagerApi import * + +if __name__ == '__main__': + # init stream manager + streamManagerApi = StreamManagerApi() + ret = streamManagerApi.InitManager() + if ret != 0: + print("Failed to init Stream manager, ret=%s" % str(ret)) + exit() + + # create streams by pipeline config file + with open("../pipeline/stayovertime.pipeline", 'rb') as f: + pipelineStr = f.read() + ret = streamManagerApi.CreateMultipleStreams(pipelineStr) + if ret != 0: + print("Failed to create Stream, ret=%s" % str(ret)) + exit() + + # Inputs data to a specified stream based on streamName. + # streamName is the same as the name in pipeline. + streamName = b'detection+tracking' + inPluginId = 0 + + retStr = '' + key = b'mxpi_motsimplesort0' + keyVec = StringVector() + keyVec.push_back(key) + while True: + # Obtain the inference result by specifying streamName and uniqueId. + inferResult = streamManagerApi.GetResult(streamName, 0) + if inferResult is None: + exit() + if inferResult.errorCode != 0: + print("GetResultWithUniqueId error. errorCode=%d, errorMsg=%s" % ( + inferResult.errorCode, inferResult.data.decode())) + continue + retStr = inferResult.data.decode() + print(retStr) + + # destroy streams + streamManagerApi.DestroyAllStreams() \ No newline at end of file diff --git a/contrib/ActionRecognition/models/ECONet/eco_aipp.cfg b/contrib/ActionRecognition/models/ECONet/eco_aipp.cfg new file mode 100644 index 0000000000000000000000000000000000000000..f01079fbee26c7a20ee5b6b11d1473fd81a8970e --- /dev/null +++ b/contrib/ActionRecognition/models/ECONet/eco_aipp.cfg @@ -0,0 +1,29 @@ +aipp_op { +aipp_mode : static +related_input_rank : 0 +input_format : YUV420SP_U8 +crop : false +csc_switch : true +rbuv_swap_switch : false +matrix_r0c0 : 256 +matrix_r0c1 : 0 +matrix_r0c2 : 359 +matrix_r1c0 : 256 +matrix_r1c1 : -88 +matrix_r1c2 : -183 +matrix_r2c0 : 256 +matrix_r2c1 : 454 +matrix_r2c2 : 0 +input_bias_0 : 0 +input_bias_1 : 128 +input_bias_2 : 128 +mean_chn_0 : 104 +mean_chn_1 : 117 +mean_chn_2 : 128 +min_chn_0 : 0.0 +min_chn_1 : 0.0 +min_chn_2 : 0.0 +var_reci_chn_0 : 1.0 +var_reci_chn_1 : 1.0 +var_reci_chn_2 : 1.0 +} diff --git a/contrib/ActionRecognition/models/ECONet/trans_pb2om.sh b/contrib/ActionRecognition/models/ECONet/trans_pb2om.sh new file mode 100644 index 0000000000000000000000000000000000000000..c12848050bad982a9d35ac1f691371e7be477113 --- /dev/null +++ b/contrib/ActionRecognition/models/ECONet/trans_pb2om.sh @@ -0,0 +1,18 @@ +export install_path=/usr/local/Ascend/ascend-toolkit/latest +export PATH=/usr/local/python3.7.5/bin:${install_path}/atc/ccec_compiler/bin:${install_path}/atc/bin:$PATH +export PYTHONPATH=${install_path}/atc/python/site-packages:${install_path}/atc/python/site-packages/auto_tune.egg/auto_tune:${install_path}/atc/python/site-packages/schedule_search.egg:$PYTHONPATH +export LD_LIBRARY_PATH=${install_path}/atc/lib64:${install_path}/acllib/lib64:$LD_LIBRARY_PATH +export ASCEND_OPP_PATH=${install_path}/opp +export SLOG_PRINT_TO_STDOUT=1 + + +/usr/local/Ascend/ascend-toolkit/latest/atc/bin/atc \ + --mode=0 \ + --model=${pbmodel_path} \ + --output=${om_output_path} \ + --soc_version=Ascend310 \ + --input_format=NHWC\ + --input_shape="clip_holder:8,224,224,3" \ + --insert_op_conf=${aipp_path} \ + --framework=3 \ + log=debug > log.txt diff --git a/contrib/ActionRecognition/models/yolov3/.keep b/contrib/ActionRecognition/models/yolov3/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/contrib/ActionRecognition/pipeline/plugin_all.pipeline b/contrib/ActionRecognition/pipeline/plugin_all.pipeline new file mode 100644 index 0000000000000000000000000000000000000000..aa3f26348071aa4538e6e530c69348e2af6a050c --- /dev/null +++ b/contrib/ActionRecognition/pipeline/plugin_all.pipeline @@ -0,0 +1,136 @@ +{ + "detection+tracking": { + "stream_config": { + "deviceId": "0" + }, + "mxpi_rtspsrc0": { + "props": { + "rtspUrl": "rtsp_Url" + }, + "factory": "mxpi_rtspsrc", + "next": "mxpi_videodecoder0" + }, + "mxpi_videodecoder0":{ + "props": { + "vdecChannelId": "0" + }, + "factory": "mxpi_videodecoder", + "next": "mxpi_imageresize0" + }, + "mxpi_imageresize0":{ + "props": { + "dataSource": "mxpi_videodecoder0", + "resizeHeight": "416", + "resizeWidth": "416" + }, + "factory": "mxpi_imageresize", + "next": "mxpi_tensorinfer0" + }, + "mxpi_tensorinfer0":{ + "props": { + "dataSource": "mxpi_imageresize0", + "modelPath": "../models/yolov3/yolov3_tf_bs1_fp16.om" + }, + "factory": "mxpi_tensorinfer", + "next": "mxpi_objectpostprocessor0" + }, + "mxpi_objectpostprocessor0": { + "props": { + "dataSource": "mxpi_tensorinfer0", + "funcLanguage":"c++", + "postProcessConfigPath": "../models/yolov3/yolov3_tf_bs1_fp16.cfg", + "labelPath": "../models/yolov3/coco.names", + "postProcessLibPath": "../../../lib/modelpostprocessors/libyolov3postprocess.so" + }, + "factory": "mxpi_objectpostprocessor", + "next": "mxpi_motsimplesort0" + }, + "mxpi_motsimplesort0": { + "props": { + "dataSourceDetection": "mxpi_objectpostprocessor0" + }, + "factory": "mxpi_motsimplesort", + "next": "mxpi_plugincounter0" + }, + "mxpi_plugincounter0": { + "props": { + "dataSourceTrack": "mxpi_motsimplesort0" + }, + "factory": "mxpi_plugincounter", + "next": "mxpi_pluginalone0" + }, + "mxpi_pluginalone0": { + "props": { + "dataSourceTrack": "mxpi_motsimplesort0", + "detectThresh":"8", + "detectRatio":"0.8", + "detectSleep":"270" + }, + "factory": "mxpi_pluginalone", + "next": "mxpi_pluginoverspeed0" + }, + "mxpi_pluginoverspeed0": { + "props": { + "dataSourceTrack": "mxpi_motsimplesort0", + "dataSourceDetection": "mxpi_objectpostprocessor0", + "speedThresh": "1", + "frames": "8", + "detectSleep": "270" + }, + "factory": "mxpi_pluginoverspeed", + "next": "mxpi_pluginoverstay0" + }, + "mxpi_pluginoverstay0": { + "props": { + "status": "1", + "dataSourceTrack": "mxpi_motsimplesort0", + "dataSourceDetection": "mxpi_objectpostprocessor0", + "staythresh":"90", + "frames":"90", + "distancethresh":"100", + "ratio":"0.7", + "detectsleep":"90" + }, + "factory": "mxpi_pluginoverstay", + "next": "mxpi_pluginclimb0" + }, + "mxpi_pluginclimb0": { + "props": { + "dataSourceTrack": "mxpi_motsimplesort0", + "dataSourceDetection": "mxpi_objectpostprocessor0", + "detectRatio": "0.6", + "filePath": "../data/roi/Climbup/*.txt", + "detectSleep": "30", + "bufferLength": "8", + "highThresh": "10" + }, + "factory": "mxpi_pluginclimb", + "next": "mxpi_pluginoutofbed0" + }, + "mxpi_pluginoutofbed0": { + "props": { + "dataSourceTrack": "mxpi_motsimplesort0", + "dataSourceDetect": "mxpi_objectpostprocessor0", + "configPath": "../data/roi/OutofBed/*.txt", + "detectThresh": "8", + "detectSleep": "15", + "detectRatio": "0.25" + }, + "factory": "mxpi_pluginoutofbed", + "next": "mxpi_dataserialize0" + }, + "mxpi_dataserialize0": { + "props": { + "outputDataKeys": "mxpi_pluginoverstay0" + }, + "factory": "mxpi_dataserialize", + "next": "appsink0" + }, + "appsink0": { + "props": { + "blocksize": "4096000" + }, + "factory": "appsink" + } + } +} \ No newline at end of file diff --git a/contrib/ActionRecognition/pipeline/plugin_alone.pipeline b/contrib/ActionRecognition/pipeline/plugin_alone.pipeline new file mode 100644 index 0000000000000000000000000000000000000000..fb36cf0f884467e161af5f3364fdf82732e540d5 --- /dev/null +++ b/contrib/ActionRecognition/pipeline/plugin_alone.pipeline @@ -0,0 +1,79 @@ +{ + "detection+tracking": { + "stream_config": { + "deviceId": "0" + }, + "mxpi_rtspsrc0": { + "props": { + "rtspUrl": "rtsp_Url" + }, + "factory": "mxpi_rtspsrc", + "next": "mxpi_videodecoder0" + }, + "mxpi_videodecoder0":{ + "props": { + "vdecChannelId": "0" + }, + "factory": "mxpi_videodecoder", + "next": "mxpi_imageresize0" + }, + "mxpi_imageresize0":{ + "props": { + "dataSource": "mxpi_videodecoder0", + "resizeHeight": "416", + "resizeWidth": "416" + }, + "factory": "mxpi_imageresize", + "next": "mxpi_tensorinfer0" + }, + "mxpi_tensorinfer0":{ + "props": { + "dataSource": "mxpi_imageresize0", + "modelPath": "../models/yolov3/yolov3_tf_bs1_fp16.om" + }, + "factory": "mxpi_tensorinfer", + "next": "mxpi_objectpostprocessor0" + }, + "mxpi_objectpostprocessor0": { + "props": { + "dataSource": "mxpi_tensorinfer0", + "funcLanguage":"c++", + "postProcessConfigPath": "../models/yolov3/yolov3_tf_bs1_fp16.cfg", + "labelPath": "../models/yolov3/coco.names", + "postProcessLibPath": "../../../lib/modelpostprocessors/libyolov3postprocess.so" + }, + "factory": "mxpi_objectpostprocessor", + "next": "mxpi_motsimplesort0" + }, + "mxpi_motsimplesort0": { + "props": { + "dataSourceDetection": "mxpi_objectpostprocessor0" + }, + "factory": "mxpi_motsimplesort", + "next": "mxpi_pluginalone0" + }, + "mxpi_pluginalone0": { + "props": { + "dataSourceTrack": "mxpi_motsimplesort0", + "detectThresh":"8", + "detectRatio":"0.8", + "detectSleep":"270" + }, + "factory": "mxpi_pluginalone", + "next": "mxpi_dataserialize0" + }, + "mxpi_dataserialize0": { + "props": { + "outputDataKeys": "mxpi_pluginalone0" + }, + "factory": "mxpi_dataserialize", + "next": "appsink0" + }, + "appsink0": { + "props": { + "blocksize": "4096000" + }, + "factory": "appsink" + } + } +} diff --git a/contrib/ActionRecognition/pipeline/plugin_climb.pipeline b/contrib/ActionRecognition/pipeline/plugin_climb.pipeline new file mode 100644 index 0000000000000000000000000000000000000000..8a2d71d370210c4929a7077b981a64b61946e092 --- /dev/null +++ b/contrib/ActionRecognition/pipeline/plugin_climb.pipeline @@ -0,0 +1,82 @@ +{ + "detection+tracking": { + "stream_config": { + "deviceId": "0" + }, + "mxpi_rtspsrc0": { + "props": { + "rtspUrl": "rtsp_Url" + }, + "factory": "mxpi_rtspsrc", + "next": "mxpi_videodecoder0" + }, + "mxpi_videodecoder0":{ + "props": { + "vdecChannelId": "0" + }, + "factory": "mxpi_videodecoder", + "next": "mxpi_imageresize0" + }, + "mxpi_imageresize0":{ + "props": { + "dataSource": "mxpi_videodecoder0", + "resizeHeight": "416", + "resizeWidth": "416" + }, + "factory": "mxpi_imageresize", + "next": "mxpi_tensorinfer0" + }, + "mxpi_tensorinfer0":{ + "props": { + "dataSource": "mxpi_imageresize0", + "modelPath": "../models/yolov3/yolov3_tf_bs1_fp16.om" + }, + "factory": "mxpi_tensorinfer", + "next": "mxpi_objectpostprocessor0" + }, + "mxpi_objectpostprocessor0": { + "props": { + "dataSource": "mxpi_tensorinfer0", + "funcLanguage":"c++", + "postProcessConfigPath": "../models/yolov3/yolov3_tf_bs1_fp16.cfg", + "labelPath": "../models/yolov3/coco.names", + "postProcessLibPath": "../../../lib/modelpostprocessors/libyolov3postprocess.so" + }, + "factory": "mxpi_objectpostprocessor", + "next": "mxpi_motsimplesort0" + }, + "mxpi_motsimplesort0": { + "props": { + "dataSourceDetection": "mxpi_objectpostprocessor0" + }, + "factory": "mxpi_motsimplesort", + "next": "mxpi_pluginclimb0" + }, + "mxpi_pluginclimb0": { + "props": { + "dataSourceTrack": "mxpi_motsimplesort0", + "dataSourceDetection": "mxpi_objectpostprocessor0", + "detectRatio": "0.6", + "filePath": "../data/roi/Climbup/*.txt", + "detectSleep": "30", + "bufferLength": "8", + "highThresh": "10" + }, + "factory": "mxpi_pluginclimb", + "next": "mxpi_dataserialize0" + }, + "mxpi_dataserialize0": { + "props": { + "outputDataKeys": "mxpi_pluginclimb0" + }, + "factory": "mxpi_dataserialize", + "next": "appsink0" + }, + "appsink0": { + "props": { + "blocksize": "4096000" + }, + "factory": "appsink" + } + } +} diff --git a/contrib/ActionRecognition/pipeline/plugin_outofbed.pipeline b/contrib/ActionRecognition/pipeline/plugin_outofbed.pipeline new file mode 100644 index 0000000000000000000000000000000000000000..f49063887efc85cdfe85b284c3dafdedff1e86f3 --- /dev/null +++ b/contrib/ActionRecognition/pipeline/plugin_outofbed.pipeline @@ -0,0 +1,81 @@ +{ + "detection+tracking": { + "stream_config": { + "deviceId": "0" + }, + "mxpi_rtspsrc0": { + "props": { + "rtspUrl": "rtsp_Url" + }, + "factory": "mxpi_rtspsrc", + "next": "mxpi_videodecoder0" + }, + "mxpi_videodecoder0":{ + "props": { + "vdecChannelId": "0" + }, + "factory": "mxpi_videodecoder", + "next": "mxpi_imageresize0" + }, + "mxpi_imageresize0":{ + "props": { + "dataSource": "mxpi_videodecoder0", + "resizeHeight": "416", + "resizeWidth": "416" + }, + "factory": "mxpi_imageresize", + "next": "mxpi_tensorinfer0" + }, + "mxpi_tensorinfer0":{ + "props": { + "dataSource": "mxpi_imageresize0", + "modelPath": "../models/yolov3/yolov3_tf_bs1_fp16.om" + }, + "factory": "mxpi_tensorinfer", + "next": "mxpi_objectpostprocessor0" + }, + "mxpi_objectpostprocessor0": { + "props": { + "dataSource": "mxpi_tensorinfer0", + "funcLanguage":"c++", + "postProcessConfigPath": "../models/yolov3/yolov3_tf_bs1_fp16.cfg", + "labelPath": "../models/yolov3/coco.names", + "postProcessLibPath": "../../../lib/modelpostprocessors/libyolov3postprocess.so" + }, + "factory": "mxpi_objectpostprocessor", + "next": "mxpi_motsimplesort0" + }, + "mxpi_motsimplesort0": { + "props": { + "dataSourceDetection": "mxpi_objectpostprocessor0" + }, + "factory": "mxpi_motsimplesort", + "next": "mxpi_pluginoutofbed0" + }, + "mxpi_pluginoutofbed0": { + "props": { + "dataSourceTrack": "mxpi_motsimplesort0", + "dataSourceDetect": "mxpi_objectpostprocessor0", + "configPath": "../datas/OutofBed/*.txt", + "detectThresh": "8", + "detectSleep": "15", + "detectRatio": "0.25" + }, + "factory": "mxpi_pluginoutofbed", + "next": "mxpi_dataserialize0" + }, + "mxpi_dataserialize0": { + "props": { + "outputDataKeys": "mxpi_pluginoutofbed0" + }, + "factory": "mxpi_dataserialize", + "next": "appsink0" + }, + "appsink0": { + "props": { + "blocksize": "4096000" + }, + "factory": "appsink" + } + } +} diff --git a/contrib/ActionRecognition/pipeline/plugin_overspeed.pipeline b/contrib/ActionRecognition/pipeline/plugin_overspeed.pipeline new file mode 100644 index 0000000000000000000000000000000000000000..f973f750bd73e6db32e9424f301b41c5cad9fcc3 --- /dev/null +++ b/contrib/ActionRecognition/pipeline/plugin_overspeed.pipeline @@ -0,0 +1,80 @@ +{ + "detection+tracking": { + "stream_config": { + "deviceId": "0" + }, + "mxpi_rtspsrc0": { + "props": { + "rtspUrl": "rtsp_Url" + }, + "factory": "mxpi_rtspsrc", + "next": "mxpi_videodecoder0" + }, + "mxpi_videodecoder0":{ + "props": { + "vdecChannelId": "0" + }, + "factory": "mxpi_videodecoder", + "next": "mxpi_imageresize0" + }, + "mxpi_imageresize0":{ + "props": { + "dataSource": "mxpi_videodecoder0", + "resizeHeight": "416", + "resizeWidth": "416" + }, + "factory": "mxpi_imageresize", + "next": "mxpi_tensorinfer0" + }, + "mxpi_tensorinfer0":{ + "props": { + "dataSource": "mxpi_imageresize0", + "modelPath": "../models/yolov3/yolov3_tf_bs1_fp16.om" + }, + "factory": "mxpi_tensorinfer", + "next": "mxpi_objectpostprocessor0" + }, + "mxpi_objectpostprocessor0": { + "props": { + "dataSource": "mxpi_tensorinfer0", + "funcLanguage":"c++", + "postProcessConfigPath": "../models/yolov3/yolov3_tf_bs1_fp16.cfg", + "labelPath": "../models/yolov3/coco.names", + "postProcessLibPath": "../../../lib/modelpostprocessors/libyolov3postprocess.so" + }, + "factory": "mxpi_objectpostprocessor", + "next": "mxpi_motsimplesort0" + }, + "mxpi_motsimplesort0": { + "props": { + "dataSourceDetection": "mxpi_objectpostprocessor0" + }, + "factory": "mxpi_motsimplesort", + "next": "mxpi_pluginoverspeed0" + }, + "mxpi_pluginoverspeed0": { + "props": { + "dataSourceTrack": "mxpi_motsimplesort0", + "dataSourceDetection": "mxpi_objectpostprocessor0", + "speedThresh": "10", + "frames": "8", + "detectSleep": "270" + }, + "factory": "mxpi_pluginoverspeed", + "next": "mxpi_dataserialize0" + }, + "mxpi_dataserialize0": { + "props": { + "outputDataKeys": "mxpi_pluginoverspeed0" + }, + "factory": "mxpi_dataserialize", + "next": "appsink0" + }, + "appsink0": { + "props": { + "blocksize": "4096000" + }, + "factory": "appsink" + } + } +} diff --git a/contrib/ActionRecognition/pipeline/plugin_overstay.pipeline b/contrib/ActionRecognition/pipeline/plugin_overstay.pipeline new file mode 100644 index 0000000000000000000000000000000000000000..b0c09eb147554f8109ec982ba614a7cdad1750a6 --- /dev/null +++ b/contrib/ActionRecognition/pipeline/plugin_overstay.pipeline @@ -0,0 +1,82 @@ +{ + "detection+tracking": { + "stream_config": { + "deviceId": "0" + }, + "mxpi_rtspsrc0": { + "props": { + "rtspUrl": "rtsp_Url" + }, + "factory": "mxpi_rtspsrc", + "next": "mxpi_videodecoder0" + }, + "mxpi_videodecoder0":{ + "props": { + "vdecChannelId": "0" + }, + "factory": "mxpi_videodecoder", + "next": "mxpi_imageresize0" + }, + "mxpi_imageresize0":{ + "props": { + "dataSource": "mxpi_videodecoder0", + "resizeHeight": "416", + "resizeWidth": "416" + }, + "factory": "mxpi_imageresize", + "next": "mxpi_tensorinfer0" + }, + "mxpi_tensorinfer0":{ + "props": { + "dataSource": "mxpi_imageresize0", + "modelPath": "../models/yolov3/yolov3_tf_bs1_fp16.om" + }, + "factory": "mxpi_tensorinfer", + "next": "mxpi_objectpostprocessor0" + }, + "mxpi_objectpostprocessor0": { + "props": { + "dataSource": "mxpi_tensorinfer0", + "funcLanguage":"c++", + "postProcessConfigPath": "../models/yolov3/yolov3_tf_bs1_fp16.cfg", + "labelPath": "../models/yolov3/coco.names", + "postProcessLibPath": "../../../lib/modelpostprocessors/libyolov3postprocess.so" + }, + "factory": "mxpi_objectpostprocessor", + "next": "mxpi_motsimplesort0" + }, + "mxpi_motsimplesort0": { + "props": { + "dataSourceDetection": "mxpi_objectpostprocessor0" + }, + "factory": "mxpi_motsimplesort", + "next": "mxpi_pluginoverstay0" + }, + "mxpi_pluginoverstay0": { + "props": { + "dataSourceTrack": "mxpi_motsimplesort0", + "dataSourceDetection": "mxpi_objectpostprocessor0", + "stayThresh":"90", + "frames":"90", + "distanceThresh":"100", + "detectRatio":"0.7", + "detectSleep":"90" + }, + "factory": "mxpi_pluginoverstay", + "next": "mxpi_dataserialize0" + }, + "mxpi_dataserialize0": { + "props": { + "outputDataKeys": "mxpi_pluginoverstay0" + }, + "factory": "mxpi_dataserialize", + "next": "appsink0" + }, + "appsink0": { + "props": { + "blocksize": "4096000" + }, + "factory": "appsink" + } + } +} diff --git a/contrib/ActionRecognition/pipeline/plugin_violentaction.pipeline b/contrib/ActionRecognition/pipeline/plugin_violentaction.pipeline new file mode 100644 index 0000000000000000000000000000000000000000..7d1018f1a0a558c81ac1a72850355d39d6ba79ff --- /dev/null +++ b/contrib/ActionRecognition/pipeline/plugin_violentaction.pipeline @@ -0,0 +1,125 @@ +{ + "detection+action recognition": { + "stream_config": { + "deviceId": "0" + }, + "mxpi_rtspsrc0": { + "props": { + "rtspUrl": "rtsp_Url" + }, + "factory": "mxpi_rtspsrc", + "next": "mxpi_videodecoder0" + }, + "mxpi_videodecoder0":{ + "props": { + "vdecChannelId": "0" + }, + "factory": "mxpi_videodecoder", + "next": "mxpi_imageresize0" + }, + "mxpi_imageresize0":{ + "props": { + "dataSource": "mxpi_videodecoder0", + "resizeHeight": "416", + "resizeWidth": "416" + }, + "factory": "mxpi_imageresize", + "next": "mxpi_tensorinfer0" + }, + "mxpi_tensorinfer0":{ + "props": { + "dataSource": "mxpi_imageresize0", + "modelPath": "../models/yolov3/yolov3_tf_bs1_fp16.om" + }, + "factory": "mxpi_tensorinfer", + "next": "mxpi_objectpostprocessor0" + }, + "mxpi_objectpostprocessor0": { + "props": { + "dataSource": "mxpi_tensorinfer0", + "funcLanguage":"c++", + "postProcessConfigPath": "../models/yolov3/yolov3_tf_bs1_fp16.cfg", + "labelPath": "../models/yolov3/coco.names", + "postProcessLibPath": "../../../lib/modelpostprocessors/libyolov3postprocess.so" + }, + "factory": "mxpi_objectpostprocessor", + "next": "mxpi_distributor0" + }, + "mxpi_distributor0": { + "props": { + "dataSource": "mxpi_objectpostprocessor0", + "classIds": "0" + }, + "factory": "mxpi_distributor", + "next": "mxpi_motsimplesort0" + }, + "mxpi_motsimplesort0": { + "props": { + "dataSourceDetection": "mxpi_distributor0_0" + }, + "factory": "mxpi_motsimplesort", + "next": "mxpi_imagecrop0" + }, + "mxpi_imagecrop0": { + "props": { + "dataSource": "mxpi_distributor0_0", + "dataSourceImage": "mxpi_videodecoder0", + "resizeHeight":"224", + "resizeWidth":"224", + "resizeType":"Resizer_KeepAspectRatio_Fit" + }, + "factory": "mxpi_imagecrop", + "next": "mxpi_stackframe0" + }, + "mxpi_stackframe0":{ + "props": { + "visionSource": "mxpi_imagecrop0", + "trackSource": "mxpi_motsimplesort0", + "frameNum": "3" + }, + "factory": "mxpi_stackframe", + "next": "mxpi_tensorinfer1" + }, + "mxpi_tensorinfer1":{ + "props": { + "dataSource": "mxpi_stackframe0", + "skipModelCheck": "1", + "modelPath": "../models/ECONet/ECONet.om" + }, + "factory": "mxpi_tensorinfer", + "next": "mxpi_classpostprocessor0" + }, + "mxpi_classpostprocessor0":{ + "props": { + "dataSource": "mxpi_tensorinfer1", + "postProcessConfigPath": "../models/ECONet/eco_post.cfg", + "labelPath":"../models/ECONet/ucf101.names, + "postProcessLibPath":"../../../lib/modelpostprocessors/libresnet50postprocess.so" + }, + "factory": "mxpi_classpostprocessor", + "next": "mxpi_violentaction0" + }, + "mxpi_violentaction0":{ + "props": { + "classSource": "mxpi_classpostprocessor0", + "filePath": "../data/roi/ViolentAction/aoi.txt", + "detectSleep": "5" + }, + "factory": "mxpi_violentaction", + "next": "mxpi_dataserialize0" + }, + "mxpi_dataserialize0": { + "props": { + "outputDataKeys": "mxpi_violentaction0" + }, + "factory": "mxpi_dataserialize", + "next": "appsink0" + }, + "appsink0": { + "props": { + "blocksize": "4096000" + }, + "factory": "appsink" + } + } +} diff --git a/contrib/ActionRecognition/plugins/MxpiStackFrame/BlockingMap.cpp b/contrib/ActionRecognition/plugins/MxpiStackFrame/BlockingMap.cpp new file mode 100644 index 0000000000000000000000000000000000000000..91f37b73149dccdb9b1a1e8d23b0e0d2cf669a5a --- /dev/null +++ b/contrib/ActionRecognition/plugins/MxpiStackFrame/BlockingMap.cpp @@ -0,0 +1,123 @@ +/* + * Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "BlockingMap.h" + +namespace MxPlugins { + BlockingMap::BlockingMap() {} + + BlockingMap::~BlockingMap() {} + + void BlockingMap::Insert(const uint32_t &id, MxBase::MemoryData newData) { + // add std::lock_guard + std::lock_guard guard(mtx_); + // get current timestamp + using Time = std::chrono::high_resolution_clock; + auto currentTime = Time::now(); + std::pair> time_MxpiVisionList; + // set MxpiVisionInfo and MxpiVisionData + auto mxpiVisionList = copyList(newData); + time_MxpiVisionList = std::make_pair(currentTime, mxpiVisionList); + blockingMap_[id] = time_MxpiVisionList; + keys_.insert(id); + } + + void BlockingMap::Update(const uint32_t &id, MxBase::MemoryData newData) { + // add std::lock_guard + std::lock_guard guard(mtx_); + std::pair> + time_MxpiVisionList = blockingMap_[id]; + auto mxpiVisionList = time_MxpiVisionList.second; + // set MxpiVisionInfo and MxpiVisionData + MxTools::MxpiVision *dstMxpivision = mxpiVisionList->add_visionvec(); + MxTools::MxpiVisionInfo *mxpiVisionInfo = dstMxpivision->mutable_visioninfo(); + mxpiVisionInfo->set_format(1); + mxpiVisionInfo->set_height(224); + mxpiVisionInfo->set_width(224); + mxpiVisionInfo->set_heightaligned(224); + mxpiVisionInfo->set_widthaligned(224); + // set MxpiVisionData by MemoryData + MxTools::MxpiVisionData *mxpiVisionData = dstMxpivision->mutable_visiondata(); + mxpiVisionData->set_dataptr((uint64_t) newData.ptrData); + mxpiVisionData->set_datasize(newData.size); + mxpiVisionData->set_deviceid(newData.deviceId); + mxpiVisionData->set_memtype((MxTools::MxpiMemoryType) newData.type); + //visionlist->pair + time_MxpiVisionList = std::make_pair(time_MxpiVisionList.first, mxpiVisionList); + blockingMap_[id] = time_MxpiVisionList; + } + + std::pair> + BlockingMap::Get(const uint32_t &id) { + // add std::lock_guard + std::lock_guard guard(mtx_); + if (blockingMap_.find(id) != blockingMap_.end()) { + return blockingMap_[id]; + } else { + // If can't find the element, manually assign nullptr + std::pair> empty; + using Time = std::chrono::high_resolution_clock; + auto currentTime = Time::now(); + empty = std::make_pair(currentTime, nullptr); + return empty; + } + } + + void BlockingMap::Clear(const uint32_t &id) { + std::lock_guard guard(mtx_); + blockingMap_.erase(id); + keys_.erase(id); + } + + std::uint32_t BlockingMap::count(const uint32_t &id) { + std::lock_guard guard(mtx_); + return blockingMap_.count(id); + } + + size_t BlockingMap::Size() const { + return blockingMap_.size(); + } + + std::vector BlockingMap::Keys() { + // id<->key + std::vector keys; + std::lock_guard guard(mtx_); + for (auto iter = keys_.begin(); iter != keys_.end(); iter++) { + keys.push_back(*iter); + } + return keys; + } + + std::shared_ptr BlockingMap::copyList(MxBase::MemoryData newData) { + // new shared_ptr MxpiVisionList; + std::shared_ptr dstMxpiVisionListSptr(new MxTools::MxpiVisionList, + MxTools::g_deleteFuncMxpiVisionList); + MxTools::MxpiVision *dstMxpivision = dstMxpiVisionListSptr->add_visionvec(); + MxTools::MxpiVisionInfo *mxpiVisionInfo = dstMxpivision->mutable_visioninfo(); + mxpiVisionInfo->set_format(1); + mxpiVisionInfo->set_height(224); + mxpiVisionInfo->set_width(224); + mxpiVisionInfo->set_heightaligned(224); + mxpiVisionInfo->set_widthaligned(224); + // set MxpiVisionData by MemoryData + MxTools::MxpiVisionData *mxpiVisionData = dstMxpivision->mutable_visiondata(); + mxpiVisionData->set_dataptr((uint64_t) newData.ptrData); + mxpiVisionData->set_datasize(newData.size); + mxpiVisionData->set_deviceid(newData.deviceId); + mxpiVisionData->set_memtype((MxTools::MxpiMemoryType) newData.type); + return dstMxpiVisionListSptr; + } +} \ No newline at end of file diff --git a/contrib/ActionRecognition/plugins/MxpiStackFrame/BlockingMap.h b/contrib/ActionRecognition/plugins/MxpiStackFrame/BlockingMap.h new file mode 100644 index 0000000000000000000000000000000000000000..beeaa6b253d28553fbed48245a1b6df9e39a07eb --- /dev/null +++ b/contrib/ActionRecognition/plugins/MxpiStackFrame/BlockingMap.h @@ -0,0 +1,77 @@ +/* + * Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INC_FACE_BLOCKING_MAP_H +#define INC_FACE_BLOCKING_MAP_H + +#include +#include +#include +#include +#include +#include +#include +#include "MxBase/Tensor/TensorBase/TensorBase.h" +#include "MxTools/Proto/MxpiDataType.pb.h" +#include "MxBase/Log/Log.h" +#include "MxBase/MemoryHelper/MemoryHelper.h" +#include "MxStream/StreamManager/MxStreamManager.h" +#include "MxTools/Proto/MxpiDataTypeDeleter.h" + +/** + * This is a thread safe map. All functions use std::lock_guard. +*/ + +namespace MxPlugins { + class BlockingMap { + public: + /** + * @api + * @brief Initialize a new mxpivisionlist for this id, and insert the visiondata passed from upstream into the mxpivisionlist. + * @param trackID, MemoryData + */ + void Insert(const uint32_t &id, const MxBase::MemoryData newData); + + /** + * @api + * @brief Update mxpivisionlist and insert the visiondata for this id. + * @param trackID, MemoryData + */ + void Update(const uint32_t &id, const MxBase::MemoryData newData); + + std::pair> + Get(const uint32_t &id); // get std::pair instance + void Clear(const uint32_t &id); // erase map and id + std::vector Keys(); // save tackId into keys set + size_t Size() const; + + std::uint32_t count(const uint32_t &id); // return the number of elements of this id + std::shared_ptr + copyList(const MxBase::MemoryData newData); // set MxpiVisionInfo and MxpiVisionData + BlockingMap(); + + ~BlockingMap(); + + private: + std::mutex mtx_ = {}; + // > + std::map>> blockingMap_ = {}; + std::set keys_ = {}; + }; +} +#endif + diff --git a/contrib/ActionRecognition/plugins/MxpiStackFrame/CMakeLists.txt b/contrib/ActionRecognition/plugins/MxpiStackFrame/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..3b681e17d31675fd9e5438ee0c7d512fdb0d15be --- /dev/null +++ b/contrib/ActionRecognition/plugins/MxpiStackFrame/CMakeLists.txt @@ -0,0 +1,45 @@ +cmake_minimum_required(VERSION 3.10) +project(MxpiStackFrame) + +set(CMAKE_CXX_STANDARD 11) + +set(PLUGIN_NAME "mxpi_stackframe") +set(TARGET_LIBRARY ${PLUGIN_NAME}) + +add_compile_options(-fPIC -fstack-protector-all -g -Wl,-z,relro,-z,now,-z -pie -Wall) +add_compile_options(-std=c++11 -Wno-deprecated-declarations) +add_compile_options("-DPLUGIN_NAME=${PLUGIN_NAME}") + +add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0 -Dgoogle=mindxsdk_private) +add_definitions(-DENABLE_DVPP_INTERFACE) + +set(MX_SDK_HOME "$ENV{MX_SDK_HOME}") + +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${MX_SDK_HOME}/lib/plugins/) + +include_directories(${MX_SDK_HOME}/include) +include_directories(${MX_SDK_HOME}/opensource/include) +include_directories(${MX_SDK_HOME}/opensource/include/gstreamer-1.0) +include_directories(${MX_SDK_HOME}/opensource/include/opencv4) +include_directories(${MX_SDK_HOME}/opensource/include/glib-2.0) +include_directories(${MX_SDK_HOME}/opensource/lib/glib-2.0/include) + +link_directories(${MX_SDK_HOME}/lib) +link_directories(${MX_SDK_HOME}/opensource/lib) + +file(GLOB PLUGIN_SRC ./*.cpp) +message(${PLUGIN_SRC}) + +add_library(${TARGET_LIBRARY} SHARED ${PLUGIN_SRC}) +target_link_libraries(${TARGET_LIBRARY} + mxpidatatype + plugintoolkit + mxbase + streammanager + mindxsdk_protobuf + glib-2.0 + gstreamer-1.0 + gobject-2.0 + gstbase-1.0 + gmodule-2.0 + ) diff --git a/contrib/ActionRecognition/plugins/MxpiStackFrame/MxpiStackFrame.cpp b/contrib/ActionRecognition/plugins/MxpiStackFrame/MxpiStackFrame.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1a82652d14eb3d8afacc4fe43035d8d45f73ab6b --- /dev/null +++ b/contrib/ActionRecognition/plugins/MxpiStackFrame/MxpiStackFrame.cpp @@ -0,0 +1,324 @@ +/* + * Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "MxpiStackFrame.h" + +using namespace MxBase; +using namespace MxTools; +using namespace MxPlugins; +using namespace std; + +namespace { + bool isStop_ = true; + std::shared_ptr ObjectFrame = std::make_shared(); +} + +APP_ERROR MxpiStackFrame::Init(std::map> &configParamMap) { + LogInfo << "Begin to initialize MxpiStackFrame(" << pluginName_ << ")."; + std::shared_ptr visionSource = std::static_pointer_cast(configParamMap["visionSource"]); + visionsource_ = *visionSource; + + std::shared_ptr trackSource = std::static_pointer_cast(configParamMap["trackSource"]); + tracksource_ = *trackSource; + + std::shared_ptr framenum = std::static_pointer_cast(configParamMap["frameNum"]); + skipFrameNum_ = *framenum; + + std::shared_ptr timeout = std::static_pointer_cast(configParamMap["timeOut"]); + timeout_ = *timeout; + + std::shared_ptr sleeptime = std::static_pointer_cast(configParamMap["sleepTime"]); + sleepTime_ = *sleeptime; + + isStop_ = false; + // crate CheckFrame thread in Init function + CreateThread(); + LogInfo << "End to initialize MxpiStackFrame(" << pluginName_ << ")."; + return APP_ERR_OK; +} + +APP_ERROR MxpiStackFrame::DeInit() { + LogInfo << "Begin to deinitialize MxpiStackFrame(" << pluginName_ << ")."; + // Block the current thread until the thread identified by *this ends its execution + stopFlag_ = true; + if (thread_->joinable()) { + thread_->join(); + } + LogInfo << "End to deinitialize MxpiStackFrame(" << pluginName_ << ")."; + return APP_ERR_OK; +} + +APP_ERROR MxpiStackFrame::CheckDataSource(MxTools::MxpiMetadataManager &mxpiMetadataManager) { + if (mxpiMetadataManager.GetMetadata(visionsource_) == nullptr) { + LogDebug << GetError(APP_ERR_METADATA_IS_NULL, pluginName_) << "vision metadata is null. please check" + << "Your property visionSource(" << visionsource_ << ")."; + return APP_ERR_METADATA_IS_NULL; + } + if (mxpiMetadataManager.GetMetadata(tracksource_) == nullptr) { + LogDebug << GetError(APP_ERR_METADATA_IS_NULL, pluginName_) << "track metadata is null. please check" + << "Your property dataSourceFeature(" << tracksource_ << ")."; + return APP_ERR_METADATA_IS_NULL; + } + return APP_ERR_OK; +} + +void MxpiStackFrame::StackFrame(const std::shared_ptr &visionList, + const std::shared_ptr &trackLetList, + std::shared_ptr &blockingMap) { + for (int32_t i = 0; i < (int32_t) trackLetList->trackletvec_size(); i++) { + auto &trackObject = trackLetList->trackletvec(i); + uint32_t trackId = trackObject.trackid(); + // lost object has no header vector; no object ; + if (trackObject.headervec_size() == 0 || visionList->visionvec_size() == 0) { + continue; + } + // get the visionvec by memberid + int32_t memberId = trackObject.headervec(0).memberid(); + auto vision = visionList->visionvec(memberId); + // transfer visiondata to memorydata + MxBase::MemoryData memoryData = {}; + memoryData.deviceId = vision.visiondata().deviceid(); + memoryData.type = (MxBase::MemoryData::MemoryType) 1; + memoryData.size = (uint32_t) vision.visiondata().datasize(); + memoryData.ptrData = (void *) vision.visiondata().dataptr(); + // add the vision to the blockingMap by trackId + if (blockingMap->count(trackId) == 0) { + // Initialize a new Mxpivisionlist + blockingMap->Insert(trackId, memoryData); + } else { + auto time_visionlist = blockingMap->Get(trackId); + LogInfo << "time_visionlist.second->visionvec_size() " << time_visionlist.second->visionvec_size(); + if (time_visionlist.second->visionvec_size() >= 8) { + continue; + } else { + blockingMap->Update(trackId, memoryData); + } + } + } + visionList->clear_visionvec(); +} + +void MxpiStackFrame::CreateThread() { + thread_.reset(new std::thread(&MxpiStackFrame::WatchThread, this)); +} + +void MxpiStackFrame::WatchThread() { + // set current device context ; same as deviceId in pipeline + DeviceContext context = {}; + context.devId = 0; + APP_ERROR ret = DeviceManager::GetInstance()->SetDevice(context); + if (ret != APP_ERR_OK) { + LogError << "SetDevice failed"; + } + while (!stopFlag_) { + std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime_)); + CheckFrames(ObjectFrame); + } +} + +std::shared_ptr MxpiStackFrame::ConvertVisionList2TensorPackageList( + std::shared_ptr &mxpiVisionList) { + // use make_shared with caution + std::shared_ptr tensorPackageList(new MxpiTensorPackageList, + g_deleteFuncMxpiTensorPackageList); + MxBase::MemoryData concatData = {}; + concatData.deviceId = mxpiVisionList->visionvec(0).visiondata().deviceid(); + concatData.type = (MxBase::MemoryData::MemoryType) 1; + concatData.size = + mxpiVisionList->visionvec_size() * (uint32_t) mxpiVisionList->visionvec(0).visiondata().datasize(); + // malloc new memory + APP_ERROR ret = MxBase::MemoryHelper::MxbsMalloc(concatData); + if (ret != APP_ERR_OK) { + LogError << "MxbsMalloc failed"; + LogError << "concatData.size:" << concatData.size << " concatData.type:" << concatData.type + << "concatData.deviceId:" << concatData.deviceId; + return tensorPackageList; + } + for (int i = 0; i < mxpiVisionList->visionvec_size(); i++) { + MxBase::MemoryData memoryData = {}; + memoryData.deviceId = mxpiVisionList->visionvec(i).visiondata().deviceid(); + memoryData.type = (MxBase::MemoryData::MemoryType) 1; + memoryData.size = (uint32_t) mxpiVisionList->visionvec(i).visiondata().datasize(); + memoryData.ptrData = (void *) mxpiVisionList->visionvec(i).visiondata().dataptr(); + // memory copy + MxBase::MemoryData newData = {}; + newData.deviceId = mxpiVisionList->visionvec(i).visiondata().deviceid(); + newData.type = (MxBase::MemoryData::MemoryType) 1; + newData.size = (uint32_t) mxpiVisionList->visionvec(i).visiondata().datasize(); + newData.ptrData = (void *) ((uint8_t *) concatData.ptrData + i * newData.size); + // MxbsMallocAndCopy cannot be used here, cause npu memory leak + ret = MxBase::MemoryHelper::MxbsMemcpy(newData, memoryData, memoryData.size); + if (ret != APP_ERR_OK) { + LogError << "MxbsMemcpy failed"; + MxBase::MemoryHelper::Free(concatData); + return tensorPackageList; + } + } + auto tensorPackage = tensorPackageList->add_tensorpackagevec(); + auto tensorVector = tensorPackage->add_tensorvec(); + tensorVector->set_tensordataptr((uint64) concatData.ptrData); + tensorVector->set_tensordatasize(concatData.size); + tensorVector->set_deviceid(concatData.deviceId); + tensorVector->set_memtype((MxpiMemoryType) concatData.type); + // explicitly specify tenspr shape + tensorVector->add_tensorshape(8); + tensorVector->add_tensorshape(224); + tensorVector->add_tensorshape(224); + tensorVector->add_tensorshape(3); + return tensorPackageList; +} + +void MxpiStackFrame::CheckFrames(std::shared_ptr &blockingMap) { + // Get current timestamp + LogInfo << "Begin to check frames"; + using Time = std::chrono::high_resolution_clock; + using Duration = std::chrono::duration; + using Millisecond = std::chrono::duration>; + auto currentTime = Time::now(); + std::vector keys = blockingMap->Keys(); + for (uint32_t key : keys) { // key <-> trackId + auto TimeMxpiVisionList = blockingMap->Get(key); + if (TimeMxpiVisionList.second == nullptr) { + continue; + } + Duration duration = currentTime - TimeMxpiVisionList.first; + double lastTime = std::chrono::duration_cast(duration).count(); + if (lastTime > timeout_) { + LogInfo << "Object:" << key << " is TimeOut"; + blockingMap->Clear(key); + continue; + } + if (TimeMxpiVisionList.second->visionvec_size() == 8) { + // Add MxpiTensorPackageList to metadata and Send data to downstream plugin + const string metaKey = pluginName_; + auto dstMxpiVisionLitsSptr = TimeMxpiVisionList.second; + auto tensorPackageListPtr = ConvertVisionList2TensorPackageList(dstMxpiVisionLitsSptr); + blockingMap->Clear(key); + // CreateDeviceBuffer; need inputParam + auto *outbuffer = MxpiBufferManager::CreateHostBuffer(inputParam); + MxpiMetadataManager mxpiMetadataManager(*outbuffer); + auto ret = mxpiMetadataManager.AddProtoMetadata(metaKey, static_pointer_cast(tensorPackageListPtr)); + if (ret != APP_ERR_OK) { + LogError << ErrorInfo_.str(); + SendMxpiErrorInfo(*outbuffer, pluginName_, ret, ErrorInfo_.str()); + SendData(0, *outbuffer); + } + LogInfo << "Object:" << key << " has stacked enough frames and begin to send data to downstream plugin"; + SendData(0, *outbuffer); + } + } +} + +APP_ERROR MxpiStackFrame::Process(std::vector &mxpiBuffer) { + LogInfo << "Begin to process MxpiStackFrame(" << elementName_ << ")."; + // Get MxpiVisionList and MxpiTrackletList from mxpibuffer + MxpiBuffer *inputMxpiBuffer = mxpiBuffer[0]; + MxpiMetadataManager mxpiMetadataManager(*inputMxpiBuffer); + ErrorInfo_.str(""); + // check data source + APP_ERROR ret = CheckDataSource(mxpiMetadataManager); + if (ret != APP_ERR_OK) { + SendData(0, *inputMxpiBuffer); + return ret; + } + // Get metadata by key + std::shared_ptr vision_metadata = mxpiMetadataManager.GetMetadata(visionsource_); + std::shared_ptr srcVisionListPtr = std::static_pointer_cast(vision_metadata); + std::shared_ptr track_metadata = mxpiMetadataManager.GetMetadata(tracksource_); + std::shared_ptr srcTrackLetListPtr = std::static_pointer_cast(track_metadata); + // Stacking frames by track ID ; Choose skipFrameNum_ to skip frame + // DestroyBuffer (Get buffer to Host if needed) + if (count % skipFrameNum_ == 0) { + count = 1; + LogInfo << "Begin to stack frames"; + StackFrame(srcVisionListPtr, srcTrackLetListPtr, ObjectFrame); + MxpiBufferManager::DestroyBuffer(inputMxpiBuffer); + } else { + count++; + MxpiBufferManager::DestroyBuffer(inputMxpiBuffer); + } + LogInfo << "End to process MxpiStackFrame(" << elementName_ << ")."; + return APP_ERR_OK; +} + +std::vector> MxpiStackFrame::DefineProperties() { + std::vector> properties; + // the cropped image + auto visionsource = std::make_shared>(ElementProperty{ + STRING, + "visionSource", + "imageSource", + "the name of cropped image source", + "default", "NULL", "NULL" + }); + // the tracklet information + auto tracksource = std::make_shared>(ElementProperty{ + STRING, + "trackSource", + "objectSource", + "tracklelist to get the responding id", + "defalut", "NULL", "NULL" + }); + // skip frame number + auto framenum = std::make_shared>(ElementProperty{ + UINT, + "frameNum", + "frameNum", + "the number of skip frame", + 1, 1, 5 + }); + auto timeout = std::make_shared>(ElementProperty{ + DOUBLE, + "timeOut", + "timeOut", + "Time to discard the frames", + 800., 100., 2000. + }); + auto sleeptime = std::make_shared>(ElementProperty{ + UINT, + "sleepTime", + "sleepTime", + "The Time CheckFrames thread hangs", + 100, 100, 1000 + }); + properties.push_back(visionsource); + properties.push_back(tracksource); + properties.push_back(framenum); + properties.push_back(timeout); + properties.push_back(sleeptime); + return properties; +} + +MxpiPortInfo MxpiStackFrame::DefineInputPorts() { + MxpiPortInfo inputPortInfo; + // Input: {{Mxpivisionlist}, {MxpiTrackLetList}} + std::vector> value = {{"ANY"}}; + GenerateStaticInputPortsInfo(value, inputPortInfo); + return inputPortInfo; +} + +MxpiPortInfo MxpiStackFrame::DefineOutputPorts() { + MxpiPortInfo outputPortInfo; + // Output: {{MxpiTensorPackageList}} + std::vector> value = {{"ANY"}}; + GenerateStaticOutputPortsInfo(value, outputPortInfo); + return outputPortInfo; +} + +namespace { + MX_PLUGIN_GENERATE(MxpiStackFrame) +} + diff --git a/contrib/ActionRecognition/plugins/MxpiStackFrame/MxpiStackFrame.h b/contrib/ActionRecognition/plugins/MxpiStackFrame/MxpiStackFrame.h new file mode 100644 index 0000000000000000000000000000000000000000..817698d4014536a4e6a25ef8f49425ac28ae4db6 --- /dev/null +++ b/contrib/ActionRecognition/plugins/MxpiStackFrame/MxpiStackFrame.h @@ -0,0 +1,128 @@ +/* + * Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * imitations under the License. + */ + +#ifndef MXPLUGINS_MXPIFRAMESTACK_H +#define MXPLUGINS_MXPIFRAMESTACK_H + +#include "MxBase/Tensor/TensorBase/TensorBase.h" +#include "MxBase/ErrorCode/ErrorCode.h" +#include "MxTools/PluginToolkit/base/MxPluginGenerator.h" +#include "MxTools/PluginToolkit/base/MxPluginBase.h" +#include "MxTools/PluginToolkit/buffer/MxpiBufferManager.h" +#include "MxTools/PluginToolkit/metadata/MxpiMetadataManager.h" +#include "MxTools/Proto/MxpiDataType.pb.h" +#include +#include "MxBase/Log/Log.h" +#include "opencv2/opencv.hpp" +#include "MxBase/MemoryHelper/MemoryHelper.h" +#include "MxBase/DvppWrapper/DvppWrapper.h" +#include "MxStream/StreamManager/MxStreamManager.h" +#include "MxTools/Proto/MxpiDataTypeDeleter.h" +#include "MxTools/Proto/MxpiDataType.pb.h" +#include +#include "BlockingMap.h" + +/** + * This plugin is to stack frames based on detected objects. +*/ + +namespace MxPlugins { + class MxpiStackFrame : public MxTools::MxPluginBase { + public: + /** + * @description: Init configs. + * @param configParamMap: config. + * @return: Error code. + */ + APP_ERROR Init(std::map> &configParamMap) override; + + /** + * @description: DeInit device. + * @return: Error code. + */ + APP_ERROR DeInit() override; + + /** + * @description: MxpiStackFrame plugin process. + * @param mxpiBuffer: data receive from the previous. + * @return: Error code. + */ + APP_ERROR Process(std::vector &mxpiBuffer) override; + + /** + * @description: MxpiStackFrame plugin define properties. + * @return: properties. + */ + static std::vector> DefineProperties(); + + /** + * @api + * @brief Define the number and data type of input ports. + * @return MxTools::MxpiPortInfo. + */ + static MxTools::MxpiPortInfo DefineInputPorts(); + + /** + * @api + * @brief Define the number and data type of output ports. + * @return MxTools::MxpiPortInfo. + */ + static MxTools::MxpiPortInfo DefineOutputPorts(); + + /** + * @api + * @brief Transfer MxpiVisionList to MxpiTensorPackageList. + * @param shared_ptr . + * @return shared_ptr . + */ + static std::shared_ptr + ConvertVisionList2TensorPackageList(std::shared_ptr &mxpiVisionList); + + private: + /** + * @api + * @brief Check metadata. + * @param MxTools::MxpiMetadataManager. + * @return Error Code. + */ + APP_ERROR CheckDataSource(MxTools::MxpiMetadataManager &mxpiMetadataManager); + + /** + * @api + * @brief Stack frames for each human object. + * @param MxpiVisionList, MxpiTrackLetList, BlockingMap + */ + void StackFrame(const std::shared_ptr &visionList, + const std::shared_ptr &trackLetList, + std::shared_ptr &blockingMap); + // Check Thread function; Check whether a object is 8 frames + void CheckFrames(std::shared_ptr &blockingMap); + void CreateThread(); // create CheckFrame thread + void WatchThread(); // Watch CheckFrame thread + std::string visionsource_ = ""; // cropped image from crop plugin + std::string tracksource_ = ""; // track result + uint32_t skipFrameNum_ = 0; + uint32_t count = 1; + double timeout_ = 500.; //Millisecond + uint32_t sleepTime_ = 0; + std::ostringstream ErrorInfo_; + std::unique_ptr thread_ = nullptr; + bool stopFlag_ = false; + InputParam inputParam; // to create buffer + }; +} +#endif \ No newline at end of file diff --git a/contrib/ActionRecognition/plugins/PluginAlone/CMakeLists.txt b/contrib/ActionRecognition/plugins/PluginAlone/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..1849e36805e0393229d577a8e729cff646788e7a --- /dev/null +++ b/contrib/ActionRecognition/plugins/PluginAlone/CMakeLists.txt @@ -0,0 +1,28 @@ +cmake_minimum_required(VERSION 3.5.2) +project(mxpi_pluginalone) +add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0 -Dgoogle=mindxsdk_private) +set(PLUGIN_NAME "mxpi_pluginalone") +set(TARGET_LIBRARY ${PLUGIN_NAME}) + +add_compile_options("-DPLUGIN_NAME=${PLUGIN_NAME}") +add_definitions(-DENABLE_DVPP_INTERFACE) + +add_compile_options(-std=c++11 -fPIC -fstack-protector-all -pie -Wno-deprecated-declarations) + +set(MX_SDK_HOME "$ENV{MX_SDK_HOME}") +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${MX_SDK_HOME}/lib/plugins/) + +include_directories(${MX_SDK_HOME}/include) +include_directories(${MX_SDK_HOME}/opensource/include) +include_directories(${MX_SDK_HOME}/opensource/include/gstreamer-1.0) +include_directories(${MX_SDK_HOME}/opensource/include/glib-2.0) +include_directories(${MX_SDK_HOME}/opensource/lib/glib-2.0/include) +link_directories(${MX_SDK_HOME}/lib) +link_directories(${MX_SDK_HOME}/opensource/lib) + +file(GLOB PLUGIN_SRC ./*.cpp) +message(${PLUGIN_SRC}) + +add_library(${TARGET_LIBRARY} SHARED ${PLUGIN_SRC}) +target_link_libraries(${TARGET_LIBRARY} glib-2.0 gstreamer-1.0 gobject-2.0 gstbase-1.0 gmodule-2.0) +target_link_libraries(${TARGET_LIBRARY} mxpidatatype plugintoolkit mxbase mindxsdk_protobuf) \ No newline at end of file diff --git a/contrib/ActionRecognition/plugins/PluginAlone/PluginAlone.cpp b/contrib/ActionRecognition/plugins/PluginAlone/PluginAlone.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ac46b9badfbe78571d4aa4526bf5d240ba2b16e5 --- /dev/null +++ b/contrib/ActionRecognition/plugins/PluginAlone/PluginAlone.cpp @@ -0,0 +1,238 @@ +/* +* Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#include "PluginAlone.h" +#include "MxBase/Log/Log.h" + +using namespace MxPlugins; +using namespace MxTools; +using namespace std; + +namespace { + const string SAMPLE_KEY = "MxpiObjectList"; +} +namespace MxPlugins { + APP_ERROR PluginAlone::Init(std::map > &configParamMap) { + LogInfo << "PluginAlone::Init start."; + APP_ERROR ret = APP_ERR_OK; + // Get the property values by key + // Data from track plugin + std::shared_ptr tracksourcePropSptr = + std::static_pointer_cast(configParamMap["dataSourceTrack"]); + tracksource_ = *tracksourcePropSptr.get(); + std::shared_ptr dataSourceDetection = + std::static_pointer_cast(configParamMap["dataSourceDetection"]); + detectionsource_ = *dataSourceDetection.get(); + //Description message + std::shared_ptr descriptionMessageProSptr = + std::static_pointer_cast(configParamMap["descriptionMessage"]); + descriptionMessage_ = *descriptionMessageProSptr.get(); + //Configuration parameter + confthres_ = *std::static_pointer_cast(configParamMap["detectThresh"]); + confratio_ = *std::static_pointer_cast(configParamMap["detectRatio"]); + confsleep_ = *std::static_pointer_cast(configParamMap["detectSleep"]); + + return APP_ERR_OK; + } + + APP_ERROR PluginAlone::DeInit() { + LogInfo << "PluginAlone::DeInit end."; + return APP_ERR_OK; + } + + APP_ERROR PluginAlone::SetMxpiErrorInfo(MxpiBuffer &buffer, const std::string pluginName, + const MxpiErrorInfo mxpiErrorInfo) { + APP_ERROR ret = APP_ERR_OK; + // Define an object of MxpiMetadataManager + MxpiMetadataManager mxpiMetadataManager(buffer); + ret = mxpiMetadataManager.AddErrorInfo(pluginName, mxpiErrorInfo); + if (ret != APP_ERR_OK) { + LogError << "Failed to AddErrorInfo."; + return ret; + } + ret = SendData(0, buffer); + return ret; + } + + APP_ERROR PluginAlone::Process(std::vector &mxpiBuffer) { + LogInfo << "PluginAlone::Process start"; + MxpiBuffer *buffer = mxpiBuffer[0]; + MxpiMetadataManager mxpiMetadataManager(*buffer); + MxpiErrorInfo mxpiErrorInfo; + ErrorInfo_.str(""); + auto errorInfoPtr = mxpiMetadataManager.GetErrorInfo(); + if (errorInfoPtr != nullptr) { + ErrorInfo_ << GetError(APP_ERR_COMM_FAILURE, pluginName_) << "PluginAlone process is not implemented"; + mxpiErrorInfo.ret = APP_ERR_COMM_FAILURE; + mxpiErrorInfo.errorInfo = ErrorInfo_.str(); + SetMxpiErrorInfo(*buffer, pluginName_, mxpiErrorInfo); + LogError << "PluginAlone process is not implemented"; + return APP_ERR_COMM_FAILURE; + } + + // Get the data from buffer + shared_ptr metadata = mxpiMetadataManager.GetMetadata(tracksource_); + shared_ptr Detect = mxpiMetadataManager.GetMetadata(detectionsource_); + shared_ptr att = mxpiMetadataManager.GetMetadata(("mxpi_plugincounter0")); + + if (metadata == nullptr || Detect == nullptr) { + ErrorInfo_ << GetError(APP_ERR_METADATA_IS_NULL, pluginName_) << "Metadata is NULL, failed"; + mxpiErrorInfo.ret = APP_ERR_METADATA_IS_NULL; + mxpiErrorInfo.errorInfo = ErrorInfo_.str(); + SetMxpiErrorInfo(*buffer, pluginName_, mxpiErrorInfo); + return APP_ERR_METADATA_IS_NULL; + } + // check whether the proto struct name is MxpiObjectList + google::protobuf::Message *msg = (google::protobuf::Message *) metadata.get(); + + // get trackletlist + std::shared_ptr mxpiAttribute = std::static_pointer_cast(att); + std::shared_ptr srcTrackLetListSptr = std::static_pointer_cast(metadata); + std::shared_ptr srcObjectListSptr = std::static_pointer_cast(Detect); + + // Data processing + if (sleeptime == 0) { + LogInfo << "PluginAlone start"; + uint32_t count = 0; + uint32_t thresh_ = (int) (confthres_ * confratio_); + // update data + for (uint32_t i = 0; i < (uint32_t) srcTrackLetListSptr->trackletvec_size(); i++) { + auto &trackObject = srcTrackLetListSptr->trackletvec(i); + if (trackObject.headervec_size() == 0) { + continue; + } + auto &detectObject = srcObjectListSptr->objectvec(trackObject.headervec(0).memberid()); + if (detectObject.classvec(0).classid() != 0) { + continue; + } + count++; + } + uint32_t result_present = 0; + uint32_t result_total = 0; + if (count == 1) { + result_present = 1; + } + int alarm = calculate(data_queue, confthres_, result_total, result_present, thresh_); + if (alarm == 1) { + LogInfo << "Alarm alone"; + alarm_count++; + LogInfo << "Alarmed " << alarm_count << " times"; + sleeptime = confsleep_; + data_queue.clear(); + // Send the data to downstream plugin + std::shared_ptr result = std::make_shared(); + result->set_attrname("Alarm alone"); + APP_ERROR ret = mxpiMetadataManager.AddProtoMetadata(pluginName_, result); + if (ret != APP_ERR_OK) { + ErrorInfo_ << GetError(ret, pluginName_) << "PluginAlone add metadata failed."; + mxpiErrorInfo.ret = ret; + mxpiErrorInfo.errorInfo = ErrorInfo_.str(); + SetMxpiErrorInfo(*buffer, pluginName_, mxpiErrorInfo); + return ret; + } + } + } else { + LogInfo << "PluginAlone start"; + LogInfo << "Alarmed in a short period of time"; + sleeptime--; + } + LogInfo << "PluginAlone end"; + // Send the data to downstream plugin + SendData(0, *buffer); + LogInfo << "PluginAlone::Process end"; + return APP_ERR_OK; + } + + int PluginAlone::calculate(std::vector &data_queue, int confthres_, uint32_t result_total, uint32_t result_present, uint32_t thresh_){ + int alarm = 0; + if (data_queue.size() == confthres_) { + for (int i = confthres_ - 1; i >= 0; i--) { + if (data_queue[i] == 1) { + result_total++; + } + if (i != confthres_ - 1) { + data_queue[i + 1] = data_queue[i]; + } + } + data_queue[0] = result_present; + if (result_total >= thresh_) { + alarm = 1; + } + } else { + data_queue.push_back(result_present); + } + return alarm; + } + + std::vector > PluginAlone::DefineProperties() { + // Define an A to store properties + std::vector > properties; + // Set the type and related information of the properties, and the key is the name + auto tracksourceProSptr = + std::make_shared < ElementProperty < string >> (ElementProperty < string > { + STRING, "dataSourceTrack", "detect", + "the name of previous plugin", "mxpi_motsimplesort0", + "NULL", "NULL"}); + + auto detectsourceProSptr = + std::make_shared < ElementProperty < string >> (ElementProperty < string > { + STRING, "dataSourceDetection", "name", + "the name of previous plugin", "mxpi_objectpostprocessor0", + "NULL", "NULL"}); + + auto threshProSptr = + std::make_shared < ElementProperty < uint32_t >> (ElementProperty < uint32_t > { + UINT, "detectThresh", "thresh", + "the number of frame when judging", + 100, 0, 1000}); + + auto ratioProSptr = + std::make_shared < ElementProperty < float >> (ElementProperty < float > { + FLOAT, "detectRatio", "ratio", + "the ratio of judging", + 0.8, 0.0, 1.0}); + + auto sleepProSptr = + std::make_shared < ElementProperty < uint32_t >> (ElementProperty < uint32_t > { + UINT, "detectSleep", "sleep", + "the time of stop detection", + 8, 0, 300}); + + auto descriptionMessageProSptr = + std::make_shared < ElementProperty < string >> (ElementProperty < string > { + STRING, "descriptionMessage", "message", "Description mesasge of plugin", + "This is PluginAlone", "NULL", "NULL"}); + + properties.push_back(tracksourceProSptr); + properties.push_back(detectsourceProSptr); + properties.push_back(threshProSptr); + properties.push_back(ratioProSptr); + properties.push_back(sleepProSptr); + properties.push_back(descriptionMessageProSptr); + return properties; + } + + // Register the Sample plugin through macro + MxpiPortInfo PluginAlone::DefineInputPorts() { + MxpiPortInfo inputPortInfo; + std::vector > value = {{"ANY"}}; + MxPluginBase::GenerateStaticInputPortsInfo(value, inputPortInfo); + return inputPortInfo; + }; +} + +namespace { + MX_PLUGIN_GENERATE(PluginAlone) +} diff --git a/contrib/ActionRecognition/plugins/PluginAlone/PluginAlone.h b/contrib/ActionRecognition/plugins/PluginAlone/PluginAlone.h new file mode 100644 index 0000000000000000000000000000000000000000..eae7eadd854c2f892ebe900229896d40361bbd4a --- /dev/null +++ b/contrib/ActionRecognition/plugins/PluginAlone/PluginAlone.h @@ -0,0 +1,100 @@ +/* +* Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef SDKMEMORY_PLUGINALONE_H +#define SDKMEMORY_PLUGINALONE_H + +#include "MxTools/PluginToolkit/base/MxPluginGenerator.h" +#include "MxTools/PluginToolkit/base/MxPluginBase.h" +#include "MxTools/PluginToolkit/metadata/MxpiMetadataManager.h" +#include "MxTools/Proto/MxpiDataType.pb.h" +#include "MxBase/ErrorCode/ErrorCode.h" + +/** +* @api +* @brief Definition of PluginAlone class. +*/ + +namespace MxPlugins { + class PluginAlone : public MxTools::MxPluginBase { + public: + /** + * @api + * @brief Initialize configure parameter. + * @param configParamMap + * @return APP_ERROR + */ + APP_ERROR Init(std::map > &configParamMap) override; + + /** + * * @api + * @brief DeInitialize configure parameter. + * @return APP_ERROR + */ + APP_ERROR DeInit() override; + + /** + * @api + * @brief Process the data of MxpiBuffer. + * @param mxpiBuffer + * @return APP_ERROR + */ + APP_ERROR Process(std::vector &mxpiBuffer) override; + + /** + * @api + * @brief Definition the parameter of configure properties. + * @return std::vector> + */ + static std::vector > DefineProperties(); + + /** + * @api + * @brief Get the number of class id and confidence from model inference. + * @param key + * @param buffer + * @return APP_ERROR + */ + static MxTools::MxpiPortInfo DefineInputPorts(); + /** + * @api + * @brief Define the input ports. + * @return MxpiPortInfo + */ + static int calculate(std::vector &data_queue, int confthres_, uint32_t result_total, uint32_t result_present, uint32_t thresh_); + /** + * @api + * @brief Define the input ports. + * @return MxpiPortInfo + */ + private: + APP_ERROR SetMxpiErrorInfo(MxTools::MxpiBuffer &buffer, + const std::string pluginName, + const MxTools::MxpiErrorInfo mxpiErrorInfo); + + std::string tracksource_; + std::string detectionsource_; + std::string descriptionMessage_; + std::ostringstream ErrorInfo_; + int confthres_; + float confratio_; + int confsleep_; + int sleeptime = 0; + std::vector data_queue; + int alarm_count = 0; + }; +} +#endif // SDKMEMORY_PLUGINALONE_H \ No newline at end of file diff --git a/contrib/ActionRecognition/plugins/PluginClimb/CMakeLists.txt b/contrib/ActionRecognition/plugins/PluginClimb/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..78d2ddb011eb565062c34f2665407c94f47fb1bc --- /dev/null +++ b/contrib/ActionRecognition/plugins/PluginClimb/CMakeLists.txt @@ -0,0 +1,33 @@ +cmake_minimum_required(VERSION 3.5.2) +project(mxpi_pluginclimb) +add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0 -Dgoogle=mindxsdk_private) +set(PLUGIN_NAME "mxpi_pluginclimb") +set(TARGET_LIBRARY ${PLUGIN_NAME}) + +add_compile_options("-DPLUGIN_NAME=${PLUGIN_NAME}") +add_definitions(-DENABLE_DVPP_INTERFACE) + +add_compile_options(-std=c++11 -fPIC -fstack-protector-all -pie -Wno-deprecated-declarations) + +set(MX_SDK_HOME "$ENV{MX_SDK_HOME}") +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${MX_SDK_HOME}/lib/plugins/) + +include_directories(${MX_SDK_HOME}/include) +include_directories(${MX_SDK_HOME}/opensource/include) +include_directories(${MX_SDK_HOME}/opensource/include/gstreamer-1.0) +include_directories(${MX_SDK_HOME}/opensource/include/glib-2.0) +include_directories(${MX_SDK_HOME}/opensource/lib/glib-2.0/include) +link_directories(${MX_SDK_HOME}/lib) +link_directories(${MX_SDK_HOME}/opensource/lib) +include_directories(${MX_SDK_HOME}/opensource/include/opencv4) + + +file(GLOB PLUGIN_SRC ./*.cpp) +message(${PLUGIN_SRC}) +message(${TARGET_LIBRARY}) + +add_library(${TARGET_LIBRARY} SHARED ${PLUGIN_SRC}) +target_link_libraries(${TARGET_LIBRARY} glib-2.0 gstreamer-1.0 gobject-2.0 gstbase-1.0 gmodule-2.0) +target_link_libraries(${TARGET_LIBRARY} mxpidatatype plugintoolkit mxbase mindxsdk_protobuf) +target_link_libraries(${TARGET_LIBRARY} mxpidatatype plugintoolkit mxbase mindxsdk_protobuf) +target_link_libraries(${TARGET_LIBRARY} opencv_world) \ No newline at end of file diff --git a/contrib/ActionRecognition/plugins/PluginClimb/PluginClimb.cpp b/contrib/ActionRecognition/plugins/PluginClimb/PluginClimb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a7065d84ba7e9419ca835153e1dde738b01e5f3e --- /dev/null +++ b/contrib/ActionRecognition/plugins/PluginClimb/PluginClimb.cpp @@ -0,0 +1,311 @@ +/* +* Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#include "PluginClimb.h" +#include "MxBase/Log/Log.h" +#include "math.h" +#include "MxTools/PluginToolkit/base/MxPluginBase.h" +#include + +using namespace MxPlugins; +using namespace MxTools; +using namespace std; + +namespace { + const string SAMPLE_KEY = "MxpiObjectList"; +} + +namespace MxPlugins { + APP_ERROR PluginClimb::Init(std::map> &configParamMap) { + LogInfo << "PluginClimb::Init start."; + APP_ERROR ret = APP_ERR_OK; + + // Get the property values by key + std::shared_ptr tracksourcePropSptr = + std::static_pointer_cast(configParamMap["dataSourceTrack"]); + tracksource_ = *tracksourcePropSptr.get(); + + std::shared_ptr dataSourceDetection = + std::static_pointer_cast(configParamMap["dataSourceDetection"]); + detectionsource_ = *dataSourceDetection.get(); + + std::shared_ptr descriptionMessageProSptr = + std::static_pointer_cast(configParamMap["descriptionMessage"]); + descriptionMessage_ = *descriptionMessageProSptr.get(); + + // Configuration parameter + highthresh_ = *std::static_pointer_cast(configParamMap["highThresh"]); + bufferlength_ = *std::static_pointer_cast(configParamMap["bufferLength"]); + ratio_ = *std::static_pointer_cast(configParamMap[" DetectRatio"]); + detectsleep_ = *std::static_pointer_cast(configParamMap["detectSleep"]); + filepath_ = *std::static_pointer_cast(configParamMap["filePath"]); + + return APP_ERR_OK; + } + + APP_ERROR PluginClimb::DeInit() { + LogInfo << "PluginClimb::DeInit end."; + return APP_ERR_OK; + } + + APP_ERROR PluginClimb::SetMxpiErrorInfo(MxpiBuffer &buffer, const std::string pluginName, + const MxpiErrorInfo mxpiErrorInfo) { + APP_ERROR ret = APP_ERR_OK; + // Define an object of MxpiMetadataManager + MxpiMetadataManager mxpiMetadataManager(buffer); + ret = mxpiMetadataManager.AddErrorInfo(pluginName, mxpiErrorInfo); + if (ret != APP_ERR_OK) { + LogError << "Failed to AddErrorInfo."; + return ret; + } + ret = SendData(0, buffer); + return ret; + } + + APP_ERROR PluginClimb::Process(std::vector &mxpiBuffer) { + LogInfo << "PluginClimb::Process start"; + + MxpiBuffer *buffer = mxpiBuffer[0]; + + MxpiMetadataManager mxpiMetadataManager(*buffer); + MxpiErrorInfo mxpiErrorInfo; + ErrorInfo_.str(""); + auto errorInfoPtr = mxpiMetadataManager.GetErrorInfo(); + + test_framenum++; + + if (errorInfoPtr != nullptr) { + ErrorInfo_ << GetError(APP_ERR_COMM_FAILURE, pluginName_) << "PluginClimb process is not implemented"; + mxpiErrorInfo.ret = APP_ERR_COMM_FAILURE; + mxpiErrorInfo.errorInfo = ErrorInfo_.str(); + SetMxpiErrorInfo(*buffer, pluginName_, mxpiErrorInfo); + LogError << "PluginClimb process is not implemented"; + return APP_ERR_COMM_FAILURE; + } + // Get the data from buffer + shared_ptr metadata = mxpiMetadataManager.GetMetadata(tracksource_); + shared_ptr Detect = mxpiMetadataManager.GetMetadata(detectionsource_); + + if (metadata == nullptr) { + ErrorInfo_ << GetError(APP_ERR_METADATA_IS_NULL, pluginName_) << "Metadata is NULL, failed"; + mxpiErrorInfo.ret = APP_ERR_METADATA_IS_NULL; + mxpiErrorInfo.errorInfo = ErrorInfo_.str(); + SetMxpiErrorInfo(*buffer, pluginName_, mxpiErrorInfo); + return APP_ERR_METADATA_IS_NULL; // self define the error code + } + // check whether the proto struct name is MxpiObjectList + google::protobuf::Message *msg = (google::protobuf::Message *) metadata.get(); + + // get trackletlist + std::shared_ptr srcTrackLetListSptr = std::static_pointer_cast(metadata); + std::shared_ptr srcObjectListSptr = std::static_pointer_cast(Detect); + + // update data + if (pathflag == 0) { + LogInfo << "highthresh" << highthresh_; + LogInfo << "bufferlength" << bufferlength_; + LogInfo << "detectsleep" << detectsleep_; + // log in the txt of roi + readTxt(filepath_, roi); + LogInfo << "*********************************"; + LogInfo << "log in txt"; + LogInfo << roi; + LogInfo << "*********************************"; + pathflag = 1; + } + if (sleeptime == 0) { + LogInfo << "PluginClimb start"; + for (uint32_t i = 0; i < (uint32_t) srcTrackLetListSptr->trackletvec_size(); i++) { + auto &trackObject = srcTrackLetListSptr->trackletvec(i); + if (trackObject.headervec_size() == 0) { + continue; + // As long as the person who has appeared will have a trackobject. + // Determine whether this id appears in the current frame. + } + auto &detectObject = srcObjectListSptr->objectvec(trackObject.headervec(0).memberid()); + if (detectObject.classvec(0).classid() != 0) { + continue; + } + int cnt_x = (detectObject.x0() + detectObject.x1()) / 2; // + int cnt_y = (detectObject.y0() + detectObject.y1()) / 2; + if (trackdata.count(trackObject.trackid()) == 0) { + int up_num = 0; + int in_num = 0; + if (cv::pointPolygonTest(roi, cv::Point(cnt_x, cnt_y), false) > 0) { + in_num += 1; + } + trackdata[trackObject.trackid()].push_back(cnt_x); // store cnt_x + trackdata[trackObject.trackid()].push_back(cnt_y); // store cnt_y + trackdata[trackObject.trackid()].push_back(up_num); // store up_num + trackdata[trackObject.trackid()].push_back(in_num); // store in_num + } else { + int up_num = trackdata[trackObject.trackid()][trackdata[trackObject.trackid()].size() - 2]; + int in_num = trackdata[trackObject.trackid()].back(); + if (cv::pointPolygonTest(roi, cv::Point(cnt_x, cnt_y), false) > 0) { + in_num = trackdata[trackObject.trackid()].back() + 1; + } // if the target is in the area, in_num ++ + if (cnt_y > trackdata[trackObject.trackid()][trackdata[trackObject.trackid()].size() - 3]) { + up_num = trackdata[trackObject.trackid()][trackdata[trackObject.trackid()].size() - 2] + 1; + } // if the target is rising, up_num ++ + trackdata[trackObject.trackid()].push_back(cnt_x); // store cnt_x + trackdata[trackObject.trackid()].push_back(cnt_y); // store cnt_y + trackdata[trackObject.trackid()].push_back(up_num); // store up_num + trackdata[trackObject.trackid()].push_back(in_num); // store in_num + if (trackdata[trackObject.trackid()].size() == 4 * bufferlength_) { + if (trackdata[trackObject.trackid()][4 * bufferlength_ - 2] > (int) (bufferlength_ * ratio_) + && + (trackdata[trackObject.trackid()][4 * bufferlength_ - 1] > (int) (bufferlength_ * ratio_)) + && trackdata[trackObject.trackid()][4 * bufferlength_ - 3] - + trackdata[trackObject.trackid()][1] > highthresh_) { + LogInfo << "Alarm Climb Up"; + alarm_count++; + LogInfo << "Alarmed " << alarm_count << " times"; + trackdata.clear(); + sleeptime = detectsleep_; + // Send the data to downstream plugin + std::shared_ptr result = std::make_shared(); + result->set_attrname("Alarm Climb"); + APP_ERROR ret = mxpiMetadataManager.AddProtoMetadata(pluginName_, result); + if (ret != APP_ERR_OK) { + ErrorInfo_ << GetError(ret, pluginName_) << "PluginClimb add metadata failed."; + mxpiErrorInfo.ret = ret; + mxpiErrorInfo.errorInfo = ErrorInfo_.str(); + SetMxpiErrorInfo(*buffer, pluginName_, mxpiErrorInfo); + return ret; + } + } else { + // Climbing up judgment failed, delete the last frame of the queue, continue to load data + std::vector tmp(trackdata[trackObject.trackid()].begin() + 4, + trackdata[trackObject.trackid()].end()); + trackdata[trackObject.trackid()] = tmp; + } + } + } + } + } else { + LogInfo << "PluginClimb start"; + LogInfo << "Alarmed in a short period of time"; + sleeptime--; + } + LogInfo << "PluginClimb end"; + + SendData(0, *buffer); + + LogInfo << "PluginClimb::Process end"; + return APP_ERR_OK; + } + + std::vector> PluginClimb::DefineProperties() { + // Define an A to store properties + std::vector> properties; + // Set the type and related information of the properties, and the key is the name + auto tracksourceProSptr = + std::make_shared>(ElementProperty{ + STRING, "dataSourceTrack", "name", "the name of previous plugin", "mxpi_motsimplesort0", "NULL", + "NULL"}); + + auto detectsourceProSptr = + std::make_shared>(ElementProperty{ + STRING, "dataSourceDetection", "name", "the name of previous plugin", "mxpi_fairmot0", + "NULL", "NULL"}); + + auto highthreshProSptr = // 高度差阈值 + std::make_shared>(ElementProperty{ + UINT, "highThresh", "name", "the name of previous plugin", 8, + 0, 1000}); + + auto bufferlengthProSptr = // 每个id需要的判断数据数量阈值 + std::make_shared>(ElementProperty{ + UINT, "bufferLength", "name", "the name of previous plugin", 10, + 8, 1000}); + + auto ratioProSptr = // 上升/在墙里 比例参数 + std::make_shared>(ElementProperty{ + FLOAT, "detectRatio", "name", "the name of previous plugin", 0.75, + 0, 1}); + + auto sleepProSptr = // 检测后休止时间 + std::make_shared>(ElementProperty{ + UINT, "detectSleep", "name", " ", 8, 0, + 100}); + + auto filepathProSptr = // 路径参数 + std::make_shared>(ElementProperty{ + STRING, "filePath", "name", "the name of previous plugin", "mxpi_fairmot0", + "NULL", "NULL"}); + + auto descriptionMessageProSptr = + std::make_shared>(ElementProperty{ + STRING, "descriptionMessage", "message", "Description mesasge of plugin", + "This is PluginClimb", "NULL", "NULL"}); + + properties.push_back(tracksourceProSptr); + properties.push_back(detectsourceProSptr); + properties.push_back(highthreshProSptr); + properties.push_back(bufferlengthProSptr); + properties.push_back(ratioProSptr); + properties.push_back(sleepProSptr); + properties.push_back(filepathProSptr); + properties.push_back(descriptionMessageProSptr); + return properties; + } + + // Register the Sample plugin through macro + MxpiPortInfo PluginClimb::DefineInputPorts() { + MxpiPortInfo inputPortInfo; + std::vector> value = {{"ANY"}}; + MxPluginBase::GenerateStaticInputPortsInfo(value, inputPortInfo); + return inputPortInfo; + }; + + MxpiPortInfo PluginClimb::DefineOutputPorts() { + MxpiPortInfo outputPortInfo; + std::vector> value = {{"ANY"}}; + MxPluginBase::GenerateStaticOutputPortsInfo(value, outputPortInfo); + return outputPortInfo; + } + // log in the txt of roi + + void PluginClimb::readTxt(string file, vector &roi) { + ifstream infile; + infile.open(file); + string str; + while (getline(infile, str)) { + int i = 0; + cv::Point p; + int count = 1; + while (i < str.length()) { + int j = i; + while (j < str.length() && str[j] != ';') { + j++; + } + string s = str.substr(i, j - i); + if (count == 1) { + p.x = atoi(s.c_str()); + count++; + } else if (count == 2) { + p.y = atoi(s.c_str()); + } + i = j + 1; + } + roi.push_back(p); + } + infile.close(); + } +} + +namespace { + MX_PLUGIN_GENERATE(PluginClimb) +} diff --git a/contrib/ActionRecognition/plugins/PluginClimb/PluginClimb.h b/contrib/ActionRecognition/plugins/PluginClimb/PluginClimb.h new file mode 100644 index 0000000000000000000000000000000000000000..24c8789d615329e46172cf59780a1d52c65d70cd --- /dev/null +++ b/contrib/ActionRecognition/plugins/PluginClimb/PluginClimb.h @@ -0,0 +1,107 @@ +/* +* Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#ifndef SDKMEMORY_PLUGINCLIMB_H +#define SDKMEMORY_PLUGINCLIMB_H + +#include +#include "MxTools/PluginToolkit/base/MxPluginGenerator.h" +#include "MxTools/PluginToolkit/base/MxPluginBase.h" +#include "MxTools/PluginToolkit/metadata/MxpiMetadataManager.h" +#include "MxTools/Proto/MxpiDataType.pb.h" +#include "MxBase/ErrorCode/ErrorCode.h" + +/** +* @api +* @brief Definition of PluginClimb class. +*/ + +namespace MxPlugins { + class PluginClimb : public MxTools::MxPluginBase { + public: + /** + * @api + * @brief Initialize configure parameter. + * @param configParamMap + * @return APP_ERROR + */ + APP_ERROR Init(std::map > &configParamMap) override; + + /** + * * @api + * @brief DeInitialize configure parameter. + * @return APP_ERROR + */ + APP_ERROR DeInit() override; + + /** + * @api + * @brief Process the data of MxpiBuffer. + * @param mxpiBuffer + * @return APP_ERROR + */ + APP_ERROR Process(std::vector &mxpiBuffer) override; + + /** + * @api + * @brief Definition the parameter of configure properties. + * @return std::vector> + */ + static std::vector > DefineProperties(); + + /** + * @api + * @brief Get the number of class id and confidence from model inference. + * @param key + * @param buffer + * @return APP_ERROR + */ + static MxTools::MxpiPortInfo DefineInputPorts(); + + static MxTools::MxpiPortInfo DefineOutputPorts(); + + static void readTxt(std::string file, std::vector &roi); + + static int distance(int x0, int y0, int x1, int y1); + + private: + APP_ERROR SetMxpiErrorInfo(MxTools::MxpiBuffer &buffer, + const std::string pluginName, + const MxTools::MxpiErrorInfo mxpiErrorInfo); + + std::string tracksource_; + std::string detectionsource_; + std::string descriptionMessage_; + std::ostringstream ErrorInfo_; + std::map> trackdata; + int confthresh_; + int confframes_; + int confsleep_; + int confhigh_; + int frame_num = 0; + int sleeptime = 0; + int pathflag = 0; + + int test_framenum = 0; + int highthresh_; + int bufferlength_; + float ratio_; + int detectsleep_; + int alarm_count = 0; + std::string filepath_; + std::vector roi; + }; +} +#endif \ No newline at end of file diff --git a/contrib/ActionRecognition/plugins/PluginCounter/CMakeLists.txt b/contrib/ActionRecognition/plugins/PluginCounter/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..b31f2122237d22f0ab4b1b0c2b45dffc525ea9f7 --- /dev/null +++ b/contrib/ActionRecognition/plugins/PluginCounter/CMakeLists.txt @@ -0,0 +1,28 @@ +cmake_minimum_required(VERSION 3.5.2) +project(mxpi_plugincounter) +add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0 -Dgoogle=mindxsdk_private) +set(PLUGIN_NAME "mxpi_plugincounter") +set(TARGET_LIBRARY ${PLUGIN_NAME}) + +add_compile_options("-DPLUGIN_NAME=${PLUGIN_NAME}") +add_definitions(-DENABLE_DVPP_INTERFACE) + +add_compile_options(-std=c++11 -fPIC -fstack-protector-all -pie -Wno-deprecated-declarations) + +set(MX_SDK_HOME "$ENV{MX_SDK_HOME}") +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${MX_SDK_HOME}/lib/plugins/) + +include_directories(${MX_SDK_HOME}/include) +include_directories(${MX_SDK_HOME}/opensource/include) +include_directories(${MX_SDK_HOME}/opensource/include/gstreamer-1.0) +include_directories(${MX_SDK_HOME}/opensource/include/glib-2.0) +include_directories(${MX_SDK_HOME}/opensource/lib/glib-2.0/include) +link_directories(${MX_SDK_HOME}/lib) +link_directories(${MX_SDK_HOME}/opensource/lib) + +file(GLOB PLUGIN_SRC ./*.cpp) +message(${PLUGIN_SRC}) + +add_library(${TARGET_LIBRARY} SHARED ${PLUGIN_SRC}) +target_link_libraries(${TARGET_LIBRARY} glib-2.0 gstreamer-1.0 gobject-2.0 gstbase-1.0 gmodule-2.0) +target_link_libraries(${TARGET_LIBRARY} mxpidatatype plugintoolkit mxbase mindxsdk_protobuf) \ No newline at end of file diff --git a/contrib/ActionRecognition/plugins/PluginCounter/PluginCounter.cpp b/contrib/ActionRecognition/plugins/PluginCounter/PluginCounter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..845bfd717d3a4fb163b7941e8372b6c1943cd802 --- /dev/null +++ b/contrib/ActionRecognition/plugins/PluginCounter/PluginCounter.cpp @@ -0,0 +1,160 @@ +/* +* Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#include "PluginCounter.h" +#include "MxBase/Log/Log.h" +#include "math.h" +#include "MxTools/Proto/MxpiDumpData.pb.h" +#include "MxTools/Proto/MxpiDataType.pb.h" +#include "MxTools/PluginToolkit/base/MxPluginBase.h" + +using namespace MxPlugins; +using namespace MxTools; +using namespace std; +//extern struct MxpiPortInfo; + +namespace { + const string SAMPLE_KEY = "MxpiObjectList"; +} + +namespace MxPlugins { + + APP_ERROR PluginCounter::Init(std::map > &configParamMap) { + + LogInfo << "PluginCounter::Init start."; + APP_ERROR ret = APP_ERR_OK; + + // Get the property values by key + std::shared_ptr tracksourcePropSptr = + std::static_pointer_cast(configParamMap["dataSourceTrack"]); + tracksource_ = *tracksourcePropSptr.get(); + + std::shared_ptr descriptionMessageProSptr = + std::static_pointer_cast(configParamMap["descriptionMessage"]); + descriptionMessage_ = *descriptionMessageProSptr.get(); + + return APP_ERR_OK; + } + + APP_ERROR PluginCounter::DeInit() { + LogInfo << "PluginCounter::DeInit end."; + return APP_ERR_OK; + } + + APP_ERROR PluginCounter::SetMxpiErrorInfo(MxpiBuffer &buffer, const std::string pluginName, + const MxpiErrorInfo mxpiErrorInfo) { + APP_ERROR ret = APP_ERR_OK; + // Define an object of MxpiMetadataManager + MxpiMetadataManager mxpiMetadataManager(buffer); + ret = mxpiMetadataManager.AddErrorInfo(pluginName, mxpiErrorInfo); + if (ret != APP_ERR_OK) { + LogError << "Failed to AddErrorInfo."; + return ret; + } + ret = SendData(0, buffer); + return ret; + } + + APP_ERROR PluginCounter::Process(std::vector &mxpiBuffer) { + + LogInfo << "PluginCounter::Process start"; + MxpiBuffer *buffer = mxpiBuffer[0]; + + MxpiMetadataManager mxpiMetadataManager(*buffer); + MxpiErrorInfo mxpiErrorInfo; + ErrorInfo_.str(""); + auto errorInfoPtr = mxpiMetadataManager.GetErrorInfo(); + + frame++; + LogInfo << "当前帧号 :" << frame; + if (errorInfoPtr != nullptr) { + ErrorInfo_ << GetError(APP_ERR_COMM_FAILURE, pluginName_) << "PluginCounter process is not implemented"; + mxpiErrorInfo.ret = APP_ERR_COMM_FAILURE; + mxpiErrorInfo.errorInfo = ErrorInfo_.str(); + SetMxpiErrorInfo(*buffer, pluginName_, mxpiErrorInfo); + LogError << "PluginCounter process is not implemented"; + return APP_ERR_COMM_FAILURE; + } + // Get the data from buffer + shared_ptr metadata = mxpiMetadataManager.GetMetadata(tracksource_); + + if (metadata == nullptr) { + ErrorInfo_ << GetError(APP_ERR_METADATA_IS_NULL, pluginName_) << "Metadata is NULL, failed"; + mxpiErrorInfo.ret = APP_ERR_METADATA_IS_NULL; + mxpiErrorInfo.errorInfo = ErrorInfo_.str(); + SetMxpiErrorInfo(*buffer, pluginName_, mxpiErrorInfo); + return APP_ERR_METADATA_IS_NULL; // self define the error code + } + // check whether the proto struct name is MxpiObjectList + google::protobuf::Message *msg = (google::protobuf::Message *) metadata.get(); + + // get trackletlist + std::shared_ptr srcTrackLetListSptr = std::static_pointer_cast(metadata); + // Send the data to downstream plugin + std::shared_ptr result = std::make_shared(); + result->set_attrname("PluginCounter"); + APP_ERROR ret = mxpiMetadataManager.AddProtoMetadata(pluginName_, result); + + if (ret != APP_ERR_OK) { + ErrorInfo_ << GetError(ret, pluginName_) << "PluginCounter add metadata failed."; + mxpiErrorInfo.ret = ret; + mxpiErrorInfo.errorInfo = ErrorInfo_.str(); + SetMxpiErrorInfo(*buffer, pluginName_, mxpiErrorInfo); + return ret; + } + // Send the data to downstream plugin + SendData(0, *buffer); + + LogInfo << "PluginCounter::Process end"; + return APP_ERR_OK; + } + + std::vector > PluginCounter::DefineProperties() { + // Define an A to store properties + std::vector > properties; + // Set the type and related information of the properties, and the key is the name + auto tracksourceProSptr = + std::make_shared < ElementProperty < string >> (ElementProperty < string > { + STRING, "dataSourceTrack", "name", "the name of previous plugin", "mxpi_motsimplesort0", "NULL", + "NULL"}); + + auto descriptionMessageProSptr = + std::make_shared < ElementProperty < string >> (ElementProperty < string > { + STRING, "descriptionMessage", "message", "Description mesasge of plugin", + "This is PluginCounter", "NULL", "NULL"}); + + properties.push_back(tracksourceProSptr); + properties.push_back(descriptionMessageProSptr); + return properties; + } + // Register the Sample plugin through macro + + MxpiPortInfo PluginCounter::DefineInputPorts() { + MxpiPortInfo inputPortInfo; + std::vector > value = {{"ANY"}}; + MxPluginBase::GenerateStaticInputPortsInfo(value, inputPortInfo); + return inputPortInfo; + }; + + MxpiPortInfo PluginCounter::DefineOutputPorts() { + MxpiPortInfo outputPortInfo; + std::vector > value = {{"ANY"}}; + MxPluginBase::GenerateStaticOutputPortsInfo(value, outputPortInfo); + return outputPortInfo; + } +} +namespace { + MX_PLUGIN_GENERATE(PluginCounter) +} diff --git a/contrib/ActionRecognition/plugins/PluginCounter/PluginCounter.h b/contrib/ActionRecognition/plugins/PluginCounter/PluginCounter.h new file mode 100644 index 0000000000000000000000000000000000000000..e27ec9d37f9f6216678cefa06c04f325bf892d0d --- /dev/null +++ b/contrib/ActionRecognition/plugins/PluginCounter/PluginCounter.h @@ -0,0 +1,94 @@ +/* +* Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#ifndef SDKMEMORY_PluginCounter_H +#define SDKMEMORY_PluginCounter_H + +#include "MxTools/PluginToolkit/base/MxPluginGenerator.h" +#include "MxTools/PluginToolkit/base/MxPluginBase.h" +#include "MxTools/PluginToolkit/metadata/MxpiMetadataManager.h" +#include "MxTools/Proto/MxpiDataType.pb.h" +#include "MxBase/ErrorCode/ErrorCode.h" +/** +* @api +* @brief Definition of PluginCounter class. +*/ +namespace MxPlugins { + class PluginCounter : public MxTools::MxPluginBase { + public: + /** + * @api + * @brief Initialize configure parameter. + * @param configParamMap + * @return APP_ERROR + */ + APP_ERROR Init(std::map > &configParamMap) override; + + /** + * * @api + * @brief DeInitialize configure parameter. + * @return APP_ERROR + */ + APP_ERROR DeInit() override; + + /** + * @api + * @brief Process the data of MxpiBuffer. + * @param mxpiBuffer + * @return APP_ERROR + */ + APP_ERROR Process(std::vector &mxpiBuffer) override; + + /** + * @api + * @brief Definition the parameter of configure properties. + * @return std::vector> + */ + static std::vector > DefineProperties(); + + /** + * @api + * @brief Get the number of class id and confidence from model inference. + * @param key + * @param buffer + * @return APP_ERROR + */ + static MxTools::MxpiPortInfo DefineInputPorts(); + + /** + * @api + * @brief Define the input ports. + * @return MxpiPortInfo + */ + static MxTools::MxpiPortInfo DefineOutputPorts(); + /** + * @api + * @brief Define the output ports. + * @return MxpiPortInfo + */ + private: + APP_ERROR SetMxpiErrorInfo(MxTools::MxpiBuffer &buffer, + const std::string pluginName, + const MxTools::MxpiErrorInfo mxpiErrorInfo); + + std::string tracksource_; + std::string detectionsource_; + std::string descriptionMessage_; + std::ostringstream ErrorInfo_; + std::map> trackdata; + int frame = 0; + }; +} +#endif //SDKMEMORY_PluginCounter_H \ No newline at end of file diff --git a/contrib/ActionRecognition/plugins/PluginOutOfBed/CMakeLists.txt b/contrib/ActionRecognition/plugins/PluginOutOfBed/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..d8880b70329269c68470c41962c427c27e110f1e --- /dev/null +++ b/contrib/ActionRecognition/plugins/PluginOutOfBed/CMakeLists.txt @@ -0,0 +1,34 @@ + +cmake_minimum_required(VERSION 3.5.2) +project(mxpi_pluginoutofbed) +add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0 -Dgoogle=mindxsdk_private) +set(PLUGIN_NAME "mxpi_pluginoutofbed") +set(TARGET_LIBRARY ${PLUGIN_NAME}) + +add_compile_options("-DPLUGIN_NAME=${PLUGIN_NAME}") +add_definitions(-DENABLE_DVPP_INTERFACE) + +add_compile_options(-std=c++11 -fPIC -fstack-protector-all -pie -Wno-deprecated-declarations) + +set(MX_SDK_HOME "$ENV{MX_SDK_HOME}") +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${MX_SDK_HOME}/lib/plugins/) + +include_directories(${MX_SDK_HOME}/include) +include_directories(${MX_SDK_HOME}/opensource/include) +include_directories(${MX_SDK_HOME}/opensource/include/gstreamer-1.0) +include_directories(${MX_SDK_HOME}/opensource/include/glib-2.0) +include_directories(${MX_SDK_HOME}/opensource/lib/glib-2.0/include) +link_directories(${MX_SDK_HOME}/lib) +link_directories(${MX_SDK_HOME}/opensource/lib) +include_directories(${MX_SDK_HOME}/opensource/include/opencv4) + + +file(GLOB PLUGIN_SRC ./*.cpp) +message(${PLUGIN_SRC}) +message(${TARGET_LIBRARY}) + +add_library(${TARGET_LIBRARY} SHARED ${PLUGIN_SRC}) +target_link_libraries(${TARGET_LIBRARY} glib-2.0 gstreamer-1.0 gobject-2.0 gstbase-1.0 gmodule-2.0) +target_link_libraries(${TARGET_LIBRARY} mxpidatatype plugintoolkit mxbase mindxsdk_protobuf) +target_link_libraries(${TARGET_LIBRARY} mxpidatatype plugintoolkit mxbase mindxsdk_protobuf) +target_link_libraries(${TARGET_LIBRARY} opencv_world) \ No newline at end of file diff --git a/contrib/ActionRecognition/plugins/PluginOutOfBed/PluginOutOfBed.cpp b/contrib/ActionRecognition/plugins/PluginOutOfBed/PluginOutOfBed.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a97bebd1d7df44f4a2f5b9d3ca0f5870768be2ff --- /dev/null +++ b/contrib/ActionRecognition/plugins/PluginOutOfBed/PluginOutOfBed.cpp @@ -0,0 +1,272 @@ +/* + * Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "PluginOutOfBed.h" +#include "MxBase/Log/Log.h" +#include + +using namespace MxPlugins; +using namespace MxTools; +using namespace std; +namespace { + const string SAMPLE_KEY = "MxpiObjectList"; +} +namespace MxPlugins { + APP_ERROR PluginOutOfBed::Init(std::map> &configParamMap) { + LogInfo << "PluginOutOfBed::Init start."; + APP_ERROR ret = APP_ERR_OK; + // Get the property values by key + // Data from track plugin + std::shared_ptr tracksourcePropSptr = + std::static_pointer_cast(configParamMap["dataSourceTrack"]); + tracksource_ = *tracksourcePropSptr.get(); + // Data from detect plugin + std::shared_ptr detectsourcePropSptr = + std::static_pointer_cast(configParamMap["dataSourceDetect"]); + detectionsource_ = *detectsourcePropSptr.get(); + //Description message + std::shared_ptr descriptionMessageProSptr = + std::static_pointer_cast(configParamMap["descriptionMessage"]); + descriptionMessage_ = *descriptionMessageProSptr.get(); + //Configuration parameter + confthres_ = *std::static_pointer_cast(configParamMap["detectThresh"]); + confratio_ = *std::static_pointer_cast(configParamMap["detectRatio"]); + confsleep_ = *std::static_pointer_cast(configParamMap["detectSleep"]); + configpath = *std::static_pointer_cast(configParamMap["configPath"]); + return APP_ERR_OK; + } + + APP_ERROR PluginOutOfBed::DeInit() { + LogInfo << "PluginOutOfBed::DeInit end."; + return APP_ERR_OK; + } + + APP_ERROR PluginOutOfBed::SetMxpiErrorInfo(MxpiBuffer &buffer, const std::string pluginName, + const MxpiErrorInfo mxpiErrorInfo) { + APP_ERROR ret = APP_ERR_OK; + // Define an object of MxpiMetadataManager + MxpiMetadataManager mxpiMetadataManager(buffer); + ret = mxpiMetadataManager.AddErrorInfo(pluginName, mxpiErrorInfo); + if (ret != APP_ERR_OK) { + LogError << "Failed to AddErrorInfo."; + return ret; + } + ret = SendData(0, buffer); + return ret; + } + + APP_ERROR PluginOutOfBed::Process(std::vector &mxpiBuffer) { + MxpiBuffer *buffer = mxpiBuffer[0]; + MxpiMetadataManager mxpiMetadataManager(*buffer); + MxpiErrorInfo mxpiErrorInfo; + ErrorInfo_.str(""); + auto errorInfoPtr = mxpiMetadataManager.GetErrorInfo(); + framesnum++; + if (errorInfoPtr != nullptr) { + ErrorInfo_ << GetError(APP_ERR_COMM_FAILURE, pluginName_) << "PluginOutOfBed process is not implemented"; + mxpiErrorInfo.ret = APP_ERR_COMM_FAILURE; + mxpiErrorInfo.errorInfo = ErrorInfo_.str(); + SetMxpiErrorInfo(*buffer, pluginName_, mxpiErrorInfo); + LogError << "FairMOT_Climb process is not implemented"; + return APP_ERR_COMM_FAILURE; + } + // Get the data from buffer + shared_ptr metadata = mxpiMetadataManager.GetMetadata(tracksource_); + shared_ptr Detect = mxpiMetadataManager.GetMetadata(detectionsource_); + if (metadata == nullptr) { + ErrorInfo_ << GetError(APP_ERR_METADATA_IS_NULL, pluginName_) << "Metadata is NULL, failed"; + mxpiErrorInfo.ret = APP_ERR_METADATA_IS_NULL; + mxpiErrorInfo.errorInfo = ErrorInfo_.str(); + SetMxpiErrorInfo(*buffer, pluginName_, mxpiErrorInfo); + return APP_ERR_METADATA_IS_NULL; // self define the error code + } + // check whether the proto struct name is MxpiObjectList + google::protobuf::Message *msg = (google::protobuf::Message *) metadata.get(); + // get trackletlist + std::shared_ptr srcTrackLetListSptr = std::static_pointer_cast(metadata); + std::shared_ptr srcObjectListSptr = std::static_pointer_cast(Detect); + if (sleeptime == 0) { + LogInfo << "PluginOutOfBed start"; + cv::Point2f center; + //read txt and get location of bed + if (pathflag) { + readTxt(configpath, bed); + pathflag = false; + } + for (uint32_t i = 0; i < (uint32_t) srcTrackLetListSptr->trackletvec_size(); i++) { + auto &trackObject = srcTrackLetListSptr->trackletvec(i); + if (trackObject.headervec_size() == 0) { + continue; + } + auto &detectObject = srcObjectListSptr->objectvec(trackObject.headervec(0).memberid()); + if (detectObject.classvec(0).classid() != 0) { + continue; + } + if (trackdata.count(trackObject.trackid()) == 0) { + center.x = (detectObject.x0() + detectObject.x1()) / 2; + center.y = (detectObject.y0() + detectObject.y1()) / 2; + trackdata[trackObject.trackid()].push_back(cv::pointPolygonTest(bed, center, false) > 0); + } else { + center.x = (detectObject.x0() + detectObject.x1()) / 2; + center.y = (detectObject.y0() + detectObject.y1()) / 2; + trackdata[trackObject.trackid()].push_back(cv::pointPolygonTest(bed, center, false) > 0); + if (trackdata[trackObject.trackid()].size() >= confthres_) { + if (OutOfBed(trackdata[trackObject.trackid()])) { + LogInfo << "Alarm OutOfBed"; + alarmcount++; + LogInfo << "Alarmed" << alarmcount << "times"; + sleeptime = confsleep_; + frames = 0; + trackdata.clear(); + break; + } else { + trackdata[trackObject.trackid()].erase(trackdata[trackObject.trackid()].begin()); + } + } + } + } + frames++; + } else { + LogInfo << "PluginOutOfBed start"; + LogInfo << "Alarmed in a short period of time"; + sleeptime--; + } + LogInfo << "frame:" << framesnum; + LogInfo << "PluginOutOfBed end"; + APP_ERROR ret = mxpiMetadataManager.AddProtoMetadata(pluginName_, + static_pointer_cast(srcTrackLetListSptr)); + if (ret != APP_ERR_OK) { + ErrorInfo_ << GetError(ret, pluginName_) << "PluginOutOfBed add metadata failed."; + mxpiErrorInfo.ret = ret; + mxpiErrorInfo.errorInfo = ErrorInfo_.str(); + SetMxpiErrorInfo(*buffer, pluginName_, mxpiErrorInfo); + return ret; + } + // Send the data to downstream plugin + MxTools::MxPluginBase::SendData(0, *buffer); + return APP_ERR_OK; + } + + std::vector> PluginOutOfBed::DefineProperties() { + // Define an A to store properties + std::vector> properties; + // Set the type and related information of the properties, and the key is the name + auto tracksourceProSptr = + std::make_shared> + (ElementProperty{ + STRING, "dataSourceTrack", "name", "the name of previous plugin", + "mxpi_motsimplesort0", "NULL", + "NULL"}); + auto detectsourceProSptr = + std::make_shared> + (ElementProperty{ + STRING, "dataSourceDetect", "name", "the name of previous plugin", + "mxpi_motsimplesort0", "NULL", + "NULL"}); + auto threshProSptr = + std::make_shared> + (ElementProperty{ + UINT, "detectThresh", "name", "123", 8, 0, + 100}); + auto ratioProSptr = + std::make_shared> + (ElementProperty{ + FLOAT, "detectRatio", "name", " ", 1.0, 0.0, + 1.0}); + auto sleepProSptr = + std::make_shared> + (ElementProperty{ + UINT, "detectSleep", "name", " ", 8, 0, + 100}); + auto descriptionMessageProSptr = + std::make_shared> + (ElementProperty{ + STRING, "descriptionMessage", "message", "Description mesasge of plugin", + "This is FairMOT_Alone", "NULL", "NULL"}); + auto configPathSptr = + std::make_shared> + (ElementProperty{ + STRING, "configPath", "message", "Description mesasge of plugin", + "This is FairMOT_Alone", "NULL", "NULL"}); + properties.push_back(tracksourceProSptr); + properties.push_back(detectsourceProSptr); + properties.push_back(threshProSptr); + properties.push_back(ratioProSptr); + properties.push_back(sleepProSptr); + properties.push_back(configPathSptr); + properties.push_back(descriptionMessageProSptr); + return properties; + } + + MxpiPortInfo PluginOutOfBed::DefineInputPorts() { + MxpiPortInfo inputPortInfo; + std::vector> value = {{"ANY"}}; + MxPluginBase::GenerateStaticInputPortsInfo(value, inputPortInfo); + return inputPortInfo; + } + + //Process the txt and get the location of the bed + void PluginOutOfBed::readTxt(std::string file, std::vector &roi) { + ifstream infile; + infile.open(file); + string str; + while (getline(infile, str)) { + int i = 0; + cv::Point p; + int count = 1; + while (i < str.length()) { + int j = i; + while (j < str.length() && str[j] != ';') { + j++; + } + string s = str.substr(i, j - i); + if (count == 1) { + p.x = atoi(s.c_str()); + count++; + } else if (count == 2) { + p.y = atoi(s.c_str()); + } + i = j + 1; + } + roi.push_back(p); + } + infile.close(); + } + + // out of bed + bool PluginOutOfBed::OutOfBed(std::vector queue) { + int length = queue.size(); + int count_H = 0, count_T = 0; + for (int i = 0; i < length / 2; i++) { + if (queue[i]) { + count_H++; + } + } + for (int j = length - 1; j >= length / 2; j--) { + if (!queue[j]) { + count_T++; + } + } + + if (count_H > length * confratio_ && count_T > length * confratio_) { + return 1; + } + return 0; + } +} +namespace { + MX_PLUGIN_GENERATE(PluginOutOfBed) +} diff --git a/contrib/ActionRecognition/plugins/PluginOutOfBed/PluginOutOfBed.h b/contrib/ActionRecognition/plugins/PluginOutOfBed/PluginOutOfBed.h new file mode 100644 index 0000000000000000000000000000000000000000..42493b0e360ae92466d3b2839fdb9a3c296a7957 --- /dev/null +++ b/contrib/ActionRecognition/plugins/PluginOutOfBed/PluginOutOfBed.h @@ -0,0 +1,111 @@ +/* + * Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MXPIFAIRMOT_FAIRMOT_OUTOFBED_H +#define MXPIFAIRMOT_FAIRMOT_OUTOFBED_H + +#include +#include "MxTools/PluginToolkit/base/MxPluginGenerator.h" +#include "MxTools/PluginToolkit/base/MxPluginBase.h" +#include "MxTools/PluginToolkit/metadata/MxpiMetadataManager.h" +#include "MxTools/Proto/MxpiDataType.pb.h" +#include "MxBase/ErrorCode/ErrorCode.h" +#include +#include +/** +* @api +* @brief Definition of PluginOutOfBed class. +*/ +namespace MxPlugins { + class PluginOutOfBed : public MxTools::MxPluginBase { + public: + /** + * @api + * @brief Initialize configure parameter. + * @param configParamMap + * @return APP_ERROR + */ + APP_ERROR Init(std::map> &configParamMap) override; + + /** + * * @api + * @brief DeInitialize configure parameter. + * @return APP_ERROR + */ + APP_ERROR DeInit() override; + + /** + * @api + * @brief Process the data of MxpiBuffer. + * @param mxpiBuffer + * @return APP_ERROR + */ + APP_ERROR Process(std::vector &mxpiBuffer) override; + + /** + * @api + * @brief Definition the parameter of configure properties. + * @return std::vector> + */ + static std::vector> DefineProperties(); + + /** + * @api + * @brief Get the number of class id and confidence from model inference. + * @param key + * @param buffer + * @return APP_ERROR + */ + static MxTools::MxpiPortInfo DefineInputPorts(); + + /** + * @api + * @brief read ROI + * @param ROI file path + */ + void readTxt(std::string file, std::vector &roi); + + /** + * @api + * @brief Out of bed detection. + * @param queue + * @return bool + */ + bool OutOfBed(std::vector queue); + + private: + APP_ERROR SetMxpiErrorInfo(MxTools::MxpiBuffer &buffer, const std::string pluginName, + const MxTools::MxpiErrorInfo mxpiErrorInfo); + + std::string tracksource_; + std::string detectionsource_; + std::string descriptionMessage_; + std::string configpath; + std::ostringstream ErrorInfo_; + uint confthres_; + float confratio_; + uint confsleep_; + uint sleeptime = 0; + std::map> trackdata; + std::vector bed; + uint frames; + uint framesnum = 0; + bool pathflag = true; + uint alarmcount = 0; + }; + +} +#endif //MXPIFAIRMOT_FAIRMOT_OUTOFBED_H diff --git a/contrib/ActionRecognition/plugins/PluginOverSpeed/CMakeLists.txt b/contrib/ActionRecognition/plugins/PluginOverSpeed/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..fbe215e5e7386004864c740a75fe0884c2b6279a --- /dev/null +++ b/contrib/ActionRecognition/plugins/PluginOverSpeed/CMakeLists.txt @@ -0,0 +1,28 @@ +cmake_minimum_required(VERSION 3.5.2) +project(mxpi_pluginoverspeed) +add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0 -Dgoogle=mindxsdk_private) +set(PLUGIN_NAME "mxpi_pluginoverspeed") +set(TARGET_LIBRARY ${PLUGIN_NAME}) + +add_compile_options("-DPLUGIN_NAME=${PLUGIN_NAME}") +add_definitions(-DENABLE_DVPP_INTERFACE) + +add_compile_options(-std=c++11 -fPIC -fstack-protector-all -pie -Wno-deprecated-declarations) + +set(MX_SDK_HOME "$ENV{MX_SDK_HOME}") +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${MX_SDK_HOME}/lib/plugins/) + +include_directories(${MX_SDK_HOME}/include) +include_directories(${MX_SDK_HOME}/opensource/include) +include_directories(${MX_SDK_HOME}/opensource/include/gstreamer-1.0) +include_directories(${MX_SDK_HOME}/opensource/include/glib-2.0) +include_directories(${MX_SDK_HOME}/opensource/lib/glib-2.0/include) +link_directories(${MX_SDK_HOME}/lib) +link_directories(${MX_SDK_HOME}/opensource/lib) + +file(GLOB PLUGIN_SRC ./*.cpp) +message(${PLUGIN_SRC}) + +add_library(${TARGET_LIBRARY} SHARED ${PLUGIN_SRC}) +target_link_libraries(${TARGET_LIBRARY} glib-2.0 gstreamer-1.0 gobject-2.0 gstbase-1.0 gmodule-2.0) +target_link_libraries(${TARGET_LIBRARY} mxpidatatype plugintoolkit mxbase mindxsdk_protobuf) \ No newline at end of file diff --git a/contrib/ActionRecognition/plugins/PluginOverSpeed/PluginOverSpeed.cpp b/contrib/ActionRecognition/plugins/PluginOverSpeed/PluginOverSpeed.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8798f55a03bd2b779f45f86709779a6e3fe5b24a --- /dev/null +++ b/contrib/ActionRecognition/plugins/PluginOverSpeed/PluginOverSpeed.cpp @@ -0,0 +1,249 @@ +/* +* Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#include "PluginOverSpeed.h" +#include "MxBase/Log/Log.h" +#include "math.h" + +using namespace MxPlugins; +using namespace MxTools; +using namespace MxPlugins; + +namespace { + const string SAMPLE_KEY = "MxpiObjectList"; +} + +namespace MxPlugins { + APP_ERROR PluginOverSpeed::Init(std::map> &configParamMap) { + LogInfo << "PluginOverSpeed::Init start."; + APP_ERROR ret = APP_ERR_OK; + + // Get the property values by key + std::shared_ptr tracksourcePropSptr = + std::static_pointer_cast(configParamMap["dataSourceTrack"]); + tracksource_ = *tracksourcePropSptr.get(); + + std::shared_ptr dataSourceDetection = + std::static_pointer_cast(configParamMap["dataSourceDetection"]); + detectionsource_ = *dataSourceDetection.get(); + + std::shared_ptr descriptionMessageProSptr = + std::static_pointer_cast(configParamMap["descriptionMessage"]); + descriptionMessage_ = *descriptionMessageProSptr.get(); + + //Configuration parameter + confthresh_ = *std::static_pointer_cast(configParamMap["speedthresh"]); + confframes_ = *std::static_pointer_cast(configParamMap["frames"]); + confsleep_ = *std::static_pointer_cast(configParamMap["detectsleep"]); + + return APP_ERR_OK; + } + + APP_ERROR PluginOverSpeed::DeInit() { + LogInfo << "PluginOverSpeed::DeInit end."; + return APP_ERR_OK; + } + + APP_ERROR PluginOverSpeed::SetMxpiErrorInfo(MxpiBuffer &buffer, const std::string pluginName, + const MxpiErrorInfo mxpiErrorInfo) { + APP_ERROR ret = APP_ERR_OK; + // Define an object of MxpiMetadataManager + MxpiMetadataManager mxpiMetadataManager(buffer); + ret = mxpiMetadataManager.AddErrorInfo(pluginName, mxpiErrorInfo); + if (ret != APP_ERR_OK) { + LogError << "Failed to AddErrorInfo."; + return ret; + } + ret = SendData(0, buffer); + return ret; + } + + APP_ERROR PluginOverSpeed::Process(std::vector &mxpiBuffer) { + LogInfo << "PluginOverSpeed::Process start"; + MxpiBuffer *buffer = mxpiBuffer[0]; + + MxpiMetadataManager mxpiMetadataManager(*buffer); + MxpiErrorInfo mxpiErrorInfo; + ErrorInfo_.str(""); + auto errorInfoPtr = mxpiMetadataManager.GetErrorInfo(); + + frame++; + if (errorInfoPtr != nullptr) { + ErrorInfo_ << GetError(APP_ERR_COMM_FAILURE, pluginName_) << "PluginOverSpeed process is not implemented"; + mxpiErrorInfo.ret = APP_ERR_COMM_FAILURE; + mxpiErrorInfo.errorInfo = ErrorInfo_.str(); + SetMxpiErrorInfo(*buffer, pluginName_, mxpiErrorInfo); + LogError << "PluginOverSpeed process is not implemented"; + return APP_ERR_COMM_FAILURE; + } + // Get the data from buffer + shared_ptr metadata = mxpiMetadataManager.GetMetadata(tracksource_); + shared_ptr Detect = mxpiMetadataManager.GetMetadata(detectionsource_); + + if (metadata == nullptr || Detect == nullptr) { + ErrorInfo_ << GetError(APP_ERR_METADATA_IS_NULL, pluginName_) << "Metadata is NULL, failed"; + mxpiErrorInfo.ret = APP_ERR_METADATA_IS_NULL; + mxpiErrorInfo.errorInfo = ErrorInfo_.str(); + SetMxpiErrorInfo(*buffer, pluginName_, mxpiErrorInfo); + return APP_ERR_METADATA_IS_NULL; // self define the error code + } + // check whether the proto struct name is MxpiObjectList + google::protobuf::Message *msg = (google::protobuf::Message *) metadata.get(); + + // get trackletlist + std::shared_ptr srcTrackLetListSptr = std::static_pointer_cast(metadata); + std::shared_ptr srcObjectListSptr = std::static_pointer_cast(Detect); + + //data processing + if (sleeptime == 0) { + LogInfo << "PluginOverSpeed start"; + //update data + for (uint32_t i = 0; i < (uint32_t) srcTrackLetListSptr->trackletvec_size(); i++) { + auto &trackObject = srcTrackLetListSptr->trackletvec(i); + if (trackObject.headervec_size() == 0) { + continue; + } + auto &detectObject = srcObjectListSptr->objectvec(trackObject.headervec(0).memberid()); + if (detectObject.classvec(0).classid() != 0) { + continue; + } + if (trackdata.count(trackObject.trackid()) == 0) { + trackdata[trackObject.trackid()].push_back((int) ((detectObject.x0() + detectObject.x1()) / 2)); + trackdata[trackObject.trackid()].push_back((int) ((detectObject.y0() + detectObject.y1()) / 2)); + trackdata[trackObject.trackid()][2] = 0; + trackdata[trackObject.trackid()][3] = 0; + } else { + trackdata[trackObject.trackid()][2] += 1; + int x0 = trackdata[trackObject.trackid()][0]; + int y0 = trackdata[trackObject.trackid()][1]; + int x1 = (int) ((detectObject.x0() + detectObject.x1()) / 2); + int y1 = (int) ((detectObject.y0() + detectObject.y1()) / 2); + trackdata[trackObject.trackid()][3] += distance(x0, y0, x1, y1); + trackdata[trackObject.trackid()][0] = (int) ((detectObject.x0() + detectObject.x1()) / 2); + trackdata[trackObject.trackid()][1] = (int) ((detectObject.y0() + detectObject.y1()) / 2); + } + } + frame_num++; + + //calculate data + if (frame_num == confframes_) { + frame_num = 0; +// map>::iterator iter; + for (auto iter = trackdata.begin(); iter != trackdata.end(); iter++) { + if (trackdata[iter->first][2] == 0 || trackdata[iter->first][2] < 4) { + continue; + } + if (((int) (trackdata[iter->first][3] / trackdata[iter->first][2])) > confthresh_) { + LogInfo << "Alarm OverSpeed"; + alarm_count++; + LogInfo << "Alarmed " << alarm_count << " times"; + sleeptime = confsleep_; + + std::shared_ptr result = std::make_shared(); + result->set_attrname("Alarm OverSpeed"); + APP_ERROR ret = mxpiMetadataManager.AddProtoMetadata(pluginName_, result); + if (ret != APP_ERR_OK) { + ErrorInfo_ << GetError(ret, pluginName_) << "PluginOverSpeed add metadata failed."; + mxpiErrorInfo.ret = ret; + mxpiErrorInfo.errorInfo = ErrorInfo_.str(); + SetMxpiErrorInfo(*buffer, pluginName_, mxpiErrorInfo); + return ret; + } + break; + } + } + trackdata.clear(); + } + } else { + LogInfo << "PluginOverSpeed start"; + LogInfo << "Alarmed in a short period of time"; + sleeptime--; + } + // LogInfo << "frame : " << frame; + LogInfo << "PluginOverSpeed end"; + + // Send the data to downstream plugin + SendData(0, *buffer); + + LogInfo << "PluginOverSpeed::Process end"; + return APP_ERR_OK; + } + + std::vector> PluginOverSpeed::DefineProperties() { + // Define an A to store properties + std::vector> properties; + // Set the type and related information of the properties, and the key is the name + auto tracksourceProSptr = + std::make_shared>(ElementProperty{ + STRING, "dataSourceTrack", "name", "the source of tack data", "mxpi_motsimplesort0", + "NULL", "NULL"}); + + auto detectsourceProSptr = + std::make_shared>(ElementProperty{ + STRING, "dataSourceDetection", "name", "the source of detect data", "mxpi_objectpostprocessor0", + "NULL", "NULL"}); + + auto threshProSptr = + std::make_shared>(ElementProperty{ + UINT, "speedthresh", "thresh", "the thresh of overspeed", 10, + 1, 1000}); + + auto frameProSptr = + std::make_shared>(ElementProperty{ + UINT, "frames", "frame", "the number of frame when judging", 8, + 8, 100}); + + auto sleepProSptr = + std::make_shared>(ElementProperty{ + UINT, "detectsleep", "sleep", "the time of stop detection", 8, 0, + 300}); + + auto descriptionMessageProSptr = + std::make_shared>(ElementProperty{ + STRING, "descriptionMessage", "message", "Description mesasge of plugin", + "This is PluginOverSpeed", "NULL", "NULL"}); + + properties.push_back(tracksourceProSptr); + properties.push_back(detectsourceProSptr); + properties.push_back(threshProSptr); + properties.push_back(frameProSptr); + properties.push_back(sleepProSptr); + properties.push_back(descriptionMessageProSptr); + return properties; + } + // Register the Sample plugin through macro + + MxpiPortInfo PluginOverSpeed::DefineInputPorts() { + MxpiPortInfo inputPortInfo; + std::vector> value = {{"ANY"}}; + MxPluginBase::GenerateStaticInputPortsInfo(value, inputPortInfo); + return inputPortInfo; + }; + + MxpiPortInfo PluginOverSpeed::DefineOutputPorts() { + MxpiPortInfo outputPortInfo; + std::vector> value = {{"ANY"}}; + MxPluginBase::GenerateStaticOutputPortsInfo(value, outputPortInfo); + return outputPortInfo; + } + + int PluginOverSpeed::distance(int x0, int y0, int x1, int y1) { + int distance = int(sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0))); + return distance; + } +} +namespace { + MX_PLUGIN_GENERATE(PluginOverSpeed) +} diff --git a/contrib/ActionRecognition/plugins/PluginOverSpeed/PluginOverSpeed.h b/contrib/ActionRecognition/plugins/PluginOverSpeed/PluginOverSpeed.h new file mode 100644 index 0000000000000000000000000000000000000000..1f6af4da391842d923e05e949cfed6ab9a06d928 --- /dev/null +++ b/contrib/ActionRecognition/plugins/PluginOverSpeed/PluginOverSpeed.h @@ -0,0 +1,108 @@ +/* +* Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#ifndef SDKMEMORY_PluginOverSpeed_H +#define SDKMEMORY_PluginOverSpeed_H + +#include "MxTools/PluginToolkit/base/MxPluginGenerator.h" +#include "MxTools/PluginToolkit/base/MxPluginBase.h" +#include "MxTools/PluginToolkit/metadata/MxpiMetadataManager.h" +#include "MxTools/Proto/MxpiDataType.pb.h" +#include "MxBase/ErrorCode/ErrorCode.h" +/** +* @api +* @brief Definition of PluginOverSpeed class. +*/ + +namespace MxPlugins { + class PluginOverSpeed : public MxTools::MxPluginBase { + public: + /** + * @api + * @brief Initialize configure parameter. + * @param configParamMap + * @return APP_ERROR + */ + APP_ERROR Init(std::map > &configParamMap) override; + + /** + * * @api + * @brief DeInitialize configure parameter. + * @return APP_ERROR + */ + APP_ERROR DeInit() override; + + /** + * @api + * @brief Process the data of MxpiBuffer. + * @param mxpiBuffer + * @return APP_ERROR + */ + APP_ERROR Process(std::vector &mxpiBuffer) override; + + /** + * @api + * @brief Definition the parameter of configure properties. + * @return std::vector> + */ + static std::vector > DefineProperties(); + + /** + * @api + * @brief Get the number of class id and confidence from model inference. + * @param key + * @param buffer + * @return APP_ERROR + */ + static MxTools::MxpiPortInfo DefineInputPorts(); + + /** + * @api + * @brief Define the input ports. + * @return MxpiPortInfo + */ + static MxTools::MxpiPortInfo DefineOutputPorts(); + + /** + * @api + * @brief Define the output ports. + * @return MxpiPortInfo + */ + static int distance(int x0, int y0, int x1, int y1); + /** + * @api + * @brief Calculate the distance between two points. + * @param int + * @return int + */ + private: + APP_ERROR SetMxpiErrorInfo(MxTools::MxpiBuffer &buffer, const std::string pluginName, + const MxTools::MxpiErrorInfo mxpiErrorInfo); + + std::string tracksource_; + std::string detectionsource_; + std::string descriptionMessage_; + std::ostringstream ErrorInfo_; + std::map> trackdata; + int confthresh_; + int confframes_; + int confsleep_; + int frame_num = 0; + int sleeptime = 0; + int frame = 0; + int alarm_count = 0; + }; +} +#endif //SDKMEMORY_PluginOverSpeed_H \ No newline at end of file diff --git a/contrib/ActionRecognition/plugins/PluginOverStay/CMakeLists.txt b/contrib/ActionRecognition/plugins/PluginOverStay/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..c6f0906a8d5d94aa187c2699e915c372b771fcfe --- /dev/null +++ b/contrib/ActionRecognition/plugins/PluginOverStay/CMakeLists.txt @@ -0,0 +1,28 @@ +cmake_minimum_required(VERSION 3.5.2) +project(mxpi_pluginoverstay) +add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0 -Dgoogle=mindxsdk_private) +set(PLUGIN_NAME "mxpi_pluginoverstay") +set(TARGET_LIBRARY ${PLUGIN_NAME}) + +add_compile_options("-DPLUGIN_NAME=${PLUGIN_NAME}") +add_definitions(-DENABLE_DVPP_INTERFACE) + +add_compile_options(-std=c++11 -fPIC -fstack-protector-all -pie -Wno-deprecated-declarations) + +set(MX_SDK_HOME "$ENV{MX_SDK_HOME}") +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${MX_SDK_HOME}/lib/plugins/) + +include_directories(${MX_SDK_HOME}/include) +include_directories(${MX_SDK_HOME}/opensource/include) +include_directories(${MX_SDK_HOME}/opensource/include/gstreamer-1.0) +include_directories(${MX_SDK_HOME}/opensource/include/glib-2.0) +include_directories(${MX_SDK_HOME}/opensource/lib/glib-2.0/include) +link_directories(${MX_SDK_HOME}/lib) +link_directories(${MX_SDK_HOME}/opensource/lib) + +file(GLOB PLUGIN_SRC ./*.cpp) +message(${PLUGIN_SRC}) + +add_library(${TARGET_LIBRARY} SHARED ${PLUGIN_SRC}) +target_link_libraries(${TARGET_LIBRARY} glib-2.0 gstreamer-1.0 gobject-2.0 gstbase-1.0 gmodule-2.0) +target_link_libraries(${TARGET_LIBRARY} mxpidatatype plugintoolkit mxbase mindxsdk_protobuf) \ No newline at end of file diff --git a/contrib/ActionRecognition/plugins/PluginOverStay/PluginOverStay.cpp b/contrib/ActionRecognition/plugins/PluginOverStay/PluginOverStay.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ac5ba722b836cf7599f5003af11d747075c18f56 --- /dev/null +++ b/contrib/ActionRecognition/plugins/PluginOverStay/PluginOverStay.cpp @@ -0,0 +1,267 @@ +/* +* Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#include "PluginOverStay.h" +#include "MxBase/Log/Log.h" +#include "math.h" +#include "MxTools/PluginToolkit/base/MxPluginBase.h" + +using namespace MxPlugins; +using namespace MxTools; +using namespace std; +//extern struct MxpiPortInfo; + +namespace { + const string SAMPLE_KEY = "MxpiObjectList"; +} + +namespace MxPlugins { + + APP_ERROR PluginOverStay::Init(std::map > &configParamMap) { + LogInfo << "PluginOverStay::Init start."; + APP_ERROR ret = APP_ERR_OK; + + // Get the property values by key + std::shared_ptr tracksourcePropSptr = + std::static_pointer_cast(configParamMap["dataSourceTrack"]); + tracksource_ = *tracksourcePropSptr.get(); + + std::shared_ptr dataSourceDetection = + std::static_pointer_cast(configParamMap["dataSourceDetection"]); + detectionsource_ = *dataSourceDetection.get(); + + std::shared_ptr descriptionMessageProSptr = + std::static_pointer_cast(configParamMap["descriptionMessage"]); + descriptionMessage_ = *descriptionMessageProSptr.get(); + + //Configuration parameter + confthresh_ = *std::static_pointer_cast(configParamMap["staythresh"]); + confframes_ = *std::static_pointer_cast(configParamMap["frames"]); + confsleep_ = *std::static_pointer_cast(configParamMap["detectsleep"]); + confdistance_ = *std::static_pointer_cast(configParamMap["distancethresh"]); + confratio_ = *std::static_pointer_cast(configParamMap["ratio"]); + + return APP_ERR_OK; + } + + APP_ERROR PluginOverStay::DeInit() { + LogInfo << "PluginOverStay::DeInit end."; + return APP_ERR_OK; + } + + APP_ERROR PluginOverStay::SetMxpiErrorInfo(MxpiBuffer &buffer, const std::string pluginName, + const MxpiErrorInfo mxpiErrorInfo) { + APP_ERROR ret = APP_ERR_OK; + // Define an object of MxpiMetadataManager + MxpiMetadataManager mxpiMetadataManager(buffer); + ret = mxpiMetadataManager.AddErrorInfo(pluginName, mxpiErrorInfo); + if (ret != APP_ERR_OK) { + LogError << "Failed to AddErrorInfo."; + return ret; + } + ret = SendData(0, buffer); + return ret; + } + + APP_ERROR PluginOverStay::Process(std::vector &mxpiBuffer) { + + LogInfo << "PluginOverStay::Process start"; + MxpiBuffer *buffer = mxpiBuffer[0]; + + MxpiMetadataManager mxpiMetadataManager(*buffer); + MxpiErrorInfo mxpiErrorInfo; + ErrorInfo_.str(""); + auto errorInfoPtr = mxpiMetadataManager.GetErrorInfo(); + + frame++; + if (errorInfoPtr != nullptr) { + ErrorInfo_ << GetError(APP_ERR_COMM_FAILURE, pluginName_) << "PluginOverStay process is not implemented"; + mxpiErrorInfo.ret = APP_ERR_COMM_FAILURE; + mxpiErrorInfo.errorInfo = ErrorInfo_.str(); + SetMxpiErrorInfo(*buffer, pluginName_, mxpiErrorInfo); + LogError << "PluginOverStay process is not implemented"; + return APP_ERR_COMM_FAILURE; + } + // Get the data from buffer + shared_ptr metadata = mxpiMetadataManager.GetMetadata(tracksource_); + shared_ptr Detect = mxpiMetadataManager.GetMetadata(detectionsource_); + + if (metadata == nullptr) { + ErrorInfo_ << GetError(APP_ERR_METADATA_IS_NULL, pluginName_) << "Metadata is NULL, failed"; + mxpiErrorInfo.ret = APP_ERR_METADATA_IS_NULL; + mxpiErrorInfo.errorInfo = ErrorInfo_.str(); + SetMxpiErrorInfo(*buffer, pluginName_, mxpiErrorInfo); + return APP_ERR_METADATA_IS_NULL; // self define the error code + } + // check whether the proto struct name is MxpiObjectList + google::protobuf::Message *msg = (google::protobuf::Message *) metadata.get(); + + // get trackletlist + std::shared_ptr srcTrackLetListSptr = std::static_pointer_cast(metadata); + std::shared_ptr srcObjectListSptr = std::static_pointer_cast(Detect); + + //data processing + if (sleeptime == 0) { + LogInfo << "PluginOverStay start"; + //update data + for (uint32_t i = 0; i < (uint32_t) srcTrackLetListSptr->trackletvec_size(); i++) { + auto &trackObject = srcTrackLetListSptr->trackletvec(i); + if (trackObject.headervec_size() == 0) { + continue; + } + auto &detectObject = srcObjectListSptr->objectvec(trackObject.headervec(0).memberid()); + if (detectObject.classvec(0).classid() != 0) { + continue; + } + if (trackdata.count(trackObject.trackid()) == 0) { + + trackdata[trackObject.trackid()].push_back((int) ((detectObject.x0() + detectObject.x1()) / 2)); + trackdata[trackObject.trackid()].push_back((int) ((detectObject.y0() + detectObject.y1()) / 2)); + trackdata[trackObject.trackid()][2] = 1; + } else { + + int dis = distance(trackdata[trackObject.trackid()][0], + trackdata[trackObject.trackid()][1], + (int) ((detectObject.x0() + detectObject.x1()) / 2), + (int) ((detectObject.y0() + detectObject.y1()) / 2)); + if (dis < confdistance_) { + trackdata[trackObject.trackid()][2]++; + } else trackdata[trackObject.trackid()][2] = 1; + trackdata[trackObject.trackid()][0] = (int) ((detectObject.x0() + detectObject.x1()) / 2); + trackdata[trackObject.trackid()][1] = (int) ((detectObject.y0() + detectObject.y1()) / 2); + } + } + frame_num++; + + //calculate data + if (frame_num == confframes_) { + frame_num = 0; + map < int, vector < int >> ::iterator + iter; + for (iter = trackdata.begin(); iter != trackdata.end(); iter++) { + if (trackdata[iter->first][2] >= (int) (confratio_ * confthresh_)) { + LogInfo << "Alarm Overstay"; + alarm_count++; + LogInfo << "Alarmed " << alarm_count << " times"; + sleeptime = confsleep_; + + std::shared_ptr result = std::make_shared(); + result->set_attrname("Alarm Overstay"); + APP_ERROR ret = mxpiMetadataManager.AddProtoMetadata(pluginName_, result); + if (ret != APP_ERR_OK) { + ErrorInfo_ << GetError(ret, pluginName_) << "PluginOverStay add metadata failed."; + mxpiErrorInfo.ret = ret; + mxpiErrorInfo.errorInfo = ErrorInfo_.str(); + SetMxpiErrorInfo(*buffer, pluginName_, mxpiErrorInfo); + return ret; + } + + break; + } + } + trackdata.clear(); + } + } else { + LogInfo << "PluginOverStay start"; + LogInfo << "Alarmed in a short period of time"; + sleeptime--; + } +// LogInfo << "frame : " << frame; + LogInfo << "PluginOverStay end"; + + // Send the data to downstream plugin + SendData(0, *buffer); + + LogInfo << "PluginOverStay::Process end"; + return APP_ERR_OK; + } + + std::vector > PluginOverStay::DefineProperties() { + // Define an A to store properties + std::vector > properties; + // Set the type and related information of the properties, and the key is the name + auto tracksourceProSptr = + std::make_shared < ElementProperty < string >> (ElementProperty < string > { + STRING, "dataSourceTrack", "name", "the name of previous plugin", "mxpi_motsimplesort0", "NULL", + "NULL"}); + + auto detectsourceProSptr = + std::make_shared < ElementProperty < string >> (ElementProperty < string > { + STRING, "dataSourceDetection", "name", "the name of previous plugin", "mxpi_fairmot0", + "NULL", "NULL"}); + + auto threshProSptr = //逗留时间阈值 + std::make_shared < ElementProperty < int >> (ElementProperty < int > { + UINT, "staythresh", "name", "the name of previous plugin", 10, + 10, 1000}); + + auto distanceProSptr = //移动距离阈值 + std::make_shared < ElementProperty < int >> (ElementProperty < int > { + UINT, "distancethresh", "name", "the name of previous plugin", 6, + 0, 100}); + + auto frameProSptr = //多少帧检测一次 + std::make_shared < ElementProperty < int >> (ElementProperty < int > { + UINT, "frames", "name", "the name of previous plugin", 8, + 8, 100}); + + auto sleepProSptr = //检测后休止时间 + std::make_shared < ElementProperty < int >> (ElementProperty < int > { + UINT, "detectsleep", "name", " ", 8, 0, + 100}); + + auto ratioProSptr = //检出比例 + std::make_shared < ElementProperty < float >> (ElementProperty < float > { + FLOAT, "ratio", "name", " ", 0.8, 0.1, + 1.0}); + + auto descriptionMessageProSptr = + std::make_shared < ElementProperty < string >> (ElementProperty < string > { + STRING, "descriptionMessage", "message", "Description mesasge of plugin", + "This is PluginOverStay", "NULL", "NULL"}); + + properties.push_back(tracksourceProSptr); + properties.push_back(detectsourceProSptr); + properties.push_back(threshProSptr); + properties.push_back(frameProSptr); + properties.push_back(sleepProSptr); + properties.push_back(distanceProSptr); + properties.push_back(ratioProSptr); + properties.push_back(descriptionMessageProSptr); + return properties; + } + + MxpiPortInfo PluginOverStay::DefineInputPorts() { + MxpiPortInfo inputPortInfo; + std::vector > value = {{"ANY"}}; + MxPluginBase::GenerateStaticInputPortsInfo(value, inputPortInfo); + return inputPortInfo; + }; + + MxpiPortInfo PluginOverStay::DefineOutputPorts() { + MxpiPortInfo outputPortInfo; + std::vector > value = {{"ANY"}}; + MxPluginBase::GenerateStaticOutputPortsInfo(value, outputPortInfo); + return outputPortInfo; + } + + int PluginOverStay::distance(int x0, int y0, int x1, int y1) { + int distance = int(sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0))); + return distance; + } +} +namespace { + MX_PLUGIN_GENERATE(PluginOverStay) +} diff --git a/contrib/ActionRecognition/plugins/PluginOverStay/PluginOverStay.h b/contrib/ActionRecognition/plugins/PluginOverStay/PluginOverStay.h new file mode 100644 index 0000000000000000000000000000000000000000..1fc4f06a603a2df88dd271d0cbbbc9b0b3b32f82 --- /dev/null +++ b/contrib/ActionRecognition/plugins/PluginOverStay/PluginOverStay.h @@ -0,0 +1,109 @@ +/* +* Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#ifndef SDKMEMORY_PluginOverStay_H +#define SDKMEMORY_PluginOverStay_H + +#include "MxTools/PluginToolkit/base/MxPluginGenerator.h" +#include "MxTools/PluginToolkit/base/MxPluginBase.h" +#include "MxTools/PluginToolkit/metadata/MxpiMetadataManager.h" +#include "MxTools/Proto/MxpiDataType.pb.h" +#include "MxBase/ErrorCode/ErrorCode.h" +/** +* @api +* @brief Definition of PluginOverStay class. +*/ +namespace MxPlugins { + class PluginOverStay : public MxTools::MxPluginBase { + public: + /** + * @api + * @brief Initialize configure parameter. + * @param configParamMap + * @return APP_ERROR + */ + APP_ERROR Init(std::map > &configParamMap) override; + + /** + * * @api + * @brief DeInitialize configure parameter. + * @return APP_ERROR + */ + APP_ERROR DeInit() override; + + /** + * @api + * @brief Process the data of MxpiBuffer. + * @param mxpiBuffer + * @return APP_ERROR + */ + APP_ERROR Process(std::vector &mxpiBuffer) override; + + /** + * @api + * @brief Definition the parameter of configure properties. + * @return std::vector> + */ + static std::vector > DefineProperties(); + + /** + * @api + * @brief Get the number of class id and confidence from model inference. + * @param key + * @param buffer + * @return APP_ERROR + */ + static MxTools::MxpiPortInfo DefineInputPorts(); + + /** + * @api + * @brief Define the input ports. + * @return MxpiPortInfo + */ + static MxTools::MxpiPortInfo DefineOutputPorts(); + + /** + * @api + * @brief Define the output ports. + * @return MxpiPortInfo + */ + static int distance(int x0, int y0, int x1, int y1); + /** + * @api + * @brief Calculate the distance between two points. + * @param int + * @return int + */ + private: + APP_ERROR SetMxpiErrorInfo(MxTools::MxpiBuffer &buffer, const std::string pluginName, + const MxTools::MxpiErrorInfo mxpiErrorInfo); + + std::string tracksource_; + std::string detectionsource_; + std::string descriptionMessage_; + std::ostringstream ErrorInfo_; + std::map> trackdata; + int confthresh_; + int confframes_; + int confsleep_; + int confdistance_; + float confratio_; + int frame_num = 0; + int sleeptime = 0; + int alarm_count = 0; + int frame = 0; + }; +} +#endif //SDKMEMORY_PluginOverStay_H \ No newline at end of file diff --git a/contrib/ActionRecognition/plugins/PluginViolentAction/CMakeLists.txt b/contrib/ActionRecognition/plugins/PluginViolentAction/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..7c6074a588b9a0a7a9ff7eaa099dd388551b8db8 --- /dev/null +++ b/contrib/ActionRecognition/plugins/PluginViolentAction/CMakeLists.txt @@ -0,0 +1,44 @@ +cmake_minimum_required(VERSION 3.10) +project(Plugin_ViolentAction) + +set(CMAKE_CXX_STANDARD 11) + +set(PLUGIN_NAME "mxpi_violentaction") +set(TARGET_LIBRARY ${PLUGIN_NAME}) + +add_compile_options(-fPIC -fstack-protector-all -g -Wl,-z,relro,-z,now,-z -pie -Wall) +add_compile_options(-std=c++11 -Wno-deprecated-declarations) +add_compile_options("-DPLUGIN_NAME=${PLUGIN_NAME}") + +add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0 -Dgoogle=mindxsdk_private) +add_definitions(-DENABLE_DVPP_INTERFACE) + + +set(MX_SDK_HOME "$ENV{MX_SDK_HOME}") +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${MX_SDK_HOME}/lib/plugins/) + +include_directories(${MX_SDK_HOME}/include) +include_directories(${MX_SDK_HOME}/opensource/include) +include_directories(${MX_SDK_HOME}/opensource/include/gstreamer-1.0) +include_directories(${MX_SDK_HOME}/opensource/include/opencv4) +include_directories(${MX_SDK_HOME}/opensource/include/glib-2.0) +include_directories(${MX_SDK_HOME}/opensource/lib/glib-2.0/include) + + +link_directories(${MX_SDK_HOME}/lib) +link_directories(${MX_SDK_HOME}/opensource/lib) + + +add_library(${TARGET_LIBRARY} SHARED PluginViolentAction.cpp PluginViolentAction.h) +target_link_libraries(${TARGET_LIBRARY} + mxpidatatype + plugintoolkit + mxbase + streammanager + mindxsdk_protobuf + glib-2.0 + gstreamer-1.0 + gobject-2.0 + gstbase-1.0 + gmodule-2.0 + ) diff --git a/contrib/ActionRecognition/plugins/PluginViolentAction/PluginViolentAction.cpp b/contrib/ActionRecognition/plugins/PluginViolentAction/PluginViolentAction.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c15dd84377d368543c1261131d6d34e29b93c0b --- /dev/null +++ b/contrib/ActionRecognition/plugins/PluginViolentAction/PluginViolentAction.cpp @@ -0,0 +1,195 @@ +/* + * Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "PluginViolentAction.h" +#include "MxBase/Log/Log.h" +#include + +using namespace MxPlugins; +using namespace MxTools; +using namespace MxBase; +using namespace std; + +namespace { + const std::string SAMPLE_KEY = "MxpiClassList"; +} + +APP_ERROR PluginViolentAction::Init(std::map> &configParamMap) { + LogInfo << "Begin to initialize PluginViolentAction(" << pluginName_ << ")."; + // Get the property values by key + std::shared_ptr classSource = std::static_pointer_cast(configParamMap["classSource"]); + classSource_ = *classSource; + + std::shared_ptr filePath = std::static_pointer_cast(configParamMap["filePath"]); + filePath_ = *filePath; + + std::shared_ptr detectSleep = std::static_pointer_cast(configParamMap["detectSleep"]); + detectSleep_ = *detectSleep; + LogInfo << "End to initialize PluginViolentAction(" << pluginName_ << ")."; + return APP_ERR_OK; +} + +APP_ERROR PluginViolentAction::DeInit() { + LogInfo << "Begin to deinitialize PluginViolentAction(" << pluginName_ << ")."; + LogInfo << "End to deinitialize PluginViolentAction(" << pluginName_ << ")."; + return APP_ERR_OK; +} + +APP_ERROR PluginViolentAction::CheckDataSource(MxTools::MxpiMetadataManager &mxpiMetadataManager) { + if (mxpiMetadataManager.GetMetadata(classSource_) == nullptr) { + LogDebug << GetError(APP_ERR_METADATA_IS_NULL, pluginName_) << "class metadata is null. please check" + << "Your property classSource(" << classSource_ << ")."; + return APP_ERR_METADATA_IS_NULL; + } + return APP_ERR_OK; +} + +std::shared_ptr +PluginViolentAction::ActionMatch(std::shared_ptr &mxpiClassList) { + // set MxpiAttributeList + MxpiClass mxpiClass = mxpiClassList->classvec(0); + int32_t classId = mxpiClass.classid(); + std::string className = mxpiClass.classname(); + float confidence = mxpiClass.confidence(); + std::shared_ptr mxpiAttributeList = std::make_shared(); + MxpiAttribute *mxpiAttribute = mxpiAttributeList->add_attributevec(); + mxpiAttribute->set_confidence(confidence); + mxpiAttribute->set_attrid(classId); + mxpiAttribute->set_attrname(className); + if (sleepTime_ == 0) { + auto iterator = find(aoi.begin(), aoi.end(), className); + if (iterator != aoi.end()) { + // mark the times of alarms for statistics + alarm_count ++; + sleepTime_ = detectSleep_; + alarmInformation = "Alarm Violent Action"; + mxpiAttribute->set_attrvalue(alarmInformation); + return mxpiAttributeList; + } else { + // no interested action + alarmInformation = "No Alarm"; + mxpiAttribute->set_attrvalue(alarmInformation); + return mxpiAttributeList; + } + } else { + // alarm sleep + alarmInformation = "Alarmed in a short period of time"; + mxpiAttribute->set_attrvalue(alarmInformation); + sleepTime_--; + return mxpiAttributeList; + } +} + +void PluginViolentAction::ReadTxt(std::string file, std::vector &aoi) { + // read txt file + std::ifstream infile; + infile.open(file.data()); + assert(infile.is_open()); + std::string str; + while (getline(infile, str)) { + // Remove the ending symbol + str.erase(str.end()-1); + aoi.emplace_back(str); + } + infile.close(); +} + +APP_ERROR PluginViolentAction::Process(std::vector &mxpiBuffer) { + LogInfo << "Begin to process PluginViolentAction(" << elementName_ << ")."; + // Get MxpiClassList from MxpiBuffer + MxpiBuffer *inputMxpiBuffer = mxpiBuffer[0]; + MxpiMetadataManager mxpiMetadataManager(*inputMxpiBuffer); + ErrorInfo_.str(""); + // check data source + APP_ERROR ret = CheckDataSource(mxpiMetadataManager); + if (ret != APP_ERR_OK) { + SendData(0, *inputMxpiBuffer); + return ret; + } + // Get metadata by key + std::shared_ptr class_metadata = mxpiMetadataManager.GetMetadata(classSource_); + std::shared_ptr srcClassListPtr = std::static_pointer_cast(class_metadata); + // update data; Read the action of interest file + // set pathflag to make file IO only once + if (pathflag == 0) { + ReadTxt(filePath_, aoi); + pathflag = 1; + } + // Match the upstream action to the action of interest and alarm. + auto attributeListPtr = ActionMatch(srcClassListPtr); + ret = mxpiMetadataManager.AddProtoMetadata(pluginName_, static_pointer_cast(attributeListPtr)); + if (ret != APP_ERR_OK) { + LogError << ErrorInfo_.str(); + SendMxpiErrorInfo(*inputMxpiBuffer, pluginName_, ret, ErrorInfo_.str()); + SendData(0, *inputMxpiBuffer); + } + // Send the data to downstream plugin + SendData(0, *inputMxpiBuffer); + LogInfo << "End to process PluginViolentAction(" << elementName_ << ")."; + return APP_ERR_OK; +} + +std::vector> PluginViolentAction::DefineProperties() { + std::vector> properties; + // Get the action category from previous plugin + auto classsource = std::make_shared>(ElementProperty{ + STRING, + "classSource", + "labelSource", + "Recognized Action Class", + "default", "NULL", "NULL" + }); + // The action of interest file path + auto filepath = std::make_shared>(ElementProperty{ + STRING, + "filePath", + "Action of interest file path", + "the path of predefined violent action classes file", + "NULL", "NULL", "NULL" + }); + // sleep time after alarm + auto detectsleep = std::make_shared>(ElementProperty{ + UINT, + "detectSleep", + "process sleep time", + "sleep some time to avoid frequent alarms ", + 8, 0, 100 + }); + properties.push_back(classsource); + properties.push_back(filepath); + properties.push_back(detectsleep); + return properties; +} + +MxpiPortInfo PluginViolentAction::DefineInputPorts() { + MxpiPortInfo inputPortInfo; + // Input: {{MxpiClassList}} + std::vector> value = {{"ANY"}}; + GenerateStaticInputPortsInfo(value, inputPortInfo); + return inputPortInfo; +} + +MxpiPortInfo PluginViolentAction::DefineOutputPorts() { + MxpiPortInfo outputPortInfo; + // Output: {{MxpiAttributeList}} + std::vector> value = {{"ANY"}}; + GenerateStaticOutputPortsInfo(value, outputPortInfo); + return outputPortInfo; +} + +namespace { + MX_PLUGIN_GENERATE(PluginViolentAction) +} diff --git a/contrib/ActionRecognition/plugins/PluginViolentAction/PluginViolentAction.h b/contrib/ActionRecognition/plugins/PluginViolentAction/PluginViolentAction.h new file mode 100644 index 0000000000000000000000000000000000000000..8cb037a2306bb8c0af3350ab5162167ec015f277 --- /dev/null +++ b/contrib/ActionRecognition/plugins/PluginViolentAction/PluginViolentAction.h @@ -0,0 +1,100 @@ +/* + * Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SDKMEMORY_PluginViolentAction_H +#define SDKMEMORY_PluginViolentAction_H + +#include "MxTools/PluginToolkit/base/MxPluginGenerator.h" +#include "MxTools/PluginToolkit/base/MxPluginBase.h" +#include "MxTools/PluginToolkit/metadata/MxpiMetadataManager.h" +#include "MxTools/Proto/MxpiDataType.pb.h" +#include "MxBase/ErrorCode/ErrorCode.h" + +/** + * This plug is to recognize whether the object's action is a Violent Action and alarm. +*/ +using namespace MxTools; + +namespace MxPlugins { + class PluginViolentAction : public MxTools::MxPluginBase { + public: + /** + * @description: Init configs. + * @param configParamMap: config. + * @return: Error code. + */ + APP_ERROR Init(std::map> &configParamMap) override; + + /** + * @description: DeInit device. + * @return: Error code. + */ + APP_ERROR DeInit() override; + + /** + * @description: Plugin_ViolentAction plugin process. + * @param mxpiBuffer: data receive from the previous. + * @return: Error code. + */ + APP_ERROR Process(std::vector &mxpiBuffer) override; + + /** + * @description: Plugin_ViolentAction plugin define properties. + * @return: properties. + */ + static std::vector> DefineProperties(); + + /** + * @api + * @brief Define the number and data type of input ports. + * @return MxTools::MxpiPortInfo. + */ + static MxTools::MxpiPortInfo DefineInputPorts(); + + /** + * @api + * @brief Define the number and data type of output ports. + * @return MxTools::MxpiPortInfo. + */ + static MxTools::MxpiPortInfo DefineOutputPorts(); + + private: + /** + * @api + * @brief Check metadata. + * @param MxTools::MxpiMetadataManager. + * @return Error Code. + */ + APP_ERROR CheckDataSource(MxpiMetadataManager &mxpiMetadataManager); + /** + * @api + * @brief Match the recognized action class to the action of interest and alarm. + * @return MxTools::MxpiAttributeList. + */ + std::shared_ptr ActionMatch(std::shared_ptr &mxpiClassList); + void ReadTxt(std::string file, std::vector &aoi); // read the action of interest txt file + std::string classSource_ = ""; // previous plugin MxpiClassList + std::string filePath_ = ""; + std::ostringstream ErrorInfo_; // Error Code + std::vector aoi = {}; + std::string alarmInformation = ""; + int pathflag = 0; // flag to mark file IO only once + uint32_t sleepTime_ = 0; + uint32_t detectSleep_ = 0; + int alarm_count = 0; + }; +} +#endif //SDKMEMORY_PluginViolentAction_H \ No newline at end of file diff --git a/contrib/ActionRecognition/run.sh b/contrib/ActionRecognition/run.sh new file mode 100644 index 0000000000000000000000000000000000000000..990e028d0fdd59de856ffb96c0b8d981d36ad53a --- /dev/null +++ b/contrib/ActionRecognition/run.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +# +# Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +set -e + +CUR_PATH=$(cd "$(dirname "$0")" || { warn "Failed to check path/to/run.sh" ; exit ; } ; pwd) + +# Simple log helper functions +info() { echo -e "\033[1;34m[INFO ][MxStream] $1\033[1;37m" ; } +warn() { echo >&2 -e "\033[1;31m[WARN ][MxStream] $1\033[1;37m" ; } + +export MX_SDK_HOME=${CUR_PATH}/../../.. +export LD_LIBRARY_PATH=${MX_SDK_HOME}/lib:${MX_SDK_HOME}/opensource/lib:${MX_SDK_HOME}/opensource/lib64:/usr/local/Ascend/ascend-toolkit/latest/acllib/lib64:${LD_LIBRARY_PATH} +export GST_PLUGIN_SCANNER=${MX_SDK_HOME}/opensource/libexec/gstreamer-1.0/gst-plugin-scanner +export GST_PLUGIN_PATH=${MX_SDK_HOME}/opensource/lib/gstreamer-1.0:${MX_SDK_HOME}/lib/plugins +export GST_DEBUG=3 + +#to set PYTHONPATH, import the StreamManagerApi.py +export PYTHONPATH=$PYTHONPATH:${MX_SDK_HOME}/python + +python3.7 main.py +exit 0 \ No newline at end of file