Given only a gerber file how do I automatically find out the number of pads that are there on the PCB
A Gerber artwork is mostly rendered by three "D-code" commands. D01 means move with exposure on. D02 means move with exposure off. D03 means flash. Open a Gerber file with a text editor and you can see the D-code commands at the end of the lines with the coordinates in front.

Typically, almost all the pads should be flashes on the soldermask layers. So counting the flashes on the soldermask layers would give you an estimate. But I don't think there is a sure way of knowing what is a pad from the Gerber file alone.