EmotiEffLib
Python implementation of Emotion Efficient Library
Loading...
Searching...
No Matches
utils.py
Go to the documentation of this file.
1"""
2Module with internally used utils
3"""
4
5import os
6import urllib.request
7
8
9def download_model(model_file: str, path_in_repo: str) -> str:
10 """
11 Download model from github and save to cache directory.
12
13 Args:
14 model_file (str): The name of the model (with file extension).
15 path_in_repo (str): Path to the directory with model in repo.
16
17 Returns:
18 str: The full path to the model file.
19 """
20 cache_dir = os.path.join(os.path.expanduser("~"), ".emotiefflib")
21 os.makedirs(cache_dir, exist_ok=True)
22 fpath = os.path.join(cache_dir, model_file)
23 if not os.path.isfile(fpath):
24 url = (
25 "https://github.com/sb-ai-lab/EmotiEffLib/blob/main/"
26 + path_in_repo
27 + model_file
28 + "?raw=true"
29 )
30 print("Downloading", model_file, "from", url)
31 urllib.request.urlretrieve(url, fpath)
32 return fpath
33
34
35def get_model_path_torch(model_name: str) -> str:
36 """
37 Return the local file path of a Torch model based on the given model name.
38
39 Args:
40 model_name (str): The name of the model (without file extension).
41
42 Returns:
43 str: The full path to the model file.
44 """
45 model_file = model_name + ".pt"
46 path_in_repo = "models/affectnet_emotions/"
47 return download_model(model_file, path_in_repo)
48
49
50def get_model_path_onnx(model_name: str) -> str:
51 """
52 Return the local file path of an ONNX model based on the given model name.
53
54 Args:
55 model_name (str): The name of the model (without file extension).
56
57 Returns:
58 str: The full path to the model file.
59 """
60 model_file = model_name + ".onnx"
61 path_in_repo = "models/affectnet_emotions/onnx/"
62 return download_model(model_file, path_in_repo)
63
64
66 """
67 Return the local file path of engagement classification model.
68
69 Returns:
70 str: The full path to the model file.
71 """
72 model_file = "engagement_single_attention.h5"
73 path_in_repo = "models/engagement_classification/"
74 return download_model(model_file, path_in_repo)
str get_engagement_classification_weights()
Definition utils.py:65
str get_model_path_torch(str model_name)
Definition utils.py:35
str download_model(str model_file, str path_in_repo)
Definition utils.py:9
str get_model_path_onnx(str model_name)
Definition utils.py:50