/**
  * Set newly recognized data for this artwork.
  *
  * @param info  recognition infomation
  */
 public void SetNewData(Recognition info)
 {
     this.artwork               = info.Artwork;
     this.name                  = ARTWORK_ANCHOR_PREFIX + this.artwork.artworkID;
     this.worldSpaceCamera      = info.WorldSpaceCamera;
     this.worldSpaceMiddle      = info.WorldSpaceMiddle;
     this.worldSpaceTopLeft     = info.WorldSpaceTopLeft;
     this.worldSpaceTopRight    = info.WorldSpaceTopRight;
     this.worldSpaceBottomLeft  = info.WorldSpaceBottomLeft;
     this.worldSpaceBottomRight = info.WorldSpaceBottomRight;
     this.newData               = true;
 }
示例#2
0
        /**
         * Result callback method for the Companion processing.
         *
         * @param results   list of results that represent the recognized artworks
         * @param image     the processed image with visually markers for the recognized artworks
         */
        public void ResultCallback(IList <CW.Result> results, byte[] image)
        {
            // Ignore the result if the camera matrices of the processed photo were not obtained successfully
            if (!this.newCameraMatrices || (results.Count == 0))
            {
                this.logger.Log("No results.");
                this.ProcessingStopped();
                return;
            }

            // Reset camera matrices for this photo
            this.newCameraMatrices = false;

            bool    newResults = false;
            Vector3 worldSpaceCamera;
            Vector3 worldSpaceMiddle;
            Vector3 worldSpaceTopLeft;
            Vector3 worldSpaceTopRight;
            Vector3 worldSpaceBottomLeft;
            Vector3 worldSpaceBottomRight;

            CW.Point upperLeftCorner;
            upperLeftCorner.x = 0;
            upperLeftCorner.y = 0;

            CW.Point upperRightCorner;
            upperRightCorner.x = 0;
            upperRightCorner.y = 0;

            CW.Point bottomRightCorner;
            bottomRightCorner.x = 0;
            bottomRightCorner.y = 0;

            CW.Point bottomLeftCorner;
            bottomLeftCorner.x = 0;
            bottomLeftCorner.y = 0;

            // ToDo: MultiplyPoint3x4 !!
            worldSpaceCamera = this.cameraToWorld.MultiplyPoint(new Vector3(0, 0, 0 /*, 1*/)); // camera location in world space

            // float casting
            float image_width  = this.cameraResolution.width;
            float image_height = this.cameraResolution.height;

            foreach (CW.Result result in results)
            {
                int artworkID = result.getID();

                // Ignore results for artworks, the ID of which is not within the eligible range
                if ((result.getType() != CW.ResultType.RECOGNITION) || !this.IsIdEligible(artworkID))
                {
                    this.logger.Log("Artwork " + artworkID + " has an ineligible ID.");
                    continue;
                }

                // Ignore results for artworks that were already recognized
                if (this.recognizedArtworks[artworkID])
                {
                    this.logger.Log("Artwork " + artworkID + " already recognized.");
                    continue;
                }

                this.logger.Log("Corners found for artwork " + artworkID + ".");

                upperLeftCorner   = result.getFrame().getUpperLeftCorner();
                upperRightCorner  = result.getFrame().getUpperRightCorner();
                bottomRightCorner = result.getFrame().getLowerRightCorner();
                bottomLeftCorner  = result.getFrame().getLowerLeftCorner();

                float   middleX           = upperLeftCorner.x + ((upperRightCorner.x - upperLeftCorner.x) / 2.0f);
                float   middleY           = upperLeftCorner.y + ((bottomLeftCorner.y - upperLeftCorner.y) / 2.0f);
                Vector2 ImagePosZeroToOne = new Vector2(middleX / image_width, 1.0f - (middleY / image_height));
                Vector2 ImagePosProjected = ((ImagePosZeroToOne * 2.0f) - new Vector2(1.0f, 1.0f)); // -1 to 1 space
                Vector3 CameraSpacePos    = UnProjectVector(this.projection, new Vector3(ImagePosProjected.x, ImagePosProjected.y, 1.0f));
                worldSpaceMiddle = this.cameraToWorld.MultiplyPoint(CameraSpacePos);                // ray point in world space

                ImagePosZeroToOne = new Vector2(upperLeftCorner.x / image_width, 1.0f - (upperLeftCorner.y / image_height));
                ImagePosProjected = ((ImagePosZeroToOne * 2.0f) - new Vector2(1.0f, 1.0f)); // -1 to 1 space
                CameraSpacePos    = UnProjectVector(this.projection, new Vector3(ImagePosProjected.x, ImagePosProjected.y, 1.0f));
                worldSpaceTopLeft = this.cameraToWorld.MultiplyPoint(CameraSpacePos);       // ray point in world space

                ImagePosZeroToOne  = new Vector2(upperRightCorner.x / image_width, 1.0f - (upperRightCorner.y / image_height));
                ImagePosProjected  = ((ImagePosZeroToOne * 2.0f) - new Vector2(1.0f, 1.0f)); // -1 to 1 space
                CameraSpacePos     = UnProjectVector(this.projection, new Vector3(ImagePosProjected.x, ImagePosProjected.y, 1.0f));
                worldSpaceTopRight = this.cameraToWorld.MultiplyPoint(CameraSpacePos);       // ray point in world space

                ImagePosZeroToOne     = new Vector2(bottomRightCorner.x / image_width, 1.0f - (bottomRightCorner.y / image_height));
                ImagePosProjected     = ((ImagePosZeroToOne * 2.0f) - new Vector2(1.0f, 1.0f)); // -1 to 1 space
                CameraSpacePos        = UnProjectVector(this.projection, new Vector3(ImagePosProjected.x, ImagePosProjected.y, 1.0f));
                worldSpaceBottomRight = this.cameraToWorld.MultiplyPoint(CameraSpacePos);       // ray point in world space

                ImagePosZeroToOne    = new Vector2(bottomLeftCorner.x / image_width, 1.0f - (bottomLeftCorner.y / image_height));
                ImagePosProjected    = ((ImagePosZeroToOne * 2.0f) - new Vector2(1.0f, 1.0f)); // -1 to 1 space
                CameraSpacePos       = UnProjectVector(this.projection, new Vector3(ImagePosProjected.x, ImagePosProjected.y, 1.0f));
                worldSpaceBottomLeft = this.cameraToWorld.MultiplyPoint(CameraSpacePos);       // ray point in world space

#if COMP_DEBUG_IMAGE
                // Calculate image corners in world space coordinates
                ImagePosZeroToOne = new Vector2(0.5f, 0.5f);
                ImagePosProjected = ((ImagePosZeroToOne * 2.0f) - new Vector2(1.0f, 1.0f)); // -1 to 1 space
                CameraSpacePos    = UnProjectVector(this.projection, new Vector3(ImagePosProjected.x, ImagePosProjected.y, 1.0f));
                Vector3 imageMiddle = this.cameraToWorld.MultiplyPoint(CameraSpacePos);     // ray point in world space

                ImagePosZeroToOne = new Vector2(0.0f, 1.0f);
                ImagePosProjected = ((ImagePosZeroToOne * 2.0f) - new Vector2(1.0f, 1.0f)); // -1 to 1 space
                CameraSpacePos    = UnProjectVector(this.projection, new Vector3(ImagePosProjected.x, ImagePosProjected.y, 1.0f));
                Vector3 imageTopLeft = this.cameraToWorld.MultiplyPoint(CameraSpacePos);    // ray point in world space

                ImagePosZeroToOne = new Vector2(1.0f, 1.0f);
                ImagePosProjected = ((ImagePosZeroToOne * 2.0f) - new Vector2(1.0f, 1.0f)); // -1 to 1 space
                CameraSpacePos    = UnProjectVector(this.projection, new Vector3(ImagePosProjected.x, ImagePosProjected.y, 1.0f));
                Vector3 imageTopRight = this.cameraToWorld.MultiplyPoint(CameraSpacePos);   // ray point in world space

                ImagePosZeroToOne = new Vector2(0.0f, 0.0f);
                ImagePosProjected = ((ImagePosZeroToOne * 2.0f) - new Vector2(1.0f, 1.0f)); // -1 to 1 space
                CameraSpacePos    = UnProjectVector(this.projection, new Vector3(ImagePosProjected.x, ImagePosProjected.y, 1.0f));
                Vector3 imageBottomLeft = this.cameraToWorld.MultiplyPoint(CameraSpacePos); // ray point in world space

                // Save result image
                this.resultImage = new ResultImage(image, worldSpaceCamera, imageMiddle, imageTopLeft, imageTopRight, imageBottomLeft);
#endif

                // Save information about the recognized artwork
                Artwork     artwork     = this.artworks[artworkID];
                Recognition recognition = new Recognition(artwork, worldSpaceCamera, worldSpaceMiddle, worldSpaceTopLeft, worldSpaceTopRight, worldSpaceBottomLeft, worldSpaceBottomRight);
                this.recognitions.Add(artworkID, recognition);

                newResults = true;
            }

            // Check if the found results were ignored because they were already recognized before
            if (!newResults)
            {
                this.logger.Log("Artworks already recognized.");
                this.ProcessingStopped();
            }
            else
            {
                // Beginn processing of new data
                this.newData = true;
            }
        }
示例#3
0
        /**
         * Update is called once per frame.
         */
        private void Update()
        {
            // Load world anchors
            if (!this.worldAnchorsLoaded)
            {
                this.LoadWorldAnchors();
            }

            // Start the object recognition if the data library as well as the world anchors are loaded
            if (this.worldAnchorsLoaded && this.dataLoader.DataLoadingFinished && !this.ObjectRecognitionConfigured && this.initializer.ReadyToStartRecognition)
            {
                this.InitializeData();

                // Start cached object recognition
                if (this.CurrentScanState == ScanState.UserMode)
                {
                    this.StartCachedRecognition();
                }

                this.ObjectRecognitionConfigured = true;
            }

            // Instantiate corresponding GameObjects for the newly recognized artworks
            if (this.newData)
            {
                bool validationFinished = true;
                foreach (KeyValuePair <int, Recognition> pair in this.recognitions)
                {
                    int         id   = pair.Key;
                    Recognition info = pair.Value;

                    // Ignore this artwork if its validation process has already finished
                    if (info.ValidationFinished)
                    {
                        continue;
                    }

                    // Check if the recognized artwork was already instantiated
                    if (this.instantiatedArtworks.ContainsKey(id))
                    {
                        GameObject obj = this.instantiatedArtworks[id];

                        // Check if the data was valid
                        int valid = obj.GetComponent <RecognizedArtwork>().IsValid();

                        // Artwork validation is not finished yet
                        if (valid == -1)
                        {
                            validationFinished = false;

                            // Artwork is valid
                        }
                        else if (valid == 1)
                        {
                            this.logger.Log("Artwork " + id + ": valid");
                            info.ValidationFinished = true;
                            if (this.IsIdEligible(id))
                            {
                                this.recognizedArtworks[id] = true;
                            }
#if ENABLE_WINMD_SUPPORT
                            // Remove artwork from further object recognition cycles
                            if ((this.CurrentScanState != ScanState.LiveScan) || (this.liveScanProcessing == ImageProcessingMethod.FEATURE_MATCHING))
                            {
                                this.matchRecognition.removeModel(id);
                                int count = this.matchRecognition.getModels().Count;
                                this.logger.Log("Count: " + count);
                                this.capture.IsActive(count > 0);
                                this.logger.Log("Artwork " + id + " removed from Companion.");
                                if ((this.CurrentScanState == ScanState.Caching) && (count == 0))
                                {
                                    this.infoCanvas.SetInfoText("All artworks scanned. <i>Bloom</i> to quit.", true);
                                }
                                else if (this.CurrentScanState == ScanState.Caching)
                                {
                                    this.infoCanvas.SetInfoText("Artwork #" + id + " scanned. " + count + " artworks left.");
                                }
                            }
#endif
                            // Artwork is not valid
                        }
                        else
                        {
                            this.logger.Log("Artwork " + id + ": not valid");
                            info.ValidationFinished = true;
                            // Destroy the already instantiated artwork
                            Destroy(obj);
                            this.instantiatedArtworks.Remove(id);
                        }

                        // Instantiate a recognized artwork
                    }
                    else
                    {
                        this.logger.Log("Artwork " + id + ": instantiate new artwork");
                        GameObject        artwork       = Instantiate(this.artwork);
                        RecognizedArtwork artworkScript = artwork.GetComponent <RecognizedArtwork>();
                        artworkScript.SetNewData(info);
                        validationFinished = false;
                        this.instantiatedArtworks.Add(id, artwork);
                    }
                }

                // Validation process finished
                if (validationFinished)
                {
#if COMP_DEBUG_IMAGE
                    // Instantiate the photo in the scene
                    GameObject debugPhoto = Instantiate(this.debugPhoto, this.resultImage.WorldSpaceMiddle, Utils.CreateLookAtRotation(this.resultImage.WorldSpaceMiddle, this.resultImage.WorldSpaceCamera, false));
                    this.resultImage.DisplayImage(debugPhoto.GetComponentInChildren <UnityEngine.UI.Image>().gameObject, this.cameraResolution);
#endif
                    this.newData = false;
                    this.recognitions.Clear();
                    this.ProcessingStopped();
                }
            }

            // Perform feature matching model operations
            if (this.ObjectRecognitionConfigured && !this.isPerformingFmModelOperation && (this.fmModelOperations.Count > 0))
            {
                this.isPerformingFmModelOperation = true;
                StartCoroutine(this.DoFeatureModelOperation(this.fmModelOperations.Dequeue()));
            }
        }