A camera for my Raspberry Pi

This post was originally published at: A camera for my Raspberry Pi. Not many visits later, and given the fact that I did not enjoy writing there (I’m a xmlrpc man), I’m trying to give that post a second life here.

My first idea was to attach a webcam to my Raspberry Pi and to further use it for a more complex project. This post is for reporting the initial steps with my
camera.

The main reference for buying a camera for the Raspberry was RPi USB Webcams. From the models shown there (and that where available in a local store near home) I selected the Logitech C270. As it was my understanding by that time, it should work directly connected to the USB port. Unfortunately this was not correct (it needs an USB powered HUB; it is not more clearly stated in the info now) and this made me some headaches and frustration.

During the tests I also bought another camera (second hand, this time), that is sold for the Playstation PS4 (if I’m correct). I had read that it did not need a powered hub and it was really cheap (5 euro) so it was worth a try.

We can see a picture of the cameras:

Probando otra cámara (PS3)

A post shared by Fernando Tricas García (@ftricas) on

While it is true that this camera was working better directly connected to
the USB port of the raspi, the machine was not behaving correctly (it is
important to notice that I also had a USB Wifi adapter. I’m using WiFi to
connect via SSH to the computer, and also to have it connected almost
permanently to the Internet).

Image quality with this camera is worse than with the Logitech one. It has
two positions (zoom) that need to be managed by hand (you cannot access
this feature from a program, as far as I know). I’ve kept it as a secondary
camera.

So, after a lot o days testing the cameras I decided to buy a powered USB
HUB, the EasyConnect 7 Port USB2 Powered Hub made by Trust.

Como decía la canción…

A post shared by Fernando Tricas García (@ftricas) on

During this time I was trying the different available options for the camera. It is worth remembering that there exist a camera supported by the project. It is supposed to work perfectly well directly connected to our machine: for me it was a bit expensive (I had bought the Logitech previously), and the connection seems not be very adequate to reuse the camera in other projects. For more information, you can have a look at Raspberry Pi Camera Module.

Some programs for the camera:

Of course, there are more.

But I also discovered the OpenCV project; with it you can access a camera from your programs (in Python, for example): manage several cameras, their parameters, …

You can have a look at an small example, cam.py. The program takes a picture with the camera and stores it in a file whose name is pre-configured. You can provide a name for the file where the image will be stored.

We can comment here some lines of the code:

Definition of the name of the file:

imgFile = ‘/tmp/imagen.png’

It is always the same. We could use something like this:

imgFile = time.strftime(“%Y-%m-%dT%H:%M:%S”, time.gmtime())+’.png’

in order to have several images stored without worrying about the name (be careful with the storage capacity!).

Then, there is code to check if you have provided a filename from the command line (not very robust: it does not validate anything). Later it initializes the camera:

cam=cv2.VideoCapture(0)

For capturing the image we use a small function:

def get_image():
retval, im = cam.read()
return im

Again, no validation is done, it just hopes that everything was ok.

The function is used by this sentence:

img=get_image()

And, finally, the image is written to the file:

cv2.imwrite(imgFile, img)

Hopefully, I will continue writting more things I have done with the camera
soon.

This text is a (sort of) translation of the original one, that has been published at: Una cámara en la Raspberry Pi (in Spanish).

Advertisements

2 thoughts on “A camera for my Raspberry Pi

Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s