forked from EnoxSoftware/OpenCVForUnity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebCamTextureAsyncDetectFaceSample.cs
More file actions
863 lines (672 loc) · 27.9 KB
/
Copy pathWebCamTextureAsyncDetectFaceSample.cs
File metadata and controls
863 lines (672 loc) · 27.9 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
using UnityEngine;
using System;
using System.Collections;
#if UNITY_5_3 || UNITY_5_3_OR_NEWER
using UnityEngine.SceneManagement;
#endif
using OpenCVForUnity;
using System.Collections.Generic;
using Rect = OpenCVForUnity.Rect;
#if UNITY_WSA
#if NETFX_CORE
using System.Threading;
using System.Threading.Tasks;
#else
#endif
#else
using System.Threading;
#endif
using PositionsVector = System.Collections.Generic.List<OpenCVForUnity.Rect>;
namespace OpenCVForUnitySample
{
/// <summary>
/// WebCamTexture async detect face sample.
/// This cord referred to https://github.com/Itseez/opencv/blob/master/modules/objdetect/src/detection_based_tracker.cpp.
/// </summary>
[RequireComponent(typeof(WebCamTextureToMatHelper))]
public class WebCamTextureAsyncDetectFaceSample : MonoBehaviour
{
/// <summary>
/// The colors.
/// </summary>
Color32[] colors;
/// <summary>
/// The gray mat.
/// </summary>
Mat grayMat;
/// <summary>
/// The texture.
/// </summary>
Texture2D texture;
/// <summary>
/// The cascade.
/// </summary>
CascadeClassifier cascade;
private CascadeClassifier regionCascade;
Rect[] rectsWhereRegions;
List<Rect> detectedObjectsInRegions = new List<Rect> ();
List<Rect> resultObjects = new List<Rect> ();
private List<TrackedObject> trackedObjects = new List<TrackedObject> ();
private List<float> weightsPositionsSmoothing = new List<float> ();
private List<float> weightsSizesSmoothing = new List<float> ();
private Parameters parameters;
private InnerParameters innerParameters;
// for Thread
#if UNITY_WSA
#if NETFX_CORE
private Task task_ = null;
private CancellationTokenSource tokenSource_ = null;
#else
#endif
#else
private volatile bool shouldStopThread = false;
#endif
private volatile ThreadComm threadComm = new ThreadComm ();
private System.Object thisLock = new System.Object ();
private volatile bool isThreadRunning = false;
private volatile bool didUpdateTheDetectionResult = false;
private Mat grayMat4Thread;
private MatOfRect resultDetect;
/// <summary>
/// The web cam texture to mat helper.
/// </summary>
WebCamTextureToMatHelper webCamTextureToMatHelper;
// Use this for initialization
void Start ()
{
weightsPositionsSmoothing.Add (1);
weightsSizesSmoothing.Add (0.5f);
weightsSizesSmoothing.Add (0.3f);
weightsSizesSmoothing.Add (0.2f);
//parameters.minObjectSize = 96;
//parameters.maxObjectSize = int.MaxValue;
//parameters.scaleFactor = 1.1f;
//parameters.minNeighbors = 2;
parameters.maxTrackLifetime = 5;
innerParameters.numLastPositionsToTrack = 4;
innerParameters.numStepsToWaitBeforeFirstShow = 6;
innerParameters.numStepsToTrackWithoutDetectingIfObjectHasNotBeenShown = 3;
innerParameters.numStepsToShowWithoutDetecting = 3;
innerParameters.coeffTrackingWindowSize = 2.0f;
innerParameters.coeffObjectSizeToTrack = 0.85f;
innerParameters.coeffObjectSpeedUsingInPrediction = 0.8f;
webCamTextureToMatHelper = gameObject.GetComponent<WebCamTextureToMatHelper> ();
webCamTextureToMatHelper.Init ();
}
/// <summary>
/// Raises the web cam texture to mat helper inited event.
/// </summary>
public void OnWebCamTextureToMatHelperInited ()
{
Debug.Log ("OnWebCamTextureToMatHelperInited");
Mat webCamTextureMat = webCamTextureToMatHelper.GetMat ();
colors = new Color32[webCamTextureMat.cols () * webCamTextureMat.rows ()];
texture = new Texture2D (webCamTextureMat.cols (), webCamTextureMat.rows (), TextureFormat.RGBA32, false);
grayMat = new Mat (webCamTextureMat.rows (), webCamTextureMat.cols (), CvType.CV_8UC1);
regionCascade = new CascadeClassifier (Utils.getFilePath ("lbpcascade_frontalface.xml"));
if (regionCascade.empty ()) {
Debug.LogError ("cascade file is not loaded.Please copy from “OpenCVForUnity/StreamingAssets/” to “Assets/StreamingAssets/” folder. ");
}
initThread ();
gameObject.transform.localScale = new Vector3 (webCamTextureMat.cols (), webCamTextureMat.rows (), 1);
Debug.Log ("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation);
float width = 0;
float height = 0;
width = gameObject.transform.localScale.x;
height = gameObject.transform.localScale.y;
float widthScale = (float)Screen.width / width;
float heightScale = (float)Screen.height / height;
if (widthScale < heightScale) {
Camera.main.orthographicSize = (width * (float)Screen.height / (float)Screen.width) / 2;
} else {
Camera.main.orthographicSize = height / 2;
}
gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
}
/// <summary>
/// Raises the web cam texture to mat helper disposed event.
/// </summary>
public void OnWebCamTextureToMatHelperDisposed ()
{
Debug.Log ("OnWebCamTextureToMatHelperDisposed");
StopThread ();
if (grayMat4Thread != null)
grayMat4Thread.Dispose ();
if (cascade != null)
cascade.Dispose ();
if (grayMat != null)
grayMat.Dispose ();
if (regionCascade != null)
regionCascade.Dispose ();
}
// Update is called once per frame
void Update ()
{
if (webCamTextureToMatHelper.isPlaying () && webCamTextureToMatHelper.didUpdateThisFrame ()) {
Mat rgbaMat = webCamTextureToMatHelper.GetMat ();
Imgproc.cvtColor (rgbaMat, grayMat, Imgproc.COLOR_RGBA2GRAY);
Imgproc.equalizeHist (grayMat, grayMat);
if (!threadComm.shouldDetectInMultiThread) {
grayMat.copyTo (grayMat4Thread);
threadComm.shouldDetectInMultiThread = true;
}
OpenCVForUnity.Rect[] rects;
if (didUpdateTheDetectionResult) {
didUpdateTheDetectionResult = false;
lock (thisLock) {
//Debug.Log("DetectionBasedTracker::process: get _rectsWhereRegions were got from resultDetect");
rectsWhereRegions = resultDetect.toArray ();
rects = resultDetect.toArray ();
}
for (int i = 0; i < rects.Length; i++) {
Imgproc.rectangle (rgbaMat, new Point (rects [i].x, rects [i].y), new Point (rects [i].x + rects [i].width, rects [i].y + rects [i].height), new Scalar (0, 0, 255, 255), 2);
}
} else {
//Debug.Log("DetectionBasedTracker::process: get _rectsWhereRegions from previous positions");
rectsWhereRegions = new Rect[trackedObjects.Count];
for (int i = 0; i < trackedObjects.Count; i++) {
int n = trackedObjects [i].lastPositions.Count;
//if (n > 0) UnityEngine.Debug.LogError("n > 0 is false");
Rect r = trackedObjects [i].lastPositions [n - 1].clone ();
if (r.area () == 0) {
Debug.Log ("DetectionBasedTracker::process: ERROR: ATTENTION: strange algorithm's behavior: trackedObjects[i].rect() is empty");
continue;
}
//correction by speed of rectangle
if (n > 1) {
Point center = centerRect (r);
Point center_prev = centerRect (trackedObjects [i].lastPositions [n - 2]);
Point shift = new Point ((center.x - center_prev.x) * innerParameters.coeffObjectSpeedUsingInPrediction,
(center.y - center_prev.y) * innerParameters.coeffObjectSpeedUsingInPrediction);
r.x += (int)Math.Round (shift.x);
r.y += (int)Math.Round (shift.y);
}
rectsWhereRegions [i] = r;
}
rects = rectsWhereRegions;
for (int i = 0; i < rects.Length; i++) {
Imgproc.rectangle (rgbaMat, new Point (rects [i].x, rects [i].y), new Point (rects [i].x + rects [i].width, rects [i].y + rects [i].height), new Scalar (0, 255, 0, 255), 2);
}
}
if (rectsWhereRegions.Length > 0) {
detectedObjectsInRegions.Clear ();
int len = rectsWhereRegions.Length;
for (int i = 0; i < len; i++) {
detectInRegion (grayMat, rectsWhereRegions [i], detectedObjectsInRegions);
}
}
updateTrackedObjects (detectedObjectsInRegions);
getObjects (resultObjects);
rects = resultObjects.ToArray ();
for (int i = 0; i < rects.Length; i++) {
//Debug.Log ("detect faces " + rects [i]);
Imgproc.rectangle (rgbaMat, new Point (rects [i].x, rects [i].y), new Point (rects [i].x + rects [i].width, rects [i].y + rects [i].height), new Scalar (255, 0, 0, 255), 2);
}
Utils.matToTexture2D (rgbaMat, texture, colors);
}
}
private void initThread ()
{
StopThread ();
grayMat4Thread = new Mat ();
cascade = new CascadeClassifier (Utils.getFilePath ("haarcascade_frontalface_alt.xml"));
if (cascade.empty ()) {
Debug.LogError ("cascade file is not loaded.Please copy from “OpenCVForUnity/StreamingAssets/” to “Assets/StreamingAssets/” folder. ");
}
threadComm.shouldDetectInMultiThread = false;
StartThread ();
}
#if UNITY_WSA
#if NETFX_CORE
private void ThreadWorker()
{
if (isThreadRunning) return;
Debug.Log("Thread Start");
isThreadRunning = true;
threadComm.shouldDetectInMultiThread = false;
didUpdateTheDetectionResult = false;
tokenSource_ = new CancellationTokenSource();
task_ =Task.Factory.StartNew(
(o)=>
{
ThreadComm comm = o as ThreadComm;
while(true)
{
tokenSource_.Token.ThrowIfCancellationRequested();
if(!comm.shouldDetectInMultiThread) continue;
MatOfRect faces = new MatOfRect();
if (cascade != null)
cascade.detectMultiScale(grayMat4Thread, faces, 1.1, 2, Objdetect.CASCADE_SCALE_IMAGE, // TODO: objdetect.CV_HAAR_SCALE_IMAGE
new Size(grayMat4Thread.height() * 0.2, grayMat4Thread.height() * 0.2), new Size());
lock (thisLock){
resultDetect = faces;
}
comm.shouldDetectInMultiThread = false;
didUpdateTheDetectionResult = true;
}
}
, threadComm
, tokenSource_.Token
).ContinueWith(t =>
{
tokenSource_.Dispose();
tokenSource_ = null;
isThreadRunning = false;
});
}
#else
private void ThreadWorker()
{
if (isThreadRunning) return;
Debug.Log("Thread Start");
isThreadRunning = true;
threadComm.shouldDetectInMultiThread = false;
didUpdateTheDetectionResult = false;
StartCoroutine("ThreadCoroutine", threadComm);
}
IEnumerator ThreadCoroutine(System.Object o)
{
ThreadComm comm = o as ThreadComm;
while (true)
{
while (!comm.shouldDetectInMultiThread) { yield return null; }
MatOfRect faces = new MatOfRect();
if (cascade != null)
cascade.detectMultiScale(grayMat4Thread, faces, 1.1, 2, Objdetect.CASCADE_SCALE_IMAGE, // TODO: objdetect.CV_HAAR_SCALE_IMAGE
new Size(grayMat4Thread.height() * 0.2, grayMat4Thread.height() * 0.2), new Size());
lock (thisLock){
resultDetect = faces;
}
comm.shouldDetectInMultiThread = false;
didUpdateTheDetectionResult = true;
}
}
#endif
#else
private void ThreadWorker ()
{
if (isThreadRunning)
return;
Debug.Log ("Thread Start");
isThreadRunning = true;
shouldStopThread = false;
threadComm.shouldDetectInMultiThread = false;
didUpdateTheDetectionResult = false;
ThreadPool.QueueUserWorkItem (_ThreadWorker, threadComm);
}
private void _ThreadWorker (System.Object o)
{
ThreadComm comm = o as ThreadComm;
while (!shouldStopThread) {
if (!comm.shouldDetectInMultiThread)
continue;
MatOfRect faces = new MatOfRect ();
if (cascade != null)
cascade.detectMultiScale (grayMat4Thread, faces, 1.1, 2, Objdetect.CASCADE_SCALE_IMAGE, // TODO: objdetect.CV_HAAR_SCALE_IMAGE
new Size (grayMat4Thread.height () * 0.2, grayMat4Thread.height () * 0.2), new Size ());
//Thread.Sleep(200);
lock (thisLock) {
resultDetect = faces;
}
comm.shouldDetectInMultiThread = false;
didUpdateTheDetectionResult = true;
}
isThreadRunning = false;
}
#endif
public class ThreadComm : System.Object
{
public bool shouldDetectInMultiThread = false;
}
public void StartThread ()
{
ThreadWorker ();
}
public void StopThread ()
{
if (!isThreadRunning)
return;
#if UNITY_WSA
#if NETFX_CORE
tokenSource_.Cancel();
task_ = null;
#else
StopCoroutine("ThreadCoroutine");
isThreadRunning = false;
#endif
#else
shouldStopThread = true;
#endif
while (isThreadRunning) {
//Wait threading stop
}
Debug.Log ("Thread Stop");
}
private void getObjects (List<Rect> result)
{
result.Clear ();
for (int i = 0; i < trackedObjects.Count; i++) {
Rect r = calcTrackedObjectPositionToShow (i);
if (r.area () == 0) {
continue;
}
result.Add (r);
//LOGD("DetectionBasedTracker::process: found a object with SIZE %d x %d, rect={%d, %d, %d x %d}", r.width, r.height, r.x, r.y, r.width, r.height);
}
}
public enum TrackedState : int
{
NEW_RECTANGLE = -1,
INTERSECTED_RECTANGLE = -2
}
private void updateTrackedObjects (List<Rect> detectedObjects)
{
int N1 = (int)trackedObjects.Count;
int N2 = (int)detectedObjects.Count;
for (int i = 0; i < N1; i++) {
trackedObjects [i].numDetectedFrames++;
}
int[] correspondence = new int[N2];
for (int i = 0; i < N2; i++) {
correspondence [i] = (int)TrackedState.NEW_RECTANGLE;
}
for (int i = 0; i < N1; i++) {
TrackedObject curObject = trackedObjects [i];
int bestIndex = -1;
int bestArea = -1;
int numpositions = (int)curObject.lastPositions.Count;
//if (numpositions > 0) UnityEngine.Debug.LogError("numpositions > 0 is false");
Rect prevRect = curObject.lastPositions [numpositions - 1];
for (int j = 0; j < N2; j++) {
if (correspondence [j] >= 0) {
//Debug.Log("DetectionBasedTracker::updateTrackedObjects: j=" + i + " is rejected, because it has correspondence=" + correspondence[j]);
continue;
}
if (correspondence [j] != (int)TrackedState.NEW_RECTANGLE) {
//Debug.Log("DetectionBasedTracker::updateTrackedObjects: j=" + j + " is rejected, because it is intersected with another rectangle");
continue;
}
Rect r = Intersect (prevRect, detectedObjects [j]);
if ((r.width > 0) && (r.height > 0)) {
//LOGD("DetectionBasedTracker::updateTrackedObjects: There is intersection between prevRect and detectedRect, r={%d, %d, %d x %d}",
// r.x, r.y, r.width, r.height);
correspondence [j] = (int)TrackedState.INTERSECTED_RECTANGLE;
if (r.area () > bestArea) {
//LOGD("DetectionBasedTracker::updateTrackedObjects: The area of intersection is %d, it is better than bestArea=%d", r.area(), bestArea);
bestIndex = j;
bestArea = (int)r.area ();
}
}
}
if (bestIndex >= 0) {
//LOGD("DetectionBasedTracker::updateTrackedObjects: The best correspondence for i=%d is j=%d", i, bestIndex);
correspondence [bestIndex] = i;
for (int j = 0; j < N2; j++) {
if (correspondence [j] >= 0)
continue;
Rect r = Intersect (detectedObjects [j], detectedObjects [bestIndex]);
if ((r.width > 0) && (r.height > 0)) {
//LOGD("DetectionBasedTracker::updateTrackedObjects: Found intersection between "
// "rectangles j=%d and bestIndex=%d, rectangle j=%d is marked as intersected", j, bestIndex, j);
correspondence [j] = (int)TrackedState.INTERSECTED_RECTANGLE;
}
}
} else {
//LOGD("DetectionBasedTracker::updateTrackedObjects: There is no correspondence for i=%d ", i);
curObject.numFramesNotDetected++;
}
}
//LOGD("DetectionBasedTracker::updateTrackedObjects: start second cycle");
for (int j = 0; j < N2; j++) {
int i = correspondence [j];
if (i >= 0) {//add position
//Debug.Log("DetectionBasedTracker::updateTrackedObjects: add position");
trackedObjects [i].lastPositions.Add (detectedObjects [j]);
while ((int)trackedObjects[i].lastPositions.Count > (int)innerParameters.numLastPositionsToTrack) {
trackedObjects [i].lastPositions.Remove (trackedObjects [i].lastPositions [0]);
}
trackedObjects [i].numFramesNotDetected = 0;
} else if (i == (int)TrackedState.NEW_RECTANGLE) { //new object
//Debug.Log("DetectionBasedTracker::updateTrackedObjects: new object");
trackedObjects.Add (new TrackedObject (detectedObjects [j]));
} else {
Debug.Log ("DetectionBasedTracker::updateTrackedObjects: was auxiliary intersection");
}
}
int t = 0;
TrackedObject it;
while (t < trackedObjects.Count) {
it = trackedObjects [t];
if ((it.numFramesNotDetected > parameters.maxTrackLifetime)
||
((it.numDetectedFrames <= innerParameters.numStepsToWaitBeforeFirstShow)
&&
(it.numFramesNotDetected > innerParameters.numStepsToTrackWithoutDetectingIfObjectHasNotBeenShown))
) {
//int numpos = (int)it.lastPositions.Count;
//if (numpos > 0) UnityEngine.Debug.LogError("numpos > 0 is false");
//Rect r = it.lastPositions [numpos - 1];
//Debug.Log("DetectionBasedTracker::updateTrackedObjects: deleted object " + r.x + " " + r.y + " " + r.width + " " + r.height);
trackedObjects.Remove (it);
} else {
t++;
}
}
}
private void detectInRegion (Mat img, Rect r, List<Rect> detectedObjectsInRegions)
{
Rect r0 = new Rect (new Point (), img.size ());
Rect r1 = new Rect (r.x, r.y, r.width, r.height);
Inflate (r1, (int)((r1.width * innerParameters.coeffTrackingWindowSize) - r1.width) / 2,
(int)((r1.height * innerParameters.coeffTrackingWindowSize) - r1.height) / 2);
r1 = Intersect (r0, r1);
if ((r1.width <= 0) || (r1.height <= 0)) {
Debug.Log ("DetectionBasedTracker::detectInRegion: Empty intersection");
return;
}
int d = Math.Min (r.width, r.height);
d = (int)Math.Round (d * innerParameters.coeffObjectSizeToTrack);
MatOfRect tmpobjects = new MatOfRect ();
Mat img1 = new Mat (img, r1);//subimage for rectangle -- without data copying
regionCascade.detectMultiScale (img1, tmpobjects, 1.1, 2, 0 | Objdetect.CASCADE_DO_CANNY_PRUNING | Objdetect.CASCADE_SCALE_IMAGE | Objdetect.CASCADE_FIND_BIGGEST_OBJECT, new Size (d, d), new Size ());
Rect[] tmpobjectsArray = tmpobjects.toArray ();
int len = tmpobjectsArray.Length;
for (int i = 0; i < len; i++) {
Rect tmp = tmpobjectsArray [i];
Rect curres = new Rect (new Point (tmp.x + r1.x, tmp.y + r1.y), tmp.size ());
detectedObjectsInRegions.Add (curres);
}
}
private Rect calcTrackedObjectPositionToShow (int i)
{
if ((i < 0) || (i >= trackedObjects.Count)) {
Debug.Log ("DetectionBasedTracker::calcTrackedObjectPositionToShow: ERROR: wrong i=" + i);
return new Rect ();
}
if (trackedObjects [i].numDetectedFrames <= innerParameters.numStepsToWaitBeforeFirstShow) {
//Debug.Log("DetectionBasedTracker::calcTrackedObjectPositionToShow: " + "trackedObjects[" + i + "].numDetectedFrames=" + trackedObjects[i].numDetectedFrames + " <= numStepsToWaitBeforeFirstShow=" + innerParameters.numStepsToWaitBeforeFirstShow + " --- return empty Rect()");
return new Rect ();
}
if (trackedObjects [i].numFramesNotDetected > innerParameters.numStepsToShowWithoutDetecting) {
return new Rect ();
}
List<Rect> lastPositions = trackedObjects [i].lastPositions;
int N = lastPositions.Count;
if (N <= 0) {
Debug.Log ("DetectionBasedTracker::calcTrackedObjectPositionToShow: ERROR: no positions for i=" + i);
return new Rect ();
}
int Nsize = Math.Min (N, (int)weightsSizesSmoothing.Count);
int Ncenter = Math.Min (N, (int)weightsPositionsSmoothing.Count);
Point center = new Point ();
double w = 0, h = 0;
if (Nsize > 0) {
double sum = 0;
for (int j = 0; j < Nsize; j++) {
int k = N - j - 1;
w += lastPositions [k].width * weightsSizesSmoothing [j];
h += lastPositions [k].height * weightsSizesSmoothing [j];
sum += weightsSizesSmoothing [j];
}
w /= sum;
h /= sum;
} else {
w = lastPositions [N - 1].width;
h = lastPositions [N - 1].height;
}
if (Ncenter > 0) {
double sum = 0;
for (int j = 0; j < Ncenter; j++) {
int k = N - j - 1;
Point tl = lastPositions [k].tl ();
Point br = lastPositions [k].br ();
Point c1;
//c1=tl;
//c1=c1* 0.5f;//
c1 = new Point (tl.x * 0.5f, tl.y * 0.5f);
Point c2;
//c2=br;
//c2=c2*0.5f;
c2 = new Point (br.x * 0.5f, br.y * 0.5f);
//c1=c1+c2;
c1 = new Point (c1.x + c2.x, c1.y + c2.y);
//center=center+ (c1 * weightsPositionsSmoothing[j]);
center = new Point (center.x + (c1.x * weightsPositionsSmoothing [j]), center.y + (c1.y * weightsPositionsSmoothing [j]));
sum += weightsPositionsSmoothing [j];
}
//center *= (float)(1 / sum);
center = new Point (center.x * (1 / sum), center.y * (1 / sum));
} else {
int k = N - 1;
Point tl = lastPositions [k].tl ();
Point br = lastPositions [k].br ();
Point c1;
//c1=tl;
//c1=c1* 0.5f;
c1 = new Point (tl.x * 0.5f, tl.y * 0.5f);
Point c2;
//c2=br;
//c2=c2*0.5f;
c2 = new Point (br.x * 0.5f, br.y * 0.5f);
//center=c1+c2;
center = new Point (c1.x + c2.x, c1.y + c2.y);
}
//Point2f tl=center-(Point2f(w,h)*0.5);
Point tl2 = new Point (center.x - (w * 0.5f), center.y - (h * 0.5f));
//Rect res(cvRound(tl.x), cvRound(tl.y), cvRound(w), cvRound(h));
Rect res = new Rect ((int)Math.Round (tl2.x), (int)Math.Round (tl2.y), (int)Math.Round (w), (int)Math.Round (h));
//LOGD("DetectionBasedTracker::calcTrackedObjectPositionToShow: Result for i=%d: {%d, %d, %d x %d}", i, res.x, res.y, res.width, res.height);
return res;
}
public Point centerRect (Rect r)
{
return new Point (r.x + (r.width / 2), r.y + (r.height / 2));
}
public Rect Intersect (Rect a, Rect b)
{
int x1 = Math.Max (a.x, b.x);
int x2 = Math.Min (a.x + a.width, b.x + b.width);
int y1 = Math.Max (a.y, b.y);
int y2 = Math.Min (a.y + a.height, b.y + b.height);
if (x2 >= x1 && y2 >= y1)
return new Rect (x1, y1, x2 - x1, y2 - y1);
else
return new Rect ();
}
public Rect Inflate (Rect rect, int x, int y)
{
rect.x -= x;
rect.y -= y;
rect.width += (2 * x);
rect.height += (2 * y);
return rect;
}
/// <summary>
/// Raises the disable event.
/// </summary>
void OnDisable ()
{
webCamTextureToMatHelper.Dispose ();
}
/// <summary>
/// Raises the back button event.
/// </summary>
public void OnBackButton ()
{
#if UNITY_5_3 || UNITY_5_3_OR_NEWER
SceneManager.LoadScene ("OpenCVForUnitySample");
#else
Application.LoadLevel ("OpenCVForUnitySample");
#endif
}
/// <summary>
/// Raises the play button event.
/// </summary>
public void OnPlayButton ()
{
webCamTextureToMatHelper.Play ();
}
/// <summary>
/// Raises the pause button event.
/// </summary>
public void OnPauseButton ()
{
webCamTextureToMatHelper.Pause ();
}
/// <summary>
/// Raises the stop button event.
/// </summary>
public void OnStopButton ()
{
webCamTextureToMatHelper.Stop ();
}
/// <summary>
/// Raises the change camera button event.
/// </summary>
public void OnChangeCameraButton ()
{
webCamTextureToMatHelper.Init (null, webCamTextureToMatHelper.requestWidth, webCamTextureToMatHelper.requestHeight, !webCamTextureToMatHelper.requestIsFrontFacing);
}
public struct Parameters
{
//public int minObjectSize;
//public int maxObjectSize;
//public float scaleFactor;
//public int minNeighbors;
public int maxTrackLifetime;
//public int minDetectionPeriod; //the minimal time between run of the big object detector (on the whole frame) in ms (1000 mean 1 sec), default=0
};
public struct InnerParameters
{
public int numLastPositionsToTrack;
public int numStepsToWaitBeforeFirstShow;
public int numStepsToTrackWithoutDetectingIfObjectHasNotBeenShown;
public int numStepsToShowWithoutDetecting;
public float coeffTrackingWindowSize;
public float coeffObjectSizeToTrack;
public float coeffObjectSpeedUsingInPrediction;
};
public class TrackedObject
{
public PositionsVector lastPositions;
public int numDetectedFrames;
public int numFramesNotDetected;
public int id;
static private int _id = 0;
public TrackedObject (OpenCVForUnity.Rect rect)
{
lastPositions = new PositionsVector ();
numDetectedFrames = 1;
numFramesNotDetected = 0;
lastPositions.Add (rect.clone ());
_id = getNextId ();
id = _id;
}
static int getNextId ()
{
_id++;
return _id;
}
}
}
}