How to eliminate electromagnetic interference when counting pulses using a micro controller?
If you had a scope you could also see whether the spikes are the kind where a schmitt trigger would help more.

But more likely, is to debounce and/or blank your pulses.

Debounce: Only accept a pulse after it has been stable for long enough. After a transition is detected, take two or three samples over some time interval and only count the pulse if it's value is equal every time. Doing this is a pretty safe bet. This will involve some fancy interrupt and timer programming unless you are willing to just have your MCU sit there waiting for pulses and doing nothing else.

Blank: After a transition is detected, count the pulse but then ignore any transitions for some set amount of time afterwards. This is easier to implement but probably will not be enough on its own. May be combined with debouncing where you after a signel is stable for long enough, it is accepted and then all transitions after a certain amount of time are rejected (since you know a new valid pulse could not possibly come in during that time).

Another thing that can help is to use a pulldown resistor before R1 that is 500Ohms-2KOhm (if 9V) to load your signal so that 5-20mA signal current must flow so noise has to be stronger to have the same effect. But this also loads your signal source.

Similar for filter caps. Also, you don't need R1 if Q1 is a MOSFET unless you plan to add a filter cap after it but that would mess with your rise/fall times.