def build_gallery_probe(gallery_imgs, probe_imgs): # gallery info # Key: Face Token # Value: Person id gallery = {} # probe info # Key: Face Token # Value: Person id probe = {} # init api api = API() # delete useless face_set api.faceset.delete(outer_id='face_recognition', check_empty=0) # # 1.create faceSet ret = api.faceset.create(outer_id='face_recognition') # # # 2.add pics to faceSet(face_token) img_path = gb.glob(gallery_imgs) for img in img_path: faceResStr = "" res = api.detect(image_file=File(img)) # get person's name person_name = img[-12:][0:5] print(person_name) print(res) faceList = res["faces"] for index in range(len(faceList)): gallery[faceList[index]["face_token"]] = person_name if index == 0: faceResStr = faceResStr + faceList[index]["face_token"] else: faceResStr = faceResStr + "," + faceList[index]["face_token"] api.faceset.addface(outer_id='face_recognition', face_tokens=faceResStr) print(gallery) # build probe set img_path = gb.glob(probe_imgs) for img in img_path: faceResStr = "" res = api.detect(image_file=File(img)) # get person's name person_name = img[-12:][0:5] print(person_name) print(res) faceList = res["faces"] probe[faceList[0]["face_token"]] = person_name print("probe") print(probe) json.dump(gallery, open('gallery.json', 'w')) json.dump(probe, open('probe.json', 'w')) ''' ''' with open("./gallery.json", 'r') as g: gallery = json.load(g) with open("./probe.json", 'r') as p: probe = json.load(p) genuine_scores = [] imposter_scores = [] for gallery_key, gallery_value in gallery.items(): for probe_key, probe_value in probe.items(): result = api.compare(face_token1=gallery_key, face_token2=probe_key) res = {} res['gallery'] = gallery_value res['probe'] = probe_value res['confidence'] = result['confidence'] if gallery_value == probe_value: genuine_scores.append(res) else: imposter_scores.append(res) json.dump(genuine_scores, open('genuine_scores.json', 'w')) json.dump(imposter_scores, open('imposter_scores.json', 'w'))
for i in pformat(result, width=75).split('\n'))) print(hit) print('\n'.join(" " + i for i in pformat(result, width=75).split('\n'))) def printFuctionTitle(title): return "\n" + "-" * 60 + title + "-" * 60 # 初始化对象,进行api的调用工作 api = API() # -----------------------------------------------------------人脸识别部分------------------------------------------- # 人脸检测:https://console.faceplusplus.com.cn/documents/4888373 res = api.detect(image_url=detech_img_url, return_attributes="gender,age,smiling,headpose,facequality," "blur,eyestatus,emotion,ethnicity,beauty," "mouthstatus,skinstatus") print_result(printFuctionTitle("人脸检测"), res) # 人脸比对:https://console.faceplusplus.com.cn/documents/4887586 # compare_res = api.compare(image_file1=File(face_search_img), image_file2=File(face_search_img)) # print_result("compare", compare_res) # 人脸搜索:https://console.faceplusplus.com.cn/documents/4888381 # 人脸搜索步骤 # 1,创建faceSet:用于存储人脸信息(face_token) # 2,向faceSet中添加人脸信息(face_token) # 3,开始搜索 # 删除无用的人脸库,这里删除了,如果在项目中请注意是否要删除 # api.faceset.delete(outer_id='faceplusplus', check_empty=0)
# 2,向faceSet中添加人脸信息(face_token) # 3,开始搜索 # 初始化对象,进行api的调用工作 api = API() # 删除无用的人脸库,这里删除了,如果在项目中请注意是否要删除 api.faceset.delete(outer_id='testset', check_empty=0) # 1.创建一个faceSet ret = api.faceset.create(outer_id='testset', display_name='测试用set', tags='useless') # 2.向faceSet中添加人脸信息(face_token) faceResStr = "" res = api.detect(image_file=File(faceSet_img)) faceList = res["faces"] for index in range(len(faceList)): if index == 0: faceResStr = faceResStr + faceList[index]["face_token"] else: faceResStr = faceResStr + "," + faceList[index]["face_token"] api.faceset.addface(outer_id='testset', face_tokens=faceResStr, user_id='bbb') # 3.开始搜索相似脸人脸信息 search_result = api.search(image_file=File(face_search_img), outer_id='testset') print_result('search', search_result)
#res = api.detect(image_url=detech_img_url, return_attributes="gender,age,smiling,headpose,facequality," #"blur,eyestatus,emotion,ethnicity,beauty," #"mouthstatus,skinstatus") #print_result(printFuctionTitle("人脸检测"), res) img = cv2.imread('./imgResource/img.jpg') cv2.namedWindow('raw pic') cv2.imshow('raw pic', img) cv2.waitKey(0) cv2.destroyAllWindows() faceStr = [] face_string = '' face_string_2 = '' api.faceset.delete(outer_id='test2', check_empty=0) ret = api.faceset.create(outer_id='test2') res = api.detect(image_file=File(faceSet_img), return_attributes='emotion') print_result("detect resule", res) faceList = res["faces"] print(len(faceList)) for i in range(len(faceList)): api.faceset.addface(outer_id='test2', face_tokens=faceList[i]["face_token"]) print(faceList[i]["face_token"]) for i in range(len(faceList)): faceStr.append(faceList[i]["face_token"]) r = api.faceset.getdetail(outer_id='test2') print_result('faceset结果', r)
class FaceDetection: def __init__(self, faceset, img_file, main_window, speaker, arduino): # 初始化对象,进行api的调用工作 self.api = API() self.faceset = faceset self.img_file = img_file self.main = main_window self.speaker = speaker self.arduino = arduino self.face_ids = [] self.face_tokens = [] self.popupMsg = PopupMsg() try: facetokens = self.api.faceset.getdetail(outer_id=faceset) for facetoken in facetokens.face_tokens: try: res = self.api.face.getdetail(face_token=facetoken) self.face_ids.append(res.user_id) self.face_tokens.append(facetoken) print('faces: ', res) except: self.popupMsg.popupmsg('API error', 'face.getdetail') except: try: ret = self.api.faceset.create(outer_id=faceset) except: self.popupMsg.popupmsg('API error', 'faceset.create') def identify(self): try: search_result = self.api.search(image_file=File(self.img_file), outer_id=self.faceset) face_confidence = search_result.results[0].confidence user_name = search_result.results[0].user_id if face_confidence > 50: self.main.name.setPlainText(user_name) self.speaker.speakMsg(user_name + ',你好,欢迎回来') self.arduino.open_door() else: self.speaker.speakMsg("对不起,没有你的记录") except: self.popupMsg.popupmsg('人脸检测API调用失败', '人脸检测') def addFace(self): user_name = self.main.name.toPlainText() print(user_name) if user_name: try: res = self.api.detect(image_file=File(self.img_file)) faceList = res.faces if len(faceList) > 0: try: self.api.faceset.addface( outer_id=self.faceset, face_tokens=faceList[0].face_token) try: self.api.face.setuserid( face_token=faceList[0].face_token, user_id=user_name) self.face_ids.append(user_name) self.face_tokens.append(faceList[0].face_token) self.popupMsg.popupmsg('加了: ' + user_name, '增加人脸') except: self.popupMsg.popupmsg('设置人脸名字失败', '增加人脸') except: self.popupMsg.popupmsg('增加人脸失败', '增加人脸') else: self.popupMsg.popupmsg('检测不到人脸', '增加人脸') except: self.popupMsg.popupmsg('检测人脸失败', '增加人脸') else: self.popupMsg.popupmsg('请输入名字', '增加人脸') self.main.name.setPlainText("") def deleteFace(self): user_name = self.main.name.toPlainText() name_deleted = False if user_name: i = 0 for user_id in self.face_ids: if user_name == user_id: try: res = self.api.faceset.removeface( outer_id=self.faceset, face_tokens=self.face_token[i]) if res.face_removed > 0: self.face_ids.remove(user_name) self.face_tokens.remove(self.face_token[i]) self.popupMsg.popupmsg('删除了: ' + user_name, '删除人脸') name_deleted = True break else: self.popupMsg.popupmsg('删除人脸失败: ' + user_name, '删除人脸') except: self.popupMsg.popupmsg('删除人脸失败: ' + user_name, '删除人脸') i += 1 if not name_deleted: self.popupMsg.popupmsg('找不到: ' + user_name, '删除人脸') self.main.name.setPlainText("") else: self.popupMsg.popupmsg('请输入名字', '删除人脸')
# 此方法专用来打印api返回的信息 def print_result(hit, result): print(hit) print('\n'.join(" " + i for i in pformat(result, width=75).split('\n'))) def printFuctionTitle(title): return "\n"+"-"*60+title+"-"*60; # 初始化对象,进行api的调用工作 api = API() # -----------------------------------------------------------人脸识别部分------------------------------------------- # 人脸检测:https://console.faceplusplus.com.cn/documents/4888373 res = api.detect(image_url=detech_img_url, return_attributes="gender,age,smiling,headpose,facequality," "blur,eyestatus,emotion,ethnicity,beauty," "mouthstatus,skinstatus") print_result(printFuctionTitle("人脸检测"), res) # 人脸比对:https://console.faceplusplus.com.cn/documents/4887586 # compare_res = api.compare(image_file1=File(face_search_img), image_file2=File(face_search_img)) # print_result("compare", compare_res) # 人脸搜索:https://console.faceplusplus.com.cn/documents/4888381 # 人脸搜索步骤 # 1,创建faceSet:用于存储人脸信息(face_token) # 2,向faceSet中添加人脸信息(face_token) # 3,开始搜索 # 删除无用的人脸库,这里删除了,如果在项目中请注意是否要删除