fangfufu / Linux-Fake-Background-Webcam

Faking your webcam background under GNU/Linux, now supports background blurring, animated background, colour map effect, hologram effect and on-demand processing.
GNU General Public License v3.0
1.61k stars 161 forks source link

Segmentation fault after running for a couple minutes #105

Closed brandoningli closed 3 years ago

brandoningli commented 3 years ago

While running, I get an average of 19-20fps at 720p, which is much better than previous versions getting single-digit frame rates at that resolution.

However, after running for a minute or two, the program produces a Segmentation fault and stops. I'm able to restart the script and it picks right back up, but again Seg faults after a minute or two.

Pop!_OS (Ubuntu) 20.04 Kernel 5.11.0-7614-generic Project just pulled minutes ago. Python 3.8.5 v4l2loopback-dkms version 0.12.3-1ubuntu0.4 (from repos). I haven't yet tried compiling from source, but can if requested. Pip Package versions:

numpy 1.20,3
opencv-python 4.5.2.54
requests 2.23.0
requests-unixsocket 0.2.0
pyfakewebcam 0.1.0
mediapipe 0.8.5
urllib3 1.25.8
idna 2.8
certifi 2019.11.28
chardet 3.0.4
six 1.15.0
opencv-contrib-python 4.5.2.54
absl-py 0.11.0
wheel 0.36.2
protobuf 3.14.0
attrs 20.3.0

Program output:

$ python3 fake.py  -w /dev/video2 -v /dev/video7
Real camera original values are set as: 640x480 with 30 FPS and video codec 1448695129
Real camera new values are set as: 1280x720 with 30 FPS and video codec 1196444237
Running...
Please CTRL-C to reload the background / foreground images
Please CTRL-\ to exit
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
Segmentation fault (core dumped)
fangfufu commented 3 years ago

Please try and run it in --akvcam mode. You don't have to have installed akvcam for that. Do let me know if that helps.

brandoningli commented 3 years ago

Running it with --akvcam causes weird image artifacting/duplication and an almost immediate segfault. akvcam is not installed on this machine.

fangfufu commented 3 years ago

Okay, maybe compiling a new version of v4l2loopback-dkms, or try installing the actual akvcam. It does appear that running it with --akvcam causes the program to segfault.

brandoningli commented 3 years ago

Just built v4l2lopback following the instructions in the README, and I'm still getting segfaults.

I'll try giving akvcam an install.

fangfufu commented 3 years ago

Okay, try the following:

  1. Make sure you unloaded the older version of v4l2lopback by rebooting
  2. Run fakecam.py without --akvcam If segfault still happens, do let me know which webcam software you tried to use before it segfaults.

Then do the followings:

  1. Install akvcam, run fakecam without --akvcam
  2. Run fakecam with --akvcam

Segfault has got more to do with the C library my Python code is calling, than my own Python code.

brandoningli commented 3 years ago

I've been using the Zoom desktop client and Jitsi Meet (via Firefox 89) to test (but not both programs at the same time).

ETA: It doesn't segfault when launching any program. It segfaults while using said program.

The results of doing the above:

  1. Segfault still happens
  2. Same behavior as without akvcam
  3. Giving no -v parameter to a v4l2loopback virtual camera,
    
    $ python3 fake.py -w /dev/video2 --akvcam
    Real camera original values are set as: 640x480 with 30 FPS and video codec 1448695129
    Real camera new values are set as: 1280x720 with 30 FPS and video codec 1196444237
    Traceback (most recent call last):
    File "fake.py", line 421, in <module>
    main()
    File "fake.py", line 393, in main
    cam = FakeCam(
    File "fake.py", line 155, in __init__
    self.fake_cam = AkvCameraWriter(v4l2loopback_path, self.width, self.height)
    File "/home/brandon/Documents/git/Linux-Fake-Background-Webcam/akvcam.py", line 11, in __init__
    self.d = self.open_camera()
    File "/home/brandon/Documents/git/Linux-Fake-Background-Webcam/akvcam.py", line 24, in open_camera
    ioctl(d, v4l2.VIDIOC_S_FMT, vid_format)
    OSError: [Errno 22] Invalid argument
    Exception ignored in: <function AkvCameraWriter.__del__ at 0x7ff99bd320d0>
    Traceback (most recent call last):
    File "/home/brandon/Documents/git/Linux-Fake-Background-Webcam/akvcam.py", line 32, in __del__
    os.close(self.d)
    AttributeError: 'AkvCameraWriter' object has no attribute 'd'

Giving a v4l2loopback virtual camera with `-v`, it's running, but the picture is showing weird artifacting (green and magenta, has horizontal bars, and is duplicated). It also segfaults.

I have an `akvcam` device with an informational graphic, but I don't know how to get the fake background webcam program to interface with it. I've never used this module before.
brandoningli commented 3 years ago

While this doesn't solve the segfault issue, I made a workaround to restart the script when it does. It produces a bit of a flash when the camera restarts (which can be quite frequently and very annoying), but otherwise starts back up very quickly and seems to work decently in the interim. Exiting with CTRL + \ produces a 0 exit code and this script ends gracefully.

#!/bin/bash
CODE=1
while [ "$CODE" -ne "0" ]; do
  python3 /path/to/fake.py -w /dev/video2 -v /dev/video7 -i /path/to/image/folder
  CODE=$?
done
fangfufu commented 3 years ago

You need to specify -v with the akvcam device. akvcam adds two devices after your last webcam device. Whether or not you run it with AkvCameraWriter is sort of irrelevant at this point. We just want to stop the segfault.

brandoningli commented 3 years ago

I can confirm that there's still a segfault running with akvcam and the akvcam device, even segfaulting faster on average than using v4l2loopback.

fangfufu commented 3 years ago

What's your operating system?

brandoningli commented 3 years ago

Pop!_OS 20.04 LTS. It's based on Ubuntu 20.04 LTS.

fangfufu commented 3 years ago

In that case I have no idea then. I will leave this ticket open.

fangfufu commented 3 years ago

@brandoningli , please give the latest commit a go. Please reconfigure akvcam based on the README.

brandoningli commented 3 years ago

When running fake.py, you need to set -v to the second video device that Akvcam created, e.g. if Akvcam created /dev/video5 and /dev/video6, you need to set -v /dev/video5.

Did you mean to say the first device or video6?

fangfufu commented 3 years ago

video 5, apologise. I will amend the README.

brandoningli commented 3 years ago

Also, is --akvcam still required to be used? It's being reported as an unrecognized argument.

fangfufu commented 3 years ago

Nope, I completely removed that option.

brandoningli commented 3 years ago

Ok. It's still in the README, but not in the usage info in the program.

fangfufu commented 3 years ago

Done, updated README.

brandoningli commented 3 years ago

I'm still getting frequent segfaults, and my computer seems to crash completely 15-30 seconds afterwards. I have no way of directly blaming the script or akvcam, but it happens reliably after the segfault and rarely any other time.

fangfufu commented 3 years ago

Okay maybe try the Docker version? I suspect it is the difference between OS versions. It runs pretty stably on my Debian Stable

brandoningli commented 3 years ago

Has the docker version been updated to use mediapipe? I've used the older docker version before with success, but poor performance.

fangfufu commented 3 years ago

Someone supposedly patched the Dockerfile, and if you re-generate the Docker image, it should be up-to-date.

brandoningli commented 3 years ago

It has updated to the new mediapipe implementation, but there's two issues with the Docker implementation:

  1. The Dockerfile calls for copying akvcam.py which doesn't exist anymore. I can submit that in a PR shortly.
  2. It too exits with a code 139, which could indicate a segfault.
fangfufu commented 3 years ago

Okay, definitely submit your PR.

I think I am just going to leave this issue open. I can't suggest any ways for you to fix it. I don't know if vanilla Ubuntu 20.04 would have the same problem.

brandoningli commented 3 years ago

Thanks for your help, though!

fangfufu commented 3 years ago

@brandoningli , could you give this a go please on normal v4l2loopback-dkms please? I suspect this might be some sort of race condition going on. https://github.com/fangfufu/Linux-Fake-Background-Webcam/tree/single-threaded

brandoningli commented 3 years ago

I don''t want to jinx it, but it's already been running longer without segfaulting than any other version. I'll leave it running in the background for a while and report back if it segfaults or keeps running. Performance seems on-par with the regular mediapipe version before it segfaulted as well.

fangfufu commented 3 years ago

Basically I tried to add more multithreading, and I tried putting the mask generation within the RealCam class. I kept getting segfaults myself too. This led to the suspicion of race conditions. Race condition is not exactly easy to diagnose, especially I don't really use Python enough. I wonder if this race condition was introduced after I removed all the async stuff.

But if this patch works, then I will merge it to master.

brandoningli commented 3 years ago

Ah, race conditions. Gotta love them.

It's been running stably for me now for about ten minutes, which is much longer than before the patch. If it was going to segfault, it would've by now.

brandoningli commented 3 years ago

Hold that thought. When stopping, I got this error:

Traceback (most recent call last):
  File "fake.py", line 380, in <module>
    main()
  File "fake.py", line 377, in main
    cam.run()
  File "fake.py", line 286, in run
    frame = self.compose_frame(frame)
  File "fake.py", line 258, in compose_frame
    frame[:, :, c] = frame[:, :, c] * mask + background_frame[:, :, c] * (1 - mask)
  File "fake.py", line 344, in sigquit_handler
    cam.stop()
  File "fake.py", line 274, in stop
    self.real_cam.stop()
AttributeError: 'RealCam' object has no attribute 'stop'
fangfufu commented 3 years ago

That got patched in https://github.com/fangfufu/Linux-Fake-Background-Webcam/commit/b80e1e9577dcba7873eb394a028107714e42817e

fangfufu commented 3 years ago

Thank you so much for helping out!

fangfufu commented 3 years ago

@brandoningli , btw, if you need help with job applications / Masters applications / PhD course applications, feel free to ping me an email. I saw on your Github profile that you are open for work.

fangfufu commented 3 years ago

Also, what's your CPU, if you don't mind me asking.

brandoningli commented 3 years ago

Intel i5-6300HQ (4) @ 3.200GHz