-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
177 lines (151 loc) · 6.49 KB
/
Form1.cs
File metadata and controls
177 lines (151 loc) · 6.49 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Dnn;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using Microsoft.Data.SqlClient;
using System.Configuration;
namespace WinForms_ObjectDetection
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnString"]);
SqlCommand cmd;
string[] CocoClasses;
Net CaffeModel = null;
VideoCapture videoCapture;
public Form1()
{
InitializeComponent();
}
private void processObjectDetection(object sender, EventArgs e)
{
try
{
Rectangle A1 = new Rectangle(290, 240, 110, 80);
Rectangle A2 = new Rectangle(210, 220, 110, 80);
Rectangle A3 = new Rectangle(130, 200, 110, 80);
Rectangle A4 = new Rectangle(390, 150, 90, 70);
Rectangle A5 = new Rectangle(310, 160, 90, 50);
Rectangle A6 = new Rectangle(240, 140, 90, 50);
var rois = new List<Rectangle>(); // List of ROIs
rois.Add(A1);
rois.Add(A2);
rois.Add(A3);
rois.Add(A4);
rois.Add(A5);
rois.Add(A6);
Mat frame = new Mat();
videoCapture.Retrieve(frame);
var img = frame.ToImage<Bgr, byte>();
var blob = DnnInvoke.BlobFromImage(img, 1.0, frame.Size, swapRB: true);
CaffeModel.SetInput(blob);
foreach (var (roi, n) in rois.Select((roi, n) => (roi, n)))
{
img.Draw(roi, new Bgr(0, 255, 0), 2);
}
var output = new VectorOfMat();
string[] outnames = new string[] { "detection_out_final" };
CaffeModel.Forward(output, outnames);
var threshold = 0.4;
int numDetections = output[0].SizeOfDimension[2];
int numClasses = 90;
var bboxes = output[0].GetData();
List<int> list = new List<int>();
for (int i = 0; i < numDetections; i++)
{
float score = (float)bboxes.GetValue(0, 0, i, 2);
if (score > threshold)
{
int classID = Convert.ToInt32(bboxes.GetValue(0, 0, i, 1));
//only car
if (classID != 2)
{
continue;
}
int left = Convert.ToInt32((float)bboxes.GetValue(0, 0, i, 3) * img.Cols);
int top = Convert.ToInt32((float)bboxes.GetValue(0, 0, i, 4) * img.Rows);
int right = Convert.ToInt32((float)bboxes.GetValue(0, 0, i, 5) * img.Cols);
int bottom = Convert.ToInt32((float)bboxes.GetValue(0, 0, i, 6) * img.Rows);
Rectangle rectangle = new Rectangle(left, top, right - left + 1, bottom - top + 1);
Point WeightedCentroid = new Point((rectangle.Left + rectangle.Right) / 2, (rectangle.Top + rectangle.Bottom) / 2);
img.Draw(rectangle, new Bgr(255, 0, 0), 2);
CvInvoke.Circle(img, WeightedCentroid, 4, new MCvScalar(0, 255, 0), 5, LineType.EightConnected, 0);
Point WeightedCentroidnew = WeightedCentroid;
CvInvoke.Line(img, WeightedCentroid, WeightedCentroidnew, new MCvScalar(255, 0, 0), 5, LineType.EightConnected, 0);
foreach(var (roi, n) in rois.Select((roi, n) => (roi, n)))
{
if (roi.Contains(WeightedCentroid))
{
img.Draw(roi, new Bgr(0, 0, 255), 2);
list.Add((n + 1));
}
}
foreach (var (roi, n) in rois.Select((roi, n) => (roi, n)))
{
updateDatabase("A" + (n + 1), 0);
}
foreach (var n in list)
{
updateDatabase("A" + n, 1);
}
var labels = CocoClasses[classID];
CvInvoke.PutText(img, labels, new Point(left, top - 10), FontFace.HersheySimplex, 1.0,
new MCvScalar(0, 0, 255), 2);
}
}
pictureBox1.Invoke((MethodInvoker)delegate
{
pictureBox1.Image = img.ToBitmap();
});
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
public void updateDatabase(string oznaka, int zauzeto)
{
try
{
cmd = new SqlCommand("update Mjesto set Zauzeto=@zauzeto where Oznaka=@oznaka", con);
con.Open();
cmd.Parameters.AddWithValue("@zauzeto", zauzeto);
cmd.Parameters.AddWithValue("@oznaka", oznaka);
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception)
{
MessageBox.Show("Record update fail!");
}
}
private void startMaskRCNN()
{
try
{
var modelpath = ConfigurationManager.AppSettings["ModelPath"];
var configPath = ConfigurationManager.AppSettings["ModelArchitecture"];
var coconamespath = ConfigurationManager.AppSettings["CocoClasses"];
CaffeModel = DnnInvoke.ReadNetFromTensorflow(modelpath, configPath);
CaffeModel.SetPreferableBackend(Emgu.CV.Dnn.Backend.OpenCV);
CaffeModel.SetPreferableTarget(Target.Cpu);
CocoClasses = File.ReadAllLines(coconamespath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
if (videoCapture == null)
{
videoCapture = new VideoCapture(0);
startMaskRCNN();
videoCapture.ImageGrabbed += processObjectDetection;
videoCapture.Start();
}
}
}
}