1

how to restrict the results of tess-two (Tesseract and Leptonica library),
I want Tesseract limiting the results:

  1. Only take 8 digits, calculated from letter D
  2. Don't take LowerCase, Enter, Space, and Symbol
  3. Only Take Uppercase and Numbers.

For Example:
The recognition result is "asn*&bhDK 1234 UDaks&%^jdg", then simply take is "DK1234UD".
so, don't take LowerChase, Enter, Space. Only take UperChase and numbers.

I use Java source code

this is the recognition code:

    TessBaseAPI baseApi = new TessBaseAPI();
    baseApi.setPageSegMode(TessBaseAPI.OEM_TESSERACT_CUBE_COMBINED);
    baseApi.setPageSegMode(PageSegMode.PSM_AUTO_OSD);
    baseApi.setPageSegMode(PageSegMode.PSM_SINGLE_LINE);
    baseApi.setDebug(true);
    baseApi.init(DATA_PATH, lang);
    //setImage
    baseApi.setImage(bmpOtsu);
    //set whitelist
    String whitelist = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    baseApi.setVariable(TessBaseAPI.VAR_CHAR_WHITELIST, whitelist);
    //variable for recognizing      
    String recognizedText = baseApi.getUTF8Text();
    String resultTxt = recognizedText;
    baseApi.end();

    if ( lang.equalsIgnoreCase("eng") ) {
        recognizedText = recognizedText.replaceAll("[^A-Z0-9]", " ");
    }

Can somebody tell me how can i do that? What should be added in here?

4

2 回答 2

3

如果你使用 instance of TessBaseAPI,你可以setVariable()用常量调用VAR_CHAR_WHITELIST

String whiteList = "ABCD...XYZ1234567890";
tessBaseApi.setVariable(TessBaseAPI.VAR_CHAR_WHITELIST,whiteList);

您可以根据需要调整白名单,因此如果您想忽略除 D 和 K 之外的所有其他字母,请设置它:

String whiteList = "DK1234567890";

如果需要,您可能仍需要对结果进行更多的字符串操作,例如从结果末尾删除字母,根据您的示例,您可能会得到这个结果(使用第二个 whilteList)

DK1234UD

编辑:

要从 DK123455UD 获取结果,您可以使用 substring()

String result = "DK123455UD";
int pos = result.indexOf("DK");
String finalResult = result.substring(pos,pos+8);

编辑:
像这样?

    String whitelist = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    baseApi.setVariable(TessBaseAPI.VAR_CHAR_WHITELIST, whitelist);
    //setImage
    baseApi.setImage(bmpOtsu);
    //variable for recognizing      
    String recognizedText = baseApi.getUTF8Text();
    //
    int get8digits = recognizedText.indexOf("D");
    String resultTxt = recognizedText.substring(get8digits, get8digits+8);
于 2014-10-02T07:11:00.477 回答
2

感谢@Yazan 的回答,它的工作。
我已经改进了答案。
这是我的代码:

        TessBaseAPI baseApi = new TessBaseAPI();
    baseApi.setPageSegMode(TessBaseAPI.OEM_TESSERACT_CUBE_COMBINED);
    baseApi.setPageSegMode(PageSegMode.PSM_AUTO_OSD);
    baseApi.setPageSegMode(PageSegMode.PSM_SINGLE_LINE);
    baseApi.setDebug(true);
    baseApi.init(DATA_PATH, lang);
    //set variable
    String whiteList = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    String blackList = "\\s";
    baseApi.setVariable(TessBaseAPI.VAR_CHAR_WHITELIST, whiteList);
    baseApi.setVariable(TessBaseAPI.VAR_CHAR_BLACKLIST, blackList);
    //setImage
    //baseApi.setImage(bmpOtsu, w, h, 8, (Integer) null);
    baseApi.setImage(bmpOtsu);
    //variable for recognizing      
    String recognizedText = baseApi.getUTF8Text();
    recognizedText = recognizedText.replaceAll(blackList, "");//remove space
    String resultTxt = recognizedText;
    //
    baseApi.end();

    Log.v(TAG, "OCRED TEXT: " + recognizedText);
    if ( lang.equalsIgnoreCase("eng") ) {
        int get8digits = recognizedText.indexOf("D");
        String loop = recognizedText.substring(get8digits, recognizedText.length());
        if(recognizedText.contains("D") && loop.length() >= 8){
            Log.w(TAG, "OPSI 1"+"\n"+"Length: "+loop.length()+"\n"+"Values: "+loop);                
            recognizedText = recognizedText.substring(get8digits, get8digits+8);                                                
        }else if(recognizedText.contains("D") && loop.length() < 8){
            Log.w(TAG, "OPSI 2"+"\n"+"Length: "+loop.length()+"\n"+"Values: "+loop);
            recognizedText = loop;
        }else{
            Log.w(TAG, "OPSI 3"+"\n"+"Length: "+loop.length()+"\n"+"Values: "+loop);
            recognizedText = recognizedText.replaceAll("[A-Z0-9]"," ");

        }

我希望这对任何人都有帮助。

于 2014-10-05T13:09:33.870 回答