Normally I would recommand to use
OpenCV library, used like in the article
Face and Eyes Detection Using OpenCV .
When writing an own algorithm is your task, you need to tweak it mostly with the data in the expected quality. When you check your code against the values of OpenCV you may find some issues. Start it with some simple and clean cases to from which points the problems are rising. That also helps to discuss how good the results are and where to go next.
Remember: Using an own algorithm has a lot of pitfalls as reliability, often unknown edge case are in the dark and precious resources are wasted to "re-invent the wheel" instead of pushing the project to success.