Now when we know how to interface new blocks in Scratch IDE with python and having OpenCV library up and running on OLinuXino it was matter of time to make the new project.
There is already nice project written in Scratch with eyes tracking your mouse pointer: http://scratch.mit.edu/projects/Doody/269924
Also we show you OpenCV object tracking http://www.youtube.com/watch?v=CigGvt3DXIw
Now we decided to combine both in single project:
1. Install scratchpy
# git clone git://github.com/pilliq/scratchpy.git # cd scratchpy !!!IMPORTANT: Make sure that you date is set: # date -s "3 APR 2013" # python setup.py install
2. Install OpenCV
# apt-get install libopencv-dev python-opencv
3. Install Scratch
# apt-get install scratch
4. Open Scratch and open the demo project Eyes.sb
5. A message should appear that remote sensor connection is enabled.
If doesn’t click on “Sensing”, right click on sensor value and click “Enable remote sensor connection”.
6. Open terminal and start the tracking program
# python track.py
7. This will track yellow ball and the eyes will look at this direction
8. If you want to use some other object with different color use config.py.
Start the config:
# python config.py Move the sliders until only the desired color is white and everything else is black. Open track.py and edit the following line: cv.InRangeS(imgHSV, cv.Scalar(0, 100, 255), cv.Scalar(40, 255, 256), imgThreshed)
Write your values, for example:
cv.InRangeS(imgHSV, cv.Scalar(110, 98, 100), cv.Scalar(131, 255, 256), imgThreshed) Then just start the file: # python track.py
If you wonder what does line 77 k = cv.WaitKey(70) this is delay as OpenCV track the object very fast and send coordinates faster than Scratchpy + Scratch can handle which leads to buffer overflow, with this delay the object tracking is artifically delaed to may Scratch have time to update the animation properly.
Here is video of this project: http://www.youtube.com/watch?v=Pyidx-zOsm4
Note that the camera is put on the top of the monitor and displays mirrored object i.e. when you move the heart right the animation moves the eyes to the right but on the camera you see mirrored image 🙂
And the GitHub sources are HERE
Next OpenCV project – Face tracking is in progress, stay tuned 🙂