GazeML icon indicating copy to clipboard operation
GazeML copied to clipboard

Lag on runing realtime

Open meysam-safarzadeh opened this issue 5 years ago • 15 comments

meysam-safarzadeh avatar Aug 01 '19 09:08 meysam-safarzadeh

hello, How can I fix the lag when running realtime on webcam, I implement it by GPU and cuda10 & Cudnn, but it has a periodic lag that I think its because of the program problem, thanks to anyone can help me

meysam-safarzadeh avatar Aug 01 '19 09:08 meysam-safarzadeh

I also got exactly the same problem, when I test on video. However, I saved result afterward, I can check it later. Have you tried --from_video, too?

keishatsai avatar Aug 02 '19 02:08 keishatsai

I also got exactly the same problem, when I test on video. However, I saved result afterward, I can check it later. Have you tried --from_video, too?

No, actually my need is running realtime

meysam-safarzadeh avatar Aug 02 '19 09:08 meysam-safarzadeh

Have you try to adjust "--fps" ? or what kind of lag is it? Does it lag in certain pattern, like after every 60 frames will lag? Author did a batch result during inference. Maybe this is the reason. You can edit the code without batch output.

keishatsai avatar Aug 05 '19 08:08 keishatsai

Have you try to adjust "--fps" ? or what kind of lag is it? Does it lag in certain pattern, like after every 60 frames will lag? Author did a batch result during inference. Maybe this is the reason. You can edit the code without batch output.

I tried "--fps" but didn't work, yeah it lags in a certain pattern but I don't know how can I disable the batch output.

could u help me with that?!

meysam-safarzadeh avatar Aug 06 '19 16:08 meysam-safarzadeh

@meysam97 Currently, haven't got time to look into this. If you have already solved, please share with us. Thank you.

keishatsai avatar Aug 13 '19 07:08 keishatsai

You can adjust the input image resolution in Webcam class

class Webcam(FramesSource): """Webcam frame grabbing and preprocessing."""

def __init__(self, camera_id=0, fps=60, w=1280, h=720, **kwargs):
    """Create queues and threads to read and preprocess data."""
    self._short_name = 'Webcam'

    self._capture = cv.VideoCapture(camera_id)
    self._capture.set(cv.CAP_PROP_FRAME_WIDTH, w)
    self._capture.set(cv.CAP_PROP_FRAME_HEIGHT, h)
    self._capture.set(cv.CAP_PROP_FOURCC, cv.VideoWriter_fourcc(*'MJPG'))
    self._capture.set(cv.CAP_PROP_FPS, fps)

or use another face detection to replace dlib

gy29289957 avatar Sep 05 '19 03:09 gy29289957

That's a good point.

You could try compiling dlib manually instead of using the pip installed version: http://dlib.net/compile.html

I recall that I was using such a version of the library.

On Thu, 5 Sep 2019, 05:05 LZJ, [email protected] wrote:

You can adjust the input image resolution in Webcam class

class Webcam(FramesSource): """Webcam frame grabbing and preprocessing."""

def init(self, camera_id=0, fps=60, w=1280, h=720, **kwargs): """Create queues and threads to read and preprocess data.""" self._short_name = 'Webcam'

self._capture = cv.VideoCapture(camera_id)
self._capture.set(cv.CAP_PROP_FRAME_WIDTH, w)
self._capture.set(cv.CAP_PROP_FRAME_HEIGHT, h)
self._capture.set(cv.CAP_PROP_FOURCC, cv.VideoWriter_fourcc(*'MJPG'))
self._capture.set(cv.CAP_PROP_FPS, fps)

or use another face detection to replace dlib

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/swook/GazeML/issues/38?email_source=notifications&email_token=AAFYRUSTCQHL6C5UJOMTRG3QIBZPHA5CNFSM4IIORIUKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD55VMMQ#issuecomment-528176690, or mute the thread https://github.com/notifications/unsubscribe-auth/AAFYRUWIGYW52E56SFMVZ4LQIBZPHANCNFSM4IIORIUA .

swook avatar Sep 05 '19 07:09 swook

The lag still persists. The lag actually happens in multiples of 60 frames. Is this lag related to the batch inference?

saksham16085 avatar Sep 30 '19 15:09 saksham16085

You can comment these lines of _record_frame() function, it will solve the problem:

                    #now_time = time.time()
                    #if last_frame_time is not None:
                    #    time_diff = now_time - last_frame_time
                    #    while time_diff > 0.0:
                    #        video_out.write(frame)
                    #        time_diff -= out_frame_interval
                    #last_frame_time = now_time

hoangthang1607 avatar Dec 17 '19 10:12 hoangthang1607

@hoangthang1607 Didn't help for me

filyp avatar Mar 29 '20 02:03 filyp

I am getting the same issue. Did anyone of you people find a solution to it? Commenting the lines mentioned by @hoangthang1607 didn't work for me.

funkfuzz avatar Jun 26 '20 09:06 funkfuzz

For me, reducing the resolution helped, just as @gy29289957 said.

Other option is to keep the same resolution, but decrease fps. Unfortunately, --fps option doesn't work for me. I don't know if it falis in general, or it's just specific to my webcam. I ended up using this stupid hack:

def frame_generator(self):
        """Read frame from webcam."""
        while True:
            ret, bgr = self._capture.read()
            ret, bgr = self._capture.read()
            if ret:
                yield bgr

Which isn't elegant but was enough for me. The duplicated self._capture.read() causes frame_generator to skip every other frame, which in practice gives 30fps.

filyp avatar Jun 26 '20 11:06 filyp

I managed to solve my problem as well. I built dlib from source with cuda enabled and managed to eliminate the bottleneck and got steady 23 fps. I then downloaded the tensorflow-gpu instead of the cpu version and managed to boost the speed up to steady 30 fps.

funkfuzz avatar Jun 29 '20 16:06 funkfuzz

I, too, have to problem with a powerful PC, Ubuntu 20.04, tf 1.14, CUDA 11.

It sometimes gets a weird lag peak even though mostly it's running with nice stable 30fps. Maybe the program is doing some heavy operations such as copying stuff to queue, moving them in memory or saving things on disk.

Hyrtsi avatar Apr 12 '22 09:04 Hyrtsi