forked from Dylan-Zheng/Rise-of-Kingdoms-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
70 lines (49 loc) · 1.84 KB
/
utils.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
from filepath.file_relative_paths import FilePaths
import cv2
import pytesseract as tess
import sys
import os
import inspect
import ctypes
import requests
import json
import traceback
def _async_raise(tid, exctype):
tid = ctypes.c_long(tid)
if not inspect.isclass(exctype):
exctype = type(exctype)
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
if res == 0:
raise ValueError("invalid thread id")
elif res != 1:
ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
raise SystemError("PyThreadState_SetAsyncExc failed")
def stop_thread(thread):
_async_raise(thread.ident, SystemExit)
def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
def build_command(program_path, *args):
return [program_path, *args]
def img_to_string(pil_image):
# pil_image.save(resource_path("test.png"))
tess.pytesseract.tesseract_cmd = resource_path(FilePaths.TESSERACT_EXE_PATH.value)
result = tess.image_to_string(pil_image, lang='eng', config='--psm 6') \
.replace('\t', '').replace('\n', '').replace('\f', '')
return result
def img_remove_background_and_enhance_word(cv_image, lower, upper):
hsv = cv2.cvtColor(cv_image, cv2.COLOR_BGR2HSV)
return cv2.inRange(hsv, lower, upper)
def aircv_rectangle_to_box(rectangle):
return rectangle[0][0], rectangle[0][1], rectangle[3][0], rectangle[3][1]
def bot_print(msg):
print(msg)
def get_last_info():
try:
url = 'https://raw.githubusercontent.com/Dylan-Zheng/Rise-of-Kingdoms-Bot/main/docs/version.json'
resp_text = requests.get(url).text
return json.loads(resp_text)
except Exception as e:
traceback.print_exc()
return {}