Debian Codegraph @

lios/ocr/ocr_engine_tesseract.py

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 #!/usr/bin/python3
2 ###########################################################################
3 # Lios - Linux-Intelligent-Ocr-Solution
4 # Copyright (C) 2015-2016 Nalin.x.Linux GPL-3
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 ###########################################################################
19
20 import os
21 import subprocess
22 from lios.ocr.ocr_engine_base import OcrEngineBase
23
24 TESSDATA_POSSIBLE_PATHS = [
25 "/usr/share/tesseract-ocr/tessdata",
26 "/usr/share/tesseract-ocr/4.00/tessdata",
•27 "/usr/share/tesseract-ocr/5.00/tessdata",
28 "/usr/share/tesseract-ocr/5/tessdata",
29 "/usr/share/tesseract/tessdata",
30 "/usr/share/tessdata",
31 "/usr/local/share/tesseract-ocr/tessdata",
32 "/usr/local/share/tesseract/tessdata",
33 "/usr/local/share/tessdata",
•34 "/usr/local/share/tesseract-ocr/4.00/tessdata",
35 "/usr/local/share/tesseract-ocr/5.00/tessdata",
36 "/usr/local/share/tesseract-ocr/5/tessdata"]
37
38 TESSDATA_EXTENSION = ".traineddata"
39
40
41 class OcrEngineTesseract(OcrEngineBase):
42 name = "Tesseract"
43
44 def __init__(self,language=None):
45 self.set_language(language)
46
47 def is_available():
48 if ("/bin/tesseract" in subprocess.getoutput("whereis tesseract")):
49 return True
50 else:
51 return False
52
53 def ocr_image_to_text(self,file_name):
54 os.system("convert {} -background white -flatten +matte /tmp/{}_for_ocr.png".format(file_name,file_name.split("/")[-1]))
55
56 languages = self.language
57 if(self.language_2 != False):
58 languages = languages+"+"+self.language_2
59 if(self.language_3 != False):
60 languages = languages+"+"+self.language_3
61
62 os.system("tesseract /tmp/{0}_for_ocr.png /tmp/{0}_output -l {1}".format(file_name.split("/")[-1],languages))
63
64 os.remove("/tmp/{0}_for_ocr.png".format(file_name.split("/")[-1]))
65
66 try:
67 with open("/tmp/{0}_output.txt".format(file_name.split("/")[-1]),encoding="utf-8") as file:
68 text = file.read().strip()
69 os.remove("/tmp/{0}_output.txt".format(file_name.split("/")[-1]))
70 return text
71 except:
72 return ""
73 def cancel():
74 os.system("pkill convert")
75 os.system("pkill tesseract")
76
77
78 def get_available_languages_in_dirpath(dirpath):
79 langs = []
80 if os.access(dirpath, os.R_OK):
81 for filename in os.listdir(dirpath):
82 if filename.lower().endswith(TESSDATA_EXTENSION):
83 lang = filename[:(-1 * len(TESSDATA_EXTENSION))]
84 langs.append(lang)
85 return langs
86
87 def get_available_languages():
88 langs = []
89 for dirpath in TESSDATA_POSSIBLE_PATHS[::-1]:
90 if (os.path.isfile(dirpath+"/configs/box.train")):
91 for item in OcrEngineTesseract.get_available_languages_in_dirpath(dirpath):
92 langs.append(item)
93 return sorted(langs)
94 return langs
95
96 def get_all_available_dirs():
97 result = []
98 for root, dirs, files in os.walk("/"):
99 if "tessdata" in dirs:
100 dir = os.path.join(root, "tessdata")
101 if (os.path.isfile(dir+"/configs/box.train")):
102 result.append(dir)
103
104 # Sorting according to possible list
105 # [::-1] is used to reverse
106 for path in TESSDATA_POSSIBLE_PATHS[::-1]:
107 if (path in result):
108 result.insert(0, result.pop(result.index(path)))
109 return result
110
111 def get_available_dirs():
112 dir_list = [];
113 for path in TESSDATA_POSSIBLE_PATHS:
114 if (os.path.exists(path)):
115 if (os.path.isfile(path+"/configs/box.train")):
116 dir_list.append(path);
117 return dir_list;
118
119
120 def support_multiple_languages():
121 return True