Пример #1
0
        static void Main(string[] args)
        {
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            // Handle input arguments
            byte[] image;

            if (args.Length == 0)
            {
                Console.WriteLine($"Using file: {TestImageFilePath}");
                try
                {
                    image = File.ReadAllBytes(TestImageFilePath);
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Error when loading image: {e.Message}");
                    return;
                }
            }
            else
            {
                var possibleUrl = args[0];
                Console.WriteLine($"Using url from args: {possibleUrl}");
                try
                {
                    using (var webClient = new WebClient())
                    {
                        image = webClient.DownloadData(possibleUrl);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Error when loading image from URL: {e.Message}");
                    return;
                }
            }

            // Classify
            //var classificationResults = new TensorFlowClassificator().ClassifyImage(image);
            var classificationResults = new WebClassificator().ClassifyImage(image);
            // OCR
            var text = TesseractOcr.ParseText();

            // Print
            Console.WriteLine("------------------------");
            Console.WriteLine("OBJECT CLASSIFICATION");
            Console.WriteLine("------------------------");

            foreach (var classification in classificationResults.OrderByDescending(x => x.Value))
            {
                Console.WriteLine($"{classification.Key, 15} => {Math.Round((classification.Value * 100.0), 3)}%");
            }

            Console.WriteLine("------------------------");
            Console.WriteLine("OCR WEIGHT FINDER RESUTLS");
            Console.WriteLine("------------------------");

            text = text.Trim().Replace("\r\n", " ").Replace("\n", " ");
            var regexWeightFinder = new RegexMetricWeightSubstringFinder();
            var octResult         = regexWeightFinder.FindWeightSpecifier(text);

            Console.WriteLine(octResult);

            Console.WriteLine("------------------------");
            Console.WriteLine("OCR KEYWORDS");
            Console.WriteLine("------------------------");

            var keywords = text.Split(' ')
                           .Where(word => !string.IsNullOrWhiteSpace(word))
                           .Where(word => OcrKeywords.Any(keyword => word.ToLower().Contains(keyword)));

            foreach (var keyword in keywords)
            {
                Console.WriteLine(keyword);
            }

            Console.WriteLine("------------------------");
            stopwatch.Stop();
            Console.WriteLine($"Total time: {stopwatch.Elapsed}");
        }
Пример #2
0
        private void Recognise()
        {
            int maxWebClassifierImageSize = int.Parse(ConfigurationManager.AppSettings["webClassifierImgSize"]);

            //progressBar.Visibility = ViewStates.Visible;
            Task.Run(async() =>
            {
                var image             = GetResizedImageForClassification(bitmap);
                var classifyImageTask = GetClassifyImageTask(image);
                if (!textRecognizer.IsOperational)
                {
                    System.Diagnostics.Debug.WriteLine("[Warning]: Text recognizer not operational.");
                    return(new RecognitionResult {
                        Predictions = await classifyImageTask
                    });
                }
                var recognizeTextTask = GetTextFromImageAsync(image);
                await Task.WhenAll(classifyImageTask, recognizeTextTask);
                var ocrResult         = recognizeTextTask.Result;
                var weightString      = new RegexMetricWeightSubstringFinder().FindWeightSpecifier(ocrResult);
                var recognitionResult = new RecognitionResult
                {
                    RawOcrResult    = ocrResult,
                    Predictions     = classifyImageTask.Result,
                    WeightSpecifier = weightString
                };
                return(recognitionResult);
            })
            .ContinueWith(task =>
            {
                //progressBar.Visibility = ViewStates.Gone;
                if (task.IsFaulted)
                {
                    System.Diagnostics.Debug.WriteLine(task.Exception);
                    return;
                }

                var recognitionResult = task.Result;
                var ocrResult         = recognitionResult.RawOcrResult;
                string productName    = recognitionResult.BestPrediction;

                if (productName == "can" || productName == "packet")
                {
                    productName += " of " + ocrResult;
                }

                guess       = productName.FirstCharToUpper();
                productName = productName.FirstCharToUpper();

                price = 0;

                var simpleProductName = recognitionResult.BestPrediction;

                var product =
                    MainActivity.shopLensDbContext.Products
                    .FirstOrDefault(p => p.Name == simpleProductName && p.Shop.Name == shopName);

                if (product != null)
                {
                    price = product.FullPrice;
                }

                var thingsToSay = "This is " + productName + ". It costs " + price + " euros.";
                tts.SetSpeechRate((float)0.8);
                tts.Speak(
                    thingsToSay,
                    QueueMode.Flush,
                    null,
                    null);

                while (tts.IsSpeaking)
                {
                    Thread.Sleep(500);
                }

                //if (!string.IsNullOrEmpty(ocrResult))
                //new MessageBarCreator(rootView, $"OCR: {ocrResult}").Show();
                if (areVoiceCommandsOn)
                {
                    tts.Speak("Would you like to add " + productName + " to your shopping cart?", QueueMode.Flush, null, null);
                    while (tts.IsSpeaking)
                    {
                        Thread.Sleep(500);
                    }
                    shopLensVoiceRec.ListenForAPhrase();
                }
                else
                {
                    GetShoppingCartAddItemDialog(productName, price).Show();
                }
            }, TaskScheduler.FromCurrentSynchronizationContext());
        }