A camera for my Raspberry Pi

fernand0
3 min readMay 17, 2014

--

Hello, I wanted to check this blogging system and I also wanted to try (again) to do some blogging in English (sort of). It’s been some time since I bought my Raspberry Pi and I wondered if I would be able to report about my projects here. So, here is my first try:

My idea was to attach a webcam to my Raspberry 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 is RPi USB Webcams. From the models shown there (and that where available in a local store near home) I selected the Logitech C270. As I was 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:

http://instagram.com/p/fr__VlQB4a

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.

http://instagram.com/p/lAHWuewBwc

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 have 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:

http://instagram.com/p/j7OFLgQBw_

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).

--

--

fernand0

Tuiteo de memoria. Puede que las cosas no sucedieran realmente así...