Tuesday 5 November 2013

BeagleBone Black (BBB) and Ubuntu + Python + openCV + BBIO + WiFi

BeagleBone Black (BBB)



My latest "embedded controller" toy is a BeagleBone Black or BBB. I have been playing with it for quite a while now since it was purchased in Aug 2013. The BBB came with preinstalled Linux Angstrom. One thing about Angstrom is it is very fast boot up time, and have simple LAN over USB. To get started, we just need to connect the USB mini cable to a PC, and there you go. It also have a preinstalled "Bonescript", "Cloud9", and BoneCV (small version of OpenCV).

To get started with the BoneCV on BBB, I followed the excellent tutorial by Derek Molloy (link below)

http://derekmolloy.ie/beaglebone/beaglebone-video-capture-and-image-processing-on-embedded-linux-using-opencv/

His tutorial show how to connect to a USB camera, and compile C code to perform some basic image capture and processing from the terminal. This is only text based samples, and to view the captured or processed image, you have to use other apps to open it.

I have also installed VNC server on the Angstrom so I could remote connect and eliminate the HDMI screen, as well as Python.


Since I am not able to run the standard EDGE user interface for Python, I have an SD card installed with Ubuntu image, which was downloaded from below link:

http://www.armhf.com/index.php/boards/neaglebone-black/#wheezy

To install openCV in Ubuntu, resize the SD card and refer to installopencv.sh from desktop.
To install Python Edge, run "sudo -apt -get -y install idle".


Since then, I have also installed Adafruit BBIO library as well as WiFI library. Currently I am able to execute  Python Demo programs, such as "Mosse.py", which is pattern matching image processing sample.



To get this to work with the image size that I want, I slightly modified the "video.py" to have this additional codes (in yellow):

 def create_capture(source = 0, fallback = presets['chess']):
    '''source: <int> or '<int>|<filename>|synth [:<param_name>=<value> [:...]]'
    '''
    source = str(source).strip()
    chunks = source.split(':')
    # hanlde drive letter ('c:', ...)
    if len(chunks) > 1 and len(chunks[0]) == 1 and chunks[0].isalpha():
        chunks[1] = chunks[0] + ':' + chunks[1]
        del chunks[0]

    source = chunks[0]
    try: source = int(source)
    except ValueError: pass
    params = dict( s.split('=') for s in chunks[1:] )

    cap = None

    if source == 'synth':
        Class = classes.get(params.get('class', None), VideoSynthBase)
        try: cap = Class(**params)
        except: pass
    else:
        cap = cv2.VideoCapture(source)
        if 'size' in params:
  
            w, h = map(int, params['size'].split('x'))
            cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, w)
            cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, h)
    if cap is None or not cap.isOpened():
        print 'Warning: unable to open video source: ', source
        if fallback is not None:
            return create_capture(fallback, None)

    cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 320)
    cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 240)   

    return cap



 WiFi Installation
-----------------


To get the WiFi to work, I basically followed the instruction from this page:

http://dotnetdavid.wordpress.com/2013/09/14/beaglebone-black-set-up-wireless-on-ubuntu/

To replace the original file, we need to be in root access. We can either "sudo", or just login as root. To login as root in Ubuntu, run sudo passwd root, and change root password.

To confirm that the WiFi is up and running, run "ifconfig" and ensure that the wlan0 has IP address assigned as below:



 BBIO library
--------------


To install BBIO library, just follow the instruction from this webpage:

http://learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black/installation

Using this library, I can connect an LED to one of the IO pin and blink it.
One thing to note is the library will only work if you login as root, or execute Python with sudo command.

To be continue...