-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PP_Human_Fall_Detection project.
- Loading branch information
1 parent
b3e2aa5
commit 1f8ee1e
Showing
12 changed files
with
1,215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
tutorial_examples/PP-Human_Fall_Detection/HumanFallDown.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
|
||
} | ||
} |
1 change: 1 addition & 0 deletions
1
tutorial_examples/PP-Human_Fall_Detection/OpenVINO-CSharp-API/代码资源.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
请将OpenVINO C# API 中的代码放置在当前文件夹。 |
118 changes: 118 additions & 0 deletions
118
tutorial_examples/PP-Human_Fall_Detection/PP-Human/Common.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.