emilianavt / OpenSeeFace

Robust realtime face and facial landmark tracking on CPU with Unity integration
BSD 2-Clause "Simplified" License
1.42k stars 151 forks source link

non-object-oriented code #3

Closed marzi9696 closed 4 years ago

marzi9696 commented 4 years ago

Hi.excellent work.Thank you so much for making this amazing project available for others to use. I'm trying to use the mouth_open and eye blink components separately and build two flask API out of it.one for mouth_open detection and one for eye_blink detection but I read the code so many times everything is so connected I can't seem to use only the parts I want.I want to load the model once and then only do prediction with every API request.how can I do that? I'm afraid if I edit the code I may do something wrong.do you have any separate source code or anything?

emilianavt commented 4 years ago

Hi! The code isn't very clean, it kind of just grew together like this over time. Pulling out just mouth_open and the eye blinks is probably difficult. I don't think it would make much difference from a performance point of view, as calculating the other features doesn't take much time.

I would recommend instantiating an object of the Tracker class, like in facetracker.py. As you are only looking for eye blink and mouth tracking, you can set no_gaze=True. You can then call tracker.predict(frame) to get a list of tracking results for that frame.

If you want to save a bit more time by not calculating the unneeded features, you can carefully remove them from the FeatureExtractor class. To do this, first remove the variable from its constructor and then change the lines like features["eyebrow_updown_l"] = self.eyebrow_updown_l.update(f, now) to features["eyebrow_updown_l"] = 0. You can then go through and clean out some variables or temporary values that are no longer needed. Overall I believe this probably won't save much more than 1ms though.

The tracking is stateful and follows the bounding box of each face. If you would prefer stateless tracking, you can set the detected field of the tracker object to 0 and the faces field to [] after calling predict. Doing this, it should be alright to feed frames from different videos, as long as they have the same resolution, but the face detection needs to run for each frame so it's slower.

I don't really know anything about flask, so I might have missed some important point. If you have any more questions, please let me know!