Skip to content

Commit

Permalink
Merge pull request #150 from UCSD-E4E/ctrl_c_exception_handling
Browse files Browse the repository at this point in the history
Ctrl c exception handling
  • Loading branch information
JacobGlennAyers authored Mar 1, 2023
2 parents 0f2ccb6 + f317877 commit 858714b
Show file tree
Hide file tree
Showing 12 changed files with 286 additions and 320 deletions.
35 changes: 17 additions & 18 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
.ipynb_checkpoints
**/__pycache__/
PyHa/__pycache__
PyHa/microfaune_package/microfaune/__pycache__
PyHa/microfaune_package/microfaune/__pycache__/*
PyHa/tweetynet_package/tweetynet/__pycache__
TEST/*
/mixed_bird/*
TEST/ScreamingPiha1.wav
TEST/ScreamingPiha1.wav
PyHa/tweetynet_package/tweetynet/__pycache__/*
PyHa/tweetynet_package/tweetynet/__pycache__/
outputs/result.csv
PyHa/birdnet_lite/__pycache__/*
PyHa/birdnet_lite/__pycache__/analyze.cpython-37.pyc
*.wav


outputs/*
/mixed_bird/*
TEST/*

*.csv
*.flac
*.ipynb
*.mp3
*.pyc
*.wav

PyHa_Model_Comparison.ipynb
PyHa_Testing-Copy1.ipynb
PyHa_Testing.ipynb
mixed_bird_manual.csv


PyHa/microfaune_package/microfaune/__pycache__/*
PyHa/tweetynet_package/tweetynet/__pycache__/*
PyHa/birdnet_lite/__pycache__/*
mixed_bird_manual.csv
outputs/result.csv
*.mp3
*.flac
outputs/*
*.ipynb
!PyHa_Tutorial.ipynb
*.csv
!ScreamingPiha_Manual_Labels.csv
!ScreamingPiha_Manual_Labels.csv
32 changes: 24 additions & 8 deletions PyHa/IsoAutio.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import numpy as np
from math import ceil
from copy import deepcopy
from sys import exit


def checkVerbose(
Expand Down Expand Up @@ -931,6 +932,8 @@ def generate_automated_labels_microfaune(
try:
SIGNAL, SAMPLE_RATE = librosa.load(audio_dir + audio_file, sr=None, mono=True)
SIGNAL = SIGNAL * 32768
except KeyboardInterrupt:
exit("Keyboard interrupt")
except BaseException:
checkVerbose("Failed to load" + audio_file, isolation_parameters)
continue
Expand All @@ -944,11 +947,13 @@ def generate_automated_labels_microfaune(
SIGNAL = scipy_signal.resample(
SIGNAL, int(len(SIGNAL) * rate_ratio))
SAMPLE_RATE = normalized_sample_rate
except KeyboardInterrupt:
exit("Keyboard interrupt")
except:
checkVerbose("Failed to Downsample" + audio_file, isolation_parameters)
# resample produces unreadable float32 array so convert back
# SIGNAL = np.asarray(SIGNAL, dtype=np.int16)

# print(SIGNAL.shape)
# convert stereo to mono if needed
# Might want to compare to just taking the first set of data.
Expand All @@ -958,10 +963,13 @@ def generate_automated_labels_microfaune(
try:
microfaune_features = detector.compute_features([SIGNAL])
global_score, local_scores = detector.predict(microfaune_features)
except KeyboardInterrupt:
exit("Keyboard interrupt")
except BaseException as e:
checkVerbose("Error in detection, skipping" + audio_file, isolation_parameters)
continue



# get duration of clip
duration = len(SIGNAL) / SAMPLE_RATE

Expand All @@ -982,6 +990,8 @@ def generate_automated_labels_microfaune(
annotations = new_entry
else:
annotations = annotations.append(new_entry)
except KeyboardInterrupt:
exit("Keyboard interrupt")
except BaseException as e:

checkVerbose("Error in isolating bird calls from" + audio_file, isolation_parameters)
Expand Down Expand Up @@ -1058,22 +1068,24 @@ def generate_automated_labels_tweetynet(
try:
SIGNAL, SAMPLE_RATE = librosa.load(audio_dir + audio_file, sr=None, mono=True)
SIGNAL = SIGNAL * 32768
except KeyboardInterrupt:
exit("Keyboard interrupt")
except BaseException:
checkVerbose("Failed to load" + audio_file, isolation_parameters)
continue

# downsample the audio if the sample rate isn't 44.1 kHz
# Force everything into the human hearing range.
# May consider reworking this function so that it upsamples as well

# Resample the audio if it isn't the normalized sample rate
try:
if SAMPLE_RATE != normalized_sample_rate:
rate_ratio = normalized_sample_rate / SAMPLE_RATE
SIGNAL = scipy_signal.resample(
SIGNAL, int(len(SIGNAL) * rate_ratio))
SAMPLE_RATE = normalized_sample_rate
except KeyboardInterrupt:
exit("Keyboard interrupt")
except:
checkVerbose("Failed to Downsample" + audio_file, isolation_parameters)

# convert stereo to mono if needed
# Might want to compare to just taking the first set of data.
if len(SIGNAL.shape) == 2:
Expand All @@ -1082,10 +1094,12 @@ def generate_automated_labels_tweetynet(
try:
tweetynet_features = compute_features([SIGNAL])
predictions, local_scores = detector.predict(tweetynet_features, model_weights=weight_path, norm=normalize_local_scores)
except KeyboardInterrupt:
exit("Keyboard interrupt")
except BaseException as e:
checkVerbose("Error in detection, skipping" + audio_file, isolation_parameters)
continue

try:
# Running moment to moment algorithm and appending to a master
# dataframe.
Expand All @@ -1112,6 +1126,8 @@ def generate_automated_labels_tweetynet(
annotations = new_entry
else:
annotations = annotations.append(new_entry)
except KeyboardInterrupt:
exit("Keyboard interrupt")
except BaseException as e:

checkVerbose("Error in isolating bird calls from" + audio_file, isolation_parameters)
Expand Down
Binary file removed PyHa/birdnet_lite/__pycache__/analyze.cpython-39.pyc
Binary file not shown.
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
535 changes: 241 additions & 294 deletions PyHa_Tutorial.ipynb

Large diffs are not rendered by default.

0 comments on commit 858714b

Please sign in to comment.