They could see the one pirate through a window, the other two had their heads out, it was dark, they thought they were safe(why I don’t know). The head shot on the one holding the rifle on the Captain would not have endangered him, the pirate’s head was probably higher than the Captain’s.
Makes you wonder why the pirates didn't put up something to block the window. Maybe they were not that bright?
I'm not an expert, but I thought that the military also has infrared vision (and who knows what else) devices that can see through some walls. Also, they had the 4th pirate on board, possibly telling them information about where the hostage was.