【问题标题】:How to Encoding Japanese text in Japanese window OS?如何在日文窗口操作系统中编码日文文本?
【发布时间】:2018-07-24 14:56:38
【问题描述】:

我正在使用 Tesseract 阅读日语文本。我正在从 OCR 获取以下文本。

æ—¥ä»~請求書

C++ 代码

 extern "C" _declspec(dllexport) char* _cdecl Test(char* imagePath)
    {
        char *outText;

        tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
        // Initialize tesseract-ocr with English, without specifying tessdata path
        if (api->Init("D:\\tessdata", "jpn", tesseract::OcrEngineMode::OEM_TESSERACT_ONLY))
        {
            fprintf(stderr, "Could not initialize tesseract.\n");           
        }

        api->SetPageSegMode(tesseract::PageSegMode::PSM_AUTO);      
        outText = api->GetUTF8Text();

        return outText;
    }

c#

[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
        public static extern string Test(string imagePath);

        void Tessrect()
        {
            string result = Test("D:\\japan4.png");
            byte[] bytes = System.Text.Encoding.Default.GetBytes(result);
            MessageBox.Show(System.Text.Encoding.UTF8.GetString(bytes));
        }

输入文件:

上面的代码在窗口英语中运行良好。但它不适用于窗口日本。它在窗口的日本操作系统中给出了错误的输出。

谁能指导我如何正确使用日本窗口?

【问题讨论】:

  • 我想你会想给它 UTF-16 而不是 UTF-8,因为 winapi 主要处理这个......我猜?
  • 文中有BOM说明是什么类型的吗?如果没有,请尝试 UTF-8 或 Shift-JIS。
  • 我已经尝试过使用 UTF-8 和 Shift-JIS。但没有帮助我

标签: c# c++ encoding tesseract


【解决方案1】:

outText 似乎已经是 UTF-8 格式了

outText = api->GetUTF8Text();

现在...从 C++ 返回 byte[](或类似的)是一种痛苦...更改为:

[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr Test(string imagePath);

然后从here 中取出StringFromNativeUtf8(因为即使将IntPtr 转换为UTF-8 c-string 也是一种痛苦…….NET 本身没有类似的东西):

void Tessrect()
{
    IntPtr result = IntPtr.Zero;
    string result2;

    try
    {
        result = Test("D:\\japan4.png");
        result2 = StringFromNativeUtf8(result);
    }
    finally
    {
        Free(result);
    }

    MessageBox.Show(result2);
}

那么你将不得不释放 IntPtr... 另一个痛苦。

[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern void Free(IntPtr ptr);

extern "C" _declspec(dllexport) void _cdecl Free(char* ptr)
{
    delete[] ptr;
}

【讨论】:

    【解决方案2】:

    您正在向非 UTF-8 的窗口发送 UTF-8 文本。 您需要在显示之前进行转换

    这是可能导致问题的代码(因为它试图使用您无法控制的默认系统编码); byte[] bytes = System.Text.Encoding.Default.GetBytes(result);

    您是否尝试在那里使用 Encoding.UTF8?

    如果仅此一项不起作用,请尝试更改 Encoding.UTF8 到 Encoding.Default 也在下面的行中。

    【讨论】:

    • 你是在建议 ?byte[] bytes = System.Text.Encoding.UTF8.GetBytes(result); MessageBox.Show(System.Text.Encoding.UTF8.GetString(bytes));
    【解决方案3】:

    你必须先从 imagePath 创建一个图像对象。

    在我的例子中,这是通过使用著名的 opencv 来完成的。 然后,使用 SetImage 函数。

    void detectJpn(cv::Mat& img)
    {
        char *outText;
    
        // Create Tesseract object
        tesseract::TessBaseAPI *ocr = new tesseract::TessBaseAPI();
    
        ocr->Init(NULL, "jpn", tesseract::OEM_TESSERACT_ONLY);
    
        // Set Page segmentation mode to PSM_AUTO (3)
        ocr->SetPageSegMode(tesseract::PSM_AUTO);
    
        ocr->SetImage((uchar*)img.data, img.size().width, img.size().height, img.channels(), img.step1());
    
        // Run Tesseract OCR on image
        outText = ocr->GetUTF8Text();
    
        // print recognized text
        std::cout << outText << std::endl; // Destroy used object and release memory ocr->End();
    
        //ocr->Clear();
        //ocr->End();
    
        delete ocr;
        ocr = nullptr;
    }
    
    
    int main(int argc, char *argv[])
    {
        cv::Mat img = imread(argv[1], cv::IMREAD_UNCHANGED);
    
        detectJpn(img);     
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-09
      • 2017-08-27
      • 1970-01-01
      • 2020-03-22
      • 2021-01-15
      • 2011-08-26
      相关资源
      最近更新 更多