using System;
|
using System.Collections.Generic;
|
using System.Diagnostics;
|
using System.IO;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using System.Xml;
|
using Emgu.CV;
|
using Emgu.CV.CvEnum;
|
using Emgu.CV.ML;
|
using Emgu.CV.Structure;
|
|
namespace RichCreator.Editor.Utils
|
{
|
public class HOGDetector
|
{
|
public HOGDetector(System.Drawing.Size winSize,
|
System.Drawing.Size blockSize,
|
System.Drawing.Size strideSize,
|
System.Drawing.Size cellSize,
|
Int32 channel,
|
string xmlFile)
|
{
|
this.WinSize = winSize;
|
this.BlockSize = blockSize;
|
this.StrideSize = strideSize;
|
this.CellSize = cellSize;
|
this.Channel = channel;
|
this.XmlFile = xmlFile;
|
|
Initizlize();
|
|
}
|
|
|
public System.Drawing.Size WinSize { get; set; }
|
|
public System.Drawing.Size BlockSize { get; set; }
|
|
public System.Drawing.Size StrideSize { get; set; }
|
|
public System.Drawing.Size CellSize { get; set; }
|
|
public Int32 Channel { get; set; }
|
|
public string XmlFile { get; set; }
|
|
|
private HOGDescriptor hog;
|
|
|
private float rhoValue = 0;
|
private string alphaValue = "";
|
private int sv_count = 0;
|
private float[] alphaArr = null;
|
private Matrix<float> svmMat = null;
|
|
private void Initizlize()
|
{
|
hog = new HOGDescriptor(this.WinSize, this.BlockSize, this.StrideSize, this.CellSize, this.Channel);
|
float[] svmDetector = GetSVMDetector(this.XmlFile);
|
try
|
{
|
hog.SetSVMDetector(svmDetector);
|
}
|
catch (Exception e)
|
{
|
Trace.Write(e.Message);
|
}
|
|
}
|
|
/// <summary>
|
/// 识别
|
/// </summary>
|
/// <param name="targetImage"></param>
|
/// <param name="svmDetector"></param>
|
/// <returns></returns>
|
public ZTRect[] DetectorImage(string targetImage)
|
{
|
ZTRect[] regions;
|
Mat image = new Mat(targetImage, ImreadModes.Color);
|
MCvObjectDetection[] results = hog.DetectMultiScale(image);
|
if (results.Length > 0)
|
{
|
regions = new ZTRect[results.Length];
|
for (int i = 0; i < results.Length; i++)
|
{
|
regions[i] = new ZTRect(results[i].Rect, results[i].Score, results[i].ClassId);
|
}
|
return regions;
|
}
|
else
|
{
|
return new ZTRect[0];
|
}
|
}
|
|
public static ZTRect[] DetectorPeople(string targetImage)
|
{
|
Mat image = new Mat(targetImage, ImreadModes.Color);
|
|
using (HOGDescriptor hog = new HOGDescriptor())
|
{
|
float[] arr = HOGDescriptor.GetDefaultPeopleDetector();
|
hog.SetSVMDetector(arr);
|
|
MCvObjectDetection[] results = hog.DetectMultiScale(image);
|
return GetRects(results);
|
}
|
}
|
|
private static ZTRect[] GetRects(MCvObjectDetection[] results)
|
{
|
if (results.Length > 0)
|
{
|
|
ZTRect[] regions = new ZTRect[results.Length];
|
for (int i = 0; i < results.Length; i++)
|
{
|
regions[i] = new ZTRect(results[i].Rect, results[i].Score, results[i].ClassId);
|
}
|
return regions;
|
}
|
else
|
{
|
return new ZTRect[0];
|
}
|
}
|
|
|
/// <summary>
|
/// 得到SVM Detector
|
/// </summary>
|
/// <returns></returns>
|
private float[] GetSVMDetector(string xmlFile)
|
{
|
#region 读取已经训练好的XML
|
SVM svm = new SVM();
|
FileStorage fsr = new FileStorage(xmlFile, FileStorage.Mode.Read);
|
svm.Read(fsr.GetFirstTopLevelNode());
|
#endregion
|
|
XmlDocument xml = new XmlDocument();
|
xml.Load(xmlFile);
|
get_rho(xml.DocumentElement);
|
getAlpha_str(xml.DocumentElement);
|
getSv_count(xml.DocumentElement);
|
getAlpha();
|
|
int supportVectors = svm.GetSupportVectors().Height;
|
int DescriptorDim = svm.GetSupportVectors().Width;
|
|
svmMat = new Matrix<float>(supportVectors, DescriptorDim);
|
get_supportVector(xml.DocumentElement);
|
|
Matrix<float> alphaMat = new Matrix<float>(1, supportVectors);
|
for (int i = 0; i < alphaArr.Length; i++)
|
{
|
alphaMat[0, i] = alphaArr[i];
|
}
|
|
Matrix<float> resultMat = new Matrix<float>(1, DescriptorDim);
|
resultMat = -1 * alphaMat * svmMat;
|
|
float[] mydetector = new float[DescriptorDim + 1];
|
for (int i = 0; i < DescriptorDim; i++)
|
{
|
mydetector[i] = resultMat[0, i];
|
}
|
mydetector[DescriptorDim] = rhoValue;
|
|
return mydetector;
|
}
|
|
|
#region 读取xml
|
public void get_rho(XmlNode nodes)
|
{
|
if (nodes.HasChildNodes)
|
{
|
foreach (XmlNode node in nodes.ChildNodes)
|
{
|
if (nodes.Name == "rho")
|
{
|
rhoValue = float.Parse(nodes.InnerText);
|
return;
|
}
|
get_rho(node);
|
}
|
}
|
}
|
|
public void getAlpha_str(XmlNode nodes)
|
{
|
if (nodes.HasChildNodes)
|
{
|
foreach (XmlNode node in nodes.ChildNodes)
|
{
|
if (nodes.Name == "alpha")
|
{
|
alphaValue = nodes.InnerText;
|
return;
|
}
|
getAlpha_str(node);
|
}
|
}
|
}
|
|
public void getSv_count(XmlNode nodes)
|
{
|
if (nodes.HasChildNodes)
|
{
|
foreach (XmlNode node in nodes.ChildNodes)
|
{
|
if (nodes.Name == "sv_count")
|
{
|
sv_count = int.Parse(nodes.InnerText);
|
return;
|
}
|
getSv_count(node);
|
}
|
}
|
}
|
|
public void getAlpha()
|
{
|
byte[] array = Encoding.ASCII.GetBytes(alphaValue);
|
MemoryStream stream = new MemoryStream(array); //convert stream 2 string
|
StreamReader sr = new StreamReader(stream);
|
alphaArr = new float[sv_count];
|
sr.ReadLine();
|
int i = 0;
|
while (true)
|
{
|
|
string tmp = sr.ReadLine();
|
if (tmp == "")
|
continue;
|
|
string[] tmp2 = tmp.Split(' ');
|
foreach (string ele in tmp2)
|
{
|
if (ele != "")
|
{
|
alphaArr[i] = float.Parse(ele);
|
i++;
|
}
|
}
|
|
if (i == sv_count)
|
break;
|
}
|
}
|
|
public void get_supportVector(XmlNode nodes)
|
{
|
if (nodes.HasChildNodes)
|
{
|
foreach (XmlNode node in nodes.ChildNodes)
|
{
|
if (nodes.Name == "support_vectors")
|
{
|
for (int i = 0; i < node.ChildNodes.Count; i++)
|
{
|
string strValue = node.ChildNodes[i].InnerText;
|
string[] tempStrArr = strValue.Replace("\r\n", "")
|
.Replace(" ", " ").Replace(" ", " ").Replace(" ", " ")
|
.Replace(" ", " ").Replace(" ", " ").Replace(" ", " ")
|
.Replace(" ", " ").Split(' ');
|
int z = 0;
|
for (int j = 0; j < tempStrArr.Length; j++)
|
{
|
if (tempStrArr[j] != "")
|
{
|
svmMat[i, z] = float.Parse(tempStrArr[j]);
|
z++;
|
}
|
}
|
}
|
return;
|
}
|
get_supportVector(node);
|
}
|
}
|
}
|
#endregion
|
}
|
}
|