Tag Archives: pi camera

myR: The Pi Camera p2

camserver and CamHandler

in order to stream the video on a  browser , I add this camserver object that it takes care to pass the last available image.

Consider this webserver is working on port 9094, You can easily test it by  run:

sudo python camera.py

than typing on a browser:


and (you need to close the previous page first)

They are respectively the last image with result of ball tracking and the result of the filters before search for the contors.

This can help to define which values of HSV are correct to look for green color.This part can be quite tricky, so I created also a html page that can help me by moving slides on it.You can open it in the browser but for the moment it does not apply the change on the mask (this will work only when I will introduce also the server.py and the rc.py -remotecontrol- in the future posts).


In my github you can find now the upload of this part of the code.

PS: Note that now there is a bug that I did not yet solved: since in the camHandler.do_GET there is an infinite loop continuously updating image, you can not open two pages  connected to the same server.You need first to close a webpage than open the new one.


myR: The Pi Camera

The Pi camera is an argument that can take some posts.

The hw installation has been pretty simple , also for me.I follow the indications reported in the  https://www.raspberrypi.org/help/camera-module-setup/   and https://www.raspberrypi.org/documentation/usage/camera/README.md.

Then  I tested it worked using raspivid: https://www.raspberrypi.org/documentation/usage/camera/raspicam/raspivid.md

So, ready to develop my camera.py class.

Requirements are:

  • stream images on a browser
  • recognize  items in the image (ball tracking, as first attempt)
  • stream the result in the browser
  • run the camera code in a parallel thread

My camera.py module  includes 4 objects (classes):

  • camera: dedicated to collect images from camera and elaborate this information, in particular  performing the ball tracking feature.
  • camserver : used to serve the images to the browser.It control the camHandler
  • camHandler: the webserver class that pack data and return it to browser.
  • camera_data: the class including the data to share ( camera parameters , results, images.



It first inits the camera by :

self.camera = picamera.PiCamera()

Then it is possible to start the man loop of the camera by   camera.start().

This loop include  a call to :

  • the procedure  self.configure(). Here it is possible set up the camera parameters by writing a command in the variable  self.data.config. It is useful  for tuning the thresholds for  colors during the  ball tracking.
  • the self.camera.capture(rawCapture, format=’bgr’, use_video_port=True). It just the the picture
  • finally the self.balltracking(image). where it is done the balltracking and returning some information, for example the position of the ball in the image and the estimated distance of the ball respect the rover.


The balltracking is obtained by  using the famous openCV  library: http://docs.opencv.org/2.4/

In order to install this library I found quite a lot complicated  posts googling around, finally I decide to follow this raw method , that worked (at least for me):

sudo apt-get install python-numpy
sudo apt-get install python-scipy
sudo apt-get install python-imaging
sudo apt-get install libopencv-dev
sudo apt-get install python-opencv

The ball tracking  is also a well known  exercise. I used this blog  to start my dev: http://www.pyimagesearch.com/2015/09/14/ball-tracking-with-opencv/



Summing up, the image is first filtered.

Then I decide to search for a green ball, so I setup my HSV parameters (I’ll show how later).

The image than become a black and white image: all the pixel inside my HSV thresholds are set to black , all the rest are set to white.

With cv2.findContours(…)  you can get a list of contours of the “black items” in the image.So , look for the biggest one ( I suppose in the image there is only one green item: my ball), find its center calculate its radius, and estimate the distance (by a proportion). Finally add some info and draw some lines.Done!


In the next post I’ll explain how to expose the images on the web and how to find the right  thresholds for Hue , Saturation and Value (HSV).