Is there an algorithm? Yes. What you're talking about is called image classification, and there are a ton of algorithms that do just that.

What have you tried? What has google told you?

This...