-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.py
136 lines (116 loc) · 3.52 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import gradio as gr
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from batdetect2 import api, plot
MAX_DURATION = 2
DETECTION_THRESHOLD = 0.3
examples = [
[
"example_data/audio/20170701_213954-MYOMYS-LR_0_0.5.wav",
DETECTION_THRESHOLD,
],
[
"example_data/audio/20180530_213516-EPTSER-LR_0_0.5.wav",
DETECTION_THRESHOLD,
],
[
"example_data/audio/20180627_215323-RHIFER-LR_0_0.5.wav",
DETECTION_THRESHOLD,
],
]
def make_prediction(file_name, detection_threshold=DETECTION_THRESHOLD):
# configure the model run
run_config = api.get_config(
detection_threshold=detection_threshold,
max_duration=MAX_DURATION,
)
# process the file to generate predictions
results = api.process_file(file_name, config=run_config)
# extract the detections
detections = results["pred_dict"]["annotation"]
# create a dataframe of the predictions
df = pd.DataFrame(
[
{
"species": pred["class"],
"time": pred["start_time"],
"detection_prob": pred["class_prob"],
"species_prob": pred["class_prob"],
}
for pred in detections
]
)
im = generate_results_image(file_name, detections, run_config)
return im, df
def generate_results_image(file_name, detections, config):
audio = api.load_audio(
file_name,
max_duration=config["max_duration"],
time_exp_fact=config["time_expansion"],
target_samp_rate=config["target_samp_rate"],
)
spec = api.generate_spectrogram(audio, config=config)
# create fig
plt.close("all")
fig = plt.figure(
1,
figsize=(15, 4),
dpi=100,
frameon=False,
)
ax = fig.add_subplot(111)
plot.spectrogram_with_detections(spec, detections, ax=ax)
plt.tight_layout()
# convert fig to image
fig.canvas.draw()
data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
w, h = fig.canvas.get_width_height()
im = data.reshape((int(h), int(w), -1))
return im
descr_txt = (
"Demo of BatDetect2 deep learning-based bat echolocation call detection. "
"<br>This model is only trained on bat species from the UK. If the input "
"file is longer than 2 seconds, only the first 2 seconds will be processed."
"<br>Check out the paper [here](https://www.biorxiv.org/content/10.1101/2022.12.14.520490v1)."
)
gr.Interface(
fn=make_prediction,
inputs=[
gr.Audio(
source="upload",
type="filepath",
label="Audio File",
info="Upload an audio file to be processed.",
),
gr.Slider(
minimum=0,
maximum=1,
value=DETECTION_THRESHOLD,
label="Detection Threshold",
step=0.1,
info=(
"All detections with a detection probability below this "
"threshold will be ignored."
),
),
],
live=True,
outputs=[
gr.Image(label="Visualisation"),
gr.Dataframe(
headers=["species", "time", "detection_prob", "species_prob"],
datatype=["str", "number", "number", "number"],
row_count=1,
col_count=(4, "fixed"),
label="Predictions",
),
],
theme="huggingface",
title="BatDetect2 Demo",
description=descr_txt,
examples=examples,
allow_flagging="never",
).launch()