Azure OCR和Unity3D从图像捕获中提取文本

时间:2018-06-01 15:19:57

标签: c# azure unity3d ocr azure-cognitive-services

我正在尝试从Hololens上的图像中提取文本。我可以检测是否有文字。但如果有文本它会抛出一个exeption(见下文)我试图将这些单词提取成一个单独的字符串,这样我就可以只显示文本而不需要所有Json格式化。

我一直关注以下项目: MS azure visionHololens AcademyAzureOCRsearch

   [Serializable]
public class AnalysedText
{
    public RegionData[] regions;
    public float textAngle;
    public string orientation;
    public string language;
}

 [Serializable]
public class RegionData
{
    public LineData[] lineData;
    public string boundingBox;
}

[Serializable]
public class LineData
{
    public string boundingBox;
    public WordData[] words;
}

[Serializable]
public class WordData
{
    public string boundingBox;
    public string text;
}

                //BEGIN TEXT ANALISIS
            if (isTextAnalysis == true)
            {
                AnalysedText analysedText = new AnalysedText();
                analysedText = JsonUtility.FromJson<AnalysedText>(jsonResponse);

                if (analysedText.language == "unk")
                {
                    Debug.Log("I did not see any text");
                    //ResultsLabel.instance.SetStringToLastLabel(JsonPrettyPrint(jsonResponse));

                }
                else
                {


                    Dictionary<string, string> wordDictionary = new Dictionary<string, string>();
                    List<LineData[]> lineList = new List<LineData[]>();
                    List<WordData[]> wordList = new List<WordData[]>();


                    foreach (RegionData rd in analysedText.regions)
                    {


                        RegionData region = rd as RegionData;
                        lineList.Add(rd.lineData);

                        Debug.Log(lineList[0]);

                        foreach (LineData ld in rd.lineData)
                        {

                            LineData line = ld as LineData;
                            Debug.Log("");
                            wordList.Add(ld.words);
                            foreach (WordData wd in ld.words)
                            {

                                WordData word = wd as WordData;
                                Debug.Log("wordDictionary");
                                wordDictionary.Add(word.text, word.boundingBox);
                                Debug.Log(wordDictionary);

                            }
                        }
                    }
                    for (int i = 0; i < wordList.Count; i++)
                    {

                        ResultsLabel.instance.SetText(wordDictionary);

                    }

当我跑步时,我拍摄了图像并获得了预测代码: &#34; Json exception.Message:对象引用未设置为对象的实例&#34;参考这一行:

foreach (LineData ld in rd.lineData)

LineList [0]的调试也返回null。

我也试过用

直接访问数据
                            for (int i = 0; i < analysedText.regions.lineData.words.text.Length; i++)
                        {
ResultsLabel.instance.SetTags(analysedText.regions.lineData.words.text[i].ToString());
                        }

但这也行不通。

为了清楚起见,我可以简单地在其好的json&#34;中打印出完整的Json。形式,但这不是我想要的。如果有多行,我想将文本提取为字符串或字符串集,以便我可以根据需要将其传递。

谢谢

0 个答案:

没有答案