예제 #1
0
    private void HandleOcrJsonResponse(JSONObject j)
    {
        List <string> wordList  = new List <string>();
        string        textAngle = string.Empty;

        try { textAngle = j.GetField("textAngle").ToString(); }
        catch { }

        //Add computerVisionOCRDto
        OCRObject computerVisionOCR = new OCRObject()
        {
            language    = j.GetField("language").ToString(),
            textAngle   = textAngle,
            orientation = j.GetField("orientation").ToString()
        };

        //Add region, words
        var region = j.GetField("regions");

        if (region.list.Count != 0)
        {
            foreach (var regionItem in region.list)
            {
                var lines = regionItem.GetField("lines");
                foreach (var line in lines.list)
                {
                    var words = line.GetField("words");
                    foreach (var word in words.list)
                    {
                        wordList.Add(word.GetField("text").ToString().Replace("\"", ""));
                    }
                }
            }
        }

        computerVisionOCR.text = string.Join(" ", wordList.ToArray());

        if (wordList.Count > 0)
        {
            Globals.instance.textToSpeech.StartSpeaking(computerVisionOCR.text);

            // Globals.instance.textCycler.strings.AddRange(wordList);
        }
        else
        {
            Globals.instance.textToSpeech.StartSpeaking("I wasn't able to read anything");
        }
    }
        public virtual Dictionary<string, object> GetRecognizeDataByTemplate(byte[] image, string template)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();

            try
            {
                DateTime timestamp = DateTime.Now;

                OCRObject ocr = new OCRObject()
                {
                    IDCaptureRequest = new IDCaptureWCFSupport.RecognizeDataInputWCF(),
                    OCRObjectType = OCRObjectType.IDCapture, // this parameters tells system to recognize documents using templates
                    BatchName = Path.GetFileName(Path.GetDirectoryName(_documentPath)), // gradient direcotry name
                    BatchesDirectory = Path.GetDirectoryName(Path.GetDirectoryName(_documentPath)), // path of the gradient direcotry
                };

                // loading templates from disk
                List<Gradient.RecognitionTemplate.RecoData> templates = new List<Gradient.RecognitionTemplate.RecoData>()
                {
                    Gradient.ObjectSerializer.ObjectSerializer.Load<Gradient.RecognitionTemplate.RecoData>(template)
                };

                ocr.IDCaptureRequest.Threshold = 0;
                ocr.IDCaptureRequest.ImageData = image; // image bytes
                ocr.IDCaptureRequest.Templates = new IDCaptureWCFSupport.TemplatesWCF();
                ocr.IDCaptureRequest.Templates.Templates = ObjectSerializer.DeepClone<RecoData[]>(templates.ToArray());

                string [] parts = template.Split(new string[] { @"\" },  StringSplitOptions.RemoveEmptyEntries);
                if (parts.Length > 0)
                {
                    string filterStrongName = parts[parts.Length - 1].Replace(".xml", "");
                    string filter = string.Concat(_filterPath, @"\", filterStrongName, ".gcf");

                    if (File.Exists(filter))
                    {
                        ocr.IDCaptureRequest.ClassificationFilterName = filterStrongName; // used color filter
                        ocr.IDCaptureRequest.ColorFiltersPath = _filterPath; // filter directory

                        OnSetFilter(filter);
                    }
                }

                ocr.Priority = 1;

                FulltextProcessor.CurrentOCREngine.AddObjectToProcess(ocr, FulltextProcessor.CurrentProcessor);

                //wait async finish
                FulltextProcessor.CurrentOCREngine.WaitForAllToBeProcessed();

                if (ocr.ProcessedType == Gradient.MultiObjectsProcessor.ProcessedType.ProcessingError)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(ocr.Exception.ToString());
                }

                if (ocr.ProcessedType == Gradient.MultiObjectsProcessor.ProcessedType.Finished && ocr.IDCaptureResultData != null)
                {
                    Regex regex = new Regex(this.DetectRegex);
                    var docNo = ocr.IDCaptureResultData.Items.SingleOrDefault(i => i.Name == this.DetectKey);
                    if (docNo != null && regex.IsMatch(docNo.TextValue))
                    {
                        //save modified image
                        //IOSupport.RepeatingSaveAllBytes(_templatePath + "modified.tif", obj.IDCaptureResultData.ResultImageData);

                        foreach (var item in ocr.IDCaptureResultData.Items)
                        {
                            dic.Add(item.Name, item.Value);
                        }

                        OnRecognizeDataByTemplate(DateTime.Now - timestamp, template);

                        return dic;
                    }
                }
            }
            catch (Exception ex)
            {
                OnError(ex);
            }

            return dic;
        }