1
- from classifier import *
2
1
from preprocessing import *
3
2
from staff_removal import *
4
3
from helper_methods import *
5
4
6
5
import argparse
7
6
import os
8
7
import datetime
8
+
9
9
# Initialize parser
10
10
parser = argparse .ArgumentParser ()
11
-
12
11
parser .add_argument ("inputfolder" , help = "Input File" )
13
12
parser .add_argument ("outputfolder" , help = "Output File" )
14
13
22
21
23
22
# Threshold for line to be considered as an initial staff line #
24
23
threshold = 0.8
25
- accidentals = ['x' , 'hash' , 'b' , 'symbol_bb' , 'd' ]
26
-
27
-
28
24
filename = 'model/model.sav'
29
25
model = pickle .load (open (filename , 'rb' ))
26
+ accidentals = ['x' , 'hash' , 'b' , 'symbol_bb' , 'd' ]
30
27
31
28
def preprocessing (inputfolder , fn , f ):
32
29
# Get image and its dimensions #
@@ -46,58 +43,63 @@ def preprocessing(inputfolder, fn, f):
46
43
47
44
return cutted , ref_lines , lines_spacing
48
45
46
+ def get_target_boundaries (label , cur_symbol , y2 ):
47
+ if label == 'b_8' :
48
+ cutted_boundaries = cut_boundaries (cur_symbol , 2 , y2 )
49
+ label = 'a_8'
50
+ elif label == 'b_8_flipped' :
51
+ cutted_boundaries = cut_boundaries (cur_symbol , 2 , y2 )
52
+ label = 'a_8_flipped'
53
+ elif label == 'b_16' :
54
+ cutted_boundaries = cut_boundaries (cur_symbol , 4 , y2 )
55
+ label = 'a_16'
56
+ elif label == 'b_16_flipped' :
57
+ cutted_boundaries = cut_boundaries (cur_symbol , 4 , y2 )
58
+ label = 'a_16_flipped'
59
+ else :
60
+ cutted_boundaries = cut_boundaries (cur_symbol , 1 , y2 )
61
+
62
+ return label , cutted_boundaries
63
+
64
+ def get_label_cutted_boundaries (boundary , height_before , cutted ):
65
+ # Get the current symbol #
66
+ x1 , y1 , x2 , y2 = boundary
67
+ cur_symbol = cutted [y1 - height_before :y2 + 1 - height_before , x1 :x2 + 1 ]
68
+
69
+ # Clean and cut #
70
+ cur_symbol = clean_and_cut (cur_symbol )
71
+ cur_symbol = 255 - cur_symbol
72
+
73
+ # Start prediction of the current symbol #
74
+ feature = extract_hog_features (cur_symbol )
75
+ label = str (model .predict ([feature ])[0 ])
76
+
77
+ return get_target_boundaries (label , cur_symbol , y2 )
78
+
49
79
def process_image (inputfolder , fn , f ):
50
80
cutted , ref_lines , lines_spacing = preprocessing (inputfolder , fn , f )
51
81
52
82
last_acc = ''
53
83
last_num = ''
54
84
height_before = 0
55
85
56
-
57
86
if len (cutted ) > 1 :
58
87
f .write ('{\n ' )
59
88
60
89
61
90
for it in range (len (cutted )):
62
91
f .write ('[' )
63
92
is_started = False
64
- cur_img = cutted [it ].copy ()
65
93
66
-
67
- symbols_boundries = segmentation (height_before , cutted [it ])
68
- symbols_boundries .sort (key = lambda x : (x [0 ], x [1 ]))
94
+
95
+ symbols_boundaries = segmentation (height_before , cutted [it ])
96
+ symbols_boundaries .sort (key = lambda x : (x [0 ], x [1 ]))
69
97
70
- symbols = []
71
- for boundry in symbols_boundries :
72
- # Get the current symbol #
73
- x1 , y1 , x2 , y2 = boundry
74
- cur_symbol = cutted [it ][y1 - height_before :y2 + 1 - height_before , x1 :x2 + 1 ]
75
-
76
- # Clean and cut #
77
- cur_symbol = clean_and_cut (cur_symbol )
78
- cur_symbol = 255 - cur_symbol
98
+ for boundary in symbols_boundaries :
99
+ label , cutted_boundaries = get_label_cutted_boundaries (boundary , height_before , cutted [it ])
79
100
80
- # Start prediction of the current symbol #
81
- feature = extract_features (cur_symbol , 'hog' )
82
- label = str (model .predict ([feature ])[0 ])
83
-
84
101
if label == 'clef' :
85
102
is_started = True
86
-
87
- if label == 'b_8' :
88
- cutted_boundaries = cut_boundaries (cur_symbol , 2 , y2 )
89
- label = 'a_8'
90
- elif label == 'b_8_flipped' :
91
- cutted_boundaries = cut_boundaries (cur_symbol , 2 , y2 )
92
- label = 'a_8_flipped'
93
- elif label == 'b_16' :
94
- cutted_boundaries = cut_boundaries (cur_symbol , 4 , y2 )
95
- label = 'a_16'
96
- elif label == 'b_16_flipped' :
97
- cutted_boundaries = cut_boundaries (cur_symbol , 4 , y2 )
98
- label = 'a_16_flipped'
99
- else :
100
- cutted_boundaries = cut_boundaries (cur_symbol , 1 , y2 )
101
103
102
104
for cutted_boundary in cutted_boundaries :
103
105
_ , y1 , _ , y2 = cutted_boundary
@@ -126,29 +128,29 @@ def process_image(inputfolder, fn, f):
126
128
if len (cutted ) > 1 :
127
129
f .write ('}' )
128
130
129
- for i in [ args . inputfolder ] :
131
+ def main () :
130
132
try :
131
133
os .mkdir (args .outputfolder )
132
- except OSError as error :
134
+ except OSError as error :
133
135
pass
134
-
135
136
136
137
list_of_images = os .listdir (args .inputfolder )
137
-
138
- for i , fn in enumerate ( list_of_images ):
139
- # Open the output text file #
140
- file_prefix = fn . split ( '.' )[ 0 ]
141
- f = open ( f" { args . outputfolder } / { file_prefix } .txt" , "w" )
142
-
143
-
144
- # Process each image separately #
145
- try :
146
- process_image ( args . inputfolder , fn , f )
147
- except :
148
- print ( f' { args . inputfolder } - { fn } has been failed !!' )
149
- pass
150
-
151
- f . close ()
138
+ for _ , fn in enumerate ( list_of_images ):
139
+ # Open the output text file #
140
+ file_prefix = fn . split ( '.' )[ 0 ]
141
+ f = open ( f" { args . outputfolder } / { file_prefix } .txt" , "w" )
142
+
143
+ # Process each image separately #
144
+ try :
145
+ process_image ( args . inputfolder , fn , f )
146
+ except Exception as e :
147
+ print ( e )
148
+ print ( f' { args . inputfolder } - { fn } has been failed !!' )
149
+ pass
150
+
151
+ f . close ()
152
+ print ( 'Finished !!' )
152
153
153
154
154
- print ('Finished !!' )
155
+ if __name__ == "__main__" :
156
+ main ()
0 commit comments