But if I understand the specification correctly even if a MIDI device swaps pins 4 and 5 there should still be a detectable voltage between either of them and pin 2 (GND) whilst idle, since it specifies idle to be high state.
The specification specifies (p. 1):
5 mA current loop type. Logical 0 is current ON.
It says nothing about the idle
voltage. The idle state just has to prevent the current flow; this can be done either by driving pins 4 and 5 to the same voltage (push/pull output), or by making at least one pin open (open-collector output). (The original specifcation does not say what kind the buffer should be; the 2014 update recommends an open-collector buffer because this works better when the output is short circuited.)
When pin 4 is switched off, then all you see in the idle state is pin 2 as ground, and pin 5 connected to ground with a resistor.
Anyway, there are very few devices that switch pin 4, so I'd just ignore this case.
Assuming this would work, my main question would be how I would implement a high impedance voltage detector that maintains ground isolation?
Use a second optocoupler with very high CTR (e.g., 6N139), and disconnect it when you've actually detected a connection. Or invent some clever trickery to use the same optocoupler for both detection and the actual receiver.