forked from shrubin/Genshin-Artifact-Rater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rate_artifact.py
234 lines (210 loc) · 7.7 KB
/
rate_artifact.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import translations as tr
import aiohttp
import asyncio
import os
import re
import sys
import numpy as np
from cv2 import cv2
from dotenv import load_dotenv
from fuzzywuzzy import fuzz, process
from unidecode import unidecode
load_dotenv()
OCR_API_KEY = os.getenv('OCR_SPACE_API_KEY')
reg = re.compile(r'\d+(?:[.,]\d+)?')
bad_reg = re.compile(r'\d+/1000$')
hp_reg = re.compile(r'\d[.,]\d{3}')
lvl_reg = re.compile(r'^\+\d\d?$')
bad_lvl_reg_1 = re.compile(r'^\+?\d\d?$')
bad_lvl_reg_2 = re.compile(r'^\d{4}\d*$')
async def ocr(url, num, lang=tr.en()):
if not OCR_API_KEY:
print('Error: OCR_SPACE_API_KEY not found')
return False, 'Error: OCR_SPACE_API_KEY not found'
async with aiohttp.ClientSession() as session:
async with session.get(url) as r:
size = int(r.headers['Content-length'])
if size > 5e6:
img = np.asarray(bytearray(await r.read()), dtype="uint8")
flag = cv2.IMREAD_GRAYSCALE
if size > 8e6 or os.path.splitext(url)[1] == '.jpg':
flag = cv2.IMREAD_REDUCED_GRAYSCALE_2
img = cv2.imdecode(img, flag)
_, img = cv2.imencode('.png', img)
data = aiohttp.FormData()
data.add_field('apikey', OCR_API_KEY)
if lang.supported:
data.add_field('OCREngine', '2')
else:
data.add_field('language', lang.code)
data.add_field('file', img.tobytes(), content_type='image/png', filename='image.png')
ocr_url = f'https://apipro{num}.ocr.space/parse/image'
async with session.post(ocr_url, data=data) as r:
json = await r.json()
else:
ocr_url = f'https://apipro{num}.ocr.space/parse/imageurl?apikey={OCR_API_KEY}&url={url}'
if lang.supported:
ocr_url += '&OCREngine=2'
else:
ocr_url += f'&language={lang.code}'
async with session.get(ocr_url) as r:
json = await r.json()
print(f'OCR Response: {json}')
if json['OCRExitCode'] != 1:
return False, f'{lang.err}: ' + '. '.join(json['ErrorMessage'])
if 'ParsedResults' not in json:
return False, lang.err_unknown_ocr
return True, json['ParsedResults'][0]['ParsedText']
def parse(text, lang=tr.en()):
stat = None
results = []
level = None
prev = None
del_prev = True
elements = [lang.anemo, lang.elec, lang.pyro, lang.hydro, lang.cryo, lang.geo, lang.dend]
choices = elements + [lang.hp, lang.heal, lang.df, lang.er, lang.em, lang.atk, lang.cd, lang.cr, lang.phys]
choices = {unidecode(choice).lower(): choice for choice in choices}
for line in text.splitlines():
if not line:
continue
if del_prev:
prev = None
del_prev = True
for k,v in lang.replace.items():
line = line.replace(k,v)
line = unidecode(line).lower()
line = line.replace(':','.').replace('-','').replace('0/0','%')
if line.replace(' ','') in lang.ignore or bad_reg.search(line.replace(' ','')):
continue
if fuzz.partial_ratio(line, unidecode(lang.piece_set).lower()) > 80 and len(line) > 4:
break
value = lvl_reg.search(line.replace(' ',''))
if value:
if level == None or (len(results) == 1 and not stat):
print('1', line)
level = int(value[0].replace('+',''))
continue
value = hp_reg.search(line.replace(' ',''))
if value:
print('2', line)
value = int(value[0].replace(',','').replace('.',''))
results += [[lang.hp, value]]
stat = None
continue
extract = process.extractOne(line, list(choices))
if extract[1] <= 80:
extract = process.extractOne(line, list(choices), scorer=fuzz.partial_ratio)
if ((extract[1] > 80) and len(line.replace(' ','')) > 1) or stat:
print('3', line)
if (extract[1] > 80):
stat = choices[extract[0]]
value = reg.findall(line.replace(' ','').replace(',','.'))
if not value:
if not prev:
continue
print('4', prev)
value = prev
value = max(value, key=len)
if len(value) < 2:
continue
if line.find('%', line.find(value)) != -1 and '.' not in value:
value = value[:-1] + '.' + value[-1]
if '.' in value:
value = float(value)
stat += '%'
else:
value = int(value)
results += [[stat, value]]
stat = None
if len(results) == 5:
break
continue
value = bad_lvl_reg_1.search(line.replace(' ','')) or bad_lvl_reg_2.search(line.replace(' ','').replace('+',''))
if not value:
line = line.replace(',','')
prev = reg.findall(line.replace(' ',''))
del_prev = False
print(level, results)
return level, results
def validate(value, max_stat, percent):
while value > max_stat * 1.05:
value = str(value)
removed = False
for i in reversed(range(1, len(value))):
if value[i] == value[i-1]:
value = value[:i-1] + value[i:]
removed = True
break
if not removed:
if percent:
pos = value.find('.')
value = value[:pos-1] + value[pos:]
else:
value = value[:-1]
value = float(value) if percent else int(value)
if int(value) == 1:
value += 10
return value
def rate(level, results, options={}, lang=tr.en()):
main = True
main_score = 0.0
sub_score = 0.0
sub_weight = 0
main_weight = 3 + level / 4
elements = [lang.anemo, lang.elec, lang.pyro, lang.hydro, lang.cryo, lang.geo, lang.dend]
min_mains = {lang.hp: 717.0, lang.atk: 47.0, f'{lang.atk}%': 7.0, f'{lang.er}%': 7.8, lang.em: 28.0,
f'{lang.phys}%': 8.7, f'{lang.cr}%': 4.7, f'{lang.cd}%': 9.3, f'{lang.elem}%': 7.0,
f'{lang.hp}%': 7.0, f'{lang.df}%': 8.7, f'{lang.heal}%': 5.4}
max_mains = {lang.hp: 4780, lang.atk: 311.0, f'{lang.atk}%': 46.6, f'{lang.er}%': 51.8, lang.em: 187.0,
f'{lang.phys}%': 58.3, f'{lang.cr}%': 31.1, f'{lang.cd}%': 62.2, f'{lang.elem}%': 46.6,
f'{lang.hp}%': 46.6, f'{lang.df}%': 58.3, f'{lang.heal}%': 35.9}
max_subs = {lang.atk: 19.0, lang.em: 23.0, f'{lang.er}%': 6.5, f'{lang.atk}%': 5.8,
f'{lang.cr}%': 3.9, f'{lang.cd}%': 7.8, lang.df: 23.0, lang.hp: 299.0, f'{lang.df}%': 7.3, f'{lang.hp}%': 5.8}
weights = {lang.hp: 0, lang.atk: 0.5, f'{lang.atk}%': 1, f'{lang.er}%': 0.5, lang.em: 0.5,
f'{lang.phys}%': 1, f'{lang.cr}%': 1, f'{lang.cd}%': 1, f'{lang.elem}%': 1,
f'{lang.hp}%': 0, f'{lang.df}%': 0, lang.df: 0, f'{lang.heal}%': 0}
# Replaces weights with options
weights = {**weights, **options}
for result in results:
stat, value = result
key = stat if stat[:-1] not in elements else f'{lang.elem}%'
if main:
main = False
max_main = max_mains[key] - (max_mains[key] - min_mains[key]) * (1 - level / 20.0)
value = validate(value, max_main, '%' in key)
main_score = value / max_main * weights[key] * main_weight
if key in [lang.atk, lang.hp]:
main_weight *= weights[key]
count = 0
for k,v in sorted(weights.items(), reverse=True, key=lambda item: item[1]):
if k == key or k not in max_subs:
continue
if count == 0:
sub_weight += v * (1 + level / 4)
else:
sub_weight += v
count += 1
if count == 4:
break
else:
value = validate(value, max_subs[key] * 6, '%' in key)
sub_score += value / max_subs[key] * weights[key]
result[1] = value
score = (main_score + sub_score) / (main_weight + sub_weight) * 100 if main_weight + sub_weight > 0 else 100
main_score = main_score / main_weight * 100 if main_weight > 0 else 100
main_score = 100 if main_score > 99 else main_score
sub_score = sub_score / sub_weight * 100 if sub_weight > 0 else 100
print(f'Gear Score: {score:.2f}% (main {main_score:.2f}% {main_weight}, sub {sub_score:.2f}% {sub_weight})')
return score, main_score, main_weight, sub_score, sub_weight
if __name__ == '__main__':
if sys.version_info[0] == 3 and sys.version_info[1] >= 8 and sys.platform.startswith('win'):
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
url = 'https://cdn.discordapp.com/attachments/787747793228922910/794556364059705374/image0.png'
lang = tr.vi()
suc, text = asyncio.run(ocr(url, 2, lang))
print(text)
if suc:
level, results = parse(text, lang)
if level == None:
level = 20
rate(level, results, {}, lang)