/
classifier.py
56 lines (43 loc) · 1.43 KB
/
classifier.py
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
from subprocess import call
from os.path import join as create_path
from os.path import basename as base
from os.path import splitext as split
try:
from langdetect import detect
except:
print "please run \'pip install langdetect\' "
exit(1)
class Classifier(object):
def __init__(self, image_name=None, user_id=None):
self.image_name = image_name
self.path_to_img_translation = create_path(split(base(image_name))[0]+"_translation")
self.langs = {'es':'spanish','fr':'french','en':'english','du':'dutch'}
def build_directory():
try:
call(["mkdir","-p",path_to_translations])
except:
print "error building translation directory"
exit(1)
def extract_text(self):
try:
call(["tesseract", self.image_name, self.path_to_img_translation])
except OSError:
print "Please run \'brew install tesseract\'"
exit(1)
def classify_text(self):
self.extract_text()
ff = open(self.path_to_img_translation+".txt", 'r')
text = str(ff.read())
text = text.decode("utf-8")
translation = detect(text)
try:
print self.langs[translation]
except:
print translation
def output_translation():
pass
def main():
cc = Classifier("spanish.png")
cc.classify_text()
if __name__ == "__main__":
main()