Skip to content

Commit

Permalink
Add PP_Human_Fall_Detection project.
Browse files Browse the repository at this point in the history
  • Loading branch information
guojin-yan committed Sep 19, 2023
1 parent b3e2aa5 commit 1f8ee1e
Show file tree
Hide file tree
Showing 12 changed files with 1,215 additions and 0 deletions.
11 changes: 11 additions & 0 deletions CSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "test_csharp_api", "tests\te
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CSharpAPI", "src\CSharpAPI\CSharpAPI.csproj", "{E7805C46-40CE-4402-B480-421BA8823A12}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PP-Human_Fall_Detection", "tutorial_examples\PP-Human_Fall_Detection\PP-Human_Fall_Detection.csproj", "{7CA4EB58-4CD1-42A3-B7A1-7EA37DB79103}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -75,6 +77,14 @@ Global
{E7805C46-40CE-4402-B480-421BA8823A12}.Release|Any CPU.Build.0 = Release|Any CPU
{E7805C46-40CE-4402-B480-421BA8823A12}.Release|x64.ActiveCfg = Release|Any CPU
{E7805C46-40CE-4402-B480-421BA8823A12}.Release|x64.Build.0 = Release|Any CPU
{7CA4EB58-4CD1-42A3-B7A1-7EA37DB79103}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7CA4EB58-4CD1-42A3-B7A1-7EA37DB79103}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7CA4EB58-4CD1-42A3-B7A1-7EA37DB79103}.Debug|x64.ActiveCfg = Debug|Any CPU
{7CA4EB58-4CD1-42A3-B7A1-7EA37DB79103}.Debug|x64.Build.0 = Debug|Any CPU
{7CA4EB58-4CD1-42A3-B7A1-7EA37DB79103}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7CA4EB58-4CD1-42A3-B7A1-7EA37DB79103}.Release|Any CPU.Build.0 = Release|Any CPU
{7CA4EB58-4CD1-42A3-B7A1-7EA37DB79103}.Release|x64.ActiveCfg = Release|Any CPU
{7CA4EB58-4CD1-42A3-B7A1-7EA37DB79103}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -85,6 +95,7 @@ Global
{CE8198D1-5CE4-405A-92D6-2F0B44F527E5} = {9FE4DAB4-B697-4D9C-9D5A-A96985950C31}
{AC0AD3DF-ADBC-4917-A30F-B1D95B79D4B5} = {3737D82A-86E9-4D03-81D3-030E2F9BF006}
{E7805C46-40CE-4402-B480-421BA8823A12} = {DCFCA0E4-2267-42D0-894E-88DCE18927E8}
{7CA4EB58-4CD1-42A3-B7A1-7EA37DB79103} = {9FE4DAB4-B697-4D9C-9D5A-A96985950C31}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {26D31CCE-2B57-4891-8ECC-5031281210D9}
Expand Down
159 changes: 159 additions & 0 deletions tutorial_examples/PP-Human_Fall_Detection/HumanFallDown.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
using OpenCvSharp;
using PP_Human;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PP_Human_Fall_Detection
{
/// <summary>
/// 测试人体摔倒检测
/// </summary>
public class HumanFallDown
{

/// <summary>
/// 人体摔倒检测
/// </summary>
static public void human_fall_down(string yoloe_path, string tinypose_path, string STGCN_path, string device_name)
{

// 定义模型预测器
// yoloe 模型预测
YOLOE yoloe_predictor = new YOLOE(yoloe_path, device_name);
Console.WriteLine("目标检测模型加载成功!!");
// tinypose 预测器
TinyPose tinyPose_predictor = new TinyPose(tinypose_path, device_name);
Console.WriteLine("关键点识别模型加载成功!!");
// STGCN 模型
STGCN stgcn_predictor = new STGCN(STGCN_path, device_name);
Console.WriteLine("摔倒识别模型加载成功!!");

// 测试视频信息
// 视频路径
string test_video = "/home/ygj/Program/PP-Human_Fall_Detection/demo/摔倒.mp4";
//string test_video = @"E:\Git_space\基于Csharp和OpenVINO部署PP-Human\demo\摔倒2.mp4";
// 视频读取器
VideoCapture video_capture = new VideoCapture(test_video);
// 视频帧率
double fps = video_capture.Fps;
// 视频帧数
int frame_count = video_capture.FrameCount;
Console.WriteLine("video fps: {0}, frame_count: {1}", Math.Round(fps), frame_count);



// 定义相关变量
// 视频帧
int frame_id = 0;
// 视频帧图像
Mat frame = new Mat();
// 可视化
Mat visualize_frame = new Mat();

// 关键点处理类
MotPoint mot_point = new MotPoint();
// 关键点快开始预测标志位
bool flag_stgcn = false;

// 可视化窗口
Window window = new Window("image", WindowFlags.AutoSize);
//window.Resize(100, 100);
// 摔倒预测结果
KeyValuePair<string, float> fall_down_result = new KeyValuePair<string, float>("unfalling", 1.0f);
// 设置起始帧
//video_capture.Set(VideoCaptureProperties.PosFrames, 100);

// 创建视频保存器
VideoWriter video_writer = new VideoWriter(@"output.avi",
FourCC.MP42, 30, new Size(video_capture.FrameWidth, video_capture.FrameHeight));

while (true)
{
// 判断视频是否打开
if (!video_capture.IsOpened())
{
Console.WriteLine("视频打开失败!!");
break;
}
// 每10帧打印一次过程
if (frame_id % 10 == 0)
{
Console.WriteLine("检测进程 frame id: {0} - {1}", frame_id, frame_id + 10);
}

// 读取视频帧
if (!video_capture.Read(frame))
{
Console.WriteLine("视频读取完毕!!{0}", frame_id);
break;
}

// 复制可视化图片
visualize_frame = frame.Clone();

//****************************1. 行人区域识别 ******************************//
ResBboxs person_result = yoloe_predictor.predict(frame);
// 判断是否识别到人
if (person_result.bboxs.Count < 1)
{
continue;
}
// 绘制行人区域
yoloe_predictor.draw_boxes(person_result, ref visualize_frame);

//****************************2. 行人关键点识别 ******************************//
//裁剪行人区域
List<Rect> point_rects;
List<Mat> person_rois = tinyPose_predictor.get_point_roi(frame, person_result.bboxs, out point_rects);


for (int p = 0; p < person_rois.Count; p++)
{
Cv2.ImShow("www", person_rois[p]);
// 关键点识别
float[,] person_point = tinyPose_predictor.predict(person_rois[p]);
KeyPoints key_point = new KeyPoints(frame_id, person_point, point_rects[p]);
//for (int i = 0; i < 17; ++i) {
// Console.WriteLine(key_point.points[i,0] + " " + key_point.points[i, 1]);
//}

flag_stgcn = mot_point.add_point(key_point);
tinyPose_predictor.draw_poses(key_point, ref visualize_frame);
}


//****************************3. 行人摔倒识别 ******************************//

if (flag_stgcn)
{
List<List<KeyPoints>> predict_points = mot_point.get_points();
for (int p = 0; p < predict_points.Count; p++)
{
Console.WriteLine(predict_points[p].Count);
fall_down_result = stgcn_predictor.predict(predict_points[p]);
}
}
stgcn_predictor.draw_result(ref visualize_frame, fall_down_result, person_result.bboxs[0]);

window.ShowImage(visualize_frame);
video_writer.Write(visualize_frame);
Cv2.WaitKey(1);

frame_id++; // 帧号累加

}

yoloe_predictor.release();
tinyPose_predictor.release();
stgcn_predictor.release();

visualize_frame.Release();
video_capture.Release();
}


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
请将OpenVINO C# API 中的代码放置在当前文件夹。
118 changes: 118 additions & 0 deletions tutorial_examples/PP-Human_Fall_Detection/PP-Human/Common.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCvSharp;

namespace PP_Human
{
/// <summary>
/// 识别检测结果类
/// </summary>
public class ResBboxs
{
public List<Rect> bboxs; // 预测框
public float[] scores; // 预测分数
public int[] classes; // 预测分类

/// <summary>
/// 构造函数
/// </summary>
/// <param name="bboxs">预测框</param>
/// <param name="scores">预测分数</param>
/// <param name="classes">预测分类</param>
public ResBboxs(List<Rect> bboxs, float[] scores, int[] classes)
{
this.bboxs = bboxs;
this.scores = scores;
this.classes = classes;
}
}

public class KeyPoints
{
// 视频帧
public int mot_id { get; set; }
// 关键点和分数
public float[,] points { get; set; }
// 行人位置框
public Rect bbox { get; set; }
// 识别框中心
public Point center { get; set; }

public KeyPoints(int mot_id, float[,] points, Rect bbox)
{
this.mot_id = mot_id;
this.points = (float[,])points.Clone();
this.bbox = bbox;
this.center = new Point((int)(bbox.X + bbox.Width / 2), (int)(bbox.Y + bbox.Height / 2));
}
}

public class MotPoint
{
public List<List<KeyPoints>> mot_point;
public bool flag_predict=false;
public List<int> predict_id = new List<int>();

public MotPoint()
{
mot_point = new List<List<KeyPoints>>();
}
public bool add_point(KeyPoints point)
{
bool flag = false;
for (int c = 0; c < mot_point.Count; c++)
{
List<KeyPoints> points = mot_point[c];
// 添加新的的关键点
Point center1 = point.center;
Point center2 = points[points.Count - 1].center;
if (Math.Sqrt(Math.Pow((center1.X - center2.X), 2.0) + Math.Pow((center1.Y - center2.Y), 2.0)) < 500)
{
mot_point[c].Add(point);
flag = true;
}
// 判断是否满足预测条件
if (mot_point[c].Count >= 50)
{
flag_predict = true;
predict_id.Add(c);
}
}
if (!flag)
{
mot_point.Add(new List<KeyPoints>());
mot_point[mot_point.Count-1].Add(point);
return false;
}
//Console.WriteLine(mot_point.Count);
return flag_predict;

}


public List<List<KeyPoints>> get_points()
{
List <List<KeyPoints>> points = new List<List <KeyPoints>> ();
if (flag_predict)
{
for (int l = 0; l < predict_id.Count; l++)
{
List<KeyPoints> new_points = new List<KeyPoints> (mot_point[predict_id[l]]);
points.Add(new_points);
Console.WriteLine(mot_point.Count);
for (int i = 0; i < 20; i++)
{
mot_point[predict_id[l]].RemoveAt(0);
}
flag_predict = false;
}
flag_predict = false;
predict_id.Clear();
}
return points;
}
}
}
Loading

0 comments on commit 1f8ee1e

Please sign in to comment.