-
Notifications
You must be signed in to change notification settings - Fork 1
/
inference.py
77 lines (63 loc) · 1.97 KB
/
inference.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
#! /usr/bin/env python
"""
Run face expression detection locally on your system in several modes:
1. On a library of images.
2. On a video stream from your webcam, in real time.
3. On a saved video file (currently unimplemented).
"""
import argparse
import os
import json
import torch
from torch.hub import download_url_to_file
from frontend import *
argparser = argparse.ArgumentParser(
"Run face expression inference on files, saved video or camera feed")
argparser.add_argument(
'-m',
'--mode',
type = int,
help = "Mode of inference (1, 2 or 3; See README)")
argparser.add_argument(
'-w',
'--weights',
type = str,
default = 'weights/weights.pkl',
help = "Path to EmotionNet weights file")
argparser.add_argument(
'-d',
'--device',
type = str,
default = "cpu",
help = "Compute device - 'cuda' or 'cpu'")
argparser.add_argument(
'-c',
'--config',
type = str,
default = "config.json",
help = "Path to config.json file")
PRETRAINED_WEIGHTS_URL = "https://github.com/codedev99/fast-face-exp/releases/download/v0.3/newenet_paperv3_exp1_net2_5emo.pkl"
class ModeError(Exception):
def __init__(self):
message = "The mode of inference is invalid. Please use an acceptable mode (see README)"
super(ModeError, self).__init__(message)
def main(argv):
if not os.path.exists(args.weights):
download_url_to_file(PRETRAINED_WEIGHTS_URL, args.weights, progress=True)
device = args.device
if not torch.cuda.is_available():
device = 'cpu'
with open(args.config) as config_buffer:
config = json.loads(config_buffer.read())
if args.mode == 1:
files_inference(args.weights, config["inference"]["files"]["data_folder"],
config["inference"]["emotionnet"]["class_labels"], device)
elif args.mode == 2:
camfeed_inference(args.weights, config["inference"]["emotionnet"]["class_labels"], device)
elif args.mode == 3:
print("Not implemented yet. Please wait for version update.")
else:
raise ModeError
if __name__ == '__main__':
args = argparser.parse_args()
main(args)