Aside from the "why" issues, this is a classic example of a machine vision application as previously pointed out. Assuming color recognition is irrelevant, there are (relatively) low cost camera systems that can be "taught" to recognize a specific size and shape of object, with or without tolerances that can allow for slight variations. Most are now very intuitive and can be set up to give a "go/no-go" output, or communicate to a higher level system for decision making. Cognex, as also previously mentioned, is one of the bigger players in this, but now there are many others who make simpler less expensive versions for applications like yours. I used one from Balluf that I trained to recognize various different size product bottles coming down a line to a capper so that it would pick and place the correct size cap on it. Worked flawlessly, allowing the facility to have one capper for 6 different production lines all feeding into it randomly rather than 6 different cappers.
" We are all here on earth to help others; what on earth the others are here for I don't know." -- W. H. Auden