yarin Cohen
yarin Cohen

Reputation: 1142

Python face_recognition takes too long to process each frame

Hey I am using the following code from face_recognition github: code Everything works out beside that I am getting 2 fps/sec when It detects a face, when it is not, the frame rate is ~30.

I looked up on task manager to see how's my computer doing and The CPU is on ~15% as well as the GPU so it isn't hardware problem. I also tried changing the resolution to 320x240 but It did nothing same problem.

How can I increase the fps? Edit: I have only one encoding to process so multi threading will probably not help

Upvotes: 3

Views: 3497

Answers (2)

Xingyu Pan
Xingyu Pan

Reputation: 1

I tried the same project on both my Windows PC and Macbook pro with OSX. I found that the FPS on MacBook pro can reach more than 30 FPS while the windows PC can only run at 2 FPS. The CPU on my Windows PC is Ryzen 3900X and my MacBook uses a i9 CPU. From my perspective, the bottleneck is not the CPU itself, the operating system should be the main issues. Try to install a Linux based OS and do it again. I don't know the reason but Linux does have these advantages.

Upvotes: 0

rocket_brain
rocket_brain

Reputation: 89

There are two processing steps to this program:

  1. In the while loop, face_detection searches for faces in the frame, and then encodes them (uses linear algebra to simplify the face data into unique vectors).

  2. In the for loop (which only runs once a face is detected), the program runs the encoding from step 1 against all other "known" encodings, returning the best match if it meets the default threshold of certainty.

If you have modified the code at all, increasing the number of "known" faces, it would slow the process down some. Also, I do not know how optimized the threading is. It is possible that some of the program is running on a single thread, which would show lower CPU usage even though the program is maxing what it has efficient access to.

Troubleshooting steps: Inside of loop #2 (the for loop), start by commenting out the framing lines (or everything after compare_faces), and see if it speeds up. That way you can narrow in on what is taking the most processing power. If you find that compare_faces IS the bottleneck, try decreasing the number of "known" faces. If that drastically increases fps, it is likely you are limited by number of cores/threading.


The example code you are using is a less optimized version. Here is a slightly faster version. Tweaks include:

  1. Process each video frame at 1/4 resolution (though still display it at full resolution)

  2. Only detect faces in every other frame of video.

Basically by decreasing the amount of processing it will speed up the processing rate.

Upvotes: 2

Related Questions