As already noted by 0x01AA:
The inner while loop is never left once a motion has been detected.
To be more precise:
Your outer loop is working and looping until a motion is detected. Then you have another infinite while loop which prevents returning to the outer loop.
So you have to remove the inner while loop statement or leave the loop using a
break[
^] statement.
You should avoid using infinite loops without break conditions. When using such, you can only use a single one within an application because it can be only left by killing the application.