Skip to content

Commit

Permalink
better logging, better local dir creation (#21)
Browse files Browse the repository at this point in the history
* better logging, better local dir creation

* Updated readme
  • Loading branch information
chpoit authored Jun 1, 2021
1 parent 6185aa8 commit b078646
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 23 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ This repo contains code that will allow you to extract all of your charms in Mon

It's called Utsushi's charm because I thought it would be funny to make a complementary "Utsushi's Armor Search System", but ["this armor set searcher exists"](https://mhrise.wiki-db.com/sim/?hl=en). I might still try to port Athena's ASS for MHW to MHR, but for now this works for me.

# New Update (Updated May 28th)
- Now with a User Interface
- Support for [Multiple Game languages](#supported-game-language)
# New Update (Updated June 1st)
- 1.5.1
- Resolves issues with local directory creation found in #20
- 1.5
- Now with a User Interface
- Support for [Multiple Game languages](#supported-game-language)

# Usage

Expand Down Expand Up @@ -85,7 +88,8 @@ It's called Utsushi's charm because I thought it would be funny to make a comple
# FAQ
- Q: Can I use a capture card instead?
- I tested recording in 720p and 1080p with OBS using a Genki Shadowcast and everything worked. (MKV, others should work too)
- I recommend recording in 30fps to avoid doubling the frames to process
- Make sure you properly set the resolution of the Shadowcast at or above 720p. The default appears to be 360/480p. [Example found here](https://github.com/chpoit/utsushis-charm/issues/20#issuecomment-852183555)
- I recommend recording in 30fps to avoid increasingt the frames to process
- 60 fps is fine, half of the frames are skipped.
- I see no reason other capture cards should not work
- 1080p adds extra processing due to a downscaling step to 720p (20-30% slower)
Expand Down
6 changes: 5 additions & 1 deletion src/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .utils import print_licenses
from .ui.MainWindow import MainWindow
from .translator import Translator
from .resources import get_language_code
from .resources import get_language_code, get_resource_path
import logging

logging.basicConfig(
Expand All @@ -24,6 +24,7 @@

def handle_exception(exception, value, traceback):
logger.error(f"An error occured {exception}, {value}, {str(traceback)}")
logger.exception(f"An error occured")
print("An error occured", exception)


Expand All @@ -32,6 +33,9 @@ def main(args):
print_licenses()
sys.exit(0)

local_dir = get_resource_path("LOCAL_DIR")
os.makedirs(local_dir, exist_ok=True)

translator = Translator()

if args.console:
Expand Down
3 changes: 3 additions & 0 deletions src/charm_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,11 @@ def keep_existing_and_update(x):
logger.warn(_("logger-skill-less").format(frame_loc))
except Exception as e:
logger.error(_("logger-charm-error").format(frame_loc, e))
logger.exception("Traceback")

except Exception as e:
logger.error(f"Crashed with {e}")
logger.exception("Traceback")

return remove_duplicates(charms, charm_callback)

Expand Down Expand Up @@ -310,6 +312,7 @@ def extract_basic_info(tess: Tesseract, frame_loc, frame):
return frame_loc, slots, skills, skill_text
except Exception as e:
logger.error(f"An error occured when analysing frame {frame_loc}. Error: {e}")
logger.exception("Traceback")
return None


Expand Down
53 changes: 36 additions & 17 deletions src/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import platform
from pathlib import Path
from symspellpy.symspellpy import SymSpell
import logging

logger = logging.getLogger(__name__)


HOME = str(Path.home())
Expand Down Expand Up @@ -54,18 +57,32 @@ def get_language_from_code(language_code):
return _reverse_language_code_mappings[language_code]


def _backup_corrections(language_code):
backup_corr = {}
all_skills = get_all_skills()
for skill in all_skills.values():
for word in skill.split():
backup_corr[word] = word

return backup_corr


def load_corrections(language_code, known_corrections=None):
known_corrections = known_corrections or {}
corrections_path = _corrections_path(language_code)
if not os.path.exists(corrections_path):
_create_default_skill_corrections(language_code)
try:
known_corrections = known_corrections or {}
corrections_path = _corrections_path(language_code)
if not os.path.isfile(corrections_path):
_create_default_skill_corrections(language_code)

with open(corrections_path, encoding="utf-8") as scf:
known_corrections = {
w: c for w, c in map(lambda x: x.strip().split(","), scf.readlines())
}
with open(corrections_path, encoding="utf-8") as scf:
known_corrections = {
w: c for w, c in map(lambda x: x.strip().split(","), scf.readlines())
}

return known_corrections
return known_corrections
except FileNotFoundError as e:
logger.exception("Had to load backup corrections")
return _backup_corrections(language_code)


def get_spell_checker(language_code):
Expand All @@ -85,24 +102,21 @@ def add_corrections(language_code, known_corrections, *new_tuples):


def _create_default_skill_corrections(language_code):
packaged_corrections = _alter_resource_path(
os.path.join("data", "skills", f"corrections.{language_code}.csv")
packaged_corrections = os.path.join(
get_resource_path("PACKAGED_SKILLS"), f"corrections.{language_code}.csv"
)
corrections_path = _corrections_path(language_code)
shutil.copy(packaged_corrections, corrections_path)


def _corrections_path(language_code):
return get_resource_path("skill_corrections").format(language_code)
return os.path.join(
get_resource_path("LOCAL_DIR"), f"corrections.{language_code}.csv"
)


_resources = {
"skill_directory": _alter_resource_path(os.path.join("data", "skills")),
"skill_corrections": os.path.join(
(os.getenv("LOCALAPPDATA") or HOME if WINDOWS else HOME),
"utsushis-charm",
"corrections.{}.csv",
),
"lv1": _alter_resource_path(os.path.join("images", "levels", "lv1.png")),
"lv2": _alter_resource_path(os.path.join("images", "levels", "lv2.png")),
"lv3": _alter_resource_path(os.path.join("images", "levels", "lv3.png")),
Expand All @@ -116,6 +130,11 @@ def _corrections_path(language_code):
"licences": _alter_resource_path("LICENSES"),
"TRANSLATIONS": _alter_resource_path(os.path.join("data", "translation")),
"ICON": _alter_resource_path(os.path.join("media", "icon.ico")),
"PACKAGED_SKILLS": _alter_resource_path(os.path.join("data", "skills")),
"LOCAL_DIR": os.path.join(
(os.getenv("LOCALAPPDATA") or HOME if WINDOWS else HOME),
"utsushis-charm",
),
}

_language_code_mappings = {
Expand Down
1 change: 1 addition & 0 deletions src/tesseract/Tesseract.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def set_variable(self, key, val):
pass
except Exception as e:
print(e)
logger.exception("Traceback")
pass

for t in test_img2:
Expand Down
7 changes: 6 additions & 1 deletion src/tesseract/tesseract_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def find_tesseract():
locations = [
ctypes.util.find_library("libtesseract-4"), # win32
ctypes.util.find_library("libtesseract302"), # win32 version 3.2
ctypes.util.find_library("libtesseract"), # others
ctypes.util.find_library("tesseract"), # others
]

Expand Down Expand Up @@ -70,7 +71,7 @@ def find_tesseract():
]

for potential in filter(lambda x: x, locations):
if os.path.isfile(potential):
if os.path.isfile(potential) or potential.startswith("libtesseract.so."):
logger.debug(f"Using tesseract at {potential}")
return potential

Expand Down Expand Up @@ -107,11 +108,15 @@ def get_datapath():
if "TESSDATA_PREFIX" not in os.environ:
set_tessdata()

if os.environ["TESSDATA_PREFIX"] == "tessdata":
override_tessdata()

return os.environ["TESSDATA_PREFIX"]


def download_language_data(lang="eng", _=lambda x: x, retry=False):
target_dir = get_datapath()
os.makedirs(target_dir, exist_ok=True)
full_name = os.path.join(target_dir, f"{lang}.traineddata")
if os.path.isfile(full_name):
print(_("tess-found-language"))
Expand Down

0 comments on commit b078646

Please sign in to comment.