コード例 #1
0
        void UseTextRecognitionModel()
        {
            System.Diagnostics.Debug.WriteLine($"##### {currentTxtRecogScript}");

            CommonTextRecognizerOptions options = currentTxtRecogScript switch {
                TextRecognitionScript.Latin => new LatinTextRecognizerOptions(),
                TextRecognitionScript.Chinese => new ChineseTextRecognizerOptions(),
                TextRecognitionScript.Devanagari => new DevanagariTextRecognizerOptions(),
                TextRecognitionScript.Japanese => new JapaneseTextRecognizerOptions(),
                TextRecognitionScript.Korean => new KoreanTextRecognizerOptions(),
                _ => throw new NotImplementedException()
            };

            var textRecognizer = TextRecognizer.TextRecognizerWithOptions(options);
            var image          = new MLImage(ImgSample.Image);

            textRecognizer.ProcessImage(image, (text, err) => {
                TxtData.Text = err?.Description ?? text?.Text;
            });
        }

        void UseFaceDetectionModel()
        {
            var options = new FaceDetectorOptions();

            options.PerformanceMode    = FacePerformanceMode.Accurate;
            options.LandmarkMode       = FaceLandmarkMode.All;
            options.ClassificationMode = FaceClassificationMode.All;
            var faceDetector = FaceDetector.FaceDetectorWithOptions(options);

            var image = new MLImage(ImgSample.Image);

            faceDetector.ProcessImage(image, HandleFaceDetectorCallback);

            void HandleFaceDetectorCallback(Face [] faces, NSError error)
            {
                if (error != null)
                {
                    TxtData.Text = error.Description;
                    return;
                }

                if (faces == null || faces.Length == 0)
                {
                    TxtData.Text = "No faces were found.";
                    return;
                }

                var imageSize = ImgSample.Image.Size;

                UIGraphics.BeginImageContextWithOptions(imageSize, false, 0);
                var context = UIGraphics.GetCurrentContext();

                context.SetStrokeColor(UIColor.Red.CGColor);
                context.SetLineWidth(10);

                ImgSample.Image.Draw(CGPoint.Empty);

                foreach (var face in faces)
                {
                    context.AddRect(face.Frame);
                    context.DrawPath(CGPathDrawingMode.Stroke);
                }

                var newImage = UIGraphics.GetImageFromCurrentImageContext();

                UIGraphics.EndImageContext();

                ImgSample.Image = newImage;
            }
        }

        void UseBarcodeScanningModel()
        {
            var options        = new BarcodeScannerOptions(BarcodeFormat.All);
            var barcodeScanner = BarcodeScanner.BarcodeScannerWithOptions(options);

            var image = new MLImage(ImgSample.Image);

            barcodeScanner.ProcessImage(image, HandleBarcodeScannerCallback);

            void HandleBarcodeScannerCallback(Barcode [] barcodes, NSError error)
            {
                if (error != null)
                {
                    TxtData.Text = error.Description;
                    return;
                }

                if (barcodes == null || barcodes.Length == 0)
                {
                    TxtData.Text = "No barcodes were found.";
                    return;
                }

                var stringBuilder = new StringBuilder();

                foreach (var barcode in barcodes)
                {
                    stringBuilder.AppendLine($"Raw Value: {barcode.RawValue}");
                    stringBuilder.AppendLine($"Display Value: {barcode.DisplayValue}");
                    stringBuilder.AppendLine($"Format: {barcode.Format}");
                    stringBuilder.AppendLine($"Value Type: {barcode.ValueType}");
                    stringBuilder.AppendLine();
                }

                TxtData.Text = stringBuilder.ToString();
            }
        }

        void UseDigitalInkRecognitionModel()
        {
            strokes.Clear();

            if (inkRecognizer == null)
            {
                var lang         = "en-US";
                var identifier   = DigitalInkRecognitionModelIdentifier.ModelIdentifierForLanguageTag(lang);
                var model        = new DigitalInkRecognitionModel(identifier);
                var modelManager = ModelManager.DefaultInstance;
                var conditions   = new ModelDownloadConditions(true, true);
                // This works on device, but downloads seems to fail on simulators
                modelManager.DownloadModel(model, conditions);

                var options = new DigitalInkRecognizerOptions(model);
                inkRecognizer = DigitalInkRecognizer.DigitalInkRecognizerWithOptions(options);
            }
        }

        void UseImageLabeling()
        {
            var options = new ImageLabelerOptions();

            options.ConfidenceThreshold = 0.7;
            var labeler = ImageLabeler.ImageLabelerWithOptions(options);

            var image = new MLImage(ImgSample.Image);

            labeler.ProcessImage(image, (labels, error) => {
                if (error != null)
                {
                    TxtData.Text = error.Description;
                    return;
                }

                if (labels == null || labels.Length == 0)
                {
                    TxtData.Text = "No labels were found.";
                    return;
                }

                var stringBuilder = new StringBuilder();

                for (var i = 0; i < labels.Length; i++)
                {
                    stringBuilder.AppendLine($"Label: {i}");
                    stringBuilder.AppendLine($"Text: {labels [i].Text}");
                    stringBuilder.AppendLine($"Confidence: {labels [i].Confidence}");
                    stringBuilder.AppendLine($"Index: {labels [i].Index}");
                    stringBuilder.AppendLine();
                }

                TxtData.Text = stringBuilder.ToString();
            });
        }

        void UseObjectDetectionAndTracking()
        {
            var options = new ObjectDetectorOptions();

            options.Mode = DetectorMode.SingleImage;
            options.ShouldEnableClassification  = true;
            options.ShouldEnableMultipleObjects = true;
            var objectDetector = ObjectDetector.ObjectDetectorWithOptions(options);

            var image = new MLImage(ImgSample.Image);

            objectDetector.ProcessImage(image, (objects, error) => {
                if (error != null)
                {
                    TxtData.Text = error.Description;
                    return;
                }

                if (objects == null || objects.Length == 0)
                {
                    TxtData.Text = "No objects were found.";
                    return;
                }

                var imageSize = ImgSample.Image.Size;

                UIGraphics.BeginImageContextWithOptions(imageSize, false, 0);
                var context = UIGraphics.GetCurrentContext();
                context.SetStrokeColor(UIColor.Red.CGColor);
                context.SetLineWidth(10);

                ImgSample.Image.Draw(CGPoint.Empty);

                var stringBuilder = new StringBuilder();

                for (var i = 0; i < objects.Length; i++)
                {
                    stringBuilder.AppendLine($"Object: {i}");
                    stringBuilder.AppendLine($"Tracking ID: {objects [i].TrackingId?.Int32Value ?? 0}");

                    foreach (var lbl in objects[i].Labels)
                    {
                        stringBuilder.AppendLine($" - Text: {lbl.Text}");
                        stringBuilder.AppendLine($" - Confidence: {lbl.Confidence}");
                        stringBuilder.AppendLine($" - Index: {lbl.Index}");
                    }

                    stringBuilder.AppendLine();

                    context.AddRect(objects [i].Frame);
                    context.DrawPath(CGPathDrawingMode.Stroke);
                }

                var newImage = UIGraphics.GetImageFromCurrentImageContext();
                UIGraphics.EndImageContext();

                ImgSample.Image = newImage;

                TxtData.Text = stringBuilder.ToString();
            });
        }

        #endregion

        #region Digital Ink
        const int msPerTimeInterval = 1000;