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 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 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 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 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 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