From IPRE Wiki
from pyrobot.brain.conx import *
def shrinking(pic):
X = getWidth(pic)
Y = getHeight(pic)
# Input the shrink factor and computer size of new image
F = 8
newx = X/F
newy = Y/F
# create the new image
newPic = makePicture(newx, newy)
for x in range(1, newx):
for y in range(1, newy):
setPixel(newPic, x, y, getPixel(pic, x*F, y*F))
return newPic
def locateObject(picture):
#This is what I'm using to check to see if the neural network returns the
#correct results
tot_x = 0
count = 0
for pixel in getPixels(picture):
r, g, b = getRGB(pixel)
if g == 255:
tot_x = tot_x + int(getX(pixel))
count = count + 1
if count > 0:
return tot_x/count
else:
return -1
def inputPictures():
# take picture and locate the object
pic = takePicture()
show(pic, "normal")
nPic = shrinking(pic)
picList = []
show(nPic)
#I could have just left this as a grayscale image but I already wrote the
#code changing it to black and white. I realize I made more work for myself
for pixel in getPixels(nPic):
r, g, b = getRGB(pixel)
if g > 200 and r < 130:
setRGB(pixel, (255, 255, 255))
picList.append(1)
else:
setRGB(pixel, (0, 0, 0))
picList.append(0)
show(nPic, "changed")
#Object location is not fed into the neural network. This prints out the
#location so that I can check it with the neural network's results
objectLocation = locateObject(nPic)
print objectLocation
if objectLocation < 0:
print "Not in Picture"
elif objectLocation <= 11:
print "Left"
elif objectLocation <= 22:
print "Center"
else:
print "Right"
return picList #returns the list of 0's and 1's for the input
def main():
#FORMAT python
# create the network
n = Network()
# add layers in the order they will be connected
n.addLayer('input',738) # The input layer has # nodes
n.addLayer('output',3) # The output layer has # node
n.connect('input','output') # The input layer is connected to the output layer
# provide training patterns (inputs and outputs)
theInput = []
for i in range (3):
theInput.append(inputPictures())
wait(5)
n.setInputs([theInput[0], theInput[1], theInput[2]])
n.setOutputs([[0.0, 1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]])
# set learning parameters
n.setEpsilon(0.5)
n.setTolerance(0.2)
n.setReportRate(1)
# learn
n.saveWeightsToFile("before.wts")
n.train()
n.saveWeightsToFile("after.wts")
# verify learning
n.setLearning(0)
n.setInteractive(1)
n.sweep()