He knows that it's a six-digit code from the make of the safe.
I see from the make that it's a six-digit code.
He gets a number of clues from seeing how much the keys have been pressed.
You should always use gloves with these, you know. Heaviest oil deposit is always on the first key used - that's quite clearly a three - but after that the sequence is almost impossible to read.
It can't be your birthday - no disrespect, but clearly you were born in the 80s, and 8's barely used.
After that, Irene herself clues him in. First she teases him, as he was teasing her with the hiker case:
I'd tell you the code right now, but you know what? I already have. Think.
Then after the CIA agents burst in and threaten to shoot John, she genuinely wants him to guess it. She's been forbidden from speaking, but just as the CIA man is about to count to three, Sherlock looks at her and she looks significantly and meaningfully down at her own body.
At this point, he already knows it's a six-digit code starting with 3 and comprised predominantly - probably solely - of 2s, 3s and 4s, and he knows it's something she's "told" him already. With this information, that meaningful glance was enough for Sherlock to correctly deduce that the safe code is her measurements.
Knowing which keys were most pressed probably also helped him with getting the measurements right as well (guessing 34 instead of 36 for example) as, unlike cigarette ash, women's measurements at a glance doesn't really seem like the sort of thing Sherlock would have practice at calculating.