From 9c1480ad4d5ad5edf7c2e428fbf474711188414e Mon Sep 17 00:00:00 2001 From: Josef Haupt Date: Fri, 20 Dec 2024 14:37:32 +0100 Subject: [PATCH] updated server.py and client.py to work with current analyze.py (#532) --- birdnet_analyzer/client.py | 6 +++++- birdnet_analyzer/server.py | 17 +++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/birdnet_analyzer/client.py b/birdnet_analyzer/client.py index 2c65c025..f68b2066 100644 --- a/birdnet_analyzer/client.py +++ b/birdnet_analyzer/client.py @@ -8,6 +8,8 @@ import requests +SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__)) + def sendRequest(host: str, port: int, fpath: str, mdata: str): """Sends a classification request to the server. @@ -65,7 +67,9 @@ def saveResult(data, fpath): parser = argparse.ArgumentParser(description="Client that queries an analyzer API endpoint server.") parser.add_argument("--host", default="localhost", help="Host name or IP address of API endpoint server.") parser.add_argument("--port", type=int, default=8080, help="Port of API endpoint server.") - parser.add_argument("--i", default="example/soundscape.wav", help="Path to file that should be analyzed.") + parser.add_argument( + "--i", default=os.path.join(SCRIPT_DIR, "example/soundscape.wav"), help="Path to file that should be analyzed." + ) parser.add_argument("--o", default="", help="Path to result file. Leave blank to store with audio file.") parser.add_argument("--lat", type=float, default=-1, help="Recording location latitude. Set -1 to ignore.") parser.add_argument("--lon", type=float, default=-1, help="Recording location longitude. Set -1 to ignore.") diff --git a/birdnet_analyzer/server.py b/birdnet_analyzer/server.py index f92b4395..d448a249 100644 --- a/birdnet_analyzer/server.py +++ b/birdnet_analyzer/server.py @@ -9,6 +9,7 @@ import tempfile from datetime import date, datetime from multiprocessing import freeze_support +import shutil import bottle @@ -107,8 +108,7 @@ def handleRequest(): file_path = os.path.join(save_path, name + ext) else: save_path = "" - file_path_tmp = tempfile.NamedTemporaryFile(suffix=ext.lower(), delete=False) - file_path_tmp.close() + file_path_tmp = tempfile.mkstemp(suffix=ext.lower(), dir=cfg.OUTPUT_PATH) file_path = file_path_tmp.name upload.save(file_path, overwrite=True) @@ -157,7 +157,8 @@ def handleRequest(): # Parse results if success: # Open result file - lines = utils.readLines(cfg.OUTPUT_PATH) + output_path = success["audacity"] + lines = utils.readLines(output_path) pmode = mdata.get("pmode", "avg").lower() # Pool results @@ -221,6 +222,9 @@ def handleRequest(): args = parser.parse_args() + cfg.CODES_FILE = os.path.join(SCRIPT_DIR, cfg.CODES_FILE) + cfg.LABELS_FILE = os.path.join(SCRIPT_DIR, cfg.LABELS_FILE) + # Load eBird codes, labels cfg.CODES = analyze.loadCodes() cfg.LABELS = utils.readLines(cfg.LABELS_FILE) @@ -241,11 +245,8 @@ def handleRequest(): # Set min_conf to 0.0, because we want all results cfg.MIN_CONFIDENCE = 0.0 - output_file = tempfile.NamedTemporaryFile(suffix=".txt", delete=False) - output_file.close() - # Set path for temporary result file - cfg.OUTPUT_PATH = output_file.name + cfg.OUTPUT_PATH = tempfile.mkdtemp() # Set result types cfg.RESULT_TYPES = ["audacity"] @@ -259,4 +260,4 @@ def handleRequest(): try: bottle.run(host=args.host, port=args.port, quiet=True) finally: - os.unlink(output_file.name) + shutil.rmtree(cfg.OUTPUT_PATH)