From b2265cee3cde8b1ac3315cce158af1783ae7e77b Mon Sep 17 00:00:00 2001 From: Alexander James Wallar Date: Fri, 7 Feb 2014 14:35:04 +0000 Subject: [PATCH] added a config.json file and integrated it with the program --- .../src/com/locaudio/api/NotifyResponse.java | 3 ++ config.json | 10 +++++ locaudio/__init__.py | 29 ++++++++++-- locaudio/config.py | 34 +++++++------- locaudio/db.py | 7 +-- locaudio/detectionserver.py | 44 ++++++++++--------- locaudio/fingerprint.py | 22 ++-------- locaudio/triangulation.py | 3 -- run.py | 6 +-- tests/test_server.py | 7 +-- tests/test_triangulation.py | 2 +- 11 files changed, 92 insertions(+), 75 deletions(-) create mode 100644 config.json diff --git a/app/Locabean/src/com/locaudio/api/NotifyResponse.java b/app/Locabean/src/com/locaudio/api/NotifyResponse.java index 894e525..d030e3c 100644 --- a/app/Locabean/src/com/locaudio/api/NotifyResponse.java +++ b/app/Locabean/src/com/locaudio/api/NotifyResponse.java @@ -14,6 +14,9 @@ public class NotifyResponse { @SerializedName("confidence") public float confidence; + + @SerializedName("added") + public boolean added; @Override public String toString() { diff --git a/config.json b/config.json new file mode 100644 index 0000000..7ae9905 --- /dev/null +++ b/config.json @@ -0,0 +1,10 @@ + +{ + "jvm_path": "/System/Library/Frameworks/JavaVM.framework/JavaVM", + "db_host": "localhost", + "db_port": 28015, + "max_node_events": 10, + "min_confidence": 0.3, + "debug_mode": true +} + diff --git a/locaudio/__init__.py b/locaudio/__init__.py index 18aae43..f373b7c 100644 --- a/locaudio/__init__.py +++ b/locaudio/__init__.py @@ -12,8 +12,31 @@ """ -import triangulation -import detectionserver -import fingerprint +def run(host, port, config_filename): + """ + + Runs the server. + + @param host The host for the server + + @param port The port for the server + + """ + + import config + config.load_config_file(config_filename) + + global triangulation, detectionserver, fingerprint + + import triangulation + import detectionserver + import fingerprint + + import db + import detectionserver + import pageserver + + db.init() + config.app.run(host=host, port=int(port), debug=True) diff --git a/locaudio/config.py b/locaudio/config.py index f4ab2be..cef9711 100644 --- a/locaudio/config.py +++ b/locaudio/config.py @@ -1,6 +1,8 @@ +import json +import sys + from flask import Flask -import db app = Flask(__name__) app.config.from_object(__name__) @@ -8,24 +10,22 @@ detection_events = dict() new_data = dict() -# So the routes get initiated -import detectionserver -import pageserver - - -def run(host, port): - """ - - Runs the server. - - @param host The host for the server - - @param port The port for the server +this = sys.modules[__name__] - """ +# setting default values +jvm_path = "/System/Library/Frameworks/JavaVM.framework/JavaVM" +db_host = "localhost" +db_port = 28015 +max_node_events = 10 +min_confidence = 0.3 +debug_mode = True - db.init() - app.run(host=host, port=int(port), debug=True) +def load_config_file(filename): + global this + with open(filename) as f: + config_dict = json.loads(f.read()) + for config_key, config_value in config_dict.items(): + setattr(this, config_key, config_value) diff --git a/locaudio/db.py b/locaudio/db.py index 8628794..6383385 100644 --- a/locaudio/db.py +++ b/locaudio/db.py @@ -1,6 +1,7 @@ import rethinkdb as r import fingerprint +import config """ @@ -22,14 +23,14 @@ # Connection Information -HOST = "localhost" -PORT = 28015 +HOST = config.db_host +PORT = config.db_port DB = "reference" # Sample entry information SAMPLE_PATH = "sounds/Cock.wav" SAMPLE_R_REF = 1 -SAMPLE_L_REF = 100 +SAMPLE_L_REF = 65 SAMPLE_NAME = "Cock" diff --git a/locaudio/detectionserver.py b/locaudio/detectionserver.py index 00bf942..02c35e1 100644 --- a/locaudio/detectionserver.py +++ b/locaudio/detectionserver.py @@ -11,8 +11,6 @@ from flask import request, jsonify, render_template from point import Point -import time -import util import config import json import triangulation as tri @@ -22,8 +20,10 @@ import os -MAX_NODE_EVENTS = 10 -DEBUG = True +MAX_NODE_EVENTS = config.max_node_events +MIN_CONFIDENCE = config.min_confidence + +CREATE_PLOTS = False IMG_DIR = "imgs/" @@ -62,23 +62,25 @@ def post_notify(): sound_name, confidence = db.get_best_matching_print(req_print) - if not sound_name in config.detection_events.keys(): - config.detection_events[sound_name] = list() + if confidence > MIN_CONFIDENCE: + if not sound_name in config.detection_events.keys(): + config.detection_events[sound_name] = list() - config.new_data[sound_name] = True + config.new_data[sound_name] = True - if len(config.detection_events[sound_name]) + 1 >= MAX_NODE_EVENTS: - del config.detection_events[sound_name][0] + if len(config.detection_events[sound_name]) + 1 >= MAX_NODE_EVENTS: + del config.detection_events[sound_name][0] - config.detection_events[sound_name].append( - request_to_detection_event(request.form, confidence) - ) + config.detection_events[sound_name].append( + request_to_detection_event(request.form, confidence) + ) return jsonify( error=0, message="No error", name=sound_name, - confidence=confidence + confidence=confidence, + added=confidence > MIN_CONFIDENCE ) @@ -131,15 +133,15 @@ def get_position_viewer(sound_name): img_path = IMG_DIR + sound_name + ".png" img_web_path = "/" + img_path - # if config.new_data[sound_name]: - # plot.plot_detection_events( - # location_list, - # radius, spl, - # config.detection_events[sound_name], - # img_path - # ) + if config.new_data[sound_name] and CREATE_PLOTS: + plot.plot_detection_events( + location_list, + radius, spl, + config.detection_events[sound_name], + img_path + ) - # config.new_data[sound_name] = False + config.new_data[sound_name] = False ret_list = list() diff --git a/locaudio/fingerprint.py b/locaudio/fingerprint.py index e8d97f8..023d463 100644 --- a/locaudio/fingerprint.py +++ b/locaudio/fingerprint.py @@ -5,9 +5,10 @@ import jpype import math import json +import config -jvm_path = "/System/Library/Frameworks/JavaVM.framework/JavaVM" +jvm_path = config.jvm_path def surround_jvm(func): @@ -21,16 +22,6 @@ def _inner(*args, **kwargs): return _inner -def load_fingerprint_from_file(filename): - with open(filename) as f: - print_dict = json.loads(f.read()) - return ReferencePrint( - print_dict["fingerprint"], - print_dict["radius"], - print_dict["sound_pressure_level"] - ) - - #@atexit.register def destroy_env(): if jpype.isJVMStarted(): @@ -44,7 +35,7 @@ def get_similarity(f_1, f_2): sim_obj = com_obj.getFingerprintsSimilarity() sim = sim_obj.getSimilarity() if math.isnan(sim): - return 0.5 + return 0.0 else: return sim @@ -56,10 +47,3 @@ def get_fingerprint(wav_path): return list(wv.getFingerprint()) -class ReferencePrint(object): - - def __init__(self, fingerprint, radius, spl): - self.fingerprint = fingerprint - self.radius = radius - self.spl = spl - diff --git a/locaudio/triangulation.py b/locaudio/triangulation.py index 1b3c24e..f2be068 100644 --- a/locaudio/triangulation.py +++ b/locaudio/triangulation.py @@ -32,9 +32,6 @@ STD_SCALE = 1.4 MIN_DIST = 0.0001 -MAX_RADIUS_INC = 10 -MIN_RADIUS_INC = -10 -RADIUS_STEP = 1 EARTH_RADIUS = 1000 * 6371 # Special distance uncertainty threshold in meters diff --git a/run.py b/run.py index 22910b5..32886b0 100644 --- a/run.py +++ b/run.py @@ -5,10 +5,10 @@ if __name__ == "__main__": if len(sys.argv) == 3: - locaudio.config.run(sys.argv[1], sys.argv[2]) - elif len(sys.argv) == 1: + locaudio.run(sys.argv[1], sys.argv[2], sys.argv[3]) + elif len(sys.argv) == 2: ip_addr = socket.gethostbyname(socket.getfqdn()) - locaudio.config.run(ip_addr, 8000) + locaudio.run(ip_addr, 8000, sys.argv[1]) else: raise Exception("Correct argument form not supplied") diff --git a/tests/test_server.py b/tests/test_server.py index 69398a4..f2fcf50 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -59,9 +59,9 @@ class ServerTest(unittest.TestCase): def test_server_notify(self): for d_dict in d_dicts: - loc.notify_event(d_dict) + ret_dict = loc.notify_event(d_dict) - print "\n=== Server Notify ===\n" + print "\n=== Server Notify === :: {0}\n".format(ret_dict) def test_server_triangulation(self): @@ -78,8 +78,5 @@ def test_names(self): if __name__ == "__main__": print "\n=== Server Testing ===\n" - global server_addr - if len(sys.argv) == 2: - server_addr = sys.argv[1] unittest.main() diff --git a/tests/test_triangulation.py b/tests/test_triangulation.py index 3ee69bf..70d6c84 100644 --- a/tests/test_triangulation.py +++ b/tests/test_triangulation.py @@ -19,7 +19,7 @@ class TriangulationTest(unittest.TestCase): def setUp(self): - self.show_plot = True + self.show_plot = False self.d_events = [ tri.DetectionEvent(-9, -1, 0.9, 90, time.time()), tri.DetectionEvent(-2, 1, 0.3, 97, time.time() - 7),