I stayed all night to finish the program..
I got a really good idea whilst staying all night(which is quite rare)! I thought of detecting the black keys first and then removing the black keys from the image when detecting the white keys, to remove the noise from the black keys.