예제 #1
0
    def respond(self, command, reply):
        # this method responds to samSupervisor commands
        b = yarp.Bottle()
        reply.clear()
        action = command.get(0).asString()
        print(action + ' received')
        print 'responding to ' + action + ' request'
        if action == "reload":
            # send a message to the interaction model to check version of currently loaded model
            # and compare it with that stored on disk. If model on disk is more recent reload model
            # interaction model to return "model reloaded correctly" or "loaded model already up to date"
            print "reloading model"
            try:
                self.mm = SAM_utils.initialiseModels(
                    [self.dataPath, self.modelPath, self.driverName], 'update',
                    'interaction')
                reply.addString('ack')
            except:
                reply.addString('nack')
        # -------------------------------------------------
        elif action == "EXIT":
            reply.addString('ack')
            self.close()
        # -------------------------------------------------
        elif action in self.callSignList:
            if 'label' in action and self.dataInConnected:
                self.classifyInstance(reply)
            elif 'instance' in action and self.dataOutConnected:
                self.generateInstance(reply)
        # -------------------------------------------------
        else:
            reply.addString("nack")

        return True
예제 #2
0
    def configure(self, rf):

        print sys.argv
        stringCommand = 'from SAM.SAM_Drivers import ' + sys.argv[
            4] + ' as Driver'
        print stringCommand
        exec stringCommand

        self.mm = [Driver()]
        self.dataPath = sys.argv[1]
        self.modelPath = sys.argv[2]
        self.driverName = sys.argv[4]
        self.configPath = sys.argv[3]

        off = 17
        print '-------------------'
        print 'Interaction Settings:'
        print
        print 'Data Path: '.ljust(off), self.dataPath
        print 'Model Path: '.ljust(off), self.modelPath
        print 'Config Path: '.ljust(off), self.configPath
        print 'Driver:'.ljust(off), self.driverName
        print '-------------------'
        print 'Configuring Interaction...'
        print

        self.mm = SAM_utils.initialiseModels(
            [self.dataPath, self.modelPath, self.driverName], 'update',
            'interaction')

        # parse settings from config file
        parser2 = SafeConfigParser()
        parser2.read(self.configPath)
        self.portNameList = parser2.items(self.dataPath.split('/')[-1])
        print self.portNameList
        self.portsList = []
        for j in range(len(self.portNameList)):
            if self.portNameList[j][0] == 'rpcbase':
                self.portsList.append(yarp.Port())
                self.portsList[j].open(self.portNameList[j][1] + ":i")
                self.svPort = j
                self.attach(self.portsList[j])
            elif self.portNameList[j][0] == 'callsign':
                # should check for repeated call signs by getting list from samSupervisor
                self.callSignList = self.portNameList[j][1].split(',')
            elif self.portNameList[j][0] == 'collectionmethod':
                self.collectionMethod = self.portNameList[j][1].split(' ')[0]
                try:
                    if self.mm[0].model_mode != 'temporal':
                        self.bufferSize = int(
                            self.portNameList[j][1].split(' ')[1])
                    elif self.mm[0].model_mode == 'temporal':
                        self.bufferSize = self.mm[0].windowSize
                except ValueError:
                    print 'collectionMethod bufferSize is not an integer'
                    print 'Should be e.g: collectionMethod = buffered 3'
                    return False

                if self.collectionMethod not in [
                        'buffered', 'continuous', 'future_buffered'
                ]:
                    print 'collectionMethod should be set to buffered / continuous / future_buffered'
                    return False
            else:
                parts = self.portNameList[j][1].split(' ')
                print parts

                if parts[1].lower() == 'imagergb':
                    self.portsList.append(yarp.BufferedPortImageRgb())
                    self.portsList[j].open(parts[0])

                elif parts[1].lower() == 'imagemono':
                    self.portsList.append(yarp.BufferedPortImageMono())
                    self.portsList[j].open(parts[0])

                elif parts[1].lower() == 'bottle':
                    self.portsList.append(yarp.BufferedPortBottle())
                    self.portsList[j].open(parts[0])

                else:
                    print 'Data type ', parts[1], 'for ', self.portNameList[j][
                        0], ' unsupported'
                    return False
                # mrd models with label/instance training will always have:
                # 1 an input data line which is used when a label is requested
                # 2 an output data line which is used when a generated instance is required
                if parts[0][-1] == 'i':
                    self.labelPort = j
                elif parts[0][-1] == 'o':
                    self.instancePort = j

        if self.svPort is None or self.labelPort is None or self.instancePort is None:
            print 'Config file properties incorrect. Should look like this:'
            print '[Actions]'
            print 'dataIn = /sam/actions/actionData:i Bottle'
            print 'dataOut = /sam/actions/actionData:o Bottle'
            print 'rpcBase = /sam/actions/rpc'
            print 'callSign = ask_action_label, ask_action_instance'
            print 'collectionMethod = buffered 3'

        # self.mm[0].configInteraction(self)
        self.inputType = self.portNameList[self.labelPort][1].split(
            ' ')[1].lower()
        self.outputType = self.portNameList[self.labelPort][1].split(
            ' ')[1].lower()
        self.dataList = []
        self.classificationList = []
        yarp.Network.init()

        self.test()

        return True