You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
4.1 KiB
121 lines
4.1 KiB
"""! @file config_parser.py
|
|
@brief Config parser for fingerprint filtering and 3d model generation application
|
|
@author xlanro00
|
|
"""
|
|
|
|
from os.path import exists
|
|
import json
|
|
import hashlib
|
|
|
|
|
|
def save_preset(filters, params, preset_name):
|
|
'''Save filter preset to database.
|
|
:param filters: list of filters to be saved
|
|
:param preset_name: name of preset to be saved
|
|
'''
|
|
filt = {}
|
|
# Create dictionary of filter parameters
|
|
for i, filter in enumerate(filters):
|
|
|
|
filt[i] = {}
|
|
filt[i]["name"] = filter
|
|
for key, value in params[i+1].items():
|
|
if value is not None:
|
|
filt[i][key] = value
|
|
|
|
new_filt = []
|
|
for i, filter in enumerate(filt):
|
|
new_filt.append(filt[filter])
|
|
|
|
# Store preset to database
|
|
store_to_db(new_filt, preset_name)
|
|
|
|
|
|
def store_to_db(preset, preset_name):
|
|
'''Store filter preset to database.
|
|
:param preset: dictionary of filter preset to be stored
|
|
:param preset_name: name of preset to be stored
|
|
'''
|
|
|
|
# Create unique id for preset, this serves to avoid duplicit preset entries
|
|
id = str(hashlib.md5(str(preset).encode('utf-8')).hexdigest())[:8]
|
|
preset_name = preset_name + "_" + id
|
|
# Create json object from preset
|
|
preset = json.dumps({preset_name: preset}, indent=4)
|
|
preset = json.loads(preset)
|
|
|
|
# If database doesn't exist, create it
|
|
if not exists("db.json"):
|
|
print("Storing preset to database")
|
|
with open("db.json", 'w') as db:
|
|
json.dump(preset, db)
|
|
else:
|
|
# If database exists, load it and check if preset already exists
|
|
try:
|
|
with open("db.json", 'r') as db:
|
|
db_presets = json.load(db)
|
|
|
|
# Empty file is an error, so we don't read it
|
|
except json.decoder.JSONDecodeError:
|
|
db_presets = {}
|
|
|
|
# If preset already exists, skip it
|
|
if preset_name in db_presets:
|
|
print("Preset already exists in database, skipping")
|
|
else:
|
|
print("Storing preset to database")
|
|
db_presets.update(preset)
|
|
|
|
# Finally write the updated entries to db file
|
|
with open("db.json", 'w') as db:
|
|
json.dump(db_presets, db, indent=4)
|
|
|
|
|
|
def parse_conf(preset_name, filters, params, config_file):
|
|
'''Parse configuration file if one was given.
|
|
Store filters and their parameters.
|
|
'''
|
|
|
|
config = json.load(open(config_file))
|
|
|
|
# Find preset in config file
|
|
if preset_name in config:
|
|
filter_array = config[preset_name]
|
|
store_to_db(filter_array, preset_name)
|
|
# Iterate over filters in preset, store them and their parameters
|
|
for i, filter in enumerate(range(len(filter_array)), start=1):
|
|
filters.append(filter_array[filter]["name"])
|
|
params[i] = {}
|
|
for attribute, value in filter_array[filter].items():
|
|
# Filter name isn't needed in here
|
|
if attribute != "name":
|
|
params[i][attribute] = value
|
|
parse_params(params[i])
|
|
print("Loaded preset: " + preset_name +
|
|
" from file: " + config_file)
|
|
else:
|
|
print("Preset not found")
|
|
|
|
|
|
def parse_params(params):
|
|
'''Parse parameters of filters. Set to None if parameter is not given.
|
|
They are later set to default values in the filter method apply.
|
|
:param params: dictionary of filter parameters
|
|
'''
|
|
|
|
# TODO: possibly too bloated, sending all possible params to each filter
|
|
# TODO: remove unnecessary params
|
|
possible_params = {"h", "searchWindowSize", "templateWindowSize",
|
|
"ksize", "kernel", "angle",
|
|
"sigmaColor", "sigmaSpace", "diameter", "anchor", "iterations",
|
|
"op", "strength", "amount", "radius", "weight", "channelAxis",
|
|
"theta", "sigma", "lambd", "gamma", "psi", "shape", "percent",
|
|
"threshold", "maxval", "type", "margin", "color", "truncate", "patch_size", "patch_distance"}
|
|
|
|
for key in possible_params:
|
|
if params.get(key) is None:
|
|
params[key] = None
|
|
else:
|
|
params[key] = params[key]
|
|
|