def update_person_fields(form, person): person.name = Name.from_str(form.get('name')) if form.get('name') else person.name person.birth_date = Day.from_str(form.get('birth_date')) person.email = form.get('email') or person.email person.gender = Gender[form.get('gender')] person.phone_number = phone_number = re.compile('[\W_]+').sub('', form.get('phone_number')) if form.get('phone_number') else person.phone_number person.member = form.get('member').lower() == 'true' person.ministry = [Ministry[form.get('ministry')]] person.sigi = int(form.get('sigi')) if form.get('sigi') else person.sigi return person
def create_person(form): name = Name.from_str(form.get('name')) birth_date = Day.from_str(form.get('birth_date')) email = form.get('email') gender = Gender[form.get('gender')] phone_number = phone_number = re.compile('[\W_]+').sub('', form.get('phone_number')) member = form.get('member').lower() == 'true' ministry = [Ministry[form.get('ministry')]] sigi = int(form.get('sigi')) return Person(name, birth_date, email, gender, phone_number, member, ministry, sigi, Calendar(), {}, {})
def is_active(self): count = 0 total = 0 today = Day.today() for sunday in self.sundays: if sunday > today: break if sunday.presence == Presence.PRESENT or sunday.presence == Presence.JUSTIFIED: count += 1 total += 1 return True if count / total >= active_rate else False
def dict_of_people(self, l): d = [] for element in l: name = Name.from_str(element[0]) gender = Gender.FEMALE if element[1] == 'F' else Gender.MALE day = element[2] if day is None: birth_date = None else: birth_date = Day(day.year, day.month, day.day) if element[3] is None: phone_number = None elif type(element[3]) == float: phone_number = str(element[3])[:-2] elif type(element[3]) == int: phone_number = str(element[3]) else: phone_number = re.compile('[\W_]+').sub('', element[3]) email = element[4] ministry = [Ministry[x.strip().upper()] for x in element[5].split(',')] member = bool(element[6]) sigi = element[7] calendar = Calendar() photos = { PhotoCategory.FRONT: Photo(PhotoCategory.FRONT, PhotoMode.RAW, 0, element[8].lower() + '-fr.jpg'), PhotoCategory.LEFT: Photo(PhotoCategory.LEFT, PhotoMode.RAW, 0, element[8].lower() + '-le.jpg'), PhotoCategory.RIGHT: Photo(PhotoCategory.RIGHT, PhotoMode.RAW, 0, element[8].lower() + '-ld.jpg'), } encodings = { PhotoCategory.FRONT: Photo(PhotoCategory.FRONT, PhotoMode.RAW, 0, element[8].lower() + '-fr.jpg'), PhotoCategory.LEFT: Photo(PhotoCategory.LEFT, PhotoMode.RAW, 0, element[8].lower() + '-le.jpg'), PhotoCategory.RIGHT: Photo( PhotoCategory.RIGHT, PhotoMode.RAW, 0, element[8].lower() + '-ld.jpg') } d.append(Person(name, birth_date, email, gender, phone_number, member, ministry, sigi, calendar, photos, encodings)) return d
def get(_id): person = recognition.db.get_member_by_id(_id) images = [] for encoding_id in person.encodings.values(): try: image_bytes = recognition.db.get_image(encoding_id) image = get_person_image_from_bytes(image_bytes, 0.15) images.append(image) except Exception as e: print('failed to retrieve image: %s' % e) if request.method == 'POST': person = update_person_fields(request.form, person) person.set_sundays([Sunday.from_str(key.split('calendar.')[1], request.form[key]) for key in request.form if 'calendar' in key]) recognition.db.update_member_calendar(person) recognition.db.replace_member(person._id, person) return render_template('person.html', person=person, images=images, today=Day.today())
def recognize(self, frame, model='hog', day=None, presence=None): if len(self.known_face_encodings) == 0: print('no face encodings found in database %s' % self.db.db) return face_locations, face_encodings = self.get_faces_from_picture( frame, model=model) results = self.identify(frame, face_locations, face_encodings) names = [] # conf that defines if should update person's presence or not if day is not None and day != '': for result in results: member = self.db.get_member_by_id(result.member_id) if member.calendar.mark_presence(Day.from_str(day), Presence[presence]): self.db.update_member_calendar(member) names.append(result.name) else: names = [x.name for x in results] print("found in frame: %s" % names) return results
def management(): persons = recognition.db.get_all_members() if request.method == 'POST': form = request.form.to_dict() date = Day.from_str(form.pop('presence_date')) presence = Presence[form.pop('presence')] ids = form.keys() # gets ids to be marked for person in persons: try: image_bytes = recognition.db.get_image(person.encodings[PhotoCategory.FRONT.name]) person.encodings[PhotoCategory.FRONT.name] = get_person_image_from_bytes(image_bytes, 0.05) except Exception as e: person.encodings[PhotoCategory.FRONT.name] = b'' # if marking presence for person, update in database if request.method == 'POST': if str(person._id) in ids: if person.calendar.mark_presence(date, presence): recognition.db.update_member_calendar(person) return render_template('management.html', persons=persons)
def identify(self, frame, face_locations, face_encodings): results = [] # iterates through each face for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings): # Draw a box around the face cv2.rectangle(frame, (left * 4, top * 4), (right * 4, bottom * 4), (0, 0, 255), 2) face_distances = face_recognition.face_distance( self.known_face_encodings_list, face_encoding) min_face_distance = np.min(face_distances) min_face_distance_index = np.argmin(face_distances) # detected and found face in database if min_face_distance <= self.tolerance: name = self.known_face_encodings[min_face_distance_index].name member_id = self.known_face_encodings[ min_face_distance_index].member_id # don't repeat for already found faces # if name in found: # print('%s already in cache. skipping..' % name) # continue # create event document and save it to mongodb event = Event( member_id, name, Day.today(), min_face_distance, self.known_face_encodings[min_face_distance_index], frame) self.save_event(event, coordinates=(top, right, bottom, left)) results.append(event) return results
def from_dict(person): return Person(Name.from_dict(person['name']), Day.from_dict(person['birth_date']), person['email'], person['gender'], person['phone_number'], person['member'], [Ministry[x] for x in person['ministry']], person['sigi'], Calendar.from_dict(person['calendar']), person['photos'], person['encodings'])