본문 바로가기
Android 기법/# Study

[Android/안드로이드]이미지 한글 문자 인식[tesseract-ocr]

by 퍼즐잎 2017. 2. 13.


라이브러리 참조

https://github.com/rmtheis/tess-two



이미지에서 텍스트를 추출하는 OCRengine 이다.

[인식률이 많이 좋은편은 아니다 ..]



구글에서 무료로 배포하고 


언어 데이터를 직접 개선할 수 있다고 한다.






언어 데이터는 다음과 같은 위치에 추가하고


https://github.com/tesseract-ocr/tessdata 이곳에서


언어별로 다양한 데이터가 많이 있다.



1
2
3
dependencies {
    compile 'com.rmtheis:tess-two:6.2.0'
}
cs


이것도 추가한다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
public class MainActivity extends AppCompatActivity {
 
    Bitmap image;
    private TessBaseAPI mTess;
    String datapath = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        image = BitmapFactory.decodeResource(getResources(), R.drawable.test1);
        datapath = getFilesDir()+ "/tesseract/";
 
        checkFile(new File(datapath + "tessdata/"));
 
        String lang = "kor";
        mTess = new TessBaseAPI();
        mTess.init(datapath, lang);
    }
 
    private void copyFiles() {
        try {
            //location we want the file to be at
            String filepath = datapath + "/tessdata/kor.traineddata";
 
            //get access to AssetManager
            AssetManager assetManager = getAssets();
 
            //open byte streams for reading/writing
            InputStream instream = assetManager.open("tessdata/kor.traineddata");
            OutputStream outstream = new FileOutputStream(filepath);
 
            //copy the file to the location specified by filepath
            byte[] buffer = new byte[1024];
            int read;
            while ((read = instream.read(buffer)) != -1) {
                outstream.write(buffer, 0, read);
            }
            outstream.flush();
            outstream.close();
            instream.close();
 
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    private void checkFile(File dir) {
        //directory does not exist, but we can successfully create it
        if (!dir.exists()&& dir.mkdirs()){
            copyFiles();
        }
        //The directory exists, but there is no data file in it
        if(dir.exists()) {
            String datafilepath = datapath+ "/tessdata/kor.traineddata";
            File datafile = new File(datafilepath);
            if (!datafile.exists()) {
                copyFiles();
            }
        }
    }
 
    public void processImage(View view){
        String OCRresult = null;
        mTess.setImage(image);
        OCRresult = mTess.getUTF8Text();
        TextView OCRTextView = (TextView) findViewById(R.id.text);
 
 
        OCRTextView.setText(OCRresult);
    }
}
 
cs







결과는 다음과 같다....




다른 OCRengine을 찾아보거나


직접 언어 데이터를 개선해서 사용해야 할거 같다.




댓글