-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_params.py
144 lines (129 loc) · 5.62 KB
/
config_params.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
import json
from os import mkdir
from os import getcwd
from os.path import join
from os.path import exists
from time import time
import logging.config
from logger import logger_config
logging.config.dictConfig(logger_config)
logger = logging.getLogger('app_logger')
class ConfigParams(object):
''' class singelton to manage configuration '''
# write default information into fields
data = []
data.append({
"version": 0.1,
"user_name": "user",
"camera": 0,
"quality": 0.75,
"show_video": 0,
"page": "train",
"monitor_on_start": 0,
"mark_frames": 1,
"save_monitor_picture": 0,
"save_monitor_video": 0,
"keep_log_file_days": 1,
"path_to_log_file": "log",
"path_to_data_file": "data",
"encoding_file": "encodings.pickle",
"path_to_monitor_file": "monitor",
"path_to_new_persons": "new_persons",
"path_to_base_frames": "base_frames",
"path_to_video": "path_to_video"
})
def __new__(cls):
''' ensure just one instance of this class '''
if not hasattr(cls, 'instance'):
cls.instance = super(ConfigParams, cls).__new__(cls)
cls.instance.config_file(file='config.json')
return cls.instance
def config_save(self, file='config.json'):
''' save configratoion info'''
try:
with open(file, 'w') as f:
ConfigParams.data[0]['HELP camera'] = 'Camera to use. 0 - built-in, 1 - plugged, etc'
ConfigParams.data[0]['HELP quality'] = 'Reduce captured image. Value in [0.25 0.5 0.75 1] -> [worse_quality better_quality]'
ConfigParams.data[0]['HELP show_video'] = 'Value in [0 1]'
ConfigParams.data[0]['HELP page'] = 'Value in [train monitor]'
ConfigParams.data[0]['HELP monitor_on_start'] = 'Value in [0 1]'
ConfigParams.data[0]['HELP mark_frames'] = 'Value in [0 1]'
ConfigParams.data[0]['HELP save_monitor_picture'] = 'Value in [0 1]'
ConfigParams.data[0]['HELP save_monitor_video'] = 'Value in [0 1]'
ConfigParams.data[0]['HELP keep_log_file_days'] = 'Value in [1 .. 365]'
ConfigParams.data[0]['HELP path_to_log_file'] = 'path to log file'
ConfigParams.data[0]['HELP path_to_data_file'] = 'path_to_data_file'
ConfigParams.data[0]['HELP encoding_file'] = 'name of encoding file; by default <encodings.pickle>'
ConfigParams.data[0]['HELP path_to_monitor_file'] = 'path_to_monitor_file'
ConfigParams.data[0]['HELP path_to_new_persons'] = 'path_to_new_persons picture'
ConfigParams.data[0]['HELP path_to_base_frames'] = 'path_to_base_frames folder'
ConfigParams.data[0]['HELP path_to_video'] = 'path_to_video folder'
json.dump(ConfigParams.data[0], f, indent=4)
except:
logger.info('Could not save configuration file')
def config_file(self, file='config.json'):
''' format configratoion info'''
try:
with open(file, 'r') as f:
ConfigParams.data.append(json.load(f))
except:
logger.info('Could not load configuration file. Using default parameters')
return
# parse all keys
if ConfigParams.data[1]['camera'] in [0, 1, 2]:
ConfigParams.data[0]['camera'] = ConfigParams.data[1]['camera']
if ConfigParams.data[1]['quality'] in [.25, .5, .75, 1.0]:
ConfigParams.data[0]['quality'] = ConfigParams.data[1]['quality']
if ConfigParams.data[1]['show_video'] in [0, 1]:
ConfigParams.data[0]['show_video'] = ConfigParams.data[1]['show_video']
if ConfigParams.data[1]['page'] in ["train", "monitor"]:
ConfigParams.data[0]['page'] = ConfigParams.data[1]['page']
if ConfigParams.data[1]['monitor_on_start'] in [0, 1]:
ConfigParams.data[0]['monitor_on_start'] = ConfigParams.data[1]['monitor_on_start']
if ConfigParams.data[1]['mark_frames'] in [0, 1]:
ConfigParams.data[0]['mark_frames'] = ConfigParams.data[1]['mark_frames']
if ConfigParams.data[1]['save_monitor_picture'] in [0, 1]:
ConfigParams.data[0]['save_monitor_picture'] = ConfigParams.data[1]['save_monitor_picture']
if ConfigParams.data[1]['save_monitor_video'] in [0, 1]:
ConfigParams.data[0]['save_monitor_video'] = ConfigParams.data[1]['save_monitor_video']
if ConfigParams.data[1]['keep_log_file_days'] >= 1 and ConfigParams.data[1]['keep_log_file_days'] <= 365:
ConfigParams.data[0]['keep_log_file_days'] = ConfigParams.data[1]['keep_log_file_days']
self.make_dir(ConfigParams, 'path_to_log_file')
self.make_dir(ConfigParams, 'path_to_data_file')
self.make_dir(ConfigParams, 'path_to_monitor_file')
self.make_dir(ConfigParams, 'path_to_new_persons')
self.make_dir(ConfigParams, 'path_to_base_frames')
self.make_dir(ConfigParams, 'path_to_video')
if ConfigParams.data[0]['encoding_file'] == '':
pass
else:
path_to_data_file = join(ConfigParams.data[1]['path_to_data_file'], ConfigParams.data[1]['encoding_file'])
if exists(path_to_data_file):
ConfigParams.data[0]['encoding_file'] = ConfigParams.data[1]['encoding_file']
else:
logger.info("Encoding file {} not exist.".format(path_to_data_file))
ConfigParams.data[0]['encoding_file'] = ''
def make_dir(self, ConfigParams, dir):
''' creates drectory <dir> if not exist and write it to configuration '''
path = join(getcwd(), ConfigParams.data[1][dir])
if exists(path):
ConfigParams.data[0][dir] = path
else:
try:
mkdir(path)
ConfigParams.data[0][dir] = path
except OSError:
logger.info('Creation of the directory {} failed'.format(path))
path = join(path, '_', str(int(time())))
mkdir(path)
ConfigParams.data[0][dir] = path
except:
logger.info('Creation of the directory {} failed. Exiting.'.format(path))
# can be tested
# python config_params.py
if __name__ == "__main__":
s = ConfigParams()
s1 = ConfigParams()
s.data[0]['user_name'] = '100'
print(s.data[0]['user_name'])
print(s1.data[0]['user_name'])