/** * Start Companion creation and configuration on button click. * * @param sender sender of the event * @param e routed event data */ private async void StartButton_Click(object sender, RoutedEventArgs e) { if (!this.isRunning && this.isReady) { this.isRunning = true; this.Reset(); this.startButton.IsEnabled = false; this.radioButtons.ForEach(r => r.IsEnabled = false); try { // Create a Companion configuration object object tag = this.radioButtons.FirstOrDefault(r => (bool)r.IsChecked).Tag; await this.CreateConfiguration((ImageProcessingMethod)Int32.Parse(tag.ToString())); // Constantly import images as the source for processing Task imageThread = Task.Run(async() => { try { // Save the source image paths to a list for the 'ImageStream' object IReadOnlyList <StorageFile> fileList = await this.imageFolder.GetFilesAsync(); for (int i = 0; (i < fileList.Count) && !this.isCanceling; i++) { // Use this control to adjust the streaming rate //await Task.Delay(TimeSpan.FromSeconds(2)); this.configuration?.getSource()?.addImage(fileList[i].Path); if (i == 20) { throw new Exception("imageThread"); } } } catch (Exception ex) { await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { this.HandleException(ex); }); } }); // Activate the cancle button this.cancelButton.IsEnabled = true; // Run Companion on a background thread await this.RunCompanion(); // Wait until the image thread has stopped or finished await imageThread; } catch (Exception ex) { this.HandleException(ex); } // Reset the application this.startButton.IsEnabled = true; this.cancelButton.IsEnabled = false; this.radioButtons.ForEach(r => r.IsEnabled = true); this.isRunning = false; this.isCanceling = false; this.configuration = null; this.UpdateUIOutput("Processing has been stopped."); } else if (this.isReady) { this.UpdateUIOutput("Processing has already started."); } }
/** * Create a 'Configuration' wrapper object. * * @param algorithm * @return returns a 'Configuration' wrapper object asynchronously */ private IAsyncAction CreateConfiguration(ImageProcessingMethod processingMethod) { return(Task.Run(() => { this.configuration = new CW.Configuration(); // Create algorithms CW.FeatureMatching feature = new CW.FeatureMatching(CW.FeatureDetector.BRISK, CW.DescriptorMatcherType.BRUTEFORCE_HAMMING); CW.LSH lsh = new CW.LSH(); CW.ShapeDetection polygonDetection = new CW.ShapeDetection(); CW.ShapeDetection quadDetection = new CW.ShapeDetection(4, 4, "Quad"); // Configure image processing CW.MatchRecognition matchRecognition = new CW.MatchRecognition(feature, CW.Scaling.SCALE_640x360); CW.HashRecognition hashRecognition = new CW.HashRecognition(polygonDetection, lsh); CW.HybridRecognition hybridRecognition = new CW.HybridRecognition(hashRecognition, feature, 50); CW.ObjectDetection objectDetection = new CW.ObjectDetection(quadDetection); // Set callback methods (result image should have an alpha channel (required for WritableBitmap)) this.configuration.setResultCallback(this.ResultCallback, CW.ColorFormat.BGRA); this.configuration.setErrorCallback(this.ErrorCallback); // Set number of frames to skip this.configuration.setSkipFrame(0); // Set image buffer this.configuration.setImageBuffer(5); // Add source to configuaration CW.ImageStream stream = new CW.ImageStream(30); this.configuration.setSource(stream); // Create image models string model0 = this.assets.Path + "\\poster_left.jpg"; string model1 = this.assets.Path + "\\poster_right.jpg"; matchRecognition.addModel(new CW.FeatureMatchingModel(model0, 0)); matchRecognition.addModel(new CW.FeatureMatchingModel(model1, 1)); hashRecognition.addModel(model0, 0); hashRecognition.addModel(model1, 1); hybridRecognition.addModel(model0, 0); hybridRecognition.addModel(model1, 1); // Choose processing method switch (processingMethod) { case ImageProcessingMethod.FEATUE_MATCHING: this.configuration.setProcessing(matchRecognition); break; case ImageProcessingMethod.IMAGE_HASHING: this.configuration.setProcessing(hashRecognition); break; case ImageProcessingMethod.HYBRID_MATCHING: this.configuration.setProcessing(hybridRecognition); break; case ImageProcessingMethod.SHAPE_DETECTION: this.configuration.setProcessing(objectDetection); break; default: throw new Exception("Image processing method not found."); } }).AsAsyncAction()); }
/** * Use this for initialization. */ private void Start() { // Variable initialization this.ObjectRecognitionConfigured = false; this.CurrentScanState = ScanState.None; #if ENABLE_WINMD_SUPPORT this.newCameraMatrices = false; this.worldAnchors = new bool[this.artworkLimit]; // initialized with false #endif this.artworks = new Dictionary <int, Artwork>(); this.artists = new Dictionary <int, Artist>(); this.videos = new Dictionary <int, Video>(); this.audio = new Dictionary <int, Audio>(); this.worldAnchorsLoaded = false; this.newRoom = true; this.newData = false; this.recognitions = new Dictionary <int, Recognition>(); this.instantiatedArtworks = new Dictionary <int, GameObject>(); this.recognizedArtworks = new bool[this.artworkLimit]; // initialized with false this.fmModelOperations = new Queue <FmModelOperationInfo>(); this.isPerformingFmModelOperation = false; // Get reference to other scripts in the scene this.logger = DebugLogger.Instance; this.dataLoader = DataLoader.Instance; this.initializer = Initializer.Instance; this.capture = CaptureManager.IsInitialized ? CaptureManager.Instance.PhotoCapture : null; this.infoCanvas = InfoCanvas.Instance; this.anchorManager = HoloToolkit.Unity.WorldAnchorManager.Instance; // Deactivate this script if necessary components are missing if ((this.scanningArea == null) || (this.artwork == null) || (this.debugPhoto == null) || (this.logger == null) || (this.initializer == null) || (this.dataLoader == null) || (this.capture == null) || (this.infoCanvas == null) || (this.anchorManager == null)) { Debug.LogError("ObjectRecognition: Script references not set properly."); this.enabled = false; return; } // Get the current camera resolution this.cameraResolution = this.capture.CameraResolution; this.logger.Log("Set new camera resolution: " + this.cameraResolution.width + " x " + this.cameraResolution.height); #if ENABLE_WINMD_SUPPORT // Create a configuration object for Companion this.configuration = new CW.Configuration(); // Configure image recognition CW.FeatureMatching feature = new CW.FeatureMatching(CW.FeatureDetector.BRISK, CW.DescriptorMatcherType.BRUTEFORCE_HAMMING); CW.LSH lsh = new CW.LSH(); CW.ShapeDetection shapeDetection = new CW.ShapeDetection(); // Configure image processing this.matchRecognition = new CW.MatchRecognition(feature, CW.Scaling.SCALE_960x540); this.hashRecognition = new CW.HashRecognition(shapeDetection, lsh); this.hybridRecognition = new CW.HybridRecognition(this.hashRecognition, feature, 70); // Set callback methods this.configuration.setResultCallback(this.ResultCallback, CW.ColorFormat.RGB); this.configuration.setErrorCallback(this.ErrorCallback); // Set number of frames to skip this.configuration.setSkipFrame(0); // Set image buffer this.configuration.setImageBuffer(1); // Add source to configuaration CW.ImageStream stream = new CW.ImageStream(1); this.configuration.setSource(stream); #endif }