1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| import math
class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self)
self.camProxy = ALProxy("ALVideoDevice", "10.3.1.254", 9559)
self.started = False
self.period = 600
self.precision = 1.0
self.resolution=2
self.colorSpace=11
self.fps=5
self.CARRE_COLOR = [ 1.0, 0.5, 0.2 ] # carre losange and disque are symbols
self.DISC_COLOR = [ 0, 0, 1]
self.LOSANGE_COLOR= [ 0, 1, 0]
self.losangeDirectionAngle=-999.9 # initially unknown
self.losangeElevationAngle=-999.9
self.carreDirectionAngle=-999.9
self.carreElevationAngle=-999.9
self.DisqueDirectionAngle=-999.9
self.DisqueElevationAngle=-999.9
def onUnload(self):
if(self.started == True):
self.started = False
self.camProxy.unsubscribe(self.nameId)
def onInput_onStart(self):
self.gotoAndStop(1)
if(self.started == False):
self.started = True
self.nameId = self.camProxy.subscribe("python_GVM",self.resolution,self.colorSpace,self.fps)
self.im=self.camProxy.getImageLocal(self.nameId)
self.l=self.findColor(self.im, LOSANGE_COLOR, 0.3, losangeDirectionAngle, losangeElevationAngle)
print losangeDirectionAngle
print losangeElevationAngle
self.k=self.findColor(self.im, CARRE_COLOR, 0.3, carreDirectionAngle, carreElevationAngle)
print carreDirectionAngle
print carreElevationAngle
self.m=self.findColor(self.im, DISC_COLOR, 0.3, DisqueDirectionAngle, DisqueElevationAngle)
print DisqueDirectionAngle
print DisqueElevationAngle
self.camProxy.releaseImage(self.nameId)
self.onStopped()
def onInput_onStop(self):
self.onUnload()
# findcolor : find a blob whose rgb components match [R G B] with given tolerance
# return blob center as angles in radian with respect to camera axis
def findcolor(self,p,ref,tolerance,direction,elevation):
self.x=0
self.y=0
self.size=self.camProxy.resolutionToSizes(2)
self.width=self.size[0]
self.height=self.size[1]
self.lenght=self.width*self.height
self.npixels=0
for i in range (0,self.lenght):
self.pixel
self.rgbnorm(p, self.pixel)
if (self.abs(self.pixel[0] - self.ref[0]) < self.tolerance and self.abs(self.pixel[1] - self.ref[1]) < self.tolerance and self.abs(self.pixel[2] - self.ref[2]) < self.tolerance):
self.x += self.i % self.width
self.y += self.i / self.width
self.npixels +=1
self.p+=3
if (self.npixels > 0) :
self.direction = (self.x / self.npixels /self.width - 0.5)
self.elevation = -(self.y / self.npixels /self.height - 0.5)
return 1
else:
self.direction =-999.9
self.elevation = -999.9
return 0
def abs(self,n,m):
if (n>m):
return n-m
else:
return m-n
# rgbnorm : normalize rgb components to be less dependant of lighting
def rgbnorm(self,in,out):
self.max = in[0]
if (in[1] > self.max):
self.max = in[1]
if (in[2] > self.max):
self.max = in[2]
out[0] = in[0] / self.max
out[1] =in[1] /self.max
out[2] =in[2] /self.max |
Partager