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
def showData(self, data: Data): """ To show data if this module start standalone """ if data.source_name == self.ME_NAME + '/Skeleton': person = data.data #Points of skeleton aux = data.strToJSon(data.aux) h = int(Misc.hasKey(aux, 'H', self.ME_CONFIG['FRAME_HEIGHT'])) w = int(Misc.hasKey(aux, 'W', self.ME_CONFIG['FRAME_WIDTH'])) rgbImage = np.zeros((h, w, 3), np.uint8) for idj, join in enumerate(person): if join[0] != None: cv2.circle(rgbImage, (join[0], join[1]), 5, self.colors[idj], thickness=-1) elif data.source_name == self.ME_NAME + '/Person': rgbImage = np.zeros((data.data.shape[0], data.data.shape[1], 3), dtype=np.uint8) rgbImage[:, :, 0] = np.where(data.data[:, :] == 1, 255, 0) rgbImage[:, :, 1] = np.where(data.data[:, :] == 1, 255, 0) rgbImage[:, :, 2] = np.where(data.data[:, :] == 1, 255, 0) else: rgbImage = data.data # For capture the image in RGB color space # Display the resulting frame cv2.imshow(data.source_name + '-' + data.source_item, cv2.cvtColor(rgbImage, cv2.COLOR_BGR2RGB)) if cv2.waitKey(1) & 0xFF == ord('q'): self.stop() cv2.destroyAllWindows()
def getData(self, device, frame=None, time_event=0, event=''): """ Returns a list of tuples like {controller, device, data} with data elements """ dev = self.Devices[device["id"]]['objOfCapture'] dataReturn = [] 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":{}, "time":"{}", "event":"{}"'.format( 'image_rgb', 'png', width, height, time_event, event) + '}' dataRgb = Data() dataRgb.source_type = self.ME_TYPE dataRgb.source_name = self.ME_NAME dataRgb.source_item = deviceName dataRgb.data = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) dataRgb.aux = auxData dataReturn.append(dataRgb) #print(auxData, end='\r') # Display the resulting frame cv2.imshow(dataRgb.source_name + '-' + dataRgb.source_item, frame) if cv2.waitKey(1) & 0xFF == ord('q'): self.stop() cv2.destroyAllWindows() return dataReturn
def start(self, queueMessages: Queue): """ Start load of all device channels """ self.COMMPOOL.logFromCore( Messages.system_channels_connect.format(self.COMMPOOL.URL_BASE), LogTypes.INFO, self.__class__.__name__) self.authoraizedChannels = Misc.hasKey(self.ME_CONFIG, 'AUTHORIZED_CHANNELS', []) self.authoraizedAttachments = Misc.hasKey(self.ME_CONFIG, 'AUTHORIZED_ATTACHMENTS', []) if not os.path.exists("./Channels"): os.makedirs("./Channels") if not os.path.exists(self.ANALYZER_PATH + "/attachments/"): os.makedirs(self.ANALYZER_PATH + "/attachments/") while True: self.COMMPOOL.logFromCore(Messages.channel_searching, LogTypes.INFO, self.__class__.__name__) channelsFolders = Misc.lsFolders("./Channels") for cf in channelsFolders: if not Misc.existsFile("config.yaml", cf): continue config = Misc.readConfig(normpath(cf + "/config.yaml")) comp = Misc.hasKey(self.channels, cf, None) if comp == None or comp.Check != hashlib.md5( str(config).encode('utf-8')).hexdigest(): comp = self.loadChannel(config, cf) self.channels[cf] = comp for _ in range(self.CHECKING_TIME): if not queueMessages.empty(): msj = queueMessages.get() self.putMessagePool(Data().parse(msj, True, True)) for disp in self.POOL_DISPATCHES: try: if disp.sent: if time() - disp.born > Misc.hasKey( self.ME_CONFIG, 'MESSAGES_LIFE', 90): # Keep 90 seconds self.POOL_DISPATCHES.remove(disp) else: disp.channel.notify(disp.message) disp.sent = True #del disp.message except: disp.attempt += 1 if disp.attempt > 2: self.POOL_DISPATCHES.remove(disp) del disp.message sleep(1) self.COMMPOOL.logFromCore(Messages.channel_stop, LogTypes.INFO, self.__class__.__name__)
def analyze(self, data): """ Exec analysis of activity """ d_current = self.Simulated_current_time if self.Simulating else datetime.fromtimestamp( time()) #""" # ONLY for test, remove for production aux_tmp = Data().strToJSon(data.aux) time_tmp = Misc.hasKey(aux_tmp['source_aux'], 'time', '') if time_tmp != '': d_current = datetime.strptime(time_tmp, '%Y-%m-%d %H:%M:%S') self.Simulated_current_time = d_current Simulating_tmp = self.Simulating self.Simulating = True #""" if self.Last_Cleaning_Day < d_current: self.remove_old_data() self.Last_Cleaning_Day = d_current + timedelta(days=1) dataReturn = [] event_name = Misc.hasKey(data.data, 'class', '') #if event_name in ['get out of bed',]: # print('atento') #print('No event' if event_name == '' else event_name, d_current, end=' \r') event_name = '' if event_name.lower() == 'none' else event_name.lower() if self.analyze_event( event_name ) < self.Threshold or event_name in self.ME_CONFIG['ALWAYS_ABNORMAL']: if not event_name in self.ME_CONFIG['ALWAYS_NORMAL']: dataInf = self.data_from_event(Event(d_current, event_name)) dataReturn.append(dataInf) backward_event_list = self.backward_events_detect() for be in backward_event_list: dataInf = self.data_from_event(be, occurred=False) dataInf.package = 'backward-' + str(time()) dataReturn.append(dataInf) expected_event_list = self.expected_events_predict() for ee in expected_event_list: dataInf = self.data_from_event(ee, occurred=False) dataInf.package = 'expected-' + str(time()) dataReturn.append(dataInf) #self.save_knowledge() #""" # ONLY for test, remove for production if time_tmp != '': self.Simulating = Simulating_tmp #""" return dataReturn
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
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
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 data_from_event(self, evnt: Event, occurred: bool = True): """ Build a Data object from an Event object occurred: indicate if event occurred or is backwarded """ dataInf = Data() dataInf.source_type = self.ME_TYPE dataInf.source_name = self.ME_NAME dataInf.source_item = '' if occurred else 'backwarded' dataInf.data = ('' if occurred else 'Missing ') + evnt.Name return dataInf
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 simulateData(self, dataFilter: Data, limit: int = -1, lastTime: float = -1): """ Implement me! :: Allows to simulate data if this module start standalone """ if self.simulationStep == 0: self.file = open(self.SimulatingPath, 'r').readlines() self.file_length = len(self.file) dataReturn = [] if dataFilter.package != '': for target_list in self.file: if len(target_list) < 10: continue dataSimulated = Data() dataSimulated = dataSimulated.parse(target_list, False, True) if dataSimulated.package == dataFilter.package and dataSimulated.source_name == dataFilter.source_name: dataReturn.append(dataSimulated) dataReturn.insert(0, {'queryTime': time()}) return dataReturn if self.simulationStep < self.file_length: if len(self.file[self.simulationStep]) < 10: dataReturn.insert(0, {'queryTime': time()}) self.simulationStep += 1 return dataReturn dataSimulated = Data() dataSimulated = dataSimulated.parse(self.file[self.simulationStep], False, True) if dataSimulated.source_name != dataFilter.source_name: dataReturn.insert(0, {'queryTime': time()}) self.simulationStep += 1 return dataReturn 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 notify(self, data:Data): """ Send data to pool of messages to notify """ self.queueMessages.put(data.toString(dataPlain=True,auxPlain=True))
def start(self): """ Start module """ self.setLoggingSettings(self.loggingLevel) self.preLoad() self.Devices = self.getDeviceList() for device in self.Devices: device['objOfCapture'] = self.initializeDevice(device) self.Running = True Sampling = Misc.hasKey(self.ME_CONFIG, 'SAMPLING', 1) # Sampling rate failedSend = 0 while self.Running: for device in self.Devices: if device in self.InactiveDevices: continue gdList = [] try: t0 = time() if self.Simulating: dsimul = Data() dsimul.source_item = device gdList = self.simulateData(dsimul) else: gdList = self.getData(device) #self.log('Time elapsed to get data: ' + str(round(time() - t0, 4)), logType=LogTypes.DEBUG, item=self.ME_NAME) #print('Time elapsed to get data: ' + str(round(time() - t0, 4))) except: self.log( Messages.controller_error_get, LogTypes.ERROR, 'Device: ' + Misc.hasKey(device, 'name', device['id'])) if not self.Simulating: self.InactiveDevices.append(device) import threading x = threading.Thread(target=self.checkDevice, args=(device, )) x.start() package = Misc.randomString() for data in gdList: try: t0 = time() data.package = package if self.ME_STANDALONE: self.showData(data) else: self.send(data) #self.log('Send data: ' + str(data.source_name), logType=LogTypes.DEBUG, item=self.ME_NAME) #print('Send data:', data.source_name, self.ME_NAME) failedSend = 0 #self.log('Time elapsed to send data: ' + str(round(time() - t0, 4)), logType=LogTypes.DEBUG, item=self.ME_NAME) #print('Time elapsed to send data: ' + str(round(time() - t0, 4))) except: self.log( Messages.controller_error_send, LogTypes.ERROR, 'Device: ' + Misc.hasKey(device, 'name', device['id'])) failedSend += 1 if failedSend > 2: self.stop() break sleep(Sampling)
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 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
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
class FactAnalyzer(Component): """ Generic class that represents all the analyzers that can be loaded. """ MODEL = None CLASSES = [] DATA_FILTER = Data() Limit:int = -1 LastTime:float = -1 LoaderOfChannelsThread:Process = None queueMessages:Queue = None 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 @abc.abstractmethod def preLoad(self): """ Implement me! :: Do anything necessary for processing """ pass @abc.abstractmethod def loadModel(self): """ Loads model """ raise ValueError('Implement me! :: Load the model') def loadChannels(self): """ Loads available channels """ self.log(Messages.system_channels_start, LogTypes.INFO) loc = LoaderOfChannel(self.ME_CONFIG, self.CP) loc.ANALYZER_PATH = self.ME_PATH self.queueMessages = Queue() self.LoaderOfChannelsThread = Process(target=loc.start, args=(self.queueMessages,)) self.LoaderOfChannelsThread.start() #del loc self.log(Messages.system_channels_started, LogTypes.INFO) @abc.abstractmethod def loaded(self): """ Implement me! :: Just after load the model """ pass @abc.abstractmethod def analyze(self, data:Data): """ Implement me! :: Exec prediction to recognize an activity """ raise ValueError('Implement me! :: Exec analyze of activity') def notify(self, data:Data): """ Send data to pool of messages to notify """ self.queueMessages.put(data.toString(dataPlain=True,auxPlain=True))
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
class EventRecognizer(Component): """ Generic class that represents all the activity recognizers modules that can be loaded. """ MODEL = None CLASSES = [] DATA_FILTER = Data() Limit: int = -1 LastTime: float = -1 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.CONTROLLER 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.loaded() self.running = True failedSend = 0 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.recognizer_error_get), LogTypes.ERROR) auxData = '"t":"json", \ "source_id":"{}", "source_type":"{}", "source_name":"{}", "source_item":"{}", \ "source_aux":{}' for objData in gdList[1:]: try: t0 = time() dataPredictedList = self.predict(objData) self.log('Time elapsed to get prediction: ' + str(round(time() - t0, 4)), logType=LogTypes.DEBUG, item=self.ME_NAME) for dataPredicted in dataPredictedList: dataPredicted.source_type = self.ME_TYPE dataPredicted.source_name = self.ME_NAME dataPredicted.package = objData.package dataPredicted.aux = auxData.format( objData.id, objData.source_type, objData.source_name, objData.source_item, '""' if dataPredicted.aux == None else dataPredicted.aux) dataPredicted.aux = '{' + dataPredicted.aux + '}' if self.ME_STANDALONE: self.showData(dataPredicted, objData) else: if Misc.hasKey(dataPredicted.data, 'class', 'none').lower() != 'none': self.send(dataPredicted) self.log('Detected: ' + str(dataPredicted.data), logType=LogTypes.DEBUG, item=self.ME_NAME) print('Detected:', dataPredicted.data, self.ME_NAME) failedSend = 0 except: self.log(Messages.recognizer_error_send, LogTypes.ERROR) failedSend += 1 if failedSend > 2: self.stop() break @abc.abstractmethod def preLoad(self): """ Implement me! :: Do anything necessary for processing """ pass @abc.abstractmethod def loadModel(self): """ Load the model """ raise ValueError('Implement me! :: Load the model') @abc.abstractmethod def loaded(self): """ Implement me! :: Just after load the model """ pass @abc.abstractmethod def predict(self, data: Data): raise ValueError( 'Implement me! :: Exec prediction to recognize an activity')