1

Using simple Windows/Python to read from Webcam:

camera = iio.get_reader("<video0>")
screenshot = camera.get_data(0)
camera.close()

I'm getting a default resolution of 1980x1920. The webcam had different, larger resolutions available. How do I set that UP?

ALSO - How do I set exposure time? image comes out pretty dark.

Thanks

Gabe
  • 630
  • 1
  • 9
  • 25

1 Answers1

1

You can set the resolution via the size kwarg, e.g. size=(1280, 720)

My webcam is my third device and the resolution defaults to 640x360 but has 1280x720 available, so I would do something like:

import imageio.v3 as iio

frame = iio.imread("<video2>", index=0, size=(1280, 720))

On a tangent, I'd also suggest switching to the easier iio.imiter for stream reading. It tends to produce cleaner code than the old iio.get_reader syntax.

import imageio.v3 as iio

for idx, frame in enumerate(iio.imiter("<video0>", size=(1980, 1920))):
    ... # do something with the frame

    if idx == 9:
        # read 10 frames
        break

Response to your edit:

Setting webcam exposure is a question that actually hasn't come up yet. Webcams typically feature automatic brightness adjustment, but that might take a few frames depending on the webcam's quality.

Manual adjustment might already be possible and I just don't know about it (never looked into it). This is a separate question though and is probably better tracked as a new issue over at the ImageIO repo.

FirefoxMetzger
  • 2,880
  • 1
  • 18
  • 32
  • Thanks for that, @FirefoxMetzger. Could you please explain the "index=0" bit? – Gabe Jul 12 '22 at 09:59
  • 1
    @Gabe The default index of `imread` for videos (and by extension webcams) is `index=...`, which will read all frames in the video and stack them. (Not super ideal for infinite streams like webcams, but very useful for many other scenarios.) To get a specific frame you have explicitly set `index` to that value, e.g. `index=0` to read the first frame. – FirefoxMetzger Jul 12 '22 at 10:11
  • Thanks for that. Two more questions, please: 1. I get an error that I should use imageio_ffmpeg, but that one doesn't have an imread method. 2. Imiter does not seem to produce a matrix or frame. What am I missing? – Gabe Jul 12 '22 at 10:31
  • 1
    @Gabe 1. `imageio_ffmpeg` is an optional dependency that provides a plugin to access FFmpeg. You simply install it and use `iio.imread` as usual. ImageIO will automatically pick up `imageio_ffmpeg` and use it to fulfill the request as needed. 2. Hard to say without seeing the code. In general `iio.imiter` returns a generator that will yield frames, so you'll have to use it inside a `for frame in iio.imiter(...)` statement or do something like `frame = next(frame_generator)` depending on the context. – FirefoxMetzger Jul 12 '22 at 11:28
  • I am reading 8MP images from the webcam and each one is taking ages (~5 seconds). Any way to make it go faster? – Gabe Jul 13 '22 at 05:37
  • 1
    @Gabe Other than improving the performance of the library code itself there is nothing that comes to mind. – FirefoxMetzger Jul 13 '22 at 10:22