Skip to content

Reading from webcam hangs unreplicably on Linux #227

Closed

Description

@igordertigor

I'm trying to read frames from a webcam using imageio's beautifully simple interface:

import imageio
reader = imageio.get_reader('<video0>')
for i, frame in zip(range(2), reader):
    pass
reader.close()

That seems to work as expected with one exception: In about 20% of the cases, the code hangs while fetching the next frame. I see that when I explicitly read frames using reader.get_next_frame() and also in the traceback I see when I press Ctrl-C after a minute or two:

  File "isolate.py", line 4, in <module>
    for i, frame in zip([1, 2], reader):
  File "/home/ingo/code/orquestra/.venv3/lib/python3.5/site-packages/imageio/core/format.py", line 386, in iter_data
    im, meta = self._get_data(i)
  File "/home/ingo/code/orquestra/.venv3/lib/python3.5/site-packages/imageio/plugins/ffmpeg.py", line 340, in _get_data
    result = self._read_frame()
  File "/home/ingo/code/orquestra/.venv3/lib/python3.5/site-packages/imageio/plugins/ffmpeg.py", line 543, in _read_frame
    s = self._read_frame_data()
  File "/home/ingo/code/orquestra/.venv3/lib/python3.5/site-packages/imageio/plugins/ffmpeg.py", line 519, in _read_frame_data
    s = self._frame_catcher.get_frame()
  File "/home/ingo/code/orquestra/.venv3/lib/python3.5/site-packages/imageio/plugins/ffmpeg.py", line 774, in get_frame
    time.sleep(0.001)
KeyboardInterrupt

Is this a bug or am I missing something important?

Working on ubuntu 16.04.

Activity

  1. igordertigor commented on Feb 24, 2017

    @igordertigor
    Author

    Notably, this seems to work reliably on Mac.

  2. almarklein commented on Feb 24, 2017

    @almarklein
    Member

    Notably, this seems to work reliably on Mac.

    Do you mean that the problem shows up reliably, or that the problem is not present on OS X? :)

    Its likely that imageio is using your system ffmpeg. You could try setting the environment variable IMAGEIO_FFMPEG_EXE to another (newer?) version of ffmpeg to try if that helps.

  3. igordertigor commented on Feb 27, 2017

    @igordertigor
    Author

    Oh, I meant that the problem is not present on OS X. I'll try how things are with the system ffmpeg.

  4. igordertigor commented on Feb 27, 2017

    @igordertigor
    Author

    Hi @almarklein, I just checked your suggestion to set the IMAGEIO_FFMPEG_EXE variable to point to ffmpeg 3.2.4 that I compiled from source. That doesn't seem to solve the problem.

  5. almarklein commented on Mar 1, 2017

    @almarklein
    Member

    Mmm, I tried reproducing this on 2 Linux VM's (Ubuntu 16.04 and Ubuntu Pingulin), and I cannot reproduce the error. Could it be that the webcam is busy, e.g. because its used by another application? Also, does it hang before even reading a frame, or after reading a few frames?

  6. changed the title [-]Reading from webcam hangs unreplicably[/-] [+]Reading from webcam hangs unreplicably on Linux[/+] on Mar 1, 2017
  7. igordertigor commented on Mar 3, 2017

    @igordertigor
    Author

    Strange. The webcam is not used by another process. After occasionally running into it, I made multiple attempts to call the script and in the beginning it usually happened after calling the script multiple times. When I tried setting IMAGEIO_FFMPEG_EXE, I found that it often hung on the first call. But that hasn't been like that before and it isn't reliably the case. So it might be that the previous call to imageio does not do proper cleanup. I can't see anything in the code that would suggest that, though. After all, it seems as if this would mostly be handled by ffmpeg anyway, right?

  8. almarklein commented on Mar 3, 2017

    @almarklein
    Member

    Imageio should shut down ffmpeg when its down. Though that bit of code has changed a few times. Maybe it does not work reliably in your case. Also, if the Python process is killed, imageio does not get a chance to kill the ffmpeg process.

    Do you know what process is using the webcam? If its not ffmpeg, then the above does not apply.

  9. igordertigor commented on Mar 10, 2017

    @igordertigor
    Author

    I'm not quite sure if what I understood you right. At any time there is only a single ffmpeg process that seems to try to read from the webcam. As to be expected, that process changes every time I rerun the above script, but it is never more than one process. Is there another way to track who is attempting to access the webcam?

  10. almarklein commented on Mar 10, 2017

    @almarklein
    Member

    Is there another way to track who is attempting to access the webcam?

    Not that I know of.

    You could try to wait a little after stopping the script, and see if the ffmpeg exe is indeed stopped. If it is not stopped, there is a problem. If the ffmpeg process does stop, and by waiting a bit the problem never occurs, perhaps the ffmpeg process needs more time to stop or something...

  11. almarklein commented on Feb 5, 2019

    @almarklein
    Member

    Termination of the ffmpeg subprocess should be a lot better with #425. I am going to assume that it will fix this error. If not, please reopen!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions