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
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:
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
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.
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:
- motion (see Conectar una Webcam con Motion en Raspberry Pi -in Spanish-; I have left pending for the future the motion-detection
- fswebcam (see Timelapse 24 horas con Raspberry Pi + Webcam USB -in Spanish-). You can see a selfie taken with this tool:http://instagram.com/p/j7OFLgQBw_
- webcam (see Comment transformer votre Raspberry Pi en serveur webcam? -this time in French-)
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:
For capturing the image we use a small function:
retval, im = cam.read()
Again, no validation is done, it just hopes that everything was ok.
The function is used by this sentence:
And, finally, the image is written to the file:
Hopefully, I will continue writting more things I have done with the camera
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).