def OnChooseImage(self, evt, bitmap, face_id): """Choose Image""" dlg = wx.FileDialog(self, wildcard=util.IMAGE_WILDCARD) if dlg.ShowModal() != wx.ID_OK: return path = dlg.GetPath() self.log.log('Request: Detecting {}'.format(path)) res = util.CF.face.detect(path) faces = [model.Face(face, path) for face in res] self.log.log('Response: Success. Detected {} face(s) in {}'.format( len(res), path)) if len(faces) > 1: text = ( 'Verification accepts two faces as input, please pick images ' 'with only one detectable face in it.') title = 'Warning' style = wx.OK | wx.ICON_WARNING wx.MessageBox(text, title, style) return bitmap.set_path(path) util.draw_bitmap_rectangle(bitmap, faces) self.face_ids[face_id] = faces[0].id self.check_btn_verify()
def async_detect(self, path): """Async detection.""" self.log.log('Request: Detecting {}'.format(path)) self.result.SetLabelText('Detecting ...') self.btn.Disable() self.face_list.Clear() self.face_list.Refresh() self.rsizer.Layout() self.vhsizer.Layout() try: attributes = ( 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,' 'makeup,occlusion,accessories,blur,exposure,noise') res = util.CF.face.detect(path, False, False, attributes) faces = [model.Face(face, path) for face in res] self.face_list.SetItems(faces) util.draw_bitmap_rectangle(self.bitmap, faces) log_text = 'Response: Success. Detected {} face(s) in {}'.format( len(res), path) self.log.log(log_text) text = '{} face(s) has been detected.'.format(len(res)) self.result.SetLabelText(text) except util.CF.CognitiveFaceException as exp: self.log.log('Response: {}. {}'.format(exp.code, exp.msg)) self.btn.Enable() self.rsizer.Layout() self.vhsizer.Layout()
def OnChooseImage(self, evt): """Choose Image.""" util.CF.util.wait_for_large_face_list_training(self.large_face_list_id) self.log.log( 'Response: Success. List "{0}" training process is Succeeded'. format(self.large_face_list_id)) dlg = wx.FileDialog(self, wildcard=util.IMAGE_WILDCARD) if dlg.ShowModal() != wx.ID_OK: return path = dlg.GetPath() self.bitmap.set_path(path) self.log.log('Detecting faces in {}'.format(path)) self.faces.clear() res = util.CF.face.detect(path) for entry in res: face = model.Face(entry, path) self.faces[face.id] = face util.draw_bitmap_rectangle(self.bitmap, self.faces.values()) self.log.log('Success. Detected {} face(s) in {}'.format( len(self.faces), path)) res_tot = { 'matchPerson': {}, 'matchFace': {}, } for face_id in self.faces: self.log.log( ('Request: Finding similar faces in Person Match Mode for ' 'face {}').format(face_id)) for mode in ('matchPerson', 'matchFace'): res_tot[mode][face_id] = [] res = util.CF.face.find_similars( face_id, large_face_list_id=self.large_face_list_id, mode=mode) self.log.log( 'Response: Found {} similar faces for face {} in {} mode'. format(len(res), face_id, mode)) for entry in res: persisted_id = entry['persistedFaceId'] confidence = entry['confidence'] res_tot[mode][face_id].append( (self.persisted_faces[persisted_id], confidence)) self.result.set_data(self.faces, res_tot) self.panel.SetupScrolling(scroll_x=False)
def OnChooseFolder(self, evt): """Choose Folder.""" dlg = wx.DirDialog(self) if dlg.ShowModal() == wx.ID_OK: path = dlg.GetPath() del self.face_paths[:] self.faces.clear() for root, dirs, files in os.walk(path): if files: self.face_paths.extend([ os.path.join(root, filename) for filename in files ]) self.btn.Disable() self.log.log( 'Request: Preparing faces for grouping, detecting faces in ' 'chosen folder.' ) self.grid.set_paths(self.face_paths) for path in self.face_paths: try: res = util.CF.face.detect(path) except util.CF.CognitiveFaceException: continue for entry in res: face = model.Face(entry, path) self.faces[face.id] = face self.grid.set_faces(self.faces.values()) self.log.log( 'Response: Success. Total {0} faces are detected.'.format( len(self.faces))) self.log.log('Request: Grouping {0} faces.'.format( len(self.faces))) res = util.CF.face.group(self.faces.keys()) self.result.set_data(self.faces, res) len_groups = len(res['groups']) if res.get('messyGroup'): len_groups += 1 self.log.log( 'Response: Success. {0} faces grouped into {1} groups'.format( len(self.faces), len_groups)) self.btn.Enable()
def traindata(): #for ef in os.listdir(traingdata): # print(ef) #del self.detected_face_paths[:] util.CF.large_face_list.create(large_face_list_id) for ef in os.listdir(trainingdata): print(ef) res = util.CF.large_face_list_face.add(trainingdata + ef, large_face_list_id) print('Response: Success. Add with Persisted Face Id {}'.format( res['persistedFaceId'])) detected_face_paths.append(ef) face = model.Face(res, ef) persisted_faces[face.persisted_id] = face time.sleep(10) res = util.CF.large_face_list.train(large_face_list_id)
def OnChooseImage(self, evt): """Choose Image.""" util.CF.util.wait_for_large_person_group_training( self.large_person_group_id) self.log.log( 'Response: Success. Group "{0}" training process is Succeeded'. format(self.large_person_group_id)) dlg = wx.FileDialog(self, wildcard=util.IMAGE_WILDCARD) if dlg.ShowModal() != wx.ID_OK: return path = dlg.GetPath() self.bitmap.set_path(path) self.log.log('Detecting faces in {}'.format(path)) self.faces.clear() del self.face_ids[:] res = util.CF.face.detect(path) for entry in res: face = model.Face(entry, path) self.faces[face.id] = face self.face_ids.append(face.id) self.log.log('Request: Identifying {0} face(s) in group "{1}"'.format( len(self.faces), self.large_person_group_id)) res = util.CF.face.identify( self.face_ids, large_person_group_id=self.large_person_group_id) for entry in res: face_id = entry['faceId'] if entry['candidates']: person_id = entry['candidates'][0]['personId'] self.faces[face_id].set_name(self.person_id_names[person_id]) else: self.faces[face_id].set_name('Unknown') util.draw_bitmap_rectangle(self.bitmap, self.faces.values()) log_text = 'Response: Success.' for face_id in self.faces: log_text += ' Face {0} is identified as {1}.'.format( face_id, self.faces[face_id].name) self.log.log(log_text)
def find_face(parameters: m.FaceParameters) -> m.Face: faces = parameters.face_cascade.detectMultiScale( parameters.image, 1.3, 5, 0 | cv2.CASCADE_SCALE_IMAGE | cv2.CASCADE_FIND_BIGGEST_OBJECT) if faces != (): print("FACE detected!") # getting the coordinates of the detected faces for (x, y, w, h) in faces: pos_face = dlib.rectangle(left=int(x), top=int(y), right=int(x + w), bottom=int(y + h)) face = m.Face(parameters.image, pos_face) return face else: print("No face detected") if not parameters.view: print("The camera is not working") return None
def async_detect(self, path): """Async detection.""" self.log.log('Request: Detecting {}'.format(path)) self.result.SetLabelText('Detecting ...') self.btn.Disable() self.face_list.Clear() self.face_list.Refresh() self.rsizer.Layout() self.vhsizer.Layout() attributes = 'age,gender,headPose,smile,facialHair,glasses,emotion' res = util.CF.face.detect(path, False, False, attributes) faces = [model.Face(face, path) for face in res] self.face_list.SetItems(faces) util.draw_bitmap_rectangle(self.bitmap, faces) log_text = 'Response: Success. Detected {} face(s) in {}'.format( len(res), path) self.log.log(log_text) text = '{} face(s) has been detected.'.format(len(res)) self.result.SetLabelText(text) self.btn.Enable() self.rsizer.Layout() self.vhsizer.Layout()
def OnChooseFolder(self, evt): """Choose Folder.""" self.log.log( ('Request: Group {0} will be used to build a person database. ' 'Checking whether the group exists.').format( self.large_person_group_id)) try: util.CF.large_person_group.get(self.large_person_group_id) self.log.log('Response: Group {0} exists.'.format( self.large_person_group_id)) text = ('Requires a clean up for group "{0}" before setting up a ' 'new person database. Click YES to proceed, group "{0}" ' 'will be cleared.').format(self.large_person_group_id) title = 'Warning' style = wx.YES_NO | wx.ICON_WARNING result = wx.MessageBox(text, title, style) if result == wx.YES: util.CF.large_person_group.delete(self.large_person_group_id) self.large_person_group_id = str(uuid.uuid1()) else: return except util.CF.CognitiveFaceException as exp: if exp.code != 'LargePersonGroupNotFound': self.log.log('Response: {}. {}'.format(exp.code, exp.msg)) return else: self.log.log( 'Response: Group {0} does not exist previously.'.format( self.large_person_group_id)) self.log.log('Request: Creating group "{0}"'.format( self.large_person_group_id)) util.CF.large_person_group.create(self.large_person_group_id) self.log.log('Response: Success. Group "{0}" created'.format( self.large_person_group_id)) self.log.log( ('Preparing faces for identification, detecting faces in chosen ' 'folder.')) dlg = wx.DirDialog(self) if dlg.ShowModal() != wx.ID_OK: return path = dlg.GetPath() self.person_id_names.clear() self.person_name_faces.clear() face_count = 0 for person_name in os.listdir(path): path_person = os.path.join(path, person_name) if os.path.isdir(path_person): self.log.log( 'Request: Creating person "{0}"'.format(person_name)) res = util.CF.large_person_group_person.create( self.large_person_group_id, person_name) person_id = res['personId'] self.log.log( 'Response: Success. Person "{0}" (PersonID: {1}) created'. format(person_name, person_id)) self.person_id_names[person_id] = person_name self.person_name_faces[person_name] = [] for entry in os.listdir(path_person): path_face = os.path.join(path_person, entry) if os.path.isfile(path_face): res = util.CF.large_person_group_person_face.add( path_face, self.large_person_group_id, person_id) if res.get('persistedFaceId'): face_count += 1 face = model.Face(res, path_face) self.person_name_faces[person_name].append(face) self.log.log('Response: Success. Total {0} faces are detected.'.format( face_count)) self.log.log('Request: Training group "{0}"'.format( self.large_person_group_id)) res = util.CF.large_person_group.train(self.large_person_group_id) self.grid.set_data(self.person_name_faces) self.panel.SetupScrolling(scroll_x=False) self.btn_file.Enable()
def OnChooseFolder(self, evt): """Choose Folder.""" large_face_list_exists = False try: self.log.log( ('Request: List {} will be used to build a person database. ' 'Checking whether the list exists.').format( self.large_face_list_id)) print(dir(util.CF)) print(util.CF.__file__) util.CF.large_face_list.get(self.large_face_list_id) large_face_list_exists = True self.log.log('Response: List {} exists.'.format( self.large_face_list_id)) except util.CF.CognitiveFaceException as exp: if exp.code != 'LargeFaceListNotFound': self.log.log('Response: {}. {}'.format(exp.code, exp.msg)) return else: self.log.log( 'Response: List {} did not exist previously.'.format( self.large_face_list_id)) if large_face_list_exists: text = ( 'Requires a clean up for list "{0}" before setting up a new' 'list. Click OK to proceed, list "{0}" will be cleared.' ).format(self.large_face_list_id) title = 'Warning' style = wx.YES_NO | wx.ICON_WARNING result = wx.MessageBox(text, title, style) if result == wx.YES: util.CF.large_face_list.delete(self.large_face_list_id) self.large_face_list_id = str(uuid.uuid1()) else: return dlg = wx.DirDialog(self) if dlg.ShowModal() == wx.ID_OK: path = dlg.GetPath() del self.face_paths[:] for root, dirs, files in os.walk(path): if files: self.face_paths.extend( [os.path.join(root, filename) for filename in files]) self.panel.SetupScrolling(scroll_x=False) self.log.log('Request: Preparing, detecting faces in chosen folder.') self.btn_folder.Disable() self.btn_file.Disable() self.persisted_faces.clear() del self.detected_face_paths[:] util.CF.large_face_list.create(self.large_face_list_id) for path in self.face_paths: try: res = util.CF.large_face_list_face.add(path, self.large_face_list_id) self.log.log( 'Response: Success. Add with Persisted Face Id {}'.format( res['persistedFaceId'])) except util.CF.CognitiveFaceException as exp: self.log.log( ('[Error] Add "{}" to List {}: Code: {}, ' 'Message: {}').format(path, self.large_face_list_id, exp.code, exp.msg)) continue self.detected_face_paths.append(path) face = model.Face(res, path) self.persisted_faces[face.persisted_id] = face self.log.log('Response: Success. Total {0} faces are detected.'.format( len(self.persisted_faces))) self.log.log('Request: Training List: "{0}"'.format( self.large_face_list_id)) res = util.CF.large_face_list.train(self.large_face_list_id) self.grid.set_paths(self.detected_face_paths) self.btn_folder.Enable() self.btn_file.Enable()