def _get_db_faces(self, db_img_dir=None): """ Get faces from images :return: a dict in the format of { <person_name> : [< a_str_of_face_id >, ...] ... ... "failed" : [< failed_photo_path_1 >, < failed_photo_path_2 > ...] } """ faces = {} for person in Person.query.all(): faces[person.name] = [] for photo in person.photos: photo_path = os.path.join(UPLOAD_DIR, photo.filename) print(photo_path) photo_file = facepp.File(photo_path) try: f_id = self._detect_face(photo_file) except NoFaceDetectedError: print("{} failed!".format(photo_path)) else: faces[person.name].append(f_id) return faces
def add(self, name, img): try: rs = self.api.detection.detect(img=facepp.File(img)) rs = self.api.person.create(person_name=name, face_id=rs['face'][0]['face_id']) rs = self.api.group.add_person(group_name=self.group_name, person_name=name) rs = self.api.train.identify(group_name=self.group_name) rs = self.api.wait_async(rs['session_id']) except facepp.APIError as e: logging.error(e.body) return e.code return 0
def add_person(self, img_path, remove_if_exist, person_name=None): """ Add one 'Person' to the 'Group' at the server directly from local image file :param img_path: the path to the image file :param remove_if_exist: namely :param person_name: the name of the person, by default is the file name without suffix """ if person_name is None: person_name = os.path.basename(img_path).split(".")[0] # remove suffix name face_id = self._detect_face(facepp.File(img_path)) self._create_person(person_name, remove_if_exist, face_id=face_id, group_name=self.group_name)
def recognise(self, img): try: self.snap() rs = self.api.recognition.identify(group_name=self.group_name, img=facepp.File(img)) except facepp.APIError as e: logging.error(e.body) return [] result = {'confidence': 0} for face in rs['face']: for candidate in face['candidate']: if candidate['confidence'] > result['confidence']: result = candidate if result['confidence'] > 30: return result
print hint result = encode(result) print '\n'.join([' ' + i for i in pformat(result, width=75).split('\n')]) # 首先,导入SDK中的API类,还有本地文件读取所需要的facepp类 from facepp import API import facepp api = API(API_KEY, API_SECRET) #face = api.detection.detect(img = facepp.File('3.jpg')) # 人名及其脸部图片 #IMAGE_DIR = 'http://www.urlteam.org/wp-content/uploads/2016/02/3.jpg' #facepp.File('3.jpg') PERSONS = [ ('yaxin', facepp.File('picture/yaxin/1.jpg')), ('yaxin', facepp.File('picture/yaxin/2.jpg')), ('xuna', facepp.File('picture/xuna/1.jpg')), ('xuna', facepp.File('picture/xuna/2.jpg')), ('luyi', facepp.File('picture/luyi/1.jpg')), ('qidao', facepp.File('picture/qidao/1.jpg')), ('qidao', facepp.File('picture/qidao/2.jpg')), ('qidao', facepp.File('picture/qidao/3.jpg')), ] TARGET_IMAGE = facepp.File('picture/xuna/3.jpg') # IMAGE_DIR # 步骤1:检测出三张输入图片中的Face,找出图片中Face的位置及属性 FACES = {name: api.detection.detect(img=url) for name, url in PERSONS} for name, face in FACES.iteritems():
if type(obj) is dict: return {encode(k): encode(v) for (k, v) in obj.iteritems()} if type(obj) is list: return [encode(i) for i in obj] return obj print hint result = encode(result) print '\n'.join([' ' + i for i in pformat(result, width=75).split('\n')]) api = API(API_KEY, API_SECRET) #face = api.detection.detect(img = facepp.File('3.jpg')) # IMAGE_DIR 此部分则是选择出需要进行分析的图片。。管理员模式下不用设置实用。(可能有文件打开错误的bug,如果本身没有end.jpg的话。) TARGET_IMAGE = facepp.File('end.jpg') print("已经读取图片,正在进行人脸识别,根据网络环境速度不定。") #识别end.jpg图中的Face rst = api.recognition.identify(group_name='test', img=TARGET_IMAGE) #print_result('识别结果:', rst) print '=' * 60 print '识别结果最匹配的对象为:', \ rst['face'][0]['candidate'][0]['person_name'] print("正在写入kaoqin.txt中,当前时间为:" + " " + time.asctime()) print '=' * 60 fp = open("kaoqin.txt", 'a') fp.write("姓名:" + " " + rst['face'][0]['candidate'][0]['person_name'] + "签到时间:" + " " + time.asctime() + "\n") fp.close()
for name in a: print (name) dir_temp = dir +'/'+name b = os.listdir(dir_temp) print(b) for img_name in b: if img_name[-1] != 'g': continue dir_img =dir_temp+'/'+img_name print (dir_img) f = open(dir_temp+'/name.txt') a = f.read() f.close() dir_temp yuan = (a,facepp.File(dir_img)) PERSONS.append(yuan) print (PERSONS) # c = facepp.File('')) TARGET_IMAGE = facepp.File('end.jpg')# IMAGE_DIR # 步骤1:检测出三张输入图片中的Face,找出图片中Face的位置及属性 FACES = {name: api.detection.detect(img = url) for name, url in PERSONS} for name, face in FACES.iteritems(): print_result(name, face)
return [(landmarks[key]['x'], landmarks[key]['y']) for key in landmarks if re.match("contour", key)] ############################################################# # Main execution ############################################################# prediction_service = build('prediction', 'v1.6', developerKey=DEV_KEY) prediction_service.hostedmodels() facepp_api = facepp.API(API_KEY, API_SECRET, srv=API_SERVER) face = facepp_api.detection.detect(img=facepp.File(IMAGE_PATH)) landmark = facepp_api.detection.landmark(face_id=face['face'][0]['face_id'], type='83p') window = pyglet.window.Window() vertex_list_eyes = pyglet.graphics.vertex_list( len(getEyes(landmark["result"][0]["landmark"])), ('v2f', [ value for point in getEyes(landmark["result"][0]["landmark"]) for value in point ])) vertex_list_nose = pyglet.graphics.vertex_list( len(getNose(landmark["result"][0]["landmark"])), ('v2f', [ value for point in getNose(landmark["result"][0]["landmark"]) for value in point ]))
def identify_new_face(self, path_to_image): ret = self.api.recognition.identify(group_name=self.group_name, post=True, img=facepp.File(path_to_image)) ret = ret['face'][0]['candidate'] #print("Result of face identification to '{}'\nCandidates:".format(path_to_image)) #for r in ret: # print(u"{}:\t{:.3f}%".format(r["person_name"], r["confidence"])) return ret
#face = api.detection.detect(img = facepp.File('3.jpg')) # 人名及其脸部图片 #IMAGE_DIR = 'http://www.urlteam.org/wp-content/uploads/2016/02/3.jpg' #facepp.File('3.jpg') import os path = r'./picture' fns = [ os.path.join(root, fn) for root, dirs, files in os.walk(path) for fn in files ] for f in fns: print(f) print(len(fns)) PERSONS = [ (u'雅新', facepp.File('picture/yaxin/1.jpg')), (u'雅新', facepp.File('picture/yaxin/2.jpg')), (u'许娜', facepp.File('picture/xuna/1.jpg')), (u'许娜', facepp.File('picture/xuna/2.jpg')), (u'陆毅', facepp.File('picture/luyi/1.jpg')), (u'其道', facepp.File('picture/qidao/1.jpg')), (u'其道', facepp.File('picture/qidao/2.jpg')), (u'其道', facepp.File('picture/qidao/3.jpg')), ] TARGET_IMAGE = facepp.File('picture/1/3.jpg') # IMAGE_DIR # 步骤1:检测出三张输入图片中的Face,找出图片中Face的位置及属性 FACES = {name: api.detection.detect(img=url) for name, url in PERSONS} for name, face in FACES.iteritems(): print_result(name, face)