forked from Ken98045/On-Guard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrameAnalyzer.cs
334 lines (285 loc) · 12.1 KB
/
FrameAnalyzer.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
namespace SAAI
{
public struct InterestingObject
{
public InterestingObject(AreaOfInterest area, ImageObject found, int overlap)
{
Area = area;
FoundObject = found;
Overlap = overlap;
}
public AreaOfInterest Area { get; set; }
public ImageObject FoundObject { get; set; }
public int Overlap { get; set; }
}
public struct AnalysisResult
{
public List<InterestingObject> InterestingObjects { get; set; }
public HashSet<string> FailureReasons { get; set; }
}
internal class FrameAnalyzer
{
readonly AreasOfInterestCollection _areas;
readonly List<ImageObject> _imageObjects;
List<InterestingObject> InterestingObjects { get; set; }
public FrameAnalyzer(AreasOfInterestCollection areas, List<ImageObject> imageObjects)
{
InterestingObjects = new List<InterestingObject>();
_areas = areas;
_imageObjects = imageObjects;
}
// because we will probably do this async
public AnalysisResult AnalyzeFrame()
{
AnalysisResult result = new AnalysisResult();
InterestingObjects.Clear();
HashSet<string> failureReasons = new HashSet<string>();
string reason = string.Empty;
if (null != _areas && _areas.Count() > 0)
{
if (null != _imageObjects)
{
foreach (ImageObject imageObject in _imageObjects)
{
// Each area cares about specific types of objects.
// The overlap for each type varies as does the confidences.
// Right now we don't care about the number of frames the object is in or whether
// the object is moving into or out of the area.
foreach (AreaOfInterest area in _areas)
{
// The search critera defines whether we are looking for people, vehicles, animals, etc.
if (null != area.SearchCriteria)
{
foreach (ObjectCharacteristics criteria in area.SearchCriteria)
{
// First find out what type of object the AI thinks this is.
ImageObjectType objectType = ObjectTypeFromLabel(imageObject.Label);
if (criteria.ObjectType != ImageObjectType.Irrelevant)
{
// If this is a type of object we generally might care about for security purposes
// then we compare the overlap to to minimum overlap to see if we still care
// (and yes, there are more elegant ways to do this, but it is easier to debug like this
if (criteria.ObjectType == objectType)
{
int percentOverlap = ObjectToAreaOverlap(imageObject, area);
if (percentOverlap >= criteria.MinPercentOverlap)
{
if (area.AOIType == AOIType.IgnoreObjects)
{
reason = area.AOIName + ": Object in ignored area - " + imageObject.Label;
failureReasons.Add(reason);
// yes, it met all of our critera, but the critera was to ignore it.
// It still may also be in an area we do care about, but we worry about that
// in the next loop
}
else
{
// OK, we probably care, but how confident are we that this object is something we care about
if ((int)Math.Round(imageObject.Confidence * 100) >= criteria.Confidence)
{
// BUT WAIT, THERE'S MORE!
if (criteria.MinimumXSize == 0 || imageObject.ObjectRectangle.Width > criteria.MinimumXSize)
{
if (criteria.MinimumYSize == 0 || imageObject.ObjectRectangle.Height > criteria.MinimumYSize)
{
InterestingObject interesting = new InterestingObject(area, imageObject, percentOverlap);
InterestingObjects.Add(interesting);
Dbg.Trace("Adding interesting object before 2nd chance ignore: " + interesting.FoundObject.Label);
}
else
{
reason = area.AOIName + ": Minimum height - too low - " + imageObject.Label;
failureReasons.Add(reason);
}
}
else
{
reason = area.AOIName + ": Minimum width too low - " + imageObject.Label;
failureReasons.Add(reason);
}
}
else
{
reason = area.AOIName + ": Confidence level too low: " + (imageObject.Confidence * 100).ToString() + " - " + imageObject.Label;
failureReasons.Add(reason);
}
}
}
else
{
reason = area.AOIName + ": Less than minimum overlap: " + percentOverlap.ToString() + " - " + imageObject.Label;
failureReasons.Add(reason);
}
}
else
{
if (objectType != ImageObjectType.Irrelevant)
{
reason = area.AOIName + ": Object mismatch: " + criteria.ObjectType.ToString() + " - " + objectType.ToString() + " - " + imageObject.Label;
failureReasons.Add(reason);
}
}
}
else { /*failureReasons.Add(area.AOIName + ": Object Irrelevant - " + imageObject.Label); */}
}
}
else
{
reason = "No search critera";
failureReasons.Add(reason);
}
}
// Double check the object to be sure it isn't in a ignore zone.
// This is a little screwy -- we added one, then we delete the one we just added.
// However, there are few good ways to do it since an object may be in several areas,
// yet we dont't want it if it really should be ignored
foreach (var ignore in _areas)
{
if (ignore.AOIType == AOIType.IgnoreObjects)
{
// Again, we look at the search criteria to see if it was the type of object we ignore
if (null != ignore.SearchCriteria)
{
foreach (var criteria in ignore.SearchCriteria)
{
ImageObjectType objectType = ObjectTypeFromLabel(imageObject.Label);
if (criteria.ObjectType == objectType)
{
// Yes, it is the type of object we ignore
int ignoreOverlap = ObjectToAreaOverlap(imageObject, ignore); // Does it overlap
{
if (ignoreOverlap > criteria.MinPercentOverlap) //
{
bool foundOne = false;
do
{
var io = InterestingObjects.FirstOrDefault(x => x.FoundObject.ID == imageObject.ID);
if (io.FoundObject != null)
{
foundOne = true;
reason = ignore.AOIName + ": Ignore area second pass validation - Object: " + io.FoundObject.Label;
failureReasons.Add(reason);
Dbg.Trace("Removing object on second pass ignore area validation: " + io.FoundObject.Label);
InterestingObjects.Remove(io);
}
else
{
foundOne = false;
}
} while (foundOne);
}
}
}
}
}
else
{
Dbg.Write("FrameAnalyzer.AnalyzeFrame - On double check ignore areas no search critera found ");
}
}
}
}
}
else { failureReasons.Add("No Interesting Objects"); }
// InterestingObjects = RemoveDuplicateAreas(InterestingObjects);
}
else { failureReasons.Add("No areas defined"); }
result.InterestingObjects = InterestingObjects;
result.FailureReasons = failureReasons;
foreach (var failure in failureReasons)
{
Dbg.Trace(failure);
}
return result;
}
// TODO Not currently used.
static List<InterestingObject> RemoveDuplicateAreas(List<InterestingObject> objects)
{
if (objects.Count > 1)
{
bool foundDuplicate;
do
{
foundDuplicate = false;
int i;
for (i = 0; i < objects.Count - 1; i++)
{
Rectangle r1 = objects[i].FoundObject.ObjectRectangle;
for (int j = i + 1; j < objects.Count; j++)
{
Rectangle r2 = objects[j].FoundObject.ObjectRectangle;
if (r1 == r2 && objects[i].FoundObject.Label == objects[j].FoundObject.Label)
{
if (objects[i].Overlap > objects[j].Overlap)
{
// They were the same type of object, the same object rectangle
// So, if the overlap on one is greater than the other we delete the other
objects.RemoveAt(j);
}
else
{
objects.RemoveAt(i);
}
i = 0; // start over again
foundDuplicate = true;
break;
}
}
}
} while (foundDuplicate && objects.Count > 1);
}
return objects;
}
public static ImageObjectType ObjectTypeFromLabel(string label)
{
ImageObjectType result;
switch (label)
{
case "person":
result = ImageObjectType.People;
break;
case "car":
result = ImageObjectType.Cars;
break;
case "truck":
result = ImageObjectType.Trucks;
break;
case "motorbike":
result = ImageObjectType.Motorcycles;
break;
case "bicycle":
result = ImageObjectType.Bikes;
break;
case "bear":
result = ImageObjectType.Bears;
break;
case "dog":
case "cat":
case "horse":
case "sheep":
case "cow:":
result = ImageObjectType.Animals;
break;
default:
result = ImageObjectType.Irrelevant;
break;
}
return result;
}
static int ObjectToAreaOverlap(ImageObject imageObject, AreaOfInterest area)
{
int overlap;
int objectArea = imageObject.ObjectRectangle.Width * imageObject.ObjectRectangle.Height;
_ = area.GetRect().Width * area.GetRect().Height;
Rectangle intersect = Rectangle.Intersect(imageObject.ObjectRectangle, area.GetRect());
int intersectArea = intersect.Width * intersect.Height;
double percentage = (100.0 * intersectArea) / objectArea;
overlap = (int)Math.Round(percentage);
return overlap;
}
}
}