Skip to content

Commit

Permalink
updated server.py and client.py to work with current analyze.py (#532)
Browse files Browse the repository at this point in the history
  • Loading branch information
Josef-Haupt authored Dec 20, 2024
1 parent 4664747 commit 9c1480a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
6 changes: 5 additions & 1 deletion birdnet_analyzer/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.")
Expand Down
17 changes: 9 additions & 8 deletions birdnet_analyzer/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import tempfile
from datetime import date, datetime
from multiprocessing import freeze_support
import shutil

import bottle

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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"]
Expand All @@ -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)

0 comments on commit 9c1480a

Please sign in to comment.