def predict(self, data: Data):
        """ Exec prediction to recognize an activity """

        rgbFilter = Data()
        rgbFilter.id = None
        rgbFilter.package = data.package
        rgbFilter.source_name = 'CamController'
        rgbFilter.source_type = SourceTypes.CONTROLLER
        rgbData = self.receive(dataFilter=rgbFilter, limit=1, lastTime=0)

        if len(rgbData) < 2:
            return []

        rgbData = rgbData[1]
        img = self.fuzzySilhouetteAndRGB(rgb=rgbData.data, mask=data.data)
        #x = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        x = cv2.resize(img, (256, 256), interpolation=cv2.INTER_AREA)
        x = np.expand_dims(x, axis=0)
        data.data = img

        array = self.MODEL.predict(x)
        result = array[0]
        answer = np.argmax(result)

        if result[answer] < 0.7:
            return []

        dataReturn = []

        dataInf = Data()
        dataInf.source_item = self.CLASSES[answer]
        dataInf.data = {'class': self.CLASSES[answer], 'acc': result[answer]}
        dataReturn.append(dataInf)

        return dataReturn
Beispiel #2
0
    def get(self, parser, sourceType: SourceTypes, origin: str):
        """ Returns all active data. 
                Params: Data
            
            Retrive a object Data with structure:
                {
                    'id'            : self.id,
                    'born'          : self.born,
                    'source_type'   : str(self.source_type),
                    'source_name'   : self.source_name,
                    'source_item'   : self.source_item,
                    'package'       : self.package,
                    'data'          : self.serialize(self.data),
                    'aux'           : self.serialize(self.aux),
                    'state'         : str(self.state),
                },
                limit,
                lastTime,
                all
            
            Controller and device could be an Exp Reg.
            Ex. 'CamController' for single or 'CamController.*?' for all from CamController
        """
        try:
            #TODO: Si en lugar de data se reciben las variables id, package, source_name y source_item
            # como se registra en la documentación. Además el tipo lo debe sacar de la url que se usa
            parser = reqparse.RequestParser()
            parser.add_argument('data')
            parser.add_argument('limit')
            parser.add_argument('lastTime')
            parser.add_argument('onlyActive')
            args = parser.parse_args()

            dataIn = args.data
            dataIn = '' if dataIn == None else dataIn
            data = Data()
            data.id = '-1'
            if dataIn != '':
                data = data.parse(dataIn)

            limit = args.limit
            lastTime = args.lastTime
            onlyActive = True if args.onlyActive == '' else Misc.toBool(
                args.onlyActive)

            result = DataPool().getPool(data=data,
                                        limit=limit,
                                        lastTime=lastTime,
                                        onlyActive=onlyActive)

            result = list(map(lambda d: d.getJson(), result))
            result.insert(0, {'msg': 'ok', 'queryTime': time()})
        except:
            message = Binnacle().errorDetail(Messages.error_pool_reading)
            Binnacle().logFromCore(message, LogTypes.ERROR, origin)
            result = [{'msg': message}]
        return result
Beispiel #3
0
    def predict(self, data: Data):
        """ Exec prediction to recognize an activity """
        rgbFilter = Data()
        rgbFilter.id = None
        rgbFilter.package = data.package
        rgbFilter.source_name = 'CamController'
        rgbFilter.source_type = SourceTypes.CONTROLLER
        rgbData = self.receive(dataFilter=rgbFilter, limit=1, lastTime=0)

        if len(rgbData) < 2:
            return []
        rgbData = rgbData[1]

        dataReturn = []

        per = data.data
        if per[0].any() == None or per[1].any() == None or per[2].any(
        ) == None or per[5].any() == None:
            return []

        skeletonResult = self.predict_skeleton(person=per)

        img = self.extract_face(frame=rgbData.data, person=per)
        faceResult = self.predict_face(frame=img)

        array = self.predict_full(skeletonResult, faceResult)
        result = array[0]
        answer = np.argmax(result)

        if result[answer] < 0.65:
            return []
        #print(result[answer], skeletonResult, faceResult)

        if answer > 0:
            dataInf = Data()
            dataInf.source_item = self.CLASSES[answer]
            dataInf.data = {
                'class': self.CLASSES[answer],
                'acc': result[answer]
            }
            dataReturn.append(dataInf)

        return dataReturn
Beispiel #4
0
    def post(self, parser, sourceType: SourceTypes, origin: str):
        """ Load data in pool. Params:
                data: Data
            
            Retrive a object Data with structure:
                {
                    'id'            : self.id,
                    'born'          : self.born,
                    'source_type'   : str(self.source_type),
                    'source_name'   : self.source_name,
                    'source_item'   : self.source_item,
                    'package'       : self.package,
                    'data'          : self.serialize(self.data),
                    'aux'           : self.serialize(self.aux),
                    'state'         : str(self.state),
                }
        """
        try:
            #TODO: Validar tipo de dato y si no coiside emitir error
            #TODO: E ltipo de dato esperado lo debe sacar de la clase en su instanciación
            parser = reqparse.RequestParser()
            parser.add_argument('data')
            args = parser.parse_args()

            dataIn = args.data
            dataIn = '' if dataIn == None else dataIn
            data = Data()
            data.id = -1
            if dataIn != '':
                data = data.parse(dataIn)
                if sourceType != SourceTypes.parse(data.source_type):
                    raise ValueError(
                        Messages.bad_source_type_deteiled.format(
                            sourceType, SourceTypes.parse(data.source_type)))
                DataPool().append(data)
                DataPool().pop()

            result = [{'msg': 'ok', 'id': data.id}]
        except:
            message = Binnacle().errorDetail(Messages.error_pool_writing)
            Binnacle().logFromCore(message, LogTypes.ERROR, origin)
            result = [{'msg': message}]
        return result
Beispiel #5
0
 def post(self):
     """ Register message in Binnacle """
     parser = reqparse.RequestParser()
     parser.add_argument('data')
     #parser.add_argument('data.data')
     #parser.add_argument('data.aux')
     parser.add_argument('logType')
     parser.add_argument('isCore')
     args = parser.parse_args()
     dataIn = args.data
     dataIn = '' if dataIn == None else dataIn
     data = Data()
     data.id = -1
     if dataIn != '':
         data = data.parse(dataIn, dataPlain=True, auxPlain=True)
         if Misc.toBool(args.isCore):
             Binnacle().logFromCore(data.data, LogTypes.parse(args.logType),
                                    data.aux)
         else:
             Binnacle().logFromComponent(data, LogTypes.parse(args.logType))
    def putMessagePool(self, data: Data):
        """ Put new messages into pool to send then """
        try:
            d = Dispatch()

            d.tokens = {}
            d.tickets = []
            d.events = []
            d.alerts = []
            d.files = []
            d.alerts.append(data)

            dataRecognizer = []
            auxAnalyzer = data.strToJSon(data.aux)
            filterRecognizer = Data()
            filterRecognizer.id = ''
            filterRecognizer.package = Misc.hasKey(auxAnalyzer,
                                                   'source_package', '-')
            filterRecognizer.source_type = SourceTypes.RECOGNIZER
            dataRecognizer = self.COMMPOOL.receive(data=filterRecognizer,
                                                   limit=-1,
                                                   lastTime=0,
                                                   onlyActive=False)

            if len(dataRecognizer) == 0:
                return
            event = Data()
            for ev in dataRecognizer[1:]:
                #event = Data().fromDict(ev)
                d.events.append(ev)

            packages = []
            for ev in d.events:
                if not ev.package in packages:
                    packages.append(ev.package)

            for pck in packages:
                dataController = []
                filterController = Data()
                filterController.id = ''
                filterController.package = pck
                filterController.source_type = SourceTypes.CONTROLLER
                dataController = self.COMMPOOL.receive(data=filterController,
                                                       limit=-1,
                                                       lastTime=0,
                                                       onlyActive=False)
                for ticket in dataController[1:]:
                    d.tickets.append(ticket)
                    for attallow in self.authoraizedAttachments:
                        attsallow = attallow.split(':')
                        if len(attsallow) == 3 and \
                            (attsallow[0] == '*' or SourceTypes.parse(attsallow[0]) == ticket.source_type) and \
                            (attsallow[1] == '*' or attsallow[1] == ticket.source_name) and \
                            (attsallow[2] == '*' or attsallow[2] == ticket.source_item):
                            f = ticket.toFile(path="./" + self.ANALYZER_PATH)
                            if f != '':
                                d.files.append(f)

            # Tokens list
            t_s = time() - (5 * 60 * 60)
            data.born -= (5 * 60 * 60)
            d.tokens['server_time'] = t_s
            d.tokens['server_time_human'] = Misc.timeToString(t_s, '%H:%M')
            d.tokens['analyzer_source_name'] = data.source_name
            d.tokens['analysis_time'] = data.born
            d.tokens['analysis_time_human'] = Misc.timeToString(
                data.born, '%H:%M')
            d.tokens['analysis_data'] = data.data
            d.tokens['analysis_aux'] = data.aux
            d.tokens['analysis_id'] = data.id
            d.tokens['event_data'] = data.data if len(
                d.events) == 0 else d.events[0].source_item
            d.tokens['recognizer_source_id'] = '' if len(
                d.events) == 0 else ','.join([str(x.id) for x in d.events])
            d.tokens['recognizer_source_id_0'] = '' if len(
                d.events) == 0 else d.events[0].id if len(d.events) > 0 else ''
            d.tokens['recognizer_source_id_1'] = '' if len(
                d.events) == 0 else d.events[1].id if len(d.events) > 1 else ''
            d.tokens['recognizer_source_id_2'] = '' if len(
                d.events) == 0 else d.events[2].id if len(d.events) > 2 else ''
            d.tokens['recognizer_source_name'] = '' if len(
                d.events) == 0 else ','.join(
                    [str(x.source_name) for x in d.events])
            d.tokens['recognizer_source_name_0'] = '' if len(
                d.events) == 0 else d.events[0].source_name if len(
                    d.events) > 0 else ''
            d.tokens['recognizer_source_name_1'] = '' if len(
                d.events) == 0 else d.events[1].source_name if len(
                    d.events) > 1 else ''
            d.tokens['recognizer_source_name_2'] = '' if len(
                d.events) == 0 else d.events[2].source_name if len(
                    d.events) > 2 else ''
            d.tokens['recognizer_source_item'] = '' if len(
                d.events) == 0 else ','.join(
                    [str(x.source_item) for x in d.events])
            d.tokens['recognizer_source_item_0'] = '' if len(
                d.events) == 0 else d.events[0].source_item if len(
                    d.events) > 0 else ''
            d.tokens['recognizer_source_item_1'] = '' if len(
                d.events) == 0 else d.events[1].source_item if len(
                    d.events) > 1 else ''
            d.tokens['recognizer_source_item_2'] = '' if len(
                d.events) == 0 else d.events[2].source_item if len(
                    d.events) > 2 else ''
            d.tokens['controller_source_id'] = '' if len(
                d.tickets) == 0 else ','.join([str(x.id) for x in d.tickets])
            d.tokens['controller_source_id_0'] = '' if len(
                d.tickets) == 0 else d.tickets[0].id if len(
                    d.tickets) > 0 else ''
            d.tokens['controller_source_id_1'] = '' if len(
                d.tickets) == 0 else d.tickets[1].id if len(
                    d.tickets) > 1 else ''
            d.tokens['controller_source_id_2'] = '' if len(
                d.tickets) == 0 else d.tickets[2].id if len(
                    d.tickets) > 2 else ''
            d.tokens['controller_source_name'] = '' if len(
                d.tickets) == 0 else ','.join(
                    [str(x.source_name) for x in d.tickets])
            d.tokens['controller_source_name_0'] = '' if len(
                d.tickets) == 0 else d.tickets[0].source_name if len(
                    d.tickets) > 0 else ''
            d.tokens['controller_source_name_1'] = '' if len(
                d.tickets) == 0 else d.tickets[1].source_name if len(
                    d.tickets) > 1 else ''
            d.tokens['controller_source_name_2'] = '' if len(
                d.tickets) == 0 else d.tickets[2].source_name if len(
                    d.tickets) > 2 else ''
            d.tokens['controller_source_item'] = '' if len(
                d.tickets) == 0 else ','.join(
                    [str(x.source_item) for x in d.tickets])
            d.tokens['controller_source_item_0'] = '' if len(
                d.tickets) == 0 else d.tickets[0].source_item if len(
                    d.tickets) > 0 else ''
            d.tokens['controller_source_item_1'] = '' if len(
                d.tickets) == 0 else d.tickets[1].source_item if len(
                    d.tickets) > 1 else ''
            d.tokens['controller_source_item_2'] = '' if len(
                d.tickets) == 0 else d.tickets[2].source_item if len(
                    d.tickets) > 2 else ''

            event_time = d.tokens['analysis_time_human']
            if len(d.events) > 0:
                aux_event0 = d.events[0].strToJSon(d.events[0].aux)
                if Misc.hasKey(aux_event0, 'source_aux', '') != '':
                    event_time = aux_event0['source_aux']['time']
                    event_time = datetime.strptime(event_time,
                                                   '%Y-%m-%d %H:%M:%S')
                    event_time = event_time.timestamp()
                    event_time -= (5 * 60 * 60)
                    event_time = Misc.timeToString(event_time, '%H:%M')

            d.tokens['analysis_phrase'] = 'At ' + event_time
            d.tokens['analysis_phrase'] += ' some ' + data.data
            d.tokens['analysis_phrase'] = d.tokens['analysis_phrase'] if len(
                d.events
            ) == 0 else d.tokens['analysis_phrase'] + ' with ' + str(
                round(d.events[0].data['acc'] *
                      100, 2)) + '% of accuracy was detected'
            d.tokens['analysis_phrase'] = d.tokens['analysis_phrase'] if len(
                d.events) == 0 else d.tokens[
                    'analysis_phrase'] + ' by ' + d.events[0].source_name
            d.tokens['analysis_phrase'] += '.'

            for c in self.channels:
                chnl = self.channels[c]
                if chnl.ENABLED:
                    if len(
                            self.authoraizedChannels
                    ) > 0 and chnl.ME_NAME not in self.authoraizedChannels:
                        continue  # Skip not authorized channels

                    msg = d.copy()
                    msg.to = Misc.hasKey(chnl.ME_CONFIG, 'TO', '')
                    msg.message = Misc.hasKey(chnl.ME_CONFIG, 'MESSAGE', '')
                    crr = Carrier(msg, chnl)
                    crr.message.message = crr.message.replace_tokens(
                        crr.message.message)

                    existsCarrier = False
                    for crrDis in self.POOL_DISPATCHES:
                        if crrDis.equals(crr):
                            existsCarrier = True
                            break
                    if not existsCarrier:
                        self.POOL_DISPATCHES.append(crr)
        except:
            dataE = Data()
            dataE.source_type = SourceTypes.ANALYZER
            dataE.source_name = 'LoaderOfChannel'
            dataE.source_item = ''
            dataE.data = self.COMMPOOL.errorDetail(
                Messages.channel_error_put_msg)
            dataE.aux = ''
            self.COMMPOOL.logFromCore(dataE, LogTypes.ERROR,
                                      self.__class__.__name__)