Ejemplo n.º 1
0
 public void UseUri(Uri predictionEndpoint)
 {
     _classifier?.Dispose();
     _classifier         = null;
     _predictionEndpoint = predictionEndpoint;
     _client             = new LobeClient(_predictionEndpoint);
 }
Ejemplo n.º 2
0
    private static Parser CreateParser()
    {
        var rootCommand = new RootCommand
        {
            Description = @"Classify images using onnx model or directly the http endpoint from the lobe app."
        };

        var signatureFileOption = new Option <FileInfo>("--signature-file", "signature file for model loading.");

        var imageFileOption = new Option <FileInfo>("--image-file", "image file to classify.");

        var imageFolderOption = new Option <FileInfo>("--image-folder", "folder that contain images to classify.");

        var predictionEndpointOption = new Option <Uri>("--prediction-endpoint", "prediction endpoint from lobe app.");

        rootCommand.AddOption(signatureFileOption);
        rootCommand.AddOption(imageFileOption);
        rootCommand.AddOption(imageFolderOption);
        rootCommand.AddOption(predictionEndpointOption);

        rootCommand.Handler = CommandHandler.Create <FileInfo, FileInfo, DirectoryInfo, Uri>(
            (signatureFile, imageFile, imageFolder, predictionEndpoint) =>
        {
            var images = GatherImages(imageFile, imageFolder);

            if (signatureFile is null && predictionEndpoint is null)
            {
                throw new InvalidOperationException("Must use a signature file or prediction endpoint.");
            }

            if (imageFile is null && imageFolder is null)
            {
                throw new InvalidOperationException("Must use a image file or image folder.");
            }

            if (signatureFile != null)
            {
                if (!signatureFile.Exists)
                {
                    throw new InvalidOperationException(
                        $"Signature file {signatureFile.FullName} does not exist.");
                }

                ImageClassifier.Register("onnx", () => new OnnxImageClassifier());

                using var classifier = ImageClassifier.CreateFromSignatureFile(
                          new FileInfo(signatureFile.FullName));

                foreach (var file in images)
                {
                    var results = Classify(file, classifier.Classify);

                    Console.WriteLine(results.Prediction.Label);
                }

                return(0);
            }

            if (predictionEndpoint != null)
            {
                var classifier = new LobeClient(predictionEndpoint);

                foreach (var file in images)
                {
                    var results = Classify(file, classifier.Classify);

                    Console.WriteLine(results.Prediction.Label);
                }

                return(0);
            }

            return(1);
        });

        return(new CommandLineBuilder(rootCommand)
               .UseDefaults()
               .Build());