Using OpenCV with a USB Webcam on a Pi

openCV errors on the pi:

libv4l2 error allocation conversion buffer

select timeout

The title of this is constructed to (hopefully) make it easy to find this for people who are having this issue with opencv on a system with not a lot of memory.  I have moved the celery/flask code over to an actual pi, and this has caused some problems.  I have been using celery to run a little python script every minute to grab an image from a webcam, save it, and then release the webcam.

I would normally get some kind of error with libv4l2.  I have found this post very useful, and this blog entry is just to make this very valuable post more visible.  The solution seems to be to allocate more memory to the ARM processor in raspi-config memory split menu.  You can run raspi-config with:

$sudo raspi-config

This error can also be stopped by basically telling the memory management system to lie and say it always has enough memory.  This can be done with:

$sysctl vm.overcommit_memory=1

and undone with:

$sysctl vm.overcommit_memory=0

which you would want to do if you actually do run low on memory (this is all paraphrased from the link!)


The above was helpful with the lib4l2 error but I was still getting select timeout errors when requesting frames.  This post explains how to fix it by running these bash commands:

$sudo rmmod uvcvideo

$sudo modprobe uvcvideo nodrop=1 timeout=5000 quirk=0x80

I will add more fixes if I have issues, but these links should really all be in one place.

Posted in: ENG

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s