Пример #1
0
        private static object DetectObjectLocalization(Image image)
        {
            // [START vision_localize_objects]
            // [START vision_localize_objects_gcs]
            var client   = ImageAnnotatorClient.Create();
            var response = client.DetectLocalizedObjects(image);

            Console.WriteLine($"Number of objects found {response.Count}");
            foreach (var localizedObject in response)
            {
                Console.Write($"\n{localizedObject.Name}");
                Console.WriteLine($" (confidence: {localizedObject.Score})");
                Console.WriteLine("Normalized bounding polygon vertices: ");

                foreach (var vertex
                         in localizedObject.BoundingPoly.NormalizedVertices)
                {
                    Console.WriteLine($" - ({vertex.X}, {vertex.Y})");
                }
            }
            // [END vision_localize_objects_gcs]
            // [END vision_localize_objects]
            return(0);
        }
        public void DetectFaces()
        {
            Image image = LoadResourceImage("SchmidtBrinPage.jpg");
            // Snippet: DetectFaces
            ImageAnnotatorClient           client = ImageAnnotatorClient.Create();
            IReadOnlyList <FaceAnnotation> result = client.DetectFaces(image);

            foreach (FaceAnnotation face in result)
            {
                Console.WriteLine($"Confidence: {(int)(face.DetectionConfidence * 100)}%; BoundingPoly: {face.BoundingPoly}");
            }
            // End snippet

            Assert.Equal(3, result.Count);

            // Check the bounding boxes of the faces, with a tolerance of 5px on each edge.
            var rectangles = result.Select(x => Rectangle.FromBoundingPoly(x.BoundingPoly)).ToList();

            Assert.True(rectangles.All(x => x != null));
            rectangles = rectangles.OrderBy(r => r.Left).ToList();
            Assert.True(rectangles[0].Equals(new Rectangle(196, 64, 293, 177), 5.0));
            Assert.True(rectangles[1].Equals(new Rectangle(721, 162, 846, 308), 5.0));
            Assert.True(rectangles[2].Equals(new Rectangle(1009, 113, 1149, 276), 5.0));
        }
Пример #3
0
        /// <summary>Snippet for AsyncBatchAnnotateFilesAsync</summary>
        public async Task AsyncBatchAnnotateFilesAsync_RequestObject()
        {
            // Snippet: AsyncBatchAnnotateFilesAsync(AsyncBatchAnnotateFilesRequest,CallSettings)
            // Create client
            ImageAnnotatorClient imageAnnotatorClient = await ImageAnnotatorClient.CreateAsync();

            // Initialize request argument(s)
            AsyncBatchAnnotateFilesRequest request = new AsyncBatchAnnotateFilesRequest
            {
                Requests = { },
            };
            // Make the request
            Operation <AsyncBatchAnnotateFilesResponse, OperationMetadata> response =
                await imageAnnotatorClient.AsyncBatchAnnotateFilesAsync(request);

            // Poll until the returned long-running operation is complete
            Operation <AsyncBatchAnnotateFilesResponse, OperationMetadata> completedResponse =
                await response.PollUntilCompletedAsync();

            // Retrieve the operation result
            AsyncBatchAnnotateFilesResponse result = completedResponse.Result;

            // Or get the name of the operation
            string operationName = response.Name;
            // This name can be stored, then the long-running operation retrieved later by name
            Operation <AsyncBatchAnnotateFilesResponse, OperationMetadata> retrievedResponse =
                await imageAnnotatorClient.PollOnceAsyncBatchAnnotateFilesAsync(operationName);

            // Check if the retrieved long-running operation has completed
            if (retrievedResponse.IsCompleted)
            {
                // If it has completed, then access the result
                AsyncBatchAnnotateFilesResponse retrievedResult = retrievedResponse.Result;
            }
            // End snippet
        }
Пример #4
0
        public void ThrowOnAnyError()
        {
            // Snippet: ThrowOnAnyError
            // We create a request which passes simple validation, but isn't a valid image.
            Image image = Image.FromBytes(new byte[10]);
            // Just a single request in this example, but usually BatchAnnotateImages would be
            // used with multiple requests.
            var request = new AnnotateImageRequest
            {
                Image    = image,
                Features = { new Feature {
                                 Type = Feature.Types.Type.SafeSearchDetection
                             } }
            };
            ImageAnnotatorClient client = ImageAnnotatorClient.Create();

            try
            {
                BatchAnnotateImagesResponse response = client.BatchAnnotateImages(new[] { request });
                // ThrowOnAnyError will throw if any individual response in response.Responses
                // contains an error. Other responses may still have useful results.
                // Errors can be detected manually by checking the Error property in each
                // individual response.
                response.ThrowOnAnyError();
            }
            catch (AggregateException e)
            {
                // Because a batch can have multiple errors, the exception thrown is AggregateException.
                // Each inner exception is an AnnotateImageException
                foreach (AnnotateImageException innerException in e.InnerExceptions)
                {
                    Console.WriteLine(innerException.Response.Error);
                }
            }
            // End snippet
        }
Пример #5
0
 public VisionClient()
 {
     _client = ImageAnnotatorClient.Create();
 }
Пример #6
0
        // [START vision_text_detection_pdf_gcs]
        private static object DetectDocument(string gcsSourceUri,
                                             string gcsDestinationBucketName, string gcsDestinationPrefixName)
        {
            var client = ImageAnnotatorClient.Create();

            var asyncRequest = new AsyncAnnotateFileRequest
            {
                InputConfig = new InputConfig
                {
                    GcsSource = new GcsSource
                    {
                        Uri = gcsSourceUri
                    },
                    // Supported mime_types are: 'application/pdf' and 'image/tiff'
                    MimeType = "application/pdf"
                },
                OutputConfig = new OutputConfig
                {
                    // How many pages should be grouped into each json output file.
                    BatchSize      = 2,
                    GcsDestination = new GcsDestination
                    {
                        Uri = $"gs://{gcsDestinationBucketName}/{gcsDestinationPrefixName}"
                    }
                }
            };

            asyncRequest.Features.Add(new Feature
            {
                Type = Feature.Types.Type.DocumentTextDetection
            });

            List <AsyncAnnotateFileRequest> requests =
                new List <AsyncAnnotateFileRequest>();

            requests.Add(asyncRequest);

            var operation = client.AsyncBatchAnnotateFiles(requests);

            Console.WriteLine("Waiting for the operation to finish");

            operation.PollUntilCompleted();

            // Once the rquest has completed and the output has been
            // written to GCS, we can list all the output files.
            var storageClient = StorageClient.Create();

            // List objects with the given prefix.
            var blobList = storageClient.ListObjects(gcsDestinationBucketName,
                                                     gcsDestinationPrefixName);

            Console.WriteLine("Output files:");
            foreach (var blob in blobList)
            {
                Console.WriteLine(blob.Name);
            }

            // Process the first output file from GCS.
            // Select the first JSON file from the objects in the list.
            var output = blobList.Where(x => x.Name.Contains(".json")).First();

            var jsonString = "";

            using (var stream = new MemoryStream())
            {
                storageClient.DownloadObject(output, stream);
                jsonString = System.Text.Encoding.UTF8.GetString(stream.ToArray());
            }

            var response = JsonParser.Default
                           .Parse <AnnotateFileResponse>(jsonString);

            // The actual response for the first page of the input file.
            var firstPageResponses = response.Responses[0];
            var annotation         = firstPageResponses.FullTextAnnotation;

            // Here we print the full text from the first page.
            // The response contains more information:
            // annotation/pages/blocks/paragraphs/words/symbols
            // including confidence scores and bounding boxes
            Console.WriteLine($"Full text: \n {annotation.Text}");

            return(0);
        }
Пример #7
0
        public static async Task InitializeAsync(Credentials credentials)
        {
            await Db.InitAsync("Sanara");

            Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", AppDomain.CurrentDomain.BaseDirectory + "/Keys/GoogleAPI.json");

            if (credentials.RavenKey != null)
            {
                RavenClient = new RavenClient(credentials.RavenKey);
            }

            HttpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 Sanara");

            Safebooru.HttpClient = HttpClient;
            Gelbooru.HttpClient  = HttpClient;
            E621.HttpClient      = HttpClient;
            E926.HttpClient      = HttpClient;
            Rule34.HttpClient    = HttpClient;
            Konachan.HttpClient  = HttpClient;

            RomajiToHiragana = JsonConvert.DeserializeObject <Dictionary <string, string> >(File.ReadAllText("LanguageResource/Hiragana.json"));
            foreach (var elem in RomajiToHiragana)
            {
                HiraganaToRomaji.Add(elem.Value, elem.Key);
            }
            RomajiToKatakana = JsonConvert.DeserializeObject <Dictionary <string, string> >(File.ReadAllText("LanguageResource/Katakana.json"));
            foreach (var elem in RomajiToKatakana)
            {
                KatakanaToRomaji.Add(elem.Value, elem.Key);
            }
            foreach (var elem in ISO639)
            {
                ISO639Reverse.Add(elem.Value, elem.Key);
            }

            await Utils.Log(new LogMessage(LogSeverity.Info, "Static Preload", "Loading game preload (might take several minutes if this is the first time)"));

            Preloads = new IPreload[]
            {
                new ShiritoriPreload(),
                new ArknightsPreload(),
                new ArknightsAudioPreload(),
                new KancollePreload(),
                new KancolleAudioPreload(),
                new GirlsFrontlinePreload(),
                new AzurLanePreload(),
                new FateGOPreload(),
                new PokemonPreload(),
                new AnimePreload(),
                new BooruQuizzPreload(),
                new BooruFillPreload()
            };
            await Utils.Log(new LogMessage(LogSeverity.Info, "Static Preload", "Game preload done"));

            await SM.InitAsync();

            GM.Init();

            if (File.Exists("Saves/Premium.txt"))
            {
                AllowedPremium = File.ReadAllLines("Saves/Premium.txt");
            }

            if (credentials.YouTubeKey != null)
            {
                YouTube = new YouTubeService(new BaseClientService.Initializer
                {
                    ApiKey = credentials.YouTubeKey
                });
            }

            if (credentials.KitsuEmail != null)
            {
                KitsuAuth = new HttpRequestMessage(HttpMethod.Post, "https://kitsu.io/api/oauth/token")
                {
                    Content = new FormUrlEncodedContent(new Dictionary <string, string>
                    {
                        { "grant_type", "password" },
                        { "username", credentials.KitsuEmail },
                        { "password", credentials.KitsuPassword }
                    })
                };
            }

            if (credentials.UploadWebsiteLocation != null)
            {
                UploadWebsiteLocation = credentials.UploadWebsiteLocation;
                if (!UploadWebsiteLocation.EndsWith("/"))
                {
                    UploadWebsiteLocation += "/";
                }

                UploadWebsiteUrl = credentials.UploadWebsiteUrl;
                if (!UploadWebsiteUrl.EndsWith("/"))
                {
                    UploadWebsiteUrl += "/";                                  // Makes sure the URL end with a /
                }
            }

            if (credentials.TopGgToken != null)
            {
                DblToken = credentials.TopGgToken;
            }

            if (credentials.GithubKey != null)
            {
                GithubKey = credentials.GithubKey;
            }

            if (File.Exists("Keys/GoogleAPI.json"))
            {
                GoogleCredential googleCredentials = GoogleCredential.FromFile("Keys/GoogleAPI.json");
                TranslationClient = TranslationClient.Create();
                VisionClient      = ImageAnnotatorClient.Create();
            }

            if (credentials.StatsWebsiteUrl != null)
            {
                Website = new UploadManager(credentials.StatsWebsiteUrl, credentials.StatsWebsiteToken);
            }

            if (credentials.MyDramaListApiKey != null)
            {
                MyDramaListApiKey = credentials.MyDramaListApiKey;
            }
        }
Пример #8
0
        static async Task Main(string[] args)
        {
            try
            {
                if (args.Length != 1)
                {
                    Console.WriteLine("Missing image");
                    return;
                }

                var imageFile = args[0];

                if (!File.Exists(imageFile))
                {
                    Console.WriteLine("Image file doesn't exist");
                    return;
                }

                await CodAuthenticatorAsync();

                Console.WriteLine("Logged in");

                var users = new List <UserModel>();

                ImageAnnotatorClient client = ImageAnnotatorClient.Create();

                var            image = Image.FromFile(imageFile);
                TextAnnotation text  = client.DetectDocumentText(image);
                foreach (var page in text.Pages)
                {
                    foreach (var block in page.Blocks)
                    {
                        foreach (var paragraph in block.Paragraphs)
                        {
                            // Realistically, if this block is actually one of the teams, you won't have less than 2 'words' here
                            // One word will always be the rank (or should be) and another would be the username.
                            // Clan tags would also show up here, but they're irrelevant and will be ignored.
                            if (paragraph.Words.Count < 2)
                            {
                                continue;
                            }

                            bool          inClanTag        = false;
                            DetectedBreak lastBreakingChar = default;
                            foreach (var word in paragraph.Words)
                            {
                                var fullWord = string.Join("", word.Symbols.Select(s => s.Text)).Trim();
                                var lastUser = users.LastOrDefault();
                                switch (fullWord)
                                {
                                case "[":
                                    if (inClanTag)
                                    {
                                        throw new Exception("Found clan tag opening bracket before a previous clan tag was closed");
                                    }
                                    inClanTag = true;
                                    continue;

                                case "]":
                                    if (!inClanTag)
                                    {
                                        throw new Exception("Found clan tag closing bracket while not in clan tag");
                                    }
                                    inClanTag = false;
                                    continue;
                                }

                                if (inClanTag)
                                {
                                    //Console.WriteLine($"Found {fullWord} in clan tag");
                                    continue;
                                }

                                if (short.TryParse(fullWord, out var level))
                                {
                                    if (level < 1 || level > 155)
                                    {
                                        Console.WriteLine($"Level {level} found but not in range.");
                                        continue;
                                    }

                                    if ((lastBreakingChar != default && lastBreakingChar.Type == DetectedBreak.Types.BreakType.EolSureSpace) || lastUser == default || lastUser.IsComplete())
                                    {
                                        users.Add(new UserModel(level, block));
                                        Console.WriteLine($"Added new possible user with level {level} to list");
                                    }
                                    else
                                    {
                                        Console.WriteLine($"Found possible level {level} but no EOL found and lastUser exists");
                                    }
                                }
                                // This regex might need to be more strict.
                                // Your Activision ID is what is displayed in-game for Modern Warfare. Display names must be between 2 and 16 characters with no special characters. Unicode characters are supported.
                                else if (new Regex(@"^(\w{2,16})$").IsMatch(fullWord))
                                {
                                    if (lastUser != default && lastUser.IsComplete() && lastBreakingChar != default && lastBreakingChar.Type == DetectedBreak.Types.BreakType.Space && block == lastUser.Block)
                                    {
                                        Console.WriteLine($"Found another part of username for {lastUser.Username} -> {fullWord}");
                                        lastUser.AppendToUsername(fullWord);
                                    }
                                    else if (lastUser == default || lastUser.IsComplete())
                                    {
                                        Console.WriteLine($"Found matching username pattern '{fullWord}' but no rank was found before");
                                        continue;
                                    }
                                    else
                                    {
                                        lastUser.SetUsername(fullWord);

                                        Console.WriteLine($"\tSet username to {fullWord}");
                                    }
                                }

                                lastBreakingChar = word.Symbols.LastOrDefault()?.Property?.DetectedBreak;
                            }
Пример #9
0
 /// <summary>
 ///  Constructor accepting all our dependencies. The clients are configured in
 ///  Startup.cs.
 /// </summary>
 public Function(ImageAnnotatorClient visionClient, StorageClient storageClient, ILogger <Function> logger) =>
 (_visionClient, _storageClient, _logger) = (visionClient, storageClient, logger);
Пример #10
0
 public override void ConfigureServices(WebHostBuilderContext context, IServiceCollection services) =>
 services
 .AddSingleton(ImageAnnotatorClient.Create())
 .AddSingleton(StorageClient.Create());
Пример #11
0
 private void Button6_Click(object sender, EventArgs e)
 {
     var client = ImageAnnotatorClient.Create();
     // var image = Image.FromFile("");
 }
Пример #12
0
        public static void RetornaImagemProduto(string CaminhoImagem)
        {
            string retorno = String.Empty;
            ImageAnnotatorClient client;
            BatchAnnotateImagesResponse responseArray;

            var credential = GoogleCredential.FromFile(ArquivoCredencialGoogle)
            .CreateScoped(ImageAnnotatorClient.DefaultScopes);
            var channel = new Grpc.Core.Channel(
                ImageAnnotatorClient.DefaultEndpoint.ToString(),
                credential.ToChannelCredentials());

            List<AnnotateImageRequest> requestArray = new List<AnnotateImageRequest>();
            int i = 1;
            int j = 0;
            int countRequest = 0;

            foreach (NotaFiscalOCR nota in lstNota)
            {
                try
                {
                    if (File.Exists(nota.Foto))
                    {
                        AnnotateImageRequest request = new AnnotateImageRequest
                        {
                            Image = Image.FromFile(produto.CaminhoImagemLocal),
                            Features = { new Feature { Type = Feature.Types.Type.DocumentTextDetection } }
                        };

                        requestArray.Add(request);
                    }

                    if (i == 15)
                    {
                        countRequest += 15;
                        Console.Write("\tFazendo requests " + countRequest + "/" + lstProduto.Count + "...");
                        Console.Write("\r");

                        client = ImageAnnotatorClient.Create(channel);

                        responseArray = client.BatchAnnotateImages(requestArray.ToArray());

                        foreach (AnnotateImageResponse response in responseArray.Responses)
                        {
                            
                            j++;
                        }

                        requestArray = new List<AnnotateImageRequest>();
                        i = 0;
                    }

                    i++;
                }
                catch (Exception ex)
                {

                }
            }

            client = ImageAnnotatorClient.Create(channel);

            responseArray = client.BatchAnnotateImages(requestArray.ToArray());

            foreach (AnnotateImageResponse response in responseArray.Responses)
            {
                if (response.Error == null && response.FullTextAnnotation != null)
                {
                    
                }

                j++;
            }
        }
Пример #13
0
        public void ExtractGoogleLabels(string filelistFilepath, string imageHostingBaseUrl, string outputPath)
        {
            var client       = ImageAnnotatorClient.Create();
            var allfilenames = File.ReadAllLines(filelistFilepath);

            var requests = new List <BatchAnnotateImagesRequest> {
                new BatchAnnotateImagesRequest()
            };

            foreach (var f in allfilenames)
            {
                if (requests.Last().Requests.Count == 16)
                {
                    requests.Add(new BatchAnnotateImagesRequest());
                }

                requests.Last().Requests.Add(new AnnotateImageRequest
                {
                    Image    = Image.FromUri(imageHostingBaseUrl + f),
                    Features =
                    {
                        new Feature {
                            Type = Feature.Types.Type.LabelDetection
                        },
                        new Feature {
                            Type = Feature.Types.Type.TextDetection
                        },
                        new Feature {
                            Type = Feature.Types.Type.ObjectLocalization
                        },
                        new Feature {
                            Type = Feature.Types.Type.FaceDetection
                        }
                    }
                });
            }

            var collected = requests
                            .Select((r, i) => client.BatchAnnotateImages(r))
                            .SelectMany(res => res.Responses)
                            .ToList();


            File.WriteAllText(outputPath, @"
<html>
<table border='1px solid black'>
<tr>
<th>Image</th>
<th>Labels</th>
<th>Text</th>
<th>Faces</th>
<th>Objects</th>
</tr>");

            var outputLines = collected
                              .Zip(allfilenames, Tuple.Create)
                              .Select(l => $@"
<tr>
    <td><a href='{imageHostingBaseUrl + l.Item2}'><img src='{imageHostingBaseUrl + l.Item2}' width='300px'/></a></td>
    <td><ol> {l.Item1.LabelAnnotations.Select(la => "<li>" + la.Description + "  (" + la.Score.ToString("0.00") + ")</li>" ).Tie()} </ol></td>
    <td><ol> {l.Item1.TextAnnotations.Select(ta => "<li>" + ta.Description + "</li>").Tie()} </ol></td>
    <td><ol> {l.Item1.FaceAnnotations.Select(fa => "<li>" + GetFaceAttributes(fa) +"  (" + fa.DetectionConfidence.ToString("0.00") + ")</li>").Tie()} </ol></td>
    <td><ol> {l.Item1.LocalizedObjectAnnotations.Select(loa => "<li>" + loa.Name + "  (" + loa.Score.ToString("0.00") + "  "+ loa.BoundingPoly.Vertices.Dump() +")</li>").Tie()} </ol></td>
</tr>")

                              .ToArray();

            File.AppendAllLines(outputPath, outputLines);
            File.AppendAllText(outputPath, "</table></html>");

            Assert.Pass();
        }
Пример #14
0
        public List <TextBox> GoogleOcr(string file)
        {
            var textBoxes = new List <TextBox>();

            // Instantiates a client
            logger.Info($"Googling {file}");
            try
            {
                var client = ImageAnnotatorClient.Create();
                // Load the image file into memory
                //  var file = @"F:\Dropbox\OCR\Single\data\13864584_3_ocr~20181130_page3pdf.png";
                var responseFile = file + ".google.response.json";
                int num          = 0;
                if (!File.Exists(responseFile))
                {
                    var image = Google.Cloud.Vision.V1.Image.FromFile(file);

                    var response   = client.DetectDocumentText(image);
                    var serializer = new JsonSerializer {
                        NullValueHandling = NullValueHandling.Ignore
                    };

                    using (StreamWriter sw = new StreamWriter(responseFile))
                        using (JsonWriter writer = new JsonTextWriter(sw))
                        {
                            writer.Formatting = Newtonsoft.Json.Formatting.Indented;
                            serializer.Serialize(writer, response);
                        }
                }


                var   json2      = File.ReadAllText(responseFile);
                var   response2  = JsonConvert.DeserializeObject <TextAnnotation>(json2);
                int   index      = 0;
                float confidence = 1.01f;
                var   pageRange  = Enumerable.Range(0, response2.Pages.Count);

                foreach (var pnum in pageRange)
                {
                    var page       = response2.Pages[pnum];
                    var blockRange = Enumerable.Range(0, page.Blocks.Count);
                    foreach (var bnum in blockRange)
                    {
                        var block = page.Blocks[bnum];
                        logger.Info($"block {bnum} {block.BoundingBox} paras={block.Paragraphs.Count}");
                        var paraRange = Enumerable.Range(0, block.Paragraphs.Count);
                        foreach (var paraNum in paraRange)
                        {
                            var paragraph = block.Paragraphs[paraNum];
                            logger.Info($"{paragraph.BoundingBox} {paragraph.CalculateSize()} {paragraph.Words.Count}");
                            var       sb   = new StringBuilder();
                            Rectangle rect = Rectangle.Empty;
                            var       line = new StringBuilder();

                            foreach (var word in paragraph.Words)
                            {
                                if (word.Confidence < confidence)
                                {
                                    confidence = word.Confidence;
                                }
                                foreach (var symbol in word.Symbols)
                                {
                                    line.Append(symbol.Text);
                                    if (rect == Rectangle.Empty)
                                    {
                                        rect = RectFromVertices.Convert(symbol.BoundingBox);
                                    }
                                    else
                                    {
                                        rect = RectFromVertices.Expand(rect, symbol.BoundingBox);
                                    }

                                    try
                                    {
                                        if (symbol.Property?.DetectedBreak != null)
                                        {
                                            switch (symbol.Property.DetectedBreak.Type)
                                            {
                                            case BreakType.Space:
                                                line.Append(" ");
                                                break;

                                            case BreakType.EolSureSpace:
                                            case BreakType.LineBreak:
                                                index = AddTextBox(index, ref confidence, line, textBoxes, ref rect);
                                                break;

                                            default:
                                                line.Append(" ");
                                                break;
                                            }
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        logger.Error(ex, $"{index} of {file} {paragraph.ToString()}");
                                    }
                                }
                            }
                            if (line.Length > 0)
                            {
                                index = AddTextBox(index, ref confidence, line, textBoxes, ref rect);
                            }

                            //logger.Info(b.ToString());
                        }
                    }
                }

                /*
                 * boxesBySize = new SortedList<long, Box>(new DuplicateKeyComparer<long>());
                 * var json = File.ReadAllText(boxFile);
                 * var boxes = JsonConvert.DeserializeObject<List<Box>>(json);
                 * foreach (var b in boxes)
                 * {
                 * num++;
                 * b.Index = num;
                 * logger.Info($"{b}");
                 *
                 * boxesBySize.Add(b.Size, b);
                 * }
                 *
                 * logger.Info("-----------------------------------------------------");
                 * foreach (var b in boxes)
                 * {
                 * b.FindParents(boxesBySize);
                 * logger.Info(
                 *    $"{b} cs:{b.Annotation.CalculateSize()} Parent:{b.Parent?.Index}");
                 * }
                 */
            }
            catch (Exception ex)
            {
                logger.Error(ex, $"error processing Google at ${file}");
            }
            return(textBoxes);

            //return boxesBySize.Values.ToList<Box>();
        }
Пример #15
0
        private async Task DetectDocumentTextAsync(string path)
        {
            vm.BoundingBoxes.Clear();
            vm.ImagePath = path;

            var image    = Image.FromFile(path);
            var client   = ImageAnnotatorClient.Create();
            var response = await client.DetectDocumentTextAsync(image);

            //var res = await client.DetectTextAsync(image);

            vm.Pages = response.Pages;

            Debug.WriteLine($"Text: {response.Text}");

            //var w = response.Pages.SelectMany(x => x.Blocks.SelectMany(x => x.Paragraphs.SelectMany(x => x.Words)));
            //var g = w.GroupBy(x => Math.Round(x.BoundingBox.Vertices[0].Y / 10.0)).OrderBy(x => x.Key);
            //var p = g.Select(x => string.Join(" ", x.Select(x => x.DebugString()))).ToList();

            var words = new List <Word>();

            foreach (var page in response.Pages)
            {
                foreach (var block in page.Blocks)
                {
                    vm.BoundingBoxes.Add(new BoundingBox(block));
                    foreach (var paragraph in block.Paragraphs)
                    {
                        vm.BoundingBoxes.Add(new BoundingBox(paragraph));
                        foreach (var word in paragraph.Words)
                        {
                            vm.BoundingBoxes.Add(new BoundingBox(word));
                            words.Add(word);
                        }
                    }
                }
            }

            var groups = new SortedDictionary <int, List <Word> >();

            foreach (var word in words)
            {
                var round = (int)Math.Round(word.BoundingBox.Vertices[0].Y / 12.5
                                            );
                if (groups.ContainsKey(round))
                {
                    groups[round].Add(word);
                }
                else
                {
                    groups.Add(round, new List <Word> {
                        word
                    });
                }
            }

            var Paragraphs = new List <string>();

            foreach (var list in groups.Values)
            {
                var s = "";
                foreach (var word in list)
                {
                    s += word.DebugString() + " ";
                }
                Paragraphs.Add(s.TrimEnd());
            }

            aa(Paragraphs);
            //Debug.WriteLine(string.Join("\n", Paragraphs));
        }
Пример #16
0
 public ImageAnalyser(ILoggerFactory loggerFactory)
 {
     _annotator = ImageAnnotatorClient.Create();
     _logger    = loggerFactory.CreateLogger("ImageAnalyser");
 }
Пример #17
0
        public async Task <IActionResult> Create(FlyerReaderViewModel model)
        {
            if (ModelState.IsValid)
            {
                List <FlyerOffer> flyerOffers = new List <FlyerOffer>();
                FlyerReader       flyerReader = new FlyerReader
                {
                    Img       = new List <ByteWraper>(),
                    DueDate   = model.DueDate,
                    StartDate = model.StartDate,
                    StoreID   = model.StoreID,
                    Data      = new String(String.Empty)
                };

                if (model.photos.Count > 0)
                {
                    var data      = String.Empty;
                    var flyerText = String.Empty;
                    foreach (IFormFile photo in model.photos)
                    {
                        var Stream = new MemoryStream();
                        await photo.CopyToAsync(Stream);

                        ByteWraper byteWraper = new ByteWraper();
                        byteWraper.photo = Stream.ToArray();
                        flyerReader.Img.Add(byteWraper);

                        var Googleimage = Image.FromBytes(byteWraper.photo);
                        var client      = ImageAnnotatorClient.Create();
                        var response    = client.DetectText(Googleimage);
                        foreach (var annotation in response)
                        {
                            if (annotation.Description != null)
                            {
                                flyerText += " " + annotation.Description;
                            }
                        }
                        String text = String.Empty;
                        text = flyerText.Replace("\n", " ");

                        string   str = text.ToLower();
                        string[] arr = str.Split(" ");
                        var      a   =
                            from k in arr
                            orderby k
                            select k;
                        String Description          = String.Empty;
                        String ItemName             = String.Empty;
                        List <DoubleWraper> numbers = new List <DoubleWraper>();
                        foreach (string res in a.Distinct())
                        {
                            Description += " " + res.ToLower();
                            if (res != "")
                            {
                                Item x;
                                if (pluralizationService.IsPlural(res))
                                {
                                    x = _context.Item.FirstOrDefault(i => i.ItemName.ToLower() == pluralizationService.Singularize(res));
                                }
                                else
                                {
                                    x = _context.Item.FirstOrDefault(i => i.ItemName.ToLower() == res);
                                }

                                if (x != null)
                                {
                                    ItemName = x.ItemName;
                                }
                                if (res.IsInt() || res.IsDecimal())
                                {
                                    numbers.Add(new DoubleWraper {
                                        num = Double.Parse(res)
                                    });
                                }
                            }
                        }

                        if (numbers.Count > 0 && ItemName != "")
                        {
                            flyerOffers.Add(new FlyerOffer
                            {
                                ItemName    = ItemName,
                                Description = Description,
                                Numbers     = numbers,
                                flyerphoto  = byteWraper.photo,
                                StoreID     = flyerReader.StoreID,
                                DueDate     = flyerReader.DueDate,
                                StartDate   = flyerReader.StartDate,
                                flyerReader = flyerReader,
                                recognized  = true
                            });
                        }
                        else if (ItemName == null || ItemName == "")
                        {
                            flyerOffers.Add(new FlyerOffer
                            {
                                Description = Description,
                                Numbers     = numbers,
                                flyerphoto  = byteWraper.photo,
                                StoreID     = flyerReader.StoreID,
                                DueDate     = flyerReader.DueDate,
                                StartDate   = flyerReader.StartDate,
                                flyerReader = flyerReader,
                                recognized  = false
                            });
                        }
                        flyerText = String.Empty;
                    }
                }
                flyerReader.flyerOffers = flyerOffers;
                _context.FlyerReader.Add(flyerReader);
                _context.SaveChanges();
                return(RedirectToAction("FlyersOffers"));
            }
            ViewData["StoreID"] = new SelectList(_context.Store, "StoreID", "StoreName", model.StoreID);
            return(View(model));
        }
Пример #18
0
        public async Task <string> CreateTalent(Talent talent, IFormFile talentImgFile)
        {
            string imageName = "";

            //Save to images
            if (talentImgFile == null)
            {
                //Do nothing
            }
            else
            {
                try
                {
                    imageName = talentImgFile.FileName;
                    var    dir  = _env.WebRootPath;
                    string path = Path.Combine(dir, talentImgFile.FileName);
                    using (var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write))
                    {
                        talentImgFile.CopyTo(fileStream);
                    }
                    // Instantiates a client
                    var client = ImageAnnotatorClient.Create();
                    // Load the image file into memory
                    var image = Image.FromFile(path);
                    // Check for NSFW images
                    var response = client.DetectSafeSearch(image);
                    if (response.Adult.ToString().Equals("Likely") || response.Adult.ToString().Equals("VeryLikely"))
                    {
                        System.IO.File.Delete(path);
                        return("NSFW");
                    }
                    if (response.Racy.ToString().Equals("Likely") || response.Racy.ToString().Equals("VeryLikely"))
                    {
                        System.IO.File.Delete(path);
                        return("NSFW");
                    }
                    if (response.Violence.ToString().Equals("Likely") || response.Violence.ToString().Equals("VeryLikely"))
                    {
                        System.IO.File.Delete(path);
                        return("NSFW");
                    }
                    if (response.Medical.ToString().Equals("Likely") || response.Medical.ToString().Equals("VeryLikely"))
                    {
                        System.IO.File.Delete(path);
                        return("NSFW");
                    }
                    if (response.Spoof.ToString().Equals("Likely") || response.Spoof.ToString().Equals("VeryLikely"))
                    {
                        System.IO.File.Delete(path);
                        return("NSFW");
                    }

                    await _service.UploadFileAsync(path);

                    System.IO.File.Delete(path);
                } catch (Exception ex)
                {
                    return(ex.Message);
                }
            }

            TalentDb talentDb = new TalentDb(configuration);

            talent.ImageURL = imageName;
            talentDb.addTalent(talent);
            return("Successfully added");
        }
Пример #19
0
        private void btAnalisar_Click(object sender, EventArgs e)
        {
            using (openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter     = "Imagens (*.jpg)|*.jpg";
                openFileDialog.DefaultExt = "*.jpg";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        lock (this)
                        {
                            //Variável setada no código devido erro ao reconhecer variável setada no ambiente
                            System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", @"C:\ApiKeys\googleKey.json");
                            string Pathsave = System.Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");

                            //Cliente da requisição
                            var client = ImageAnnotatorClient.Create();

                            //Carregar a imagem na memória
                            var imagem = Google.Cloud.Vision.V1.Image.FromFile(openFileDialog.FileName);

                            //Executa a análise da imagem
                            var resposta = client.DetectFaces(imagem);

                            foreach (var face in resposta)
                            {
                                MessageBox.Show("Imagem analisada com sucesso:\n\n\n"
                                                + "Nome: " + openFileDialog.SafeFileName + ":\n"
                                                + "Alegria: " + face.JoyLikelihood + "\n"
                                                + "Raiva: " + face.AngerLikelihood + "\n"
                                                + "Tristeza: " + face.SorrowLikelihood + "\n"
                                                + "Surpresa: " + face.SurpriseLikelihood + "\n"
                                                + "Subexposição: " + face.UnderExposedLikelihood + "\n"
                                                + "Borrado: " + face.BlurredLikelihood + "\n"
                                                + "Chapéu: " + face.HeadwearLikelihood + "\n"
                                                + "Confiança da análise: " + face.DetectionConfidence + "\n"
                                                + "delimitador: " + face.BoundingPoly + "\n"
                                                + "FdBoundingPoly: " + face.FdBoundingPoly + "\n"
                                                //+ "Landmarks: " + face.Landmarks + "\n"
                                                //+ "LandmarkingConfidence: " + face.LandmarkingConfidence + "\n"
                                                + "PanAngle: " + face.PanAngle + "\n"
                                                + "RollAngle: " + face.RollAngle + "\n"
                                                + "TiltAngle: " + face.TiltAngle + "\n"

                                                + ".", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                            //MessageBox.Show("Imagem analisada com sucesso!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Falha ao analisar imagem!\n" + ex.Message, "Erro!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    finally
                    {
                    }
                }
                else
                {
                    MessageBox.Show("Operação cancelada!", "Cancelado", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
Пример #20
0
 public DetectionAccess(ImageAnnotatorClient imageAnnotatorClient)
 {
     _imageAnnotatorClient = imageAnnotatorClient ?? throw new ArgumentNullException();
 }
Пример #21
0
        public string DetectText(params object[] parames)
        {
            string plate = "";

            try
            {
                List <Truck> trucks    = sqlCommandApiMobil.GetTruck();
                byte[]       photo     = (byte[])parames[1];
                string       idDriver  = (string)parames[0];
                var          client    = ImageAnnotatorClient.Create();
                var          image     = Google.Cloud.Vision.V1.Image.FromBytes(photo);
                var          response  = client.DetectText(image);
                var          response3 = client.DetectLocalizedObjects(image);

                foreach (var localizedObject in response3)
                {
                    string numPlateTmp = "";
                    Truck  truck       = null;
                    foreach (EntityAnnotation text in response)
                    {
                        if (trucks.FirstOrDefault(t => t.PlateTruk == text.Description) != null)
                        {
                            truck = trucks.FirstOrDefault(t => t.PlateTruk == text.Description);
                            //sqlCommandApiMobil.SetPlateTruck(truck.Id, idDriver);
                            plate = truck.PlateTruk;
                            break;
                        }
                        else if (truck != null && truck.PlateTruk.Contains(text.Description))
                        {
                            numPlateTmp += " " + text.Description;
                        }
                        else if (truck == null && trucks.FirstOrDefault(t => t.PlateTruk.Contains(text.Description)) != null)
                        {
                            numPlateTmp += text.Description;
                            truck        = trucks.FirstOrDefault(t => t.PlateTruk.Contains(text.Description));
                        }
                        if (numPlateTmp.Length >= 6)
                        {
                            if (truck != null && truck.PlateTruk == numPlateTmp)
                            {
                                //sqlCommandApiMobil.SetPlateTruck(truck.Id, idDriver);
                                plate       = truck.PlateTruk;
                                numPlateTmp = "";
                                break;
                            }
                            else if (truck.PlateTruk.Remove(truck.PlateTruk.Length - 3) == numPlateTmp || truck.PlateTruk.Remove(truck.PlateTruk.Length - 2) == numPlateTmp || truck.PlateTruk.Remove(truck.PlateTruk.Length - 1) == numPlateTmp)
                            {
                                //sqlCommandApiMobil.SetPlateTruck(truck.Id, idDriver);
                                plate       = truck.PlateTruk;
                                numPlateTmp = "";
                                break;
                            }
                            else if (numPlateTmp.Length > 7)
                            {
                                truck       = null;
                                numPlateTmp = "";
                            }
                        }
                    }
                    if (plate == "")
                    {
                        plate = numPlateTmp;
                    }
                }
            }
            catch (Exception e)
            {
            }

            return(plate);
        }
Пример #22
0
 public CloudVisionDetector(ImageAnnotatorClient client)
 {
     _client = client;
 }
 public override void Configure(IFunctionsHostBuilder builder)
 {
     builder.Services.AddSingleton(ImageAnnotatorClient.Create());
     builder.Services.AddSingleton(StorageClient.Create());
 }
Пример #24
0
 public async Task ShutdownApp()
 {
     await ImageAnnotatorClient.ShutdownDefaultChannelsAsync();
 }
Пример #25
0
 /// <summary>
 ///  Constructor accepting all our dependencies. The clients are configured in
 ///  Startup.cs.
 /// </summary>
 public Function(ImageAnnotatorClient visionClient, FirestoreDb firestoreDb, ILogger <Function> logger) =>
 (_visionClient, _firestoreDb, _logger) = (visionClient, firestoreDb, logger);
Пример #26
0
        public async Task <IHttpActionResult> describeImageWithVoice([FromBody] ImageToVoice imageToVoice)
        {
            try
            {
                var user = await UserManager.FindByIdAsync(imageToVoice.userId);

                var faceDescription = new FaceDescription();
                var googleClient    = await ImageAnnotatorClient.CreateAsync();

                var byteImage = Convert.FromBase64String(imageToVoice.base64Image);
                var image     = Image.FromBytes(byteImage);

                var responseForFacesGoogle = await googleClient.DetectFacesAsync(image);

                var responseForLabels = await googleClient.DetectLabelsAsync(image);

                var responseForLandmark = await googleClient.DetectLandmarksAsync(image);

                var responseForLogo = await googleClient.DetectLogosAsync(image);

                var analyzeImage = new AnalyzeImage();
                analyzeImage.responseFaceGoogle     = responseForFacesGoogle;
                analyzeImage.responseForLabels      = responseForLabels;
                analyzeImage.responseForLogoGoogle  = responseForLogo;
                analyzeImage.responseLandMarkGoogle = responseForLandmark;

                var responseFormMicrosoftFace = new List <CognitiveMicrosoft>();
                if (responseForFacesGoogle.Count > 0)
                {
                    responseFormMicrosoftFace = await faceDescription.MakeAnalysisRequestAsync(byteImage);

                    analyzeImage.responseForFacesMicrosft = responseFormMicrosoftFace;
                }

                string base64Voice = analyzeImage.describeImageWithVoice();

                rela.ImagesProceseds.Add(new ImagesProcesed {
                    UserId = user.Id, date = DateTime.Now, image = imageToVoice.base64Image
                });
                await rela.SaveChangesAsync();

                int imageId = rela.ImagesProceseds.OrderByDescending(img => img.date).ToList()[0].imageId;

                if (responseForFacesGoogle.Count > 0)
                {
                    rela.GoogleFaces.Add(new GoogleFace {
                        GoogleFace1 = JsonConvert.SerializeObject(responseForFacesGoogle), imageId = imageId
                    });
                }

                if (responseForLabels.Count > 0)
                {
                    rela.GoogleLabels.Add(new GoogleLabel {
                        GoogleLabel1 = JsonConvert.SerializeObject(responseForLabels), imageId = imageId
                    });
                }

                if (responseForLandmark.Count > 0)
                {
                    rela.GoogleLandmarks.Add(new GoogleLandmark {
                        GoogleLandamark = JsonConvert.SerializeObject(responseForLandmark), imageId = imageId
                    });
                }

                if (responseForLogo.Count > 0)
                {
                    rela.GoogleLogoes.Add(new GoogleLogo {
                        GoogleLogo1 = JsonConvert.SerializeObject(responseForLogo), imageId = imageId
                    });
                }

                if (responseFormMicrosoftFace.Count > 0)
                {
                    rela.MicrosoftFaces.Add(new MicrosoftFace {
                        imageId = imageId, MicrosoftFace1 = JsonConvert.SerializeObject(responseFormMicrosoftFace)
                    });
                }

                rela.Voices.Add(new Voice {
                    imageId = imageId, GoogleVoice = base64Voice
                });

                await rela.SaveChangesAsync();

                DescribeImage describeImage = new DescribeImage()
                {
                    googleFace    = responseForFacesGoogle,
                    label         = responseForLabels,
                    landmark      = responseForLandmark,
                    logo          = responseForLogo,
                    voiceBase64   = base64Voice,
                    microsoftFace = responseFormMicrosoftFace
                };

                return(Ok(describeImage));
            }
            catch (Exception ex) {
                return(BadRequest("Error"));
            }
        }
Пример #27
0
        public static async Task GetTextAsync(string uri)
        {
            var path   = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var image  = Image.FromUri(uri);
            var client = ImageAnnotatorClient.Create();
            var text   = await client.DetectDocumentTextAsync(image);

            var searchTerms = new string[] { "Lead", "1978", "Protection", "EPA's", "Phoenix" };

            Console.WriteLine($"Text: {text.Text}");
            Console.WriteLine(text.Pages.Select(p => p.Confidence.ToString()));

            var symbolsText = text.Pages
                              .SelectMany(p => p.Blocks
                                          .SelectMany(b => b.Paragraphs
                                                      .SelectMany(p => p.Words
                                                                  .SelectMany(w => string.Join("", w.Symbols
                                                                                               .Select(s => s.Text))))))
            ;

            Console.WriteLine($"symbolsText: {symbolsText}");


            //var tenantBlcok = new Block();
            //tenantBlcok.BoundingBox.Vertices
            var sorted = symbolsText.GroupBy(x => x)
                         .Select(g => new { Value = g.Key, Found = g.Count() })
                         .OrderByDescending(x => x.Found);

            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))}");
                        }
                    }
                }
            }
            using (var fs = File.Create(Path.Combine(path, $@"output\Text{DateTime.Now.Ticks}.txt")))
            {
                Utilities.AddText(fs, $"Text: {text.Text}");
                foreach (var thing in sorted)
                {
                    Utilities.AddText(fs, $"{thing.Value} appeared {thing.Found}\n");
                }
                Utilities.AddText(fs, "\n\n\n *******************End of symbols********************\n\n\n");

                Utilities.AddText(fs, $"texas appeared {Utilities.SearchWordCount(text, "texas")} times \n\n\n");

                var keywords = Utilities.SearchWordCollection(text, searchTerms);

                Utilities.AddText(fs, $"*************Sensitive keywords found*********\n\n");
                foreach (var item in keywords)
                {
                    Utilities.AddText(fs, $"{item.Item1} found {item.Item2} times\n");
                }
                var keywordsExp = Utilities.SearchWordCollectionExp(text, searchTerms);

                Utilities.AddText(fs, $"*************Experiment keywords found*********\n\n");
                foreach (var item in keywordsExp)
                {
                    Utilities.AddText(fs, $"{item.Item1} found {item.Item3} times with {item.Item2} confidence\n");
                }
            }

            using (var fs = File.Create(Path.Combine(path, $@"output\Paragraphs{DateTime.Now.Ticks}.txt")))
            {
                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})"));
                        Utilities.AddText(fs, $"Block {block.BlockType} at {box}");
                        Utilities.AddText(fs, "\n");
                        foreach (var paragraph in block.Paragraphs)
                        {
                            box = string.Join(" - ", paragraph.BoundingBox.Vertices.Select(v => $"({v.X}, {v.Y})"));
                            Utilities.AddText(fs, $"  Paragraph at {box}");
                            Utilities.AddText(fs, "\n");
                            foreach (var word in paragraph.Words)
                            {
                                Utilities.AddText(fs, $"    Word: {string.Join("", word.Symbols.Select(s => s.Text))}");
                                Utilities.AddText(fs, "\n");
                            }
                        }
                    }
                }
            }



            Console.ReadLine();
        }
Пример #28
0
        public async Task <AnalysisResult> AnalyzeAsync(Image image, ICollection <string> categories)
        {
            var client = await ImageAnnotatorClient.CreateAsync().ConfigureAwait(false);

            var labels = await client.DetectLabelsAsync(image).ConfigureAwait(false);

            var suggestions    = new List <string>();
            var category       = Constants.NoValueString;
            var categoryFound  = false;
            var bestLabel      = Constants.NoValueString;
            var bestLabelFound = false;

            foreach (var label in labels)
            {
                if (!bestLabelFound)
                {
                    var invalid = false;

                    foreach (var word in Constants.InvalidSuggestions)
                    {
                        if (label.Description.ToLower().Equals(word.ToLower()))
                        {
                            invalid = true;
                        }
                    }

                    if (!invalid)
                    {
                        bestLabel      = label.Description;
                        bestLabelFound = true;
                    }
                }

                suggestions.Add(label.Description);

                if (!categoryFound)
                {
                    foreach (var cat in categories)
                    {
                        if (!Constants.AllCategoriesToKeywords.ContainsKey(cat))
                        {
                            throw new ArgumentException(Constants.InvalidCategories);
                        }
                        else
                        {
                            foreach (var keyword in Constants.AllCategoriesToKeywords[cat])
                            {
                                if (label.Description.ToLower().Contains(keyword.ToLower()) && !categoryFound)
                                {
                                    category      = cat;
                                    categoryFound = true;
                                }
                            }
                        }
                    }
                }
            }

            if (!categoryFound)
            {
                category = Constants.NoCategory;
            }

            // Refactor: apply logic to read labels
            if (category.Equals(Constants.NoCategory) || category.Equals(Constants.ManufacturedCategory))
            {
                bestLabel = "";
            }

            Console.WriteLine(category);

            var name = bestLabel;

            AnalysisResult result = new AnalysisResult(suggestions, category, name);

            return(result);
        }
Пример #29
0
        public async Task <ActionResult> TransformFromImage(IFormFile fileImage)
        {
            ImagesPageViewModel model = new ImagesPageViewModel();
            var filePath = configuration["AppSettings:ImagesPath"] + "/" + "temp" + ".jpg";

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await fileImage.CopyToAsync(stream);
            }

            string       text         = "";
            var          image        = Image.FromFile(filePath);
            var          client       = ImageAnnotatorClient.Create();
            ImageContext imageContext = new ImageContext();

            imageContext.LanguageHints.Add("mk");
            var response = client.DetectText(image, imageContext);

            foreach (var annotation in response)
            {
                if (annotation.Description != null)
                {
                    text = annotation.Description;
                    break;
                }
            }
            text = text.Replace("o", "о").Replace("j", "ј").Replace("k", "к").Replace("a", "а").Replace("e", "е");
            text = text.Replace("O", "О").Replace("J", "Ј").Replace("K", "К").Replace("A", "А").Replace("E", "Е");

            var simplifiedText = Simplify(text);
            var sentencePart   = new List <string>();

            foreach (KeyValuePair <string, string> entry in simplifiedText)
            {
                sentencePart.Add(entry.Key);
            }

            var finalText = string.Join(" ", sentencePart);

            text = finalText;

            var words = text.Split(" ");

            string[] files =
                Directory.GetFiles(configuration["AppSettings:ImagesPath"], "*.jpg", SearchOption.AllDirectories);
            _logger.LogInformation("Config {config}", configuration["AppSettings:ImagesPath"]);
            string webRootPath = _hostingEnvironment.WebRootPath;

            model.Images = new List <ImageMeaningModel>();
            //int i = 0;
            for (var i = 0; i < words.Count(); i++)
            {
                var word = words[i];
                if (word.Trim() == "")
                {
                    continue;
                }
                var flag = 0;
                if (words.Count() > i + 1)
                {
                    var modifiedWord1 = word.Trim() + "-" + words[i + 1];
                    var fileExists    = System.IO.File.Exists(configuration["AppSettings:ImagesPath"] +
                                                              configuration["AppSettings:PathSeparator"] + modifiedWord1.Trim().ToLower() + ".jpg");

                    if (fileExists)
                    {
                        var file = configuration["AppSettings:ImagesPath"] +
                                   configuration["AppSettings:PathSeparator"] + modifiedWord1.Trim().ToLower() + ".jpg";

                        var idx = file.LastIndexOf(configuration["AppSettings:PathSeparator"]);
                        _logger.LogInformation("Idx {idx}", idx);
                        var fileName = file.Substring(idx + 1);

                        System.IO.File.Copy(file.Substring(0, file.LastIndexOf(configuration["AppSettings:PathSeparator"]) + 1) + fileName,
                                            webRootPath + "/images/" + fileName, true);
                        var imageModel = new ImageMeaningModel();
                        imageModel.Image   = "/images/" + fileName;
                        imageModel.Meaning = fileName.Split(".")[0];
                        model.Images.Add(imageModel);
                        flag = 1;
                        i   += 1;
                        //break;
                    }
                }
                if (flag != 1)
                {
                    if (words.Count() > i + 2)
                    {
                        var modifiedWord1 = word.Trim() + "-" + words[i + 1] + "-" + words[i + 2];
                        var fileExists    = System.IO.File.Exists(configuration["AppSettings:ImagesPath"] +
                                                                  configuration["AppSettings:PathSeparator"] + modifiedWord1.Trim().ToLower() + ".jpg");


                        if (fileExists)
                        {
                            var file = configuration["AppSettings:ImagesPath"] +
                                       configuration["AppSettings:PathSeparator"] + modifiedWord1.Trim().ToLower() + ".jpg";

                            var idx = file.LastIndexOf(configuration["AppSettings:PathSeparator"]);
                            _logger.LogInformation("Idx {idx}", idx);
                            var fileName = file.Substring(idx + 1);

                            if (modifiedWord1.Trim().ToLower() == fileName.Split(".")[0])
                            {
                                System.IO.File.Copy(file.Substring(0, file.LastIndexOf(configuration["AppSettings:PathSeparator"]) + 1) + fileName,
                                                    webRootPath + "/images/" + fileName, true);
                                var imageModel = new ImageMeaningModel();
                                imageModel.Image   = "/images/" + fileName;
                                imageModel.Meaning = fileName.Split(".")[0];
                                model.Images.Add(imageModel);
                                flag = 1;
                                i   += 2;
                                //break;
                            }
                        }
                    }
                    if (flag != 1)
                    {
                        if (System.IO.File.Exists(configuration["AppSettings:ImagesPath"] +
                                                  configuration["AppSettings:PathSeparator"] + word.Trim().ToLower() + ".jpg"))
                        {
                            var fileBasic = configuration["AppSettings:ImagesPath"] +
                                            configuration["AppSettings:PathSeparator"] + word.Trim().ToLower() + ".jpg";

                            var idx = fileBasic.LastIndexOf(configuration["AppSettings:PathSeparator"]);
                            _logger.LogInformation("Idx {idx}", idx);
                            var fileName = fileBasic.Substring(idx + 1);

                            System.IO.File.Copy(fileBasic.Substring(0, fileBasic.LastIndexOf(configuration["AppSettings:PathSeparator"]) + 1) + fileName,
                                                webRootPath + "/images/" + fileName, true);
                            var imageModel = new ImageMeaningModel();
                            imageModel.Image   = "/images/" + fileName;
                            imageModel.Meaning = fileName.Split(".")[0];
                            model.Images.Add(imageModel);
                            flag = 1;
                            //break;
                        }

                        if (flag != 1)
                        {
                            var modifiedWord = word.Trim() + "ње";
                            if (word.EndsWith("и"))
                            {
                                modifiedWord = word.TrimEnd('и') + "ење";
                            }

                            var fileModExists = System.IO.File.Exists(configuration["AppSettings:ImagesPath"] +
                                                                      configuration["AppSettings:PathSeparator"] + modifiedWord.ToLower() + ".jpg");

                            if (fileModExists)
                            {
                                var fileMod = configuration["AppSettings:ImagesPath"] +
                                              configuration["AppSettings:PathSeparator"] + modifiedWord.ToLower() + ".jpg";
                                var idx = fileMod.LastIndexOf(configuration["AppSettings:PathSeparator"]);
                                _logger.LogInformation("Idx {idx}", idx);
                                var fileName = fileMod.Substring(idx + 1);

                                System.IO.File.Copy(fileMod.Substring(0, fileMod.LastIndexOf(configuration["AppSettings:PathSeparator"]) + 1) + fileName,
                                                    webRootPath + "/images/" + fileName, true);
                                var imageModel = new ImageMeaningModel();
                                imageModel.Image   = "/images/" + fileName;
                                imageModel.Meaning = fileName.Split(".")[0];
                                model.Images.Add(imageModel);
                                flag = 1;
                                //break;
                            }
                            if (flag != 1)
                            {
                                if (char.IsUpper(word[0]))
                                {
                                    var charArray = word.ToCharArray();
                                    foreach (var c in charArray)
                                    {
                                        System.IO.File.Copy(configuration["AppSettings:ImagesPath"] + configuration["AppSettings:PathSeparator"] + "букви" +
                                                            configuration["AppSettings:PathSeparator"] + char.ToUpper(c) + ".jpg",
                                                            webRootPath + "/images/" + char.ToUpper(c) + ".jpg", true);
                                        var imageModel = new ImageMeaningModel();
                                        imageModel.Image   = "/images/" + char.ToUpper(c) + ".jpg";
                                        imageModel.Meaning = char.ToUpper(c).ToString();
                                        model.Images.Add(imageModel);
                                        flag = 1;
                                    }
                                    //break;
                                }
                                if (flag != 1)
                                {
                                    if (word.EndsWith("a"))
                                    {
                                        var modifiedWord1 = word.Substring(0, word.LastIndexOf("а"));
                                        modifiedWord1 += "ува";
                                        var fileModExists1 = System.IO.File.Exists(configuration["AppSettings:ImagesPath"] +
                                                                                   configuration["AppSettings:PathSeparator"] + modifiedWord1.ToLower() + ".jpg");
                                        if (fileModExists1)
                                        {
                                            var fileMod = configuration["AppSettings:ImagesPath"] +
                                                          configuration["AppSettings:PathSeparator"] + modifiedWord1.ToLower() + ".jpg";
                                            var idx = fileMod.LastIndexOf(configuration["AppSettings:PathSeparator"]);
                                            _logger.LogInformation("Idx {idx}", idx);
                                            var fileName = fileMod.Substring(idx + 1);

                                            System.IO.File.Copy(fileMod.Substring(0, fileMod.LastIndexOf(configuration["AppSettings:PathSeparator"]) + 1) + fileName,
                                                                webRootPath + "/images/" + fileName, true);
                                            var imageModel = new ImageMeaningModel();
                                            imageModel.Image   = "/images/" + fileName;
                                            imageModel.Meaning = fileName.Split(".")[0];
                                            model.Images.Add(imageModel);
                                            flag = 1;
                                            //break;
                                        }
                                    }

                                    if (flag != 1)
                                    {
                                        if (word.EndsWith("и"))
                                        {
                                            var modifiedWord1 = word.Substring(0, word.LastIndexOf("и"));
                                            modifiedWord1 += "ува";
                                            var fileModExists1 = System.IO.File.Exists(configuration["AppSettings:ImagesPath"] +
                                                                                       configuration["AppSettings:PathSeparator"] + modifiedWord1.ToLower() + ".jpg");
                                            if (fileModExists1)
                                            {
                                                var fileMod = configuration["AppSettings:ImagesPath"] +
                                                              configuration["AppSettings:PathSeparator"] + modifiedWord1.ToLower() + ".jpg";
                                                var idx = fileMod.LastIndexOf(configuration["AppSettings:PathSeparator"]);
                                                _logger.LogInformation("Idx {idx}", idx);
                                                var fileName = fileMod.Substring(idx + 1);

                                                System.IO.File.Copy(fileMod.Substring(0, fileMod.LastIndexOf(configuration["AppSettings:PathSeparator"]) + 1) + fileName,
                                                                    webRootPath + "/images/" + fileName, true);
                                                var imageModel = new ImageMeaningModel();
                                                imageModel.Image   = "/images/" + fileName;
                                                imageModel.Meaning = fileName.Split(".")[0];
                                                model.Images.Add(imageModel);
                                                flag = 1;
                                                //break;
                                            }
                                        }

                                        if (flag != 1)
                                        {
                                            if (word.EndsWith("е"))
                                            {
                                                var modifiedWord1 = word.Substring(0, word.LastIndexOf("е"));
                                                modifiedWord1 += "ува";
                                                var fileModExists1 = System.IO.File.Exists(configuration["AppSettings:ImagesPath"] +
                                                                                           configuration["AppSettings:PathSeparator"] + modifiedWord1.ToLower() + ".jpg");
                                                if (fileModExists1)
                                                {
                                                    var fileMod = configuration["AppSettings:ImagesPath"] +
                                                                  configuration["AppSettings:PathSeparator"] + modifiedWord1.ToLower() + ".jpg";
                                                    var idx = fileMod.LastIndexOf(configuration["AppSettings:PathSeparator"]);
                                                    _logger.LogInformation("Idx {idx}", idx);
                                                    var fileName = fileMod.Substring(idx + 1);

                                                    System.IO.File.Copy(fileMod.Substring(0, fileMod.LastIndexOf(configuration["AppSettings:PathSeparator"]) + 1) + fileName,
                                                                        webRootPath + "/images/" + fileName, true);
                                                    var imageModel = new ImageMeaningModel();
                                                    imageModel.Image   = "/images/" + fileName;
                                                    imageModel.Meaning = fileName.Split(".")[0];
                                                    model.Images.Add(imageModel);
                                                    flag = 1;
                                                    //break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }


                if (flag == 0)
                {
                    var imageModel = new ImageMeaningModel();
                    imageModel.Image   = "/images/noimage.jpg";
                    imageModel.Meaning = word.Trim();
                    model.Images.Add(imageModel);
                }

                //i++;
            }

            if (model.Images.Count == 0)
            {
                var imageModel = new ImageMeaningModel();
                imageModel.Image = "/images/noimage.jpg";
                imageModel.Image = "нема слика";
                model.Images     = new List <ImageMeaningModel> {
                    imageModel
                };
                return(Json(new { images = model.Images }));
            }

            if (System.IO.File.Exists(filePath))
            {
                // If file found, delete it
                System.IO.File.Delete(filePath);
            }

            return(Json(new { images = model.Images }));
        }
Пример #30
0
        public static async System.Threading.Tasks.Task Run(
            [BlobTrigger("pending/{name}")] Stream image,
            [Queue(Constants.QUEUE_NAME)] IAsyncCollector <string> applicationQueue,
            string name,
            ILogger log,
            ExecutionContext executionContext)
        {
            var sourceStream = new MemoryStream();
            await image.CopyToAsync(sourceStream);

            var bitmap = new Bitmap(sourceStream);

            var customVisionPredictionClient = new CustomVisionPredictionClient
            {
                ApiKey   = Environment.GetEnvironmentVariable("CustomVisionPredictionClient_ApiKey"),
                Endpoint = Environment.GetEnvironmentVariable("CustomVisionPredictionClient_Endpoint")
            };

            sourceStream.Position = 0;

            var response = await customVisionPredictionClient.DetectImageAsync(Guid.Parse(Environment.GetEnvironmentVariable("CustomVisionPredictionClient_ProjectId")), "Completed Route",
                                                                               sourceStream);

            var routes = new List <string>();

            foreach (var predictionModel in response.Predictions)
            {
                if (predictionModel.TagName == "Completed Route" && predictionModel.Probability > 0.85)
                {
                    var cropped = CropBitmap(bitmap,
                                             predictionModel.BoundingBox.Left,
                                             predictionModel.BoundingBox.Top,
                                             predictionModel.BoundingBox.Width,
                                             predictionModel.BoundingBox.Height);

                    var memoryStream = new MemoryStream();
                    //ONLY FOR DEBUG
                    //cropped.Save(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),Guid.NewGuid().ToString()));
                    cropped.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    memoryStream.Position = 0;

                    //https://stackoverflow.com/questions/53367132/where-to-store-files-for-azure-function
                    var path = Path.Combine(executionContext.FunctionAppDirectory, "Zwift-5c2367dfe003.json");

                    Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", path);

                    Image tmpImage = await Image.FromStreamAsync(memoryStream);

                    var client = await ImageAnnotatorClient.CreateAsync();

                    var tmp = await client.DetectTextAsync(tmpImage);

                    var annotation = tmp.FirstOrDefault();

                    if (annotation?.Description != null)
                    {
                        routes.Add(annotation.Description.Replace("\n", " ").Trim());
                    }
                }
            }

            if (routes.Count > 0)
            {
                var user = name.Split("_").First();
                await applicationQueue.AddAsync(JsonConvert.SerializeObject(new MultipleRoutesCompletedModel
                {
                    UserId = user,
                    Routes = routes
                }));

                await applicationQueue.FlushAsync();
            }
        }