I'm reading through these few bars...but it seems so tonally ambiguous (to me, at least). When it looks like g minor, it starts to look like B flat minor, and so on, until i become confused. 
It's not such a bad conclusion that it is tonally ambigous. Play a F#dim followed by an A dim. As you can see these chords contain the same pitches even if you would spell them differently. Now play a F# dim followed by a g minor and then an A dim followed by a b flat minor. Notice how the dim chords sound like dominants to the following chord. But both the dim chords are the same chord, just in different inversions, but they (and the rest of the inversions) can act as dominants to completely different keys (to E and D flat as well!), so if there is lots of dim the listener probably will be unsure where it's going to land. This is a tool composers can use to navigate between different keys.
In your passage he mixes a lot of different ways to spell the dim. And it's like 12 bars of just dim. But I'd say he is aiming to eventually land in the B flat tonality. In last 4-5 bars before the double line the chords contain a, c flat, e flat, g flat which together with an f would make an F major b9 b5 chord, which is a dominant in a B flat key (if you just listen to the pitches of the "F major" chord it sounds like a B major 7 chord but the spelling tells you he is in B flat and thus it is the dominant). However he keeps the feeling of missing a secure home key up by landing on a B flat 7 instead of a "pure" tonic. But before that it is pretty ambiguous, as you say.
It would be interesting to know what happens in the piece before and after this passage. Which piece is it?