#VSCode – Let’s do some #FaceRecognition with 20 lines in #Python (5/N)

Buy Me A Coffee

Hi!

And after yesterday’s post I realize that the code is working, but there is room for performance improvement. So, I went back to my 1st sample, the one for face detection and I added some code to get some times for Frames per Second (FPS).

In my initial code, the app was working processing almost 6 FPS. Then I started to read the code and think on improvements and I manage to get an amazing +30FPS.

So, before moving forward, I want to remark this StackOverflow post that quickly pointed me in the easiest way to do a StopWatch in Python.

My original code, was this one:

# standard face detection sample with FPS in console
import face_recognition
import cv2
import time
video_capture = cv2.VideoCapture(0)
while True:
start_time = time.time()
ret, frame = video_capture.read()
rgb_frame = frame[:, :, ::1]
face_locations = face_recognition.face_locations(rgb_frame)
for top, right, bottom, left in face_locations:
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.imshow('Video', frame)
print("FPS: ", 1.0 / (time.time() start_time)) # FPS = 1 / time to process loop
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()

And then, I realize that I may use some of the OpenCV functions to increase the face detection process. I really don’t need to process a full HD image (1920 x 1080), I may resize the frame to a quarter size and work with this. That’s how, based on some of the samples, I got the following code:

# fast face detection sample with FPS in console
import face_recognition
import cv2
import time
video_capture = cv2.VideoCapture(0)
while True:
start_time = time.time()
ret, frame = video_capture.read()
fast_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_frame = fast_frame[:, :, ::1]
face_locations = face_recognition.face_locations(rgb_frame)
for top, right, bottom, left in face_locations:
top *= 4
right *= 4
bottom *= 4
left *= 4
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.imshow('Video', frame)
print("FPS: ", 1.0 / (time.time() start_time)) # FPS = 1 / time to process loop
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()

The line 12 perform the initial resize and then I recalculate back the positions before drawing the face frame. This process works almost 6 times faster than the original one.

I’ll continue improving the code and samples, and sharing my learning path !

Happy Coding!

Greetings @ Burlington

El Bruno

Resources

Advertisement

2 comments

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: