Pan-Tilt Hat Roaming

Hi there,
I recently got the cute little pan-tilt hat and started messing around with the face tracking examples.
They work great, but I’d like the module to ‘roam’ per say when it cannot find a face - to this end, I’ve modified the ‘facetracker_lbp.py’ program with partial success.

I added this if statement just before the for loop:

lights(50 if len(faces) == 0 else 0, 50 if len(faces) > 0 else 0,0,50)

if len(faces) == 0:
roam()

for (x, y, w, h) in faces:

and defined ‘roam’ earlier on in the program (before the while True loop):

def roam():
rand_x = random.randint(-20,20)
rand_y = random.randint(-20,20)
pan(rand_x)
time.sleep(2)
tilt(rand_y)
time.sleep(2)

The roaming aspect works fine, but the sleep commands lock up the program every time it tries to refresh the frame in order to detect a face, resulting in a dreadfully slow frame rate and an unusable video output. Does anyone have any ideas on how to preserve the roaming function and the face tracking?
Thanks in advance!

I implemented something like this a while back, take a look at the code here: https://github.com/Tom-Archer/face-tracker

And a video: https://www.youtube.com/watch?v=WUjfsvxaFU4

Thanks for that, very helpful.
In the end, I hope to mount my pan-tilt hat upside-down on a ceiling as a sort of GLaDOS ripoff, so I’ll have to see if I can get the roaming to mimic some more natural movements, probably just using random integers again.
I’ll post my final code here once I get it all working.

Here we go:
Face Tracking
Thanks for your help. :)