-
Notifications
You must be signed in to change notification settings - Fork 0
/
findObject.cs
76 lines (72 loc) · 2.83 KB
/
findObject.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using Emgu.CV;
using Emgu.CV.Structure;
using System.Diagnostics;
using Emgu.CV.GPU;
namespace Detection
{
public static class findObject
{
//=============================SVM Cassifier Data Training Tanaman=========================================
private static float[] GetDataObjects(string objectData)
{
List<float> data = new List<float>();
using (System.IO.StreamReader reader = new System.IO.StreamReader(objectData))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var arr = new float[line.Length];
for (var i=0; i < line.Length; i++)
{
arr[i] = (float)Convert.ToDouble(line[i]);
data.Add(arr[i]);
}
}
}
var array = data.ToArray();
return array;
}
static Size blockSize = new Size(16, 16);
static Size blockStride = new Size(8, 8);
static Size winStride = new Size(8, 8);
static Size cellSize = new Size(8, 8);
static int nbins = 9;
//=============================Feature Descriptor (HOG) Data Training Tanaman=============================
public static Rectangle[] findObjects(Image<Bgr, Byte> image, out long processingTime, Size winSize, string dataFile)
{
Stopwatch watch;
Rectangle[] regions;
if(GpuInvoke.HasCuda)
{
using (GpuHOGDescriptor des = new GpuHOGDescriptor())
{
des.SetSVMDetector(GpuHOGDescriptor.GetDefaultPeopleDetector());
watch = Stopwatch.StartNew();
using (GpuImage<Bgr, Byte> gpuImg = new GpuImage<Bgr,byte>(image))
using (GpuImage<Bgra,Byte> gpuBgra = gpuImg.Convert<Bgra,Byte>())
{
regions = des.DetectMultiScale(gpuBgra);
}
}
}
else
{
using (HOGDescriptor des = new HOGDescriptor(winSize, blockSize, blockStride, cellSize, nbins, 1, -1, 0.2, true))
{
des.SetSVMDetector(GetDataObjects(dataFile));
watch = Stopwatch.StartNew();
regions = des.DetectMultiScale(image);
}
}
watch.Stop();
processingTime = watch.ElapsedMilliseconds;
return regions;
}
}
}