forked from mathewthe2/Game2Text
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
78 lines (68 loc) · 3.21 KB
/
tools.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import sys, os
import platform
from pathlib import Path
from config import r_config, w_config, OCR_CONFIG, PATHS_CONFIG
from tkinter import *
from tkinter.filedialog import askopenfile
bundle_dir = Path(__file__).parent
if getattr(sys, 'frozen', False):
# we are running in a bundle
frozen = 'ever so'
bundle_dir = sys._MEIPASS
else:
# we are running in a normal Python environment
bundle_dir = os.path.dirname(os.path.abspath(__file__))
OSX_TESSERACT_VERSION = "4.1.1"
WIN_TESSERACT_DIR = Path(bundle_dir, "resources", "bin", "win", "tesseract")
OSX_TESSERACT_DIR = Path(bundle_dir, "resources", "bin", "mac", "tesseract", OSX_TESSERACT_VERSION)
def path_to_ffmpeg():
platform_name = platform.system()
if platform_name == 'Windows':
return str(Path(bundle_dir, "resources", "bin", "win", "ffmpeg", "ffmpeg.exe"))
elif platform_name == 'Darwin':
return str(Path(bundle_dir, "resources", "bin", "mac", "ffmpeg", "ffmpeg"))
return ''
def path_to_ffmpeg_folder():
return str(Path(path_to_ffmpeg()).parent)
def path_to_tesseract():
exec_data = {"Windows": str(Path(WIN_TESSERACT_DIR, "tesseract.exe")),
"Darwin": str(Path(OSX_TESSERACT_DIR, "bin", "tesseract")),
"Linux": "/usr/local/bin/tesseract"}
platform_name = platform.system() # E.g. 'Windows'
return exec_data[platform_name], platform_name
def get_tessdata_dir():
platform_name = platform.system()
if platform_name == 'Darwin':
if r_config(OCR_CONFIG, "oem") == '0':
# legacy tesseract
return '--tessdata-dir {}'.format(str(Path(OSX_TESSERACT_DIR, "share", "legacy", "tessdata")))
else:
return '--tessdata-dir {}'.format(str(Path(OSX_TESSERACT_DIR, "share", "tessdata")))
elif platform_name == 'Windows':
if (r_config(OCR_CONFIG, "oem") == '0' and Path(WIN_TESSERACT_DIR, "tessdata-legacy").exists()):
# legacy tesseract by renaming tessdata folders
os.rename(Path(WIN_TESSERACT_DIR, "tessdata"), Path(WIN_TESSERACT_DIR, "tessdata-new"))
os.rename(Path(WIN_TESSERACT_DIR, "tessdata-legacy"), Path(WIN_TESSERACT_DIR, "tessdata"))
elif (r_config(OCR_CONFIG, "oem") != '0' and Path(WIN_TESSERACT_DIR, "tessdata-new").exists()):
# revert to default tessdata folder
os.rename(Path(WIN_TESSERACT_DIR, "tessdata"), Path(WIN_TESSERACT_DIR, "tessdata-legacy"))
os.rename(Path(WIN_TESSERACT_DIR, "tessdata-new"), Path(WIN_TESSERACT_DIR, "tessdata"))
return ''
def path_to_textractor():
path = r_config('PATHS', 'textractor')
return path if path != 'default' else str(Path(bundle_dir, 'resources', 'bin', 'win', 'textractor', 'TextractorCLI.exe'))
def path_to_wexpect():
return str(Path(bundle_dir, 'resources', 'bin', 'win', 'wexpect', 'wexpect.exe'))
def open_folder_textractor_path():
root = Tk()
root.withdraw()
file = askopenfile(filetypes = (("EXE files","*.exe"),("all files","*.*")), defaultextension=".exe")
if not file:
return
try:
w_config(PATHS_CONFIG, {'textractor': file.name})
except:
print('File not selected')
file.close()
root.destroy()
return file.name