[Edu-sig] example brain for collecting images from a robot as it moves around

Jason Cunliffe jason.cunliffe at verizon.net
Sun Aug 10 02:41:40 EDT 2003


http://emergent.brynmawr.edu/pyro/?page=PyroModuleVision

from pyro.brain import Brain
import time

def saveListToFile(ls, file):
    for i in range(len(ls)):
        file.write(str(ls[i]) + " ")
    file.write("\n")

def scaleList(ls, maxval):
    for i in range(len(ls)):
        ls[i] = ls[i] / (1.0 * maxval)
    return ls

class SampleImages(Brain):
    def setup(self):
        self.getRobot().startService("V4LCamera")
        self.camera = self.getRobot().getService("V4LCamera")
        print "done initializing camera"
        #to save image data in a nnet ready format, uncomment the following
line
        #self.cameradat = open("camera.dat", "w")
        self.count = 0

    def step(self):
        if self.count < 10:
            self.camera.update()
            image = self.camera.getShrunkenImage(xscale = 0.5, mode =
'sample')
            #to save image data in a nnet ready format, uncomment the
following line
            #saveListToFile(scaleList(image.data, 255), self.cameradat)
            #to save image data as ppm files, uncomment the following line
            image.saveToFile("image%d.ppm" % self.count)
            self.wander(0.9)
            time.sleep(0.3)
            self.getRobot().stop()
            self.count += 1
            print "step", self.count
        else:
            print "done collecting samples"
            self.getRobot().stop()
            #to save image data in a nnet ready format, uncomment the
following line
            #self.cameradat.close()
            self.pleaseStop()

    def wander(self, minSide):
        robot = self.getRobot()
        front = robot.get('range', 'value', 'front', 'min')[1]
        left = robot.get('range', 'value', 'front-left', 'min')[1]
        right = robot.get('range', 'value', 'front-right', 'min')[1]
        if front < minSide:
            robot.move(0,-0.3)
        elif left < minSide:
            robot.move(0,-0.3)
        elif right < minSide:
            robot.move(0,0.3)
        else:
            robot.move(0.2, 0)

def INIT(engine):
    return SampleImages('SampleImages', engine)




More information about the Edu-sig mailing list