If I’m driving and I see a person in the distance walking on the sidewalk, and at first I thought it was a woman and I saw what was haram to look at, but then upon getting close it turns out to be a man.

Does it count here as looking at haram?

No since it was not deliberate then it is not haram. Deliberately looking at the opposite sex (with lust) is haram.