def log(self, msg: str, logType: LogTypes, item: str = ''): """ Allows send message to Binnacle """ dataLog = Data() dataLog.source_type = self.ME_TYPE dataLog.source_name = self.ME_NAME dataLog.source_item = item dataLog.data = '{}'.format(msg) dataLog.aux = self.__class__.__name__ if logType.value > LogTypes.WARNING.value: exc_type, exc_obj, exc_tb = sys.exc_info() fname = exc_tb.tb_frame.f_code.co_filename dataLog.data += ' == {} :: {} :: {} :: {}'.format( exc_obj, exc_type, fname, exc_tb.tb_lineno) self.BC.logFromComponent(dataLog, logType) if not self.ME_STANDALONE: self.CP.logFromComponent(dataLog, logType)
def simulateData(self, dataFilter: Data, limit: int = -1, lastTime: float = -1): """ Allows simulate input data """ if self.simulationStep == 0: self.file = np.loadtxt(self.SimulatingPath, dtype=np.object, delimiter=',') self.file_length = len(self.file) dataReturn = [] if self.simulationStep < self.file_length: if len(self.file[self.simulationStep, 2]) < 3: dataReturn.insert(0, {'queryTime': time()}) self.simulationStep += 1 #print(' -- no -- ') return dataReturn frame = cv2.imread(self.file[self.simulationStep, 1]) height = np.size(frame, 0) width = np.size(frame, 1) time_event = self.file[self.simulationStep, 3] event = self.file[self.simulationStep, 2] auxData = '{' + '"t":"{}", "ext":"{}", "W":{}, "H":{}, "time":"{}", "event":"{}"'.format( 'image_rgb', 'png', width, height, time_event, event) + '}' dataSimulated = Data() dataSimulated.source_name = 'SimulImageController' dataSimulated.source_type = SourceTypes.CONTROLLER dataSimulated.source_item = '' dataSimulated.data = frame dataSimulated.aux = auxData self.simulationStep += 1 dataReturn.append(dataSimulated) dataReturn.insert(0, {'queryTime': time()}) else: self.simulationStep = 0 dataReturn = self.simulateData(dataFilter) return dataReturn
def predict(self, data: Data): """ Exec prediction to recognize an activity """ dataReturn = [] d = data.strToJSon(data.aux) if Misc.hasKey(d, 'time', '') == '' or Misc.hasKey(d, 'event', '') == '': return dataReturn time_event = d['time'] event = d['event'] auxData = '{' + '"time":"{}", "event":"{}"'.format(time_event, event) + '}' dataSimulated = Data() dataSimulated.source_name = self.ME_NAME dataSimulated.source_type = SourceTypes.RECOGNIZER dataSimulated.source_item = event dataSimulated.data = {'class': event, 'acc': 1.0} dataSimulated.aux = auxData dataReturn.append(dataSimulated) return dataReturn
def start(self): """ Start module isolated """ self.DATA_FILTER.id = None self.DATA_FILTER.package = Misc.hasKey(self.ME_CONFIG, 'FILTER_PACKAGE', '') self.DATA_FILTER.source_type = SourceTypes.RECOGNIZER self.DATA_FILTER.source_name = Misc.hasKey(self.ME_CONFIG, 'FILTER_NAME', '') self.DATA_FILTER.source_item = Misc.hasKey(self.ME_CONFIG, 'FILTER_ITEM', '') self.Limit = Misc.hasKey(self.ME_CONFIG, 'FILTER_LIMIT', -1) self.CLASSES = Misc.hasKey(self.ME_CONFIG, 'CLASSES', []) self.setLoggingSettings(self.loggingLevel) self.preLoad() self.loadModel() self.loadChannels() self.loaded() self.running = True failedSend = 0 lastAnalizedTime = time() - 60 while self.running: gdList = [] try: if self.Simulating: gdList = self.simulateData(self.DATA_FILTER) else: gdList = self.receive(self.DATA_FILTER, limit=self.Limit, lastTime=self.LastTime) self.LastTime = float(gdList[0]['queryTime']) except: self.log(self.CP.errorDetail(Messages.analyzer_error_get), LogTypes.ERROR) auxData = '"t":"json", \ "source_id":"{}", "source_type":"{}", "source_name":"{}", "source_item":"{}", \ "source_package":"{}", "source_aux":"{}"' if time() - lastAnalizedTime > 60 * 1: # 1 minute dataNoEvent = Data() dataNoEvent.data = '' dataNoEvent.aux = '{"no_event":"no event", "source_aux":{"no_event":"no event"} }' gdList.append(dataNoEvent) for objData in gdList[1:]: try: lastAnalizedTime = time() t0 = time() dataAnalizedList = self.analyze(objData) #self.log('Time elapsed to get prediction: ' + str(round(time() - t0, 4)), logType=LogTypes.DEBUG, item=self.ME_NAME) #print('Time elapsed to get prediction: ' + str(round(time() - t0, 4)), end='\r') for dataAnalized in dataAnalizedList: dataAnalized.source_type = self.ME_TYPE dataAnalized.source_name = self.ME_NAME if dataAnalized.package == '' or dataAnalized.package == None: dataAnalized.package = objData.package if dataAnalized.aux == '' or dataAnalized.aux == None: dataAnalized.aux = auxData.format(objData.id, objData.source_type, objData.source_name, objData.source_item, dataAnalized.package, dataAnalized.aux) dataAnalized.aux = '{' + dataAnalized.aux + '}' if self.ME_STANDALONE: self.showData(dataAnalized, objData) else: if dataAnalized.data != '': print(time(),': Notifing a', dataAnalized.data) self.notify(dataAnalized) self.send(dataAnalized) failedSend = 0 except: self.log(Messages.analyzer_error_send, LogTypes.ERROR) failedSend += 1 if failedSend > 2: self.stop() break
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__)
def getData(self, device, frame=None, time_event=0, event=''): """ Returns a list of Data objects """ dataReturn = [] try: cam = self.Devices[device["id"]]['objOfCapture'] if frame is None: _, frame = cam.read() if frame is None: return [] height = np.size(frame, 0) width = np.size(frame, 1) deviceName = Misc.hasKey(device, 'name', device["id"]) auxData = '"t":"{}", "ext":"{}", "W":"{}", "H":"{}"' if self.getRGB: dataRgb = Data() dataRgb.source_type = self.ME_TYPE dataRgb.source_name = self.ME_NAME dataRgb.source_item = deviceName #dataRgb.data = frame dataRgb.data = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) dataRgb.aux = auxData.format('image_rgb', 'png', width, height) if time_event != 0: dataRgb.aux += ', "time":"{}", "event":"{}"'.format( time_event, event) dataRgb.aux = '{' + dataRgb.aux + '}' dataReturn.append(dataRgb) if self.getGray: dataGray = Data() dataGray.source_type = self.ME_TYPE dataGray.source_name = self.ME_NAME + '/Gray' dataGray.source_item = deviceName dataGray.data = self.preProcGray(frame) dataGray.aux = '{' + auxData.format('image_rgb', 'png', width, height) + '}' dataReturn.append(dataGray) if self.getObject: auxPer = auxData.format('image_binary', 'png', width, height) auxPer += ',"ClassName":"{}", "backColor":{}, "Y1":{}, "X1":{}, "Y2":{}, "X2":{}' objs = self.preProcObject(frame) for obj in objs: dataPerson = Data() dataPerson.source_type = self.ME_TYPE dataPerson.source_name = self.ME_NAME + '/Person' dataPerson.source_item = deviceName dataPerson.data = obj.Mask dataPerson.aux = '{' + auxPer.format( obj.ClassName, obj.backColor, obj.Y1, obj.X1, obj.Y2, obj.X2) + '}' dataReturn.append(dataPerson) if self.getSkeleton and not self.joinsBodyNET is None: sklts = self.preProcSkeleton(frame) for sk in sklts: dataSkeleton = Data() dataSkeleton.source_type = self.ME_TYPE dataSkeleton.source_name = self.ME_NAME + '/Skeleton' dataSkeleton.source_item = deviceName dataSkeleton.data = sk dataSkeleton.aux = '{' + auxData.format( 'csv', 'csv', width, height) + '}' dataReturn.append(dataSkeleton) for data in dataReturn: self.showData(data) except: self.log('Fail getting data', LogTypes.ERROR, 'Device: ' + Misc.hasKey(device, 'name', device['id'])) return dataReturn