def __init__(self, device_config): self.device_config = device_config self.reported_temp_alert = False self.fallback_activated = False self.boot_time = Common.getCurrentUTCTime() self.ip_address = self.getIpAddress() self.weather = Weather() self.airCondition = AirCondition(device_config) self.temperature = Temperature(device_config) if device_config.cloud == "firebase": self.hub = Firebase(self, device_config) elif device_config.cloud == "azure": self.hub = Azure(self, device_config) else: raise Exception( "Supported cloud services are 'azure' and 'firebase'. Update 'device_config.json'." ) try: with open('desired_state.json', 'r') as f: self.desired = json.load(f) except Exception as e: print("Expection while reading saved desired state: " + str(e)) self.wash_desired() self.set_fallback_date()
def __init__(self): self.ROOT_PATH = os.path.dirname(sys.modules['__main__'].__file__) self.azureService = Azure() self.image = None self.packing = Packing() self.fileName = ["personGroupCloud.pckl"] self.personGroup = self.packing.unpack(self.fileName[0])
def __init__(self): self.ROOT_PATH = os.path.dirname(sys.modules['__main__'].__file__) self.azureService = Azure() self.codeError = 0 self.gotAttributes = False self.frame = None self.framesTrain = None self.bb_service = [] # self.service = None self.name = "desconocido" self.id_debug = None self.faceId = "" self.accuracy = None self.age = 0 # self.gender = "" #male-female self.smile = 0 #0-1 self.pose = {} #dict pitch, roll, yaw (double) self.emotion = None #anger, contempt, disgust, fear, happiness, neutral, sadness, surprise self.beard = 0 #0-1 self.glasses = "" #glasses - noglasses self.eyesOpen = 0 self.mouthOpen = 0 self.bald = 0 #número self.hairColor = "" #brown, blond, red, gray, other, black self.sideburns = 0 #0-1 self.eyeMakeUp = False #true-false self.lipMakeUp = False #true-false self.headWear = 0 #0-1 self.mask = 0 self.mustache = 0 #0-1 self.bb_actual = [] self.image_actual = None self.imageBBPair = ([], None) self.happiness = 0 self.sadness = 0 self.neutral = 0 self.surprise = 0 self.anger = 0 self.table = 0 self.country = "" self.id = "" self.id_azure = "" self.interaction = 0 self.lastInteractionTime = 0 self.G = Group() self.bb = []
def __init__(self): self.ROOT_PATH = os.path.dirname(sys.modules['__main__'].__file__) self.azureService = Azure() self.codeError = 0 self.gotAttributes = False self.frame = None self.framesTrain = None self.bb_service = [] # self.service = None self.name = "desconocido" self.id_debug = None self.faceId = "" self.accuracy = None self.age = 0 self.gender = "" self.smile = 0 self.pose = {} self.emotion = None self.beard = 0 self.glasses = "" self.eyesOpen = 0 self.mouthOpen = 0 self.bald = 0 self.hairColor = "" self.sideburns = 0 self.eyeMakeUp = False self.lipMakeUp = False self.headWear = 0 self.mask = 0 self.mustache = 0 self.bb_actual = [] self.image_actual = None self.imageBBPair = ([], None) self.happiness = 0 self.sadness = 0 self.neutral = 0 self.surprise = 0 self.anger = 0 self.table = 0 self.country = "" self.id = "" self.id_azure = "" self.interaction = 0 self.lastInteractionTime = 0 self.image = None self.G = Group()
class Person: def __init__(self): self.ROOT_PATH = os.path.dirname(sys.modules['__main__'].__file__) self.azureService = Azure() self.codeError = 0 self.gotAttributes = False self.frame = None self.framesTrain = None self.bb_service = [] # self.service = None self.name = "desconocido" self.id_debug = None self.faceId = "" self.accuracy = None self.age = 0 # self.gender = "" #male-female self.smile = 0 #0-1 self.pose = {} #dict pitch, roll, yaw (double) self.emotion = None #anger, contempt, disgust, fear, happiness, neutral, sadness, surprise self.beard = 0 #0-1 self.glasses = "" #glasses - noglasses self.eyesOpen = 0 self.mouthOpen = 0 self.bald = 0 #número self.hairColor = "" #brown, blond, red, gray, other, black self.sideburns = 0 #0-1 self.eyeMakeUp = False #true-false self.lipMakeUp = False #true-false self.headWear = 0 #0-1 self.mask = 0 self.mustache = 0 #0-1 self.bb_actual = [] self.image_actual = None self.imageBBPair = ([], None) self.happiness = 0 self.sadness = 0 self.neutral = 0 self.surprise = 0 self.anger = 0 self.table = 0 self.country = "" self.id = "" self.id_azure = "" self.interaction = 0 self.lastInteractionTime = 0 self.G = Group() self.bb = [] def frame2bytes(self, frame): retval, encoded_image = cv2.imencode('.png', frame) return encoded_image.tobytes() def check_img(self, frame): if type(frame) != bytes: return self.frame2bytes(frame) else: return frame def enrol(self, id, frames): person_id, self.codeError = self.azureService.create_person(id) success_list = [] if person_id is not None: self.id_azure = person_id #print('enrol'+str(len(frames))) for frame in frames: imgBytes = self.check_img(frame) successEnrol, self.codeError = self.azureService.add_face( imgBytes, person_id, frame) success_list.append(successEnrol) if self.azureService.attributes: for key, value in self.azureService.attributes.items(): setattr(self, key, value) self.G.add( PersonFiles(id, person_id, self.hairColor, self.glasses, self.gender, self.age, self.smile, self.pose, self.emotion, self.beard, self.eyesOpen, self.mouthOpen, self.bald, self.sideburns, self.eyeMakeUp, self.lipMakeUp, self.headWear, self.mask, self.mustache)) self.azureService.train() return success_list.count(True) def identify(self, frame): self.reset_attributes() self.frame = frame imgBytes = self.check_img(frame) attributes, self.codeError = self.azureService.identify(imgBytes) if attributes: for key, value in attributes.items(): setattr(self, key, value) self.gotAttributes = True return True else: self.reset_attributes() return False def identifyPerson(self, frame): personsList = self.persons_in_group() isIdentified = self.identify(frame) if isIdentified: for person in personsList: if person['personId'] == self.id_azure: self.name = person['name'] print('Person Identified: {}'.format(self.name)) break else: print('The person was not identified !!') def delete_person_by_name(self, name): deleted = False personsList, self.codeError = self.azureService.get_all_names() for person in personsList: if person['name'] == name: self.azureService.delete_person(person['personId']) deleted = True print('Person: {} Deleted !!'.format(name)) self.G.delete(name) break if not deleted: print('Person: {} Not Found !!'.format(name)) def persons_in_group(self): personsList, self.codeError = self.azureService.get_all_names() return personsList def reset_attributes(self): attrNoEdit = [ 'azureService', 'debug', 'ROOT_PATH', 'db_handler', 'information' ] for attr in dir(self): if not callable(getattr(self, attr)) and not attr.startswith( "__") and attr not in attrNoEdit: if attr == 'name': setattr(self, attr, "desconocido") else: if type(getattr(self, attr)) == int: setattr(self, attr, 0) elif type(getattr(self, attr)) == str: setattr(self, attr, "") elif type(getattr(self, attr)) == bool: setattr(self, attr, False) elif type(getattr(self, attr)) == dict: setattr(self, attr, {}) elif type(getattr(self, attr)) == tuple: setattr(self, attr, ([], None)) else: setattr(self, attr, None)
class IotDevice: def __init__(self, device_config): self.device_config = device_config self.reported_temp_alert = False self.fallback_activated = False self.boot_time = Common.getCurrentUTCTime() self.ip_address = self.getIpAddress() self.weather = Weather() self.airCondition = AirCondition(device_config) self.temperature = Temperature(device_config) if device_config.cloud == "firebase": self.hub = Firebase(self, device_config) elif device_config.cloud == "azure": self.hub = Azure(self, device_config) else: raise Exception( "Supported cloud services are 'azure' and 'firebase'. Update 'device_config.json'." ) try: with open('desired_state.json', 'r') as f: self.desired = json.load(f) except Exception as e: print("Expection while reading saved desired state: " + str(e)) self.wash_desired() self.set_fallback_date() def set_fallback_date(self): try: self.fallbackDateObject = datetime.strptime( self.desired[KEY_FALLBACK_DATE], "%Y-%m-%d") update_time = datetime.strptime(self.desired[KEY_UPDATE_TIME], "%Y-%m-%dT%H:%M:%S.%fZ") if self.fallbackDateObject < update_time: # Desired state updated since fallback, so ignore it self.fallbackDateObject = None self.fallback_activated = False except Exception as e: print(e.message) self.fallbackDateObject = None self.fallback_activated = False print("Fallback date format not YY-MM-DD " + self.desired[KEY_FALLBACK_DATE]) def wash_desired(self): for key, val in DESIRED_STATE_TEMPLATE.items(): if key not in self.desired: self.desired[key] = val self.desired[ KEY_TEMPERATURE_SETPOINT] = self.airCondition.validate_temp( self.desired[KEY_TEMPERATURE_SETPOINT]) self.desired[KEY_TELEMETRY_INTERVAL] = max( 30, self.desired[KEY_TELEMETRY_INTERVAL]) self.desired[KEY_TELEMETRY_INTERVAL] = min( 3600, self.desired[KEY_TELEMETRY_INTERVAL]) # Callback when the device twin stored in cloud has been updated def device_twin_update(self, desired): self.desired = desired self.wash_desired() print("New desired state received: %s" % json.dumps(self.desired, indent=4)) try: # Save new state to disk (to be read at boot) with open('desired_state.json', 'w') as f: json.dump(self.desired, f) # Set new filter time self.temperature.set_filter_time( self.desired[KEY_TELEMETRY_INTERVAL]) # Set AC temp self.airCondition.set_temp(self.desired[KEY_TEMPERATURE_SETPOINT]) # Fallback date update self.set_fallback_date() # Report new state to HUB self.update_reported_state() except Exception as e: print(e) # Send current state to HUB def update_reported_state(self): reported = {} reported[KEY_SW] = SOFTWARE_DICT reported[KEY_UPDATE_TIME] = Common.getCurrentUTCTime() reported[KEY_BOOT_TIME] = self.boot_time reported[KEY_TELEMETRY_ALERT] = self.reported_temp_alert reported[KEY_IP_ADDRESS] = self.ip_address if self.airCondition.ac_active: reported[KEY_AC_STATUS] = "AC active" else: reported[KEY_AC_STATUS] = "" if self.fallback_activated: reported[KEY_FALLBACK_ACTIVATED] = "Yes" else: reported[KEY_FALLBACK_ACTIVATED] = "No" for key in [KEY_TELEMETRY_INTERVAL, KEY_TEMPERATURE_SETPOINT]: try: reported[key] = self.desired[key] except KeyError: print("State set from HUB lack key '%s'" % key) # Report new state to HUB sent = self.hub.update_reported_state(reported) def get_temp_alert(self, temp): low_limit = TEMP_ALERT_LOW high_limit = TEMP_ALERT_HIGH if self.reported_temp_alert: # 1 degrees hysteresis low_limit += 1 high_limit -= 1 alert = (temp < low_limit) or (temp > high_limit) return alert # Sleep for t seconds while every <device_config.temp_sampling> seconds... # - checking for temp alerts # - kicking hub connection # - checking if telemetryInterval has been updated def telemetry_sleep(self): started_at = time.monotonic() - 2 while (time.monotonic() < (started_at + self.desired[KEY_TELEMETRY_INTERVAL])): self.hub.kick() time.sleep(device_config.temp_sampling) temp_c, temp_m = self.temperature.get() alert = self.get_temp_alert(temp_m) if alert != self.reported_temp_alert: break def isTimeForFallback(self): if self.fallbackDateObject is None: return False seconds = (datetime.now() - self.fallbackDateObject).total_seconds() if seconds > 0: # 00:00:00 on fallback date has passed return True return False def getIpAddress(self): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: # doesn't even have to be reachable s.connect(('10.255.255.255', 1)) IP = s.getsockname()[0] except: IP = '127.0.0.1' finally: s.close() return IP # Main loop def main_loop(self): print("Starting IoT Device with ID '{}'".format( self.device_config.deviceid)) self.hub.kick() while True: try: temp_c, temp_m = self.temperature.get() tempCurrent = temp_m # Reporting min temp telemetry = {} try: telemetry[ KEY_TEMPERATURE_SETPOINT] = self.airCondition.get_current_temp( ) except: # No temperature set point pass telemetry[KEY_TEMPERATURE_CURRENT] = tempCurrent current_alert = self.reported_temp_alert self.reported_temp_alert = self.get_temp_alert(temp_m) if current_alert != self.reported_temp_alert: self.update_reported_state() telemetry[KEY_TELEMETRY_ALERT] = self.reported_temp_alert telemetry[KEY_TELEMETRY_TIME] = Common.getCurrentUTCTime() weather = self.weather.get() if weather is not None: telemetry[KEY_OUTDOOR_CONDITIONS] = weather if self.isTimeForFallback(): # Have passed automatic fallback time # Set default AC temp print("Fallback activated @ {}".format(datetime.now())) self.desired[KEY_TEMPERATURE_SETPOINT] = self.desired[ KEY_FALLBACK_TEMP] self.airCondition.set_temp(self.desired[KEY_FALLBACK_TEMP]) self.fallbackDateObject = None self.fallback_activated = True self.update_reported_state() # print ( "Send telemetry: %s" % json.dumps(telemetry,indent=4) ) sent = self.hub.post_telemetry(telemetry) sys.stdout.flush() try: self.telemetry_sleep() except Exception as e: print("Device '{}' has no configured device twin defined". format(self.device_config.deviceid)) if device_config.cloud == "firebase": print("Use portal webpage to add the new device.") else: print( "Use e.g. iot_hub_twin_sample.py to create the new device twin" ) print(e) raise KeyboardInterrupt except KeyboardInterrupt: print("IoTHubClient sample stopped by Ctrl-C") break except Exception as e: print("Top level exception caught:") print(e)
class PersonCloud: def __init__(self): self.ROOT_PATH = os.path.dirname(sys.modules['__main__'].__file__) self.azureService = Azure() self.image = None self.packing = Packing() self.fileName = ["personGroupCloud.pckl"] self.personGroup = self.packing.unpack(self.fileName[0]) def detectPerson(self, frame): self.image = frame imgBytes = self.check_img(frame) people = self.azureService.detect(imgBytes) return people def enrol(self, name, frames): person_id, self.codeError = self.azureService.create_person(id) print("PersonID: ", person_id) succes = False if person_id is not None: self.id_azure = person_id for frame in frames: imgBytes = self.check_img(frame) response = self.azureService.add_face(imgBytes, person_id) person = response[1] if response[0]: succes = True person["person_id"] = person_id person["name"] = name print(person) # self.G.add(PersonFiles(id, person_id, self.hairColor, self.glasses, self.gender, self.age)) # if succes: # self.azureService.train() # else: # print('No entrenado') # return return [] person_locations = face_recognition.face_locations(frame) if (len(person_locations)): peoples, areas = setDictionary(person_locations) indexMax = areas.index(max(areas)) #encuentra la cara mas grande person_location = person_locations[indexMax] crop_img = frame[person_location[0]:person_location[0] + person_location[2] - person_location[0], person_location[3]:person_location[3] + person_location[1] - person_location[3]] cv2.imshow("cropped", crop_img) cv2.waitKey(0) person_encoding = face_recognition.face_encodings(crop_img)[0] person = peoples[indexMax] self.personModel.append(person_encoding) person["gender"] = None person["person"] = None person["name"] = name person["personId"] = uuid.uuid1() self.personGroup.append(person) self.packing.pack(self.personGroup, self.fileName[0]) self.packing.pack(self.personModel, self.fileName[1]) return self else: return [] def identifyPerson(self, frame): face_locations = face_recognition.face_locations(frame) person_encoding = face_recognition.face_encodings( frame, face_locations) known_faces = self.personModel for face_encoding in person_encoding: matches = face_recognition.compare_faces(known_faces, face_encoding) print("matches", matches) if True in matches: first_match_index = matches.index(True) return self.personGroup[first_match_index] else: return [] def persons_in_group(self): personsList = self.personGroup return personsList def deleteAll(self): self.personGroup = list() self.packing.pack(self.personGroup) return 0 def check_img(self, frame): retval, encoded_image = cv2.imencode('.png', frame) return encoded_image.tobytes()
i = int(msg.payload) HARD_LIMIT = i count = 0 except ValueError: print("please enter an integer") mainConfig = config.MainConfig() cameraConfig = config.CameraConfig() MQTTConfig = config.MQTTConfig() print("main Topic: ", MQTTConfig.topicMain) # How many seconds should the code wait before calling the API again api_call_time_limit = mainConfig.apiCallTimeLimit azure = Azure() camera_url = "rtsp://{0}:{1}@{2}".format(cameraConfig.username, cameraConfig.password, cameraConfig.ip) HARD_LIMIT = 15 stream = cv2.VideoCapture(camera_url) print("Stream starting !!...") t_start = time.time() count = 0 mqtt.setup() mqtt.client.on_message = on_message # LIMITED: the program is limited to 15 iterations/API calls, since we are limited with the request count while True:
class Person: def __init__(self): self.ROOT_PATH = os.path.dirname(sys.modules['__main__'].__file__) self.azureService = Azure() self.codeError = 0 self.gotAttributes = False self.frame = None self.framesTrain = None self.bb_service = [] # self.service = None self.name = "desconocido" self.id_debug = None self.faceId = "" self.accuracy = None self.age = 0 self.gender = "" self.smile = 0 self.pose = {} self.emotion = None self.beard = 0 self.glasses = "" self.eyesOpen = 0 self.mouthOpen = 0 self.bald = 0 self.hairColor = "" self.sideburns = 0 self.eyeMakeUp = False self.lipMakeUp = False self.headWear = 0 self.mask = 0 self.mustache = 0 self.bb_actual = [] self.image_actual = None self.imageBBPair = ([], None) self.happiness = 0 self.sadness = 0 self.neutral = 0 self.surprise = 0 self.anger = 0 self.table = 0 self.country = "" self.id = "" self.id_azure = "" self.interaction = 0 self.lastInteractionTime = 0 self.image = None self.G = Group() def frame2bytes(self, frame): retval, encoded_image = cv2.imencode('.png', frame) return encoded_image.tobytes() def check_img(self, frame): if type(frame) != bytes: return self.frame2bytes(frame) else: return frame def enrol(self, id, frames): person_id, self.codeError = self.azureService.create_person(id) succes = False if person_id is not None: self.id_azure = person_id for frame in frames: imgBytes = self.check_img(frame) successEnrol, self.codeError = self.azureService.add_face( imgBytes, person_id) if successEnrol: succes = True if self.azureService.attributes: for key, value in self.azureService.attributes.items(): setattr(self, key, value) self.image = frame self.G.add( PersonFiles(id, person_id, self.hairColor, self.glasses, self.gender, self.age)) if succes: self.azureService.train() else: print('No entrenado') return person_id def identify(self, frame): self.reset_attributes() self.frame = frame imgBytes = self.check_img(frame) identify, error = self.azureService.identify(imgBytes) # print('IDENTIFY VERIFICATION: ', identify) return identify def identifyPerson(self, frame): personsList = self.persons_in_group() people = self.identify(frame) for person in personsList: if people['verify_recognition']: if person['personId'] == people['id_azure']: people['name'] = person['name'] print('Person Identified: {}'.format(person['name'])) else: people['name'] = 'Desconocido' return people def faceDetector(self, frame): self.frame = frame #cap = cv2.VideoCapture(0) #ret, img = cap.read() rgb_frame = frame[:, :, ::-1] frame_size = frame.shape[0] * frame.shape[1] face_locations = face_recognition.face_locations(rgb_frame) peoples = list() print(face_locations) for face_location in face_locations: #cv2.rectangle(frame, (face_location[1], face_location[0]), (face_location[3], face_location[2]), (255, 255, 0), 2) width = face_location[1] - face_location[3] height = face_location[2] - face_location[0] dictionary_of_features = { 'faceId': None, 'faceRectangle': { 'width': int(width), 'top': int(face_location[0]), 'height': int(height), 'left': int(face_location[3]) }, 'faceAttributes': None } peoples.append(dictionary_of_features) peoples.sort(key=sortDictionary, reverse=True) return peoples def detectPerson(self, frame): self.frame = frame # imgBytes = self.check_img(frame) people = self.faceDetector(frame) print(people) return people def delete_persons(self): deleted = False personsList, self.codeError = self.azureService.get_all_names() for person in personsList: self.azureService.delete_person(person['personId']) deleted = True print('Person: {} Deleted !!'.format(person['personId'])) break if not deleted: print('Person: {} Not Found !!'.format(person['personId'])) def delete_person_by_name(self, name): deleted = False personsList, self.codeError = self.azureService.get_all_names() for person in personsList: if person['name'] == name: self.azureService.delete_person(person['personId']) deleted = True print('Person: {} Deleted !!'.format(name)) self.G.delete(name) break if not deleted: print('Person: {} Not Found !!'.format(name)) def persons_in_group(self): personsList, self.codeError = self.azureService.get_all_names() return personsList def reset_attributes(self): attrNoEdit = [ 'azureService', 'debug', 'ROOT_PATH', 'db_handler', 'information' ] for attr in dir(self): if not callable(getattr(self, attr)) and not attr.startswith( "__") and attr not in attrNoEdit: if attr == 'name': setattr(self, attr, "desconocido") else: if type(getattr(self, attr)) == int: setattr(self, attr, 0) elif type(getattr(self, attr)) == str: setattr(self, attr, "") elif type(getattr(self, attr)) == bool: setattr(self, attr, False) elif type(getattr(self, attr)) == dict: setattr(self, attr, {}) elif type(getattr(self, attr)) == tuple: setattr(self, attr, ([], None)) else: setattr(self, attr, None)