-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
54 lines (44 loc) · 1.61 KB
/
model.py
File metadata and controls
54 lines (44 loc) · 1.61 KB
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
from tqdm import tqdm
import numpy as np
from constants import PIXELSPERINPUT, SECONDSPERINPUT, MIDINAMES
def to_note_arr(bboxes, notes, scores):
notearr = []
for i in range(len(bboxes)):
if scores[i] < 0.7:
continue
x1 = bboxes[i][1] * SECONDSPERINPUT
x2 = bboxes[i][3] * SECONDSPERINPUT
pos = x1 + ((x2-x1)//2)
notearr.append((pos/PIXELSPERINPUT, notes[i]))
return notearr
def to_note_arr_v2(bboxes, notes, scores):
notearr = []
for i in range(len(bboxes)):
if scores[i] < 0.7:
continue
x1 = bboxes[i][1] * SECONDSPERINPUT
x2 = bboxes[i][3] * SECONDSPERINPUT
pos = (x1 + 1)/PIXELSPERINPUT
endpos = (x2)/PIXELSPERINPUT
notearr.append((pos, notes[i], endpos))
return notearr
def run_model_on_spectrogram(spec, mfn, model):
print("Running Model")
notes = []
ppi = PIXELSPERINPUT
spi = SECONDSPERINPUT
win = 1 # window = (win x ppi)
for i in tqdm(range(spec.shape[1]//(ppi//win))):
spec3c = spec[:,(ppi*(i//win)):(ppi*((i//win)+1))]
spec3c = np.dstack((spec3c, spec3c, spec3c))
newnotes = analyze_spec(spec3c, model)
notes += [(x[0]+((i//win)*spi), x[1], x[2]+((i//win)*spi)) for x in newnotes]
return notes
import sys
from mrcnn import visualize
def analyze_spec(spec, model):
results = model.detect([spec], verbose=0)
r = results[0]
# visualize.display_instances(spec, r['rois'], r['masks'], r['class_ids'],
# MIDINAMES, figsize=(8, 8))
return to_note_arr_v2(r['rois'], r['class_ids'], r['scores'])