Skip to content

Commit

Permalink
Add KeyboardInterrupt to "analyze" for BirdNET
Browse files Browse the repository at this point in the history
also only import exit from sys in IsoAutio
  • Loading branch information
purovida94 committed Mar 1, 2023
1 parent f85d2ba commit 417de3a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
18 changes: 9 additions & 9 deletions PyHa/IsoAutio.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import numpy as np
from math import ceil
from copy import deepcopy
import sys
from sys import exit


def checkVerbose(
Expand Down Expand Up @@ -933,7 +933,7 @@ def generate_automated_labels_microfaune(
SIGNAL, SAMPLE_RATE = librosa.load(audio_dir + audio_file, sr=None, mono=True)
SIGNAL = SIGNAL * 32768
except KeyboardInterrupt:
sys.exit("Keyboard interrupt")
exit("Keyboard interrupt")
except BaseException:
checkVerbose("Failed to load" + audio_file, isolation_parameters)
continue
Expand All @@ -948,7 +948,7 @@ def generate_automated_labels_microfaune(
SIGNAL, int(len(SIGNAL) * rate_ratio))
SAMPLE_RATE = normalized_sample_rate
except KeyboardInterrupt:
sys.exit("Keyboard interrupt")
exit("Keyboard interrupt")
except:
checkVerbose("Failed to Downsample" + audio_file, isolation_parameters)
# resample produces unreadable float32 array so convert back
Expand All @@ -964,7 +964,7 @@ def generate_automated_labels_microfaune(
microfaune_features = detector.compute_features([SIGNAL])
global_score, local_scores = detector.predict(microfaune_features)
except KeyboardInterrupt:
sys.exit("Keyboard interrupt")
exit("Keyboard interrupt")
except BaseException as e:
checkVerbose("Error in detection, skipping" + audio_file, isolation_parameters)
continue
Expand All @@ -991,7 +991,7 @@ def generate_automated_labels_microfaune(
else:
annotations = annotations.append(new_entry)
except KeyboardInterrupt:
sys.exit("Keyboard interrupt")
exit("Keyboard interrupt")
except BaseException as e:

checkVerbose("Error in isolating bird calls from" + audio_file, isolation_parameters)
Expand Down Expand Up @@ -1069,7 +1069,7 @@ def generate_automated_labels_tweetynet(
SIGNAL, SAMPLE_RATE = librosa.load(audio_dir + audio_file, sr=None, mono=True)
SIGNAL = SIGNAL * 32768
except KeyboardInterrupt:
sys.exit("Keyboard interrupt")
exit("Keyboard interrupt")
except BaseException:
checkVerbose("Failed to load" + audio_file, isolation_parameters)
continue
Expand All @@ -1082,7 +1082,7 @@ def generate_automated_labels_tweetynet(
SIGNAL, int(len(SIGNAL) * rate_ratio))
SAMPLE_RATE = normalized_sample_rate
except KeyboardInterrupt:
sys.exit("Keyboard interrupt")
exit("Keyboard interrupt")
except:
checkVerbose("Failed to Downsample" + audio_file, isolation_parameters)

Expand All @@ -1095,7 +1095,7 @@ def generate_automated_labels_tweetynet(
tweetynet_features = compute_features([SIGNAL])
predictions, local_scores = detector.predict(tweetynet_features, model_weights=weight_path, norm=normalize_local_scores)
except KeyboardInterrupt:
sys.exit("Keyboard interrupt")
exit("Keyboard interrupt")
except BaseException as e:
checkVerbose("Error in detection, skipping" + audio_file, isolation_parameters)
continue
Expand Down Expand Up @@ -1127,7 +1127,7 @@ def generate_automated_labels_tweetynet(
else:
annotations = annotations.append(new_entry)
except KeyboardInterrupt:
sys.exit("Keyboard interrupt")
exit("Keyboard interrupt")
except BaseException as e:

checkVerbose("Error in isolating bird calls from" + audio_file, isolation_parameters)
Expand Down
4 changes: 4 additions & 0 deletions PyHa/birdnet_lite/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ def analyze(audio_path, output_path = None, lat=-1, lon=-1, week=-1, overlap=0.0
os.makedirs(output_directory)
output_file = os.path.join(output_directory, 'result.csv')
df = writeResultsToDf(df, detections, min_conf, output_metadata)
except KeyboardInterrupt:
exit("Keyboard interrupt")
except:
print("Error processing file: {}".format(datafile))
elif len(dataset) > 0:
Expand All @@ -263,6 +265,8 @@ def analyze(audio_path, output_path = None, lat=-1, lon=-1, week=-1, overlap=0.0
output_metadata['IN FILE'] = os.path.split(datafile)[1]
output_metadata['CLIP LENGTH'] = clip_length
df = writeResultsToDf(df, detections, min_conf, output_metadata)
except KeyboardInterrupt:
exit("Keyboard interrupt")
except:
print("Error in processing file: {}".format(datafile))
if output_path is None:
Expand Down

0 comments on commit 417de3a

Please sign in to comment.