Exemplo n.º 1
0
        public AnnotateImageResponse AnalyzeImage(Image image)
        {
            var client = new ImageAnnotatorClientBuilder();

            client.CredentialsPath = ("HackGT.json");
            var visClient = client.Build();

            AnnotateImageRequest request = new AnnotateImageRequest
            {
                Image    = image,
                Features =
                {
                    new Feature {
                        Type = Feature.Types.Type.LabelDetection
                    },
                    new Feature {
                        Type = Feature.Types.Type.LogoDetection
                    },
                    new Feature {
                        Type = Feature.Types.Type.ImageProperties
                    }
                }
            };

            return(visClient.Annotate(request));
        }
        private async Task <GoogleOcrResult> DoOcr(Stream imageAsStream, FileFormatEnum fileFormatEnum, DateTime start)
        {
            try
            {
                var preprocessedResult = _ocrPreProcessing.AjustOrientationAndSize(imageAsStream, fileFormatEnum);
                using (var stream = preprocessedResult.ImageFileStream)
                {
                    var builder = new ImageAnnotatorClientBuilder {
                        JsonCredentials = File.ReadAllText(_configurations.CredentialsJsonFile)
                    };
                    var client = await builder.BuildAsync();

                    var img = await Image.FromStreamAsync(stream);

                    var textAnnotations = await client.DetectTextAsync(img);

                    var rawGoogleOcrResult = RawGoogleOcrResult.CreateFrom(textAnnotations.ToList());

                    var content = _googleOcrParser.Execute(rawGoogleOcrResult, preprocessedResult.NewImageHeight,
                                                           preprocessedResult.NewImageWidth);
                    return(GoogleOcrResult.CreateSuccessResult(DateTime.Now.Subtract(start), content, rawGoogleOcrResult));
                }
            }
            catch (Exception e)
            {
                return(GoogleOcrResult.CreateErrorResult(DateTime.Now.Subtract(start), e));
            }
        }
Exemplo n.º 3
0
        private static async Task GetDataAsync(string path, GoogleCredential credential)
        {
            try
            {
                int           pos           = path.LastIndexOf("\\");
                string        strFileName   = path.Substring(pos + 1, path.Length - pos - 1);
                StorageClient storageClient = await StorageClient.CreateAsync(credential);

                storageClient.Service.HttpClient.Timeout = new TimeSpan(0, 10, 0);
                var bucket = await storageClient.GetBucketAsync("bucket ocr");

                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);

                using (MemoryStream memStream = new MemoryStream())
                {
                    await fs.CopyToAsync(memStream);

                    Google.Apis.Storage.v1.Data.Object googleDataObject;
                    googleDataObject = await storageClient.UploadObjectAsync(bucket.Name, "sinProcesar/" + strFileName, "application/pdf", memStream);
                }
                var asyncRequest = new AsyncAnnotateFileRequest
                {
                    InputConfig = new InputConfig
                    {
                        GcsSource = new GcsSource
                        {
                            Uri = $"gs://{bucket.Name}/sinProcesar/{strFileName}"
                        },
                        MimeType = "application/pdf"
                    },
                    OutputConfig = new OutputConfig
                    {
                        BatchSize      = 2,
                        GcsDestination = new GcsDestination
                        {
                            Uri = $"gs://{bucket.Name}/procesados/{strFileName.Split('.')[0]}"
                        }
                    }
                };
                asyncRequest.Features.Add(new Feature
                {
                    Type = Feature.Types.Type.DocumentTextDetection
                });

                List <AsyncAnnotateFileRequest> requests = new List <AsyncAnnotateFileRequest>();
                requests.Add(asyncRequest);
                var client = new ImageAnnotatorClientBuilder
                {
                    CredentialsPath = @"D:\Google_API_key_TFM.json"
                }.Build();
                var operation = client.AsyncBatchAnnotateFiles(requests);
                operation.PollUntilCompleted();
            }
            catch (Exception e) { }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="credentialsPath">Path to the credentials JSON file for
        /// access to Google VisionAI.</param>
        /// <param name="connString">Connection string used to connect to
        /// desired database.</param>
        public OCRTool(string credentialsPath, string connString)
        {
            _credentialsPath = credentialsPath;
            ImageAnnotatorClientBuilder clientBuiler =
                new ImageAnnotatorClientBuilder {
                CredentialsPath = credentialsPath
            };

            _client = clientBuiler.Build();

            _connectionString = connString;
        }
Exemplo n.º 5
0
        private ImageAnnotatorClient CreateClient(string apiKey)
        {
            if (string.IsNullOrWhiteSpace(apiKey))
            {
                return(ImageAnnotatorClient.Create());
            }

            var builder = new ImageAnnotatorClientBuilder();

            builder.JsonCredentials = apiKey;
            return(builder.Build());
        }
Exemplo n.º 6
0
        public override async Task SetupAsync()
        {
            byte[] data = Convert.FromBase64String(Environment.GetEnvironmentVariable("GOOGLE_SERVICE_CREDENTIALS_B64",
                                                                                      EnvironmentVariableTarget.Process));

            var builder = new ImageAnnotatorClientBuilder
            {
                CredentialsPath = null,
                JsonCredentials = Encoding.UTF8.GetString(data)
            };

            AnnotatorClient = await builder.BuildAsync();
        }
Exemplo n.º 7
0
        public void JwtAccessToken_GrpcClient()
        {
            Image image = LoadResourceImage("SydneyOperaHouse.jpg");
            ImageAnnotatorClientBuilder builder = new ImageAnnotatorClientBuilder
            {
                // Making sure we have no scopes so that a JWT is sent.
                Scopes = new string[0]
            };

            var client = builder.Build();
            var result = client.DetectLandmarks(image);

            Assert.NotEmpty(result);
        }
Exemplo n.º 8
0
    /// <summary>
    /// This function is used to create a new instance of ImageAnnotatorClient
    /// </summary>
    /// <returns></returns>
    public ImageAnnotatorClient CreateImageAnnotatorClient()
    {
        var credPath = _googleCloudConfig.CredentialsPath;

        Log.Information("Instantiates a client, cred {CredPath}", credPath);
        var clientBuilder = new ImageAnnotatorClientBuilder
        {
            CredentialsPath = credPath
        };

        var client = clientBuilder.Build();

        return(client);
    }
Exemplo n.º 9
0
        private static ImageAnnotatorClient MakeClient()
        {
            var credPath = BotSettings.GoogleCloudCredentialsPath.SanitizeSlash();

            Log.Information($"Instantiates a client, cred {credPath}");
            var clientBuilder = new ImageAnnotatorClientBuilder
            {
                CredentialsPath = credPath
            };

            var client = clientBuilder.Build();

            return(client);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Constructor for a feature extractor using Google Cloud Platform SDK, optimized for analysing a single image.
        /// </summary>
        /// <param name="imageStream"></param>
        /// <param name="credentialsPath"></param>
        public GcpFeatureExtractor(Stream imageStream, string credentialsPath)
        {
            SetRotation(SKCodec.Create(imageStream).EncodedOrigin);
            var size = System.Drawing.Image.FromStream(imageStream).Size;

            Width  = size.Width;
            Height = size.Height;
            var visionImage = Image.FromStream(imageStream);

            _client = new ImageAnnotatorClientBuilder()
            {
                CredentialsPath = credentialsPath
            }.Build();
            _image = Image.FromStream(imageStream);
        }
Exemplo n.º 11
0
        private static object DetectTextWithLocation(Image image)
        {
            // [START vision_set_endpoint]
            // Instantiate a client connected to the 'eu' location.
            var client = new ImageAnnotatorClientBuilder
            {
                Endpoint = new ServiceEndpoint("eu-vision.googleapis.com")
            }.Build();
            // [END vision_set_endpoint]
            var response = client.DetectText(image);

            foreach (var annotation in response)
            {
                if (annotation.Description != null)
                {
                    Console.WriteLine(annotation.Description);
                }
            }
            return(0);
        }
Exemplo n.º 12
0
    private static ImageAnnotatorClient MakeClient()
    {
        var credPath = BotSettings.GoogleCloudCredentialsPath.SanitizeSlash();

        Log.Information("Instantiates a client, cred {CredPath}", credPath);

        var clientBuilder = new ImageAnnotatorClientBuilder
        {
            CredentialsPath = credPath
        };

        var client = clientBuilder.Build();

        VideoIntelligenceService = new VideoIntelligenceServiceClientBuilder()
        {
            CredentialsPath = credPath
        }.Build();

        return(client);
    }
Exemplo n.º 13
0
        static void GoogleVisionRequest(string path)
        {
            ImageAnnotatorClientBuilder builder = new ImageAnnotatorClientBuilder
            {
                CredentialsPath = @"C:\Users\d4gei\Workspace\ScanAndHoardData\scanandhoard-11700d373751.json"
            };

            ImageAnnotatorClient client = builder.Build();

            var            image = Image.FromFile(path);
            TextAnnotation text  = client.DetectDocumentText(image);

            string fileName = path + ".txt";

            using (StreamWriter writer = new StreamWriter(fileName))
            {
                writer.Write(text.Text);
            }
            Console.WriteLine($"Text: {text.Text}");
            foreach (var page in text.Pages)
            {
                foreach (var block in page.Blocks)
                {
                    string box = string.Join(" - ", block.BoundingBox.Vertices.Select(v => $"({v.X}, {v.Y})"));
                    Console.WriteLine($"Block {block.BlockType} at {box}");
                    foreach (var paragraph in block.Paragraphs)
                    {
                        box = string.Join(" - ", paragraph.BoundingBox.Vertices.Select(v => $"({v.X}, {v.Y})"));
                        Console.WriteLine($"  Paragraph at {box}");
                        foreach (var word in paragraph.Words)
                        {
                            Console.WriteLine($"    Word: {string.Join("", word.Symbols.Select(s => s.Text))}");
                        }
                    }
                }
            }
        }
Exemplo n.º 14
0
        public static async Task <string> MakeOCRRequest(string imageFilePath)
        {
            string credentialsPath = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");
            var    image           = Image.FromFile(imageFilePath);
            ImageAnnotatorClientBuilder builder = new ImageAnnotatorClientBuilder
            {
                CredentialsPath = credentialsPath
            };

            try
            {
                ImageAnnotatorClient client = builder.Build();
                var response = await client.DetectTextAsync(image);

                string contentString = response.ToString();

                return(JToken.Parse(contentString).ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine("\n" + e.Message);
                return("error");
            }
        }
Exemplo n.º 15
0
        private async Task DoOCR()
        {
            try
            {
                string Identifer = Utility.RandomHex();
                DebugLog.Log("Making OCR request [" + Identifer + "]");

                if (!File.Exists(Properties.Settings.Default.apiKeyPath))
                {
                    throw new FileNotFoundException("Keyfile not present at " + Properties.Settings.Default.apiKeyPath);
                }

                // Wait for rate limiter before starting the clock
                GoogleAsyncStatic.rate.Check();
                Stopwatch sw = new Stopwatch();

                // Dump the provided image to a memory stream
                var stream = new MemoryStream();
                image.Save(stream, ImageFormat.Png);
                stream.Position = 0;

                // Load the stream as a gimage
                GImage gimage = GImage.FromStream(stream);

                // Make our connection client
                ImageAnnotatorClient client = new ImageAnnotatorClientBuilder
                {
                    CredentialsPath = Properties.Settings.Default.apiKeyPath,
                }.Build();

                // Ask for OCR
                sw.Start();
                var response = await client.DetectTextAsync(gimage);

                sw.Stop();

                // If we didn't get anything back
                if (response.Count == 0)
                {
                    _bigBox     = OCRBox.ErrorBigBox();
                    _smallBoxes = new OCRBox[] { };
                }
                else
                {
                    // First result is the big box
                    _bigBox = new OCRBox(response.First());

                    // Following results are the small boxes
                    _smallBoxes = response.Skip(1)
                                  .Select(ann => new OCRBox(ann))
                                  .ToArray();
                }

                _timeStamp = string.Format("{0:00}:{1:00}:{2:00}.{3:000}",
                                           sw.Elapsed.Hours,
                                           sw.Elapsed.Minutes,
                                           sw.Elapsed.Seconds,
                                           sw.Elapsed.Milliseconds);

                isDone = true;
                callback?.Invoke(this);

                DebugLog.Log("Finished OCR request [" + Identifer + "]");
            }
            catch (Grpc.Core.RpcException e)
            {
                string url = "";

                // Define a regular expression for repeated words.
                Regex rx = new Regex(@"(http\S*)",
                                     RegexOptions.Compiled | RegexOptions.IgnoreCase);

                // Find matches.
                MatchCollection matches = rx.Matches(e.Message);

                if (matches.Count > 0)
                {
                    url = matches[0].Groups[0].Value;
                }

                Form.Invoke(Form.SafeLogWorkerError, new object[] { e.Message, url });
            } catch (Exception e)
            {
                Form.Invoke(Form.SafeLogWorkerError, new object[] { e.Message, "" });
            }
        }