-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_trial.py
1758 lines (1631 loc) · 50.6 KB
/
my_trial.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# coding: utf-8
import os
import cv2
import numpy as np
import tensorflow as tf
import sys
import pandas as pd
import random
import math
import re
import time
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import glob
import matplotlib.image as mpimg
matplotlib.use('TkAgg')
import six.moves.urllib as urllib
import tarfile
import zipfile
from distutils.version import StrictVersion
from collections import defaultdict
from io import StringIO
from PIL import Image
# This is needed since the notebook is stored in the object_detection folder.
sys.path.append("..")
# Import utilites
from utils import label_map_util
from utils import visualization_utils as vis_util
# Name of the directory containing the object detection module we're using
MODEL_NAME = 'inference_graph'
CWD_PATH = os.getcwd()
# Root directory of the project
ROOT_DIR = os.path.abspath('')
# Import Mask RCNN
sys.path.append(ROOT_DIR) # To find local version of the library
from mrcnn import utils
from mrcnn import visualize
from mrcnn.visualize import display_images
import mrcnn.model as modellib
from mrcnn.model import log
from datasets.balloon import balloon
# Directory to save logs and trained model
MODEL_DIR = os.path.join(ROOT_DIR, "logs")
# Path to Ballon trained weights
# You can download this file from the Releases page
# https://github.com/matterport/Mask_RCNN/releases
BALLON_WEIGHTS_PATH = "mask_rcnn_balloon.h5" # TODO: update this path
config = balloon.BalloonConfig()
MENSURAL_DIR = os.path.join(ROOT_DIR, "datasets/balloon")
# Override the training configurations with a few
# changes for inferencing.
class InferenceConfig(config.__class__):
# Run detection on one image at a time
GPU_COUNT = 1
IMAGES_PER_GPU = 1
config = InferenceConfig()
config.display()
# Device to load the neural network on.
# Useful if you're training a model on the same
# machine, in which case use CPU and leave the
# GPU for training.
DEVICE = "/cpu:0"
# Inspect the model in training or inference modes
# values: 'inference' or 'training'
# TODO: code for 'training' test mode not ready yet
TEST_MODE = "inference"
# Create model in inference mode
with tf.device(DEVICE):
model = modellib.MaskRCNN(mode = "inference", model_dir = MODEL_DIR, config = config)
# Set path to balloon weights file
# Download file from the Releases page and set its path
# https://github.com/matterport/Mask_RCNN/releases
# weights_path = "/path/to/mask_rcnn_balloon.h5"
# Or, load the last model you trained
weights_path = model.find_last()
# Load weights
print("Loading weights ", weights_path)
model.load_weights(weights_path, by_name = True)
PATH_TO_CKPT = os.path.join(CWD_PATH, MODEL_NAME, 'frozen_inference_graph.pb')
PATH_TO_LABELS = os.path.join(CWD_PATH, 'training', 'labelmap.pbtxt')
# In order to use music21 a version of MuseScore is required. The environment need to be set as shown below.
from music21 import *
us = environment.UserSettings()
us['musescoreDirectPNGPath'] = '/Applications/MuseScore 3.app/Contents/MacOS/mscore'
us['pdfPath'] = '/Applications/MuseScore 3.app/Contents/MacOS/mscore'
us['graphicsPath'] = '/Applications/MuseScore 3.app/Contents/MacOS/mscore'
us['musicxmlPath'] = '/Applications/MuseScore 3.app/Contents/MacOS/mscore'
# Function to clear the folders containing csv and png files.
def clean_up():
import os, shutil
folders = ['csv2/', 'csv/', 'detect/']
for folder in folders:
for the_file in os.listdir(folder):
file_path = os.path.join(folder, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
except Exception as e:
print(e)
# Class to display key signatures in tinynotation.
class KeyToken(tinyNotation.Token):
def parse(self, parent):
keyName = self.token
return key.Key(keyName)
def find_items_within(l1, l2, dist):
l1.sort()
l2.sort()
b = 0
e = 0
ans = []
for a in l1:
while b < len(l2) and a - l2[b] > dist:
b += 1
while e < len(l2) and l2[e] - a <= dist:
e += 1
ans.extend([(a,x) for x in l2[b:e]])
return ans
def grouper(iterable, img):
img = cv2.imread(img)
img_height, img_width, dim = img.shape
prev = None
group = []
for item in iterable:
if not prev or item - prev <= 0.019*img_height:
group.append(item)
else:
yield group
group = [item]
prev = item
if group:
yield group
def Average(lst):
try:
return sum(lst) / len(lst)
except:
print("average error")
def get_ax(rows=1, cols=1, size=16):
"""Return a Matplotlib Axes array to be used in
all visualizations in the notebook. Provide a
central point to control graph sizes.
Adjust the size attribute to control how big to render images
"""
_, ax = plt.subplots(rows, cols, figsize = (size * cols, size * rows))
return ax
def eroded(image):
img = cv2.imread(image)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
flag,b = cv2.threshold(gray,150,255,cv2.THRESH_BINARY)
cv2.imwrite("1tresh.jpg", b)
element = np.ones((3,3))
b = cv2.erode(b,element)
cv2.imwrite("2erodedtresh.jpg", b)
edges = cv2.Canny(b,10,100,apertureSize = 3)
cv2.imwrite("3Canny.jpg", edges)
return b
def one(IMAGE_NAME):
eroded(IMAGE_NAME)
image = '2erodedtresh.jpg'
img = cv2.imread(image)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.bitwise_not(img)
th2 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 15, -2)
horizontal = th2
vertical = th2
rows, cols = horizontal.shape
horizontalsize = int(cols / 50)
horizontalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (horizontalsize, 1))
horizontal = cv2.erode(horizontal, horizontalStructure, (-1, -1))
horizontal = cv2.dilate(horizontal, horizontalStructure, (-1, -1))
cv2.imwrite("horizontal.jpg", horizontal)
src = cv2.imread(image, cv2.IMREAD_COLOR)
if len(src.shape) != 2:
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
else:
gray = src
gray = cv2.bitwise_not(gray)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
cv2.imwrite('edges-50-150.jpg',edges)
bw = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 15, -2)
horizontal = np.copy(bw)
vertical = np.copy(bw)
cols = horizontal.shape[1]
horizontal_size = int(cols / 30)
horizontalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (horizontal_size, 1))
horizontal = cv2.erode(horizontal, horizontalStructure)
horizontal = cv2.dilate(horizontal, horizontalStructure)
cv2.imwrite("img_horizontal8.png", horizontal)
h_transpose = np.transpose(np.nonzero(horizontal))
rows = vertical.shape[0]
verticalsize = int(rows / 30)
verticalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (1, verticalsize))
vertical = cv2.erode(vertical, verticalStructure)
vertical = cv2.dilate(vertical, verticalStructure)
cv2.imwrite("img_vertical8.png", vertical)
v_transpose = np.transpose(np.nonzero(vertical))
img = src.copy()
img_height, img_width, dim = img.shape
minLineLength = 50
maxLineGap = 50
lines = cv2.HoughLinesP(horizontal, 1, np.pi / 180, 100, minLineLength, maxLineGap)
values = []
for line in lines:
for x1, y1, x2, y2 in line:
values.append(y1)
cv2.line(img, (x1, y1),(x2, y2),(0, 255, 0), 10)
cv2.imwrite('houghlinesP.jpg', img)
return values
def first_detection(img, img_width, img_height):
# Number of classes the object detector can identify
NUM_CLASSES = 33
confidence = 0.50
# Open the hitlist file.
hitf = open("csv2/hitlist-%d.csv" %(i+1), 'w')
hitf.write('image,class,score,bb0,bb1,bb2,bb3,image_width,image_height\n')
hitlim = confidence
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes = NUM_CLASSES, use_display_name = True)
category_index = label_map_util.create_category_index(categories)
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name = '')
sess = tf.Session(graph = detection_graph)
# Input tensor is the image
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
# Output tensors are the detection boxes, scores, and classes
# Each box represents a part of the image where a particular object was detected
detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
# Each score represents level of confidence for each of the objects.
# The score is shown on the result image, together with the class label.
detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
# Number of objects detected
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
# Load image using OpenCV and
# expand image dimensions to have shape: [1, None, None, 3]
# i.e. a single-column array, where each item in the column has the pixel RGB value
image = cv2.imread(img)
image_expanded = np.expand_dims(image, axis=0)
# Perform the actual detection by running the model with the image as input
(boxes, scores, classes, num) = sess.run(
[detection_boxes, detection_scores, detection_classes, num_detections],
feed_dict = {image_tensor: image_expanded})
# Draw the results of the detection (aka 'visulaize the results')
vis_util.visualize_boxes_and_labels_on_image_array(
image,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates = True,
line_thickness = 4,
min_score_thresh = confidence
)
nprehit = scores.shape[1] # 2nd array dimension
for j in range(nprehit):
fname = "image" + str(j)
classid = int(classes[0][j])
classname = category_index[classid]["name"]
score = scores[0][j]
if (score >= hitlim):
sscore = str(score)
bbox = boxes[0][j]
b0 = str(bbox[0])
b1 = str(bbox[1])
b2 = str(bbox[2])
b3 = str(bbox[3])
img_width = str(img_width)
img_height = str(img_height)
line = ",".join([fname, classname, sscore, b0, b1, b2, b3, img_width, img_height])
hitf.write(line + "\n")
# close hitlist
hitf.flush()
hitf.close()
return image
def results(img):
image = img
image = cv2.imread(image)
results = model.detect([image], verbose=1)
ax = get_ax(1)
r = results[0]
visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'],
['BG', 'note'], r['scores'], ax=ax,
title="Predictions")
return r
def mrcnn(img):
image = img
image = cv2.imread(image)
results = model.detect([image], verbose=1)
ax = get_ax(1)
r = results[0]
visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'],
['BG', 'note'], r['scores'], ax=ax,
title="Predictions")
return image
def notes_try(row):
for stave in lines:
for i in range(len(stave)-1):
if abs(stave[i]-row['y_avg']) < abs(stave[i+1] - row['y_avg']):
return stave.index(stave[i])
def comparos(df):
df = df.loc[df['object'].isin(['semifusa','topsemifusa','fusa','topfusa','semiminima',\
'topsemiminima','minima','topminima','brevis', 'semibreve'])]
return df['x'].values
def pnds(hitlist, notes, img_height, img_width):
global lines
global dff
hitlist.sort_values(by=['bb1'], inplace=True)
hitlist['x_avg'] = hitlist[['bb1', 'bb3']].mean(axis=1)
hitlist['y_avg'] = hitlist[['bb0', 'bb2']].mean(axis=1)
hitlist['bb0'] = hitlist['bb0'] * int(img_height)
hitlist['bb2'] = hitlist['bb2'] * int(img_height)
hitlist['x'] = hitlist['x_avg'] * int(img_width)
hitlist['y'] = hitlist['y_avg'] * int(img_height)
hitlist['prev_class'] = hitlist['class'].shift()
hitlist['object'] = hitlist['class']
hitlist['next_class'] = hitlist['class'].shift(-1)
hitlist = hitlist.drop(['image', 'class', 'score', 'bb1', 'bb3', 'x_avg', 'y_avg'], axis=1)
hitlist['dotted'] = np.where(hitlist['next_class'] == 'dot', 'yes', 'no')
hitlist['bridged'] = np.where(hitlist['next_class'] == 'bridge', 'yes', 'no')
hitlist['sharped'] = np.where(hitlist['prev_class'] == 'sharp', 'yes', 'no')
hitlist['flatted'] = np.where(hitlist['prev_class'] == 'flat', 'yes', 'no')
hitlist['notes'] = np.select([hitlist.object == 'semifusa', hitlist.object == 'topsemifusa',
hitlist.object == 'fusa', hitlist.object == 'topfusa',
hitlist.object == 'semiminima', hitlist.object == 'topsemiminima',
hitlist.object == 'minima', hitlist.object == 'topminima',
hitlist.object=='brevis', hitlist.object=='semibreve',
hitlist.object=='wholerest', hitlist.object=='2rest', hitlist.object=='4rest',
hitlist.object=='uprest', hitlist.object=='Rrest',
hitlist.object=='downrest', hitlist.object =='rurest'],
[0.25, 0.25, 0.5, 0.5, 1, 1, 2, 2, 8, 4, 8, 16, 32, 4, 1, 2, 1],
default=None)
hitlist['prev_x'] = hitlist['x'].shift(1)
hitlist['prev_x'].fillna(0)
hitlist['current_x'] = hitlist['x']
for index, row in hitlist.iterrows():
if row['current_x'] - row['prev_x'] < 1:
if row['object'] == row['prev_class']:
hitlist = hitlist.drop(index)
hitlist = hitlist.reset_index(drop=True)
notes.sort_values(by=['one'], inplace = True)
notes['y_avg'] = notes[['zero', 'two']].mean(axis = 1)
notes['y_diff'] = notes['two'] - notes['zero']
average_height = notes['y_avg'].values
notes['x_avg'] = notes[['one', 'three']].mean(axis=1)
a = notes['x_avg'].values
b = comparos(hitlist)
notes['line'] = notes.apply(notes_try, axis = 1)
compared = find_items_within(a,b,20)
new = pd.DataFrame(compared, columns = ['x_avg', 'one'])
new.sort_values(by=['x_avg'], inplace = True)
df = pd.merge(new, notes, on=['x_avg'])
df = df.rename(columns={'one_x': 'x'})
dff = pd.merge(df, hitlist, on=['x'], how='outer')
df2 = dff.loc[dff['object'] == 'brevis']
df2['y'] = df2['y'].astype(float)
df2['y_avg'] = df2['y']
dff['y_avg'].loc[df2.index.values] = df2['y_avg'].loc[df2.index.values]
dff['line'] = dff.apply(notes_try, axis = 1)
dff = dff.sort_values(by=['x'])
return dff
def liner(IMAGE_NAME, number):
global lines
global i
clean_up()
i = number
image = IMAGE_NAME
img = cv2.imread(image)
img_height, img_width, dim = img.shape
src = cv2.imread(image, cv2.IMREAD_COLOR)
l = sorted(one(IMAGE_NAME))
G = dict(enumerate(grouper(l, IMAGE_NAME), 1))
avgDict = {}
for k,v in G.items():
avgDict[k] = sum(v)/ float(len(v))
num = avgDict.values()
line_height = []
for numb in num:
line_height.append(numb)
staves = [list(t) for t in zip(*[iter(line_height)]*5)]
difference = [j-i for i, j in zip(line_height[:-1], line_height[1:])]
inds=[0]+[ind for ind,(i,j) in enumerate(zip(difference,difference[1:]),1) if j-i>difference[0]*2]+[len(difference)+1]
[difference[i:j] for i,j in zip(inds,inds[1:])]
difference=[]
delta = []
for stave in staves:
difference.append([j-i for i, j in zip(stave[:-1], stave[1:])])
delta.append(np.mean(difference))
delta = Average(delta)
lines = []
for stave in staves:
line0 = stave[0] - delta * 1.5
line1 = stave[0] - delta
line2 = stave[0] - delta / 2
line3 = stave[0]
line4 = stave[0] + delta / 2
line5 = stave[0] + delta
line6 = stave[0] + delta * 1.5
line7 = stave[0] + delta * 2
line8 = stave[0] + delta * 2.5
line9 = stave[0] + delta * 3
line10 = stave[0] + delta * 3.5
line11 = stave[0] + delta * 4
line12 = stave[0] + delta * 4.5
line13 = stave[0] + delta * 5
line14 = stave[0] + delta * 5.5
if line0<0:
line0=0
else:
line0=line0
if line14>img_height:
line14=img_height
else:
line14=line14
lines.append( [line0, line1, line2, line3, line4, line5, line6, line7, line8, line9, line10, line11, line12, line13, line14])
img = src.copy()
lineThickness = 2
for line in lines:
for line in line:
cv2.line(img, (int(0), int(line)), (int(img_width), int(line)), (0,255,0), lineThickness)
sys.path.append("..")
from object_detection.utils import ops as utils_ops
resss = results(IMAGE_NAME)
cv2.imwrite("detect/detect-%.3d.jpg" % (i+1), first_detection(image, img_width, img_height))
np.savetxt("csv/%d.csv" %(i+1), resss['rois'], delimiter=",")
column_names = ['zero', 'one', 'two', 'three']
try:
notes = pd.read_csv('csv/%d.csv'%(i+1))
notes.columns = column_names
except:
print('csv_error')
hitlist = pd.read_csv("csv2/hitlist-%d.csv" %(i+1))
dff = pnds(hitlist, notes, img_height, img_width)
return dff
def merge(lst):
df = pd.concat(lst,ignore_index=True)
pd.set_option('display.max_columns', 500)
pd.set_option('display.max_row', 500)
return df
def to_tenor(row):
if row['notes'] != None:
if row['line'] == 0:
return 'A4'
elif row['line'] == 1:
return 'G4'
elif row['line'] == 2:
return 'F4'
elif row['line'] == 3:
return 'E4'
elif row['line'] == 4:
return 'D4'
elif row['line'] == 5:
return 'C4'
elif row['line'] == 6:
return 'B3'
elif row['line'] == 7:
return 'A3'
elif row['line'] == 8:
return 'G3'
elif row['line'] == 9:
return 'F3'
elif row['line'] == 10:
return 'E3'
elif row['line'] == 11:
return 'D3'
elif row['line'] == 12:
return 'C3'
elif row['line'] == 13:
return 'B2'
elif row['line'] == 14:
return 'A2'
# elif row['object'] == 'wholerest' or row['object'] == '2rest' or row['object'] == '4rest' or row['object'] == 'uprest' or row['object'] == 'Rrest' or row['object'] == 'downrest' or row['object'] == 'rurest':
# return 'r'
else:
return ''
else:
return ''
def to_alto(row):
if row['notes'] != None:
if row['line'] == 0:
return 'C5'
elif row['line'] == 1:
return 'B4'
elif row['line'] == 2:
return 'A4'
elif row['line'] == 3:
return 'G4'
elif row['line'] == 4:
return 'F4'
elif row['line'] == 5:
return 'E4'
elif row['line'] == 6:
return 'D4'
elif row['line'] == 7:
return 'C4'
elif row['line'] == 8:
return 'B3'
elif row['line'] == 9:
return 'A3'
elif row['line'] == 10:
return 'G3'
elif row['line'] == 11:
return 'F3'
elif row['line'] == 12:
return 'E3'
elif row['line'] == 13:
return 'D3'
elif row['line'] == 14:
return 'C3'
# elif row['object'] == 'wholerest' or row['object'] == '2rest' or row['object'] == '4rest' or row['object'] == 'uprest' or row['object'] == 'Rrest' or row['object'] == 'downrest' or row['object'] == 'rurest':
# return 'r'
else:
return ''
else:
return ''
def to_bass(row):
if row['notes'] != None:
if row['line'] == 0:
return 'D4'
elif row['line'] == 1:
return 'C4'
elif row['line'] == 2:
return 'B3'
elif row['line'] == 3:
return 'A3'
elif row['line'] == 4:
return 'G3'
elif row['line'] == 5:
return 'F3'
elif row['line'] == 6:
return 'E3'
elif row['line'] == 7:
return 'D3'
elif row['line'] == 8:
return 'C3'
elif row['line'] == 9:
return 'B2'
elif row['line'] == 10:
return 'A2'
elif row['line'] == 11:
return 'G2'
elif row['line'] == 12:
return 'F2'
elif row['line'] == 13:
return 'E2'
elif row['line'] == 14:
return 'D2'
# elif row['object'] == 'wholerest' or row['object'] == '2rest' or row['object'] == '4rest' or row['object'] == 'uprest' or row['object'] == 'Rrest' or row['object'] == 'downrest' or row['object'] == 'rurest':
# return 'r'
else:
return ''
else:
return ''
def to_treble(row):
if row['notes'] != None:
if row['line'] == 0:
return 'B5'
elif row['line'] == 1:
return 'A5'
elif row['line'] == 2:
return 'G5'
elif row['line'] == 3:
return 'F5'
elif row['line'] == 4:
return 'E5'
elif row['line'] == 5:
return 'D5'
elif row['line'] == 6:
return 'C5'
elif row['line'] == 7:
return 'B4'
elif row['line'] == 8:
return 'A4'
elif row['line'] == 9:
return 'G4'
elif row['line'] == 10:
return 'F4'
elif row['line'] == 11:
return 'E4'
elif row['line'] == 12:
return 'D4'
elif row['line'] == 13:
return 'C4'
elif row['line'] == 14:
return 'B3'
# elif row['object'] == 'wholerest' or row['object'] == '2rest' or row['object'] == '4rest' or row['object'] == 'uprest' or row['object'] == 'Rrest' or row['object'] == 'downrest' or row['object'] == 'rurest':
# return 'r'
else:
return ''
else:
return ''
def accidentals(row):
if row['final'] != '':
if row['sharped'] == 'yes' and row['x'] > 200:
return row['final']+'#'
elif row['flatted'] == 'yes' and row['x'] > 200:
return row['final']+'-'
else:
return row['final']
else:
return ''
def tenor(df):
df['final'] = df.apply(to_tenor, axis = 1)
df['final'] = df.apply(accidentals, axis = 1)
string = df['final'].values
print(string)
return string
def alto(df):
df['final'] = df.apply(to_alto, axis = 1)
df['final'] = df.apply(accidentals, axis = 1)
string = df['final'].values
print(string)
return string
def bass(df):
df['final'] = df.apply(to_bass, axis = 1)
df['final'] = df.apply(accidentals, axis = 1)
string = df['final'].values
print(string)
return string
def treble(df):
df['final'] = df.apply(to_treble, axis = 1)
df['final'] = df.apply(accidentals, axis = 1)
string = df['final'].values
print(string)
return string
def dotted(i):
if dff['dotted'][i] == 'yes':
return True
else: return False
def length(i):
d= dff['notes'].fillna(0).astype(float)
if dotted(i):
d[i] = d[i]*1.5
else: d[i] = d[i]
return d[i]
def keyed(row):
sharps = 0
flats = 0
index = dff['notes'].index[dff['notes'].notnull()]
df_index = dff.index.values.tolist()
c=[df_index.index(i) for i in index]
g = c[0]
for x in dff.object[0:g]:
if x=='sharp':
sharps+=1
elif x=='flat':
flats+=1
else:
flats = flats
sharps = sharps
if sharps > flats:
return sharps
elif flats > sharps:
return -flats
else:
return 0
def key_element(x):
global k
if k == 1:
if x == 'F3':
x = 'F#3'
elif x == 'F2':
x = 'F#2'
elif x == 'F4':
x = 'F#4'
elif x == 'F5':
x = 'F#5'
elif x == 'F2-':
x = 'F2'
elif x == 'F3-':
x = 'F3'
elif x == 'F4-':
x = 'F4'
elif x == 'F5-':
x = 'F5'
else:
x=x
elif k == 2:
if x == 'F3':
x = 'F#3'
elif x == 'F2':
x = 'F#2'
elif x == 'F4':
x = 'F#4'
elif x == 'F5':
x = 'F#5'
elif x == 'C3':
x = 'C#3'
elif x == 'C2':
x = 'C#2'
elif x == 'F4':
x = 'C#4'
elif x == 'F5':
x = 'C#5'
else:
x=x
elif k == 3:
if x == 'F3':
x = 'F#3'
elif x == 'F2':
x = 'F#2'
elif x == 'F4':
x = 'F#4'
elif x == 'F5':
x = 'F#5'
elif x == 'C3':
x = 'C#3'
elif x == 'C2':
x = 'C#2'
elif x == 'C4':
x = 'C#4'
elif x == 'C5':
x = 'C#5'
elif x == 'G3':
x = 'G#3'
elif x == 'G2':
x = 'G#2'
elif x == 'G4':
x = 'G#4'
elif x == 'G5':
x = 'G#5'
else:
x=x
elif k == 4:
if x == 'F3':
x = 'F#3'
elif x == 'F2':
x = 'F#2'
elif x == 'F4':
x = 'F#4'
elif x == 'F5':
x = 'F#5'
elif x == 'C3':
x = 'C#3'
elif x == 'C2':
x = 'C#2'
elif x == 'C4':
x = 'C#4'
elif x == 'C5':
x = 'C#5'
elif x == 'G3':
x = 'G#3'
elif x == 'G2':
x = 'G#2'
elif x == 'G4':
x = 'G#4'
elif x == 'G5':
x = 'G#5'
elif x == 'D3':
x = 'D#3'
elif x == 'D2':
x = 'D#2'
elif x == 'D4':
x = 'D#4'
elif x == 'D5':
x = 'D#5'
else:
x=x
elif k == 5:
if x == 'F3':
x = 'F#3'
elif x == 'F2':
x = 'F#2'
elif x == 'F4':
x = 'F#4'
elif x == 'F5':
x = 'F#5'
elif x == 'C3':
x = 'C#3'
elif x == 'C2':
x = 'C#2'
elif x == 'C4':
x = 'C#4'
elif x == 'C5':
x = 'C#5'
elif x == 'G3':
x = 'G#3'
elif x == 'G2':
x = 'G#2'
elif x == 'G4':
x = 'G#4'
elif x == 'G5':
x = 'G#5'
elif x == 'D3':
x = 'D#3'
elif x == 'D2':
x = 'D#2'
elif x == 'D4':
x = 'D#4'
elif x == 'D5':
x = 'D#5'
elif x == 'A3':
x = 'A#3'
elif x == 'A2':
x = 'A#2'
elif x == 'A4':
x = 'A#4'
elif x == 'A5':
x = 'A#5'
else:
x=x
elif k == 6:
if x == 'F3':
x = 'F#3'
elif x == 'F2':
x = 'F#2'
elif x == 'F4':
x = 'F#4'
elif x == 'F5':
x = 'F#5'
elif x == 'C3':
x = 'C#3'
elif x == 'C2':
x = 'C#2'
elif x == 'C4':
x = 'C#4'
elif x == 'C5':
x = 'C#5'
elif x == 'G3':
x = 'G#3'
elif x == 'G2':
x = 'G#2'
elif x == 'G4':
x = 'G#4'
elif x == 'G5':
x = 'G#5'
elif x == 'D3':
x = 'D#3'
elif x == 'D2':
x = 'D#2'
elif x == 'D4':
x = 'D#4'
elif x == 'D5':
x = 'D#5'
elif x == 'A3':
x = 'A#3'
elif x == 'A2':
x = 'A#2'
elif x == 'A4':
x = 'A#4'
elif x == 'A5':
x = 'A#5'
elif x == 'E3':
x = 'E#3'
elif x == 'E2':
x = 'E#2'
elif x == 'E4':
x = 'E#4'
elif x == 'E5':
x = 'E#5'
else:
x=x
elif k == 7:
if x == 'F3':
x = 'F#3'
elif x == 'F2':
x = 'F#2'
elif x == 'F4':
x = 'F#4'
elif x == 'F5':
x = 'F#5'
elif x == 'C3':
x = 'C#3'
elif x == 'C2':
x = 'C#2'
elif x == 'C4':
x = 'C#4'
elif x == 'C5':
x = 'C#5'
elif x == 'G3':
x = 'G#3'
elif x == 'G2':
x = 'G#2'
elif x == 'G4':
x = 'G#4'
elif x == 'G5':
x = 'G#5'
elif x == 'D3':
x = 'D#3'
elif x == 'D2':
x = 'D#2'
elif x == 'D4':
x = 'D#4'
elif x == 'D5':
x = 'D#5'
elif x == 'A3':
x = 'A#3'
elif x == 'A2':
x = 'A#2'
elif x == 'A4':
x = 'A#4'
elif x == 'A5':
x = 'A#5'
elif x == 'E3':
x = 'E#3'
elif x == 'E2':
x = 'E#2'
elif x == 'E4':
x = 'E#4'
elif x == 'E5':
x = 'E#5'
elif x == 'B3':
x = 'B#3'
elif x == 'B2':
x = 'B#2'
elif x == 'B4':