private void DrawRects(Rect[] rects, float imageWidth, float imageHeight)
        {
            UnityEngine.Rect[] overlayRects = new UnityEngine.Rect[rects.Length];

            for (int i = 0; i < rects.Length; i++)
            {
                overlayRects[i] = new UnityEngine.Rect(rects[i].x / imageWidth
                                                       , rects[i].y / imageHeight
                                                       , rects[i].width / imageWidth
                                                       , rects[i].height / imageHeight);
            }
            rectOverlay.DrawRects(overlayRects);
        }
Esempio n. 2
0
        // Update is called once per frame
        void Update()
        {
            #if !UNITY_WEBGL || UNITY_EDITOR
            if (webCamTextureToMatHelper.IsPlaying() && webCamTextureToMatHelper.DidUpdateThisFrame())
            {
                Mat rgbaMat = webCamTextureToMatHelper.GetDownScaleMat(webCamTextureToMatHelper.GetMat());


                Mat rgbaMatClipROI = new Mat(rgbaMat, processingAreaRect);
                Imgproc.cvtColor(rgbaMatClipROI, grayMat, Imgproc.COLOR_RGBA2GRAY);

                // fill all black.
//                Imgproc.rectangle (rgbaMatClipROI, new Point (0, 0), new Point (rgbaMat.width (), rgbaMat.height ()), new Scalar (0, 0, 0, 0), -1);

                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 (rgbaMatClipROI, 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 (rgbaMatClipROI, 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);
                //getSmoothingObjects (resultObjects);


                int rectsCount = resultObjects.Count;
                UnityEngine.Rect[] overlayRects = new UnityEngine.Rect[rectsCount];

                float width   = rgbaMat.width();
                float height  = rgbaMat.height();
                float offsetX = processingAreaRect.x / width;
                float offsetY = processingAreaRect.y / height;

                for (int i = 0; i < resultObjects.Count; i++)
                {
                    overlayRects[i] = new UnityEngine.Rect(offsetX + (resultObjects[i].x / width)
                                                           , offsetY + (resultObjects[i].y / height)
                                                           , resultObjects[i].width / width
//                        , resultObjects[i].height/height);
                                                           , resultObjects[i].width / height);
                }
                rectOverlay.DrawRects(overlayRects);


                rgbaMatClipROI.Dispose();
            }

            if (webCamTextureToMatHelper.IsPlaying())
            {
                Matrix4x4 cameraToWorldMatrix = Camera.main.cameraToWorldMatrix;;

                Vector3 ccCameraSpacePos = UnProjectVector(projectionMatrix, new Vector3(0.0f, 0.0f, overlayDistance));
                Vector3 tlCameraSpacePos = UnProjectVector(projectionMatrix, new Vector3(-overlayDistance, overlayDistance, overlayDistance));

                //position
                Vector3 position = cameraToWorldMatrix.MultiplyPoint3x4(ccCameraSpacePos);
                gameObject.transform.position = position;

                //scale
                Vector3 scale = new Vector3(Mathf.Abs(tlCameraSpacePos.x - ccCameraSpacePos.x) * 2, Mathf.Abs(tlCameraSpacePos.y - ccCameraSpacePos.y) * 2, 1);
                gameObject.transform.localScale = scale;

                // Rotate the canvas object so that it faces the user.
                Quaternion rotation = Quaternion.LookRotation(-cameraToWorldMatrix.GetColumn(2), cameraToWorldMatrix.GetColumn(1));
                gameObject.transform.rotation = rotation;

                rectOverlay.UpdateOverlayTransform(gameObject.transform);
            }
            #else
            if (webCamTextureToMatHelper.IsPlaying() && webCamTextureToMatHelper.DidUpdateThisFrame())
            {
                Mat rgbaMat = webCamTextureToMatHelper.GetMat();
                Imgproc.putText(rgbaMat, "WebGL platform does not support multi-threading.", new Point(5, rgbaMat.rows() - 10), Core.FONT_HERSHEY_SIMPLEX, 0.5, new Scalar(255, 255, 255, 255), 1, Imgproc.LINE_AA, false);
                Utils.matToTexture2D(rgbaMat, texture, webCamTextureToMatHelper.GetBufferColors());
            }
            #endif
        }
        private void DrawRects(List <TrackedObject> objList, float imageWidth, float imageHeight)
        {
            TrackedObject[]    objs         = objList.ToArray();
            UnityEngine.Rect[] overlayRects = new UnityEngine.Rect[objs.Length];

            // Cleanup missing face
            Dictionary <int, Person> tmpPeople = new Dictionary <int, Person>();

            for (int i = 0; i < objs.Length; i++)
            {
                if (people.ContainsKey(objs[i].id))
                {
                    //Debug.Log("people[objs[i].id].recognizeState" + people[objs[i].id].recognizeState);
                    tmpPeople.Add(objs[i].id, people[objs[i].id]);
                }
            }
            people.Clear();
            people = tmpPeople;

            // Retrun fail recognize
            for (int i = 0; i < objs.Length; i++)
            {
                if (!people.ContainsKey(objs[i].id))
                {
                    Person           person = new Person(objs[i].position, grayMat4Thread);
                    WebRequestClient wrc    = new WebRequestClient();
                    StartCoroutine(wrc.Post(person));
                    people.Add(objs[i].id, person);
                }
                else
                {
                    if (people[objs[i].id].recognizeState == Person.RecognizeState.NOTRECOGNIZED)
                    {
                        if (people[objs[i].id].failReturnCount < FAILRETURNCOUNT)
                        {
                            // delay 24 frame for next recognize
                            if (people[objs[i].id].delayFrameCount > SKIPFRAMEPREFAIL)
                            {
                                people[objs[i].id].UpdateFace(objs[i].position, grayMat4Thread);
                                WebRequestClient wrc = new WebRequestClient();
                                StartCoroutine(wrc.Post(people[objs[i].id]));
                                people[objs[i].id].delayFrameCount = 0;
                                people[objs[i].id].failReturnCount++;
                            }
                            else
                            {
                                people[objs[i].id].delayFrameCount++;
                            }
                        }
                    }
                }

                var rect = rectangleTracker.GetSmoothingRect(i);
                //var rect = objs[i].position;


                overlayRects[i] = new UnityEngine.Rect(rect.x / imageWidth
                                                       , rect.y / imageHeight
                                                       , rect.width / imageWidth
                                                       , rect.height / imageHeight);
            }
            GameObject[] rst = rectOverlay.DrawRects(overlayRects);
            // Assume target spawned prefab are contain PersonOverlayer.
            if (rst != null)
            {
                int cnt = rst.Length;
                PersonOverlayer[] personOverlays = new PersonOverlayer[cnt];
                for (int i = 0; i < cnt; i++)
                {
                    personOverlays[i] = rst[i].GetComponent <PersonOverlayer>();

                    string username = "";
                    string detail   = "";
                    Color  color    = Color.yellow;
                    if (people.ContainsKey(objs[i].id))
                    {
                        username = people[objs[i].id].username;
                        detail   = people[objs[i].id].detail;
                        color    = people[objs[i].id].color;
                    }
                    //Debug.Log("username is " + username);
                    personOverlays[i].SetText(username, detail, color);
                }
            }
        }