you might try this...when mine would do the same thing, I started carrying the tiny jars of treats(they only get these when we are out & about), anyway when they started barking I did the classic finger to lip-sshhh!
As soon as they stopped- they got a treat. Now we're to the point that I don't give a treat every time but they are a lot better with the sshhh!
When we pass another dog or kids and there's no barking, they get a jackpot reward (several treats)...good luck