class BaiduFaceManager(object): def __init__(self): self._client = AipFace(baiduAI['APP_ID'], baiduAI['API_KEY'], baiduAI['SECRET_KEY']) def addFace(self, name, imgFile): print('Add Face for {0}.'.format(name)) with open(imgFile, 'rb') as fp: img = fp.read() image64 = base64.b64encode(img) ret = self._client.addUser(image64, "BASE64", baiduAI['GROUP'], name) print(ret) def updateFace(self, name, imgFile): print('Update Face for {0}.'.format(name)) with open(imgFile, 'rb') as fp: img = fp.read() image64 = base64.b64encode(img) ret = self._client.faceDelete(image64, "BASE64", baiduAI['GROUP'], name) print(ret) def deleteFace(self, name): print('Update Faces for {0}.'.format(name)) ret = self._client.deleteUser(baiduAI['GROUP'], name) print(ret)
class Face_id(object): def __init__(self): "your baidu ai APP_ID" self.APP_ID = '********' "your baidu ai API_KEY" self.API_KEY = '***************' "your baidu ai SECRET_KEY" self.SECRET_KEY = '*******************' self.aipFace = AipFace(self.APP_ID, self.API_KEY, self.SECRET_KEY) self.photo_name = '' self.group = 'Door' def get_file_content(self): with open(self.photo_name, 'rb') as fp: return fp.read() def get_file_constcontent(self): with open('me.jpg', 'rb') as fp: return fp.read() def face_detect(self): options = { 'max_face_num': 1, 'face_fields': 'age,beauty,expression,faceshape,gender,glasses' } result = self.aipFace.detect(self.get_file_content(), options) return result def face_add(self, group='Door', uid='', user_info='person'): result = self.aipFace.addUser(uid, user_info, group, self.get_file_content()) return result def face_recognize(self, group): options = {'user_top_num': 1} result = self.aipFace.identifyUser(group, self.get_file_content(), options) print result return result def get_user(self, uid='mrlian_door201', group='Door'): result = self.aipFace.getUser(uid) print result def get_group(self, start=0, num=100): options = {'start': start, 'num': num} result = self.aipFace.getGroupList(options) print result def get_groupuser(self, group='1', start=0, num=100): options = {'start': start, 'num': num} result = self.aipFace.getGroupUsers(group, options) return result def del_user(self, uid): result = self.aipFace.deleteUser(uid) return result
def clear_all_faces(): male_group_id = 'celebrity_male_test' female_group_id = 'celebrity_female_test' app_id = Config().get("AipConfig", "APP_ID") api_key = Config().get("AipConfig", "API_KEY") secret_key = Config().get("AipConfig", "SECRET_KEY") client = AipFace(app_id, api_key, secret_key) options = { "start": 0, "num": 500 } male_result = client.getGroupUsers(male_group_id, options).get('result') female_result = client.getGroupUsers(female_group_id, options).get('result') for m in male_result: response = client.deleteUser(m.get('uid')) print('removed: ', m.get('uid')) for f in female_result: response = client.deleteUser(f.get('uid')) print('removed: ', f.get('uid'))
class BaiduFaceIdentify: """基于百度 API 的人脸识别类""" def __init__(self, APP_ID='18174994', API_KEY='CPK3UVxCWFnTOL0BkyxSGWGy', SECRET_KEY='23uyGD9Tu4cgfC779tjjKPdenWYGgu2O'): self.client = AipFace(APP_ID, API_KEY, SECRET_KEY) def face_check(self, image_path: str, options: dict = None) -> list: """ 人脸检测:检测图片中的人脸并标记出位置信息 :param image_path: 图像路径 :param options: face_field,max_face_num,face_type,liveness_control :return: 检测到输出人脸信息,否则输出 None """ with open(image_path, "rb") as f: image_data = f.read() data = base64.b64encode(image_data) image = data.decode() imageType = "BASE64" """ 带参数调用人脸检测 """ if not options: """ 如果没有可选参数 """ options = {"face_field": "age", "max_face_num": 3} res = self.client.detect(image, imageType, options) else: res = self.client.detect(image, imageType) try: res_list = res['result'] except Exception as e: res_list = None print(e) return res_list def face_search(self, image_path: str, groupIdList: str = "group1", options: dict = None) -> dict: """ 1:N人脸搜索:也称为1:N识别,在指定人脸集合中,找到最相似的人脸; 1:N人脸认证:基于uid维度的1:N识别,由于uid已经锁定固定数量的人脸,所以检索范围更聚焦; 1:N人脸识别与1:N人脸认证的差别在于:人脸搜索是在指定人脸集合中进行直接地人脸检索操作, 而人脸认证是基于uid,先调取这个uid对应的人脸,再在这个uid对应的人脸集合中进行检索 (因为每个uid通常对应的只有一张人脸,所以通常也就变为了1:1对比); 实际应用中,人脸认证需要用户或系统先输入id,这增加了验证安全度,但也增加了复杂度,具体使用哪个接口需要视您的业务场景判断。 :param image_path: 图像路径 :param options: :param groupIdList: :return: """ with open(image_path, "rb") as f: image_data = f.read() data = base64.b64encode(image_data) image = data.decode() imageType = "BASE64" if not options: """ 如果没有可选参数 """ options = { "quality_control": "NORMAL", "liveness_control": "LOW", "max_user_num": 3, "match_threshold": 70 } """ 带参数调用人脸搜索 """ res = self.client.search(image, imageType, groupIdList, options) else: res = self.client.search(image, imageType, groupIdList) # try: # res_list = res['result'] # except Exception as e: # res_list = None return res def face_login(self, image_path: str, groupId: str = 'group1', userId: str = "0", options: dict = None) -> dict: """ 人脸注册,用于从人脸库中新增用户,可以设定多个用户所在组,及组内用户的人脸图片 :param image_path: 图像路径 :param userId: :param groupId: :param options: :return: """ with open(image_path, "rb") as f: image_data = f.read() data = base64.b64encode(image_data) image = data.decode() imageType = "BASE64" """ 如果没有可选参数 """ if not options: options = { "user_info": "face", "quality_control": "NORMAL", "liveness_control": "LOW", "action_type": "REPLACE" } """ 带参数调用人脸注册 """ res = self.client.addUser(image, imageType, groupId, userId, options) else: res = self.client.addUser(image, imageType, groupId, userId) return res def face_update(self, image_path: str, groupId: str = 'group1', userId: str = '0', options: dict = None) -> dict: """ 人脸更新:用于对人脸库中指定用户,更新其下的人脸图像。 :param image_path: :param groupId: :param userId: :param options: """ with open(image_path, "rb") as f: image_data = f.read() data = base64.b64encode(image_data) image = data.decode() imageType = "BASE64" """ 带参数调用人脸更新 """ if not options: """ 如果没有可选参数 """ options = { "user_info": "user's info", "quality_control": "NORMAL", "liveness_control": "LOW", "action_type": "REPLACE" } res = self.client.updateUser(image, imageType, groupId, userId, options) else: res = self.client.updateUser(image, imageType, groupId, userId) return res def get_user_list(self, groupId: str = 'group1', options: dict = None) -> dict: """ 获取用户列表:用于查询指定用户组中的用户列表 :param groupId: :param options: """ if not options: """ 如果没有可选参数 """ options = {"start": 0, "length": 50} """ 带参数调用获取用户列表 """ res = self.client.getGroupUsers(groupId, options) else: res = self.client.getGroupUsers(groupId) return res def delete_user(self, groupId: str = 'group1', userId: str = '0') -> dict: """ 删除用户:删除指定组中的用户 :param groupId: :param userId: """ res = self.client.deleteUser(groupId, userId) return res
class Guest_Recognition(): def __init__(self): rospy.init_node('whereisthis_image') # rospy.on_shutdown(self.cleanup) self.rate = rospy.Rate(1) # no use APP_ID = '18721308' API_KEY = 'lNQGdBNazTPv8LpSP4x0GQlI' SECRET_KEY = 'nW8grONY777n4I2KvpOVuKGDNiY03omI' self.client_face = AipFace(APP_ID, API_KEY, SECRET_KEY) self.bridge = CvBridge() self.image_type = "BASE64" self.filepath = "/home/dell/catkin_ws/src/Whereisthis/whereisthis_image/pictures/image.jpg" self.nowtask = 0 self.guest_list = ['0','1','2','3'] self.groupIdList = 'whereisthis_group' self.g1 = user() self.g1.user_id = '1' self.g1.facenum = 0 self.g2 = user() self.g2.user_id = '2' self.g2.facenum = 0 self.g3 = user() self.g3.user_id = '3' self.g3.facenum = 0 self.new = [0, 0, 0, 0] self.old = [0, 0, 0, 0] self.options_face = {} self.options_face["face_field"] = "age,gender" self.options_face["max_face_num"] = 3 self.options_face["face_type"] = "LIVE" # 如果人脸库之前有人脸建议先清掉数据,没有就忽略 self.clear_data(flag = True) #发布器 self.guest = rospy.Publisher('/image/findperson', Int8, queue_size=1) #订阅器 # rospy.Subscriber("/camera/rgb/image_raw", Image, self.imgCallback, queue_size = 1) rospy.Subscriber("/image_rate_transfer/image_raw", Image, self.imgCallback, queue_size = 1) rospy.Subscriber('/whereisthis_control', whereisthis, self.controlCallback) def clear_data(self, flag): rospy.loginfo('deleting data from cloud...') # groupid: self.goupIdList userid: '1' or '2' or '3' self.client_face.deleteUser(self.groupIdList, '1') self.client_face.deleteUser(self.groupIdList, '2') self.client_face.deleteUser(self.groupIdList, '3') rospy.loginfo('complete.') def controlCallback(self, msg): if msg.FinishState == False: self.nowtask = msg.NowTask def imgCallback(self, image_msg): t = whereisthis() if self.nowtask == t.GoalDescription: rospy.loginfo('imgCallback working...') try: cv_image = self.bridge.imgmsg_to_cv2(image_msg, "bgr8") except CvBridgeError as e: print(e) self.add_user(self.filepath, cv_image) # 打开文件 def fileopen(self, filepath): with open(filepath, 'rb') as fp: imgjson = base64.b64encode(fp.read()) data = str(imgjson).encode('utf-8') return data # 判断人脸已知 def judger(self ,result): if result['error_code'] == 0: result_sc = result['result']['user_list'][0]['score'] # print(result_sc,type(result_sc)) if result_sc > 80: return result['result']['user_list'][0]['user_id'] # return True else: return 0 else: return 0 # 人脸库搜索 groupIdList="用户组名称" def face_search(self, filepath, groupid='whereisthis_group'): image = self.fileopen(filepath) imageType = "BASE64" detect = self.client_face.detect(image, self.image_type, self.options_face) if detect: result = self.client_face.search(image, imageType, groupid) # print(result) # 打印出所有信息 return result else: return None # return self.judger(result) def face_add(self, filepath, image_msg, groupid, userid): # 人脸库增加 地址 组 用户 image = self.fileopen(filepath) imageType = "BASE64" result = self.client_face.addUser(image, imageType, groupid, userid) if result['error_code'] == 0: print("增加人脸成功") else: print("增加人脸失败") # print(result) def get_user_facenum(self, userid): if userid == '1': return self.g1 elif userid == '2': return self.g2 elif userid == '3': return self.g3 else: rospy.loginfo('there is no user id that matches.') # TODO: return what def add_user(self, file_name, image_msg): cv2.imwrite(file_name, image_msg) search_result = self.face_search(file_name, 'whereisthis_group') #id curusr = user() # 没人脸 if search_result == None: return # 加入新人脸 if search_result['error_code'] == 222207: userid = '1' self.g1.facenum += 1 curusr = self.g1 which = self.guest_list.index(userid) t = Int8() t.data = which self.guest.publish(t) rospy.loginfo('this is a new face: 222207') elif search_result['error_code'] == 0 : if search_result['result']['user_list'][0]['score'] < 80: # --------------------------------------- num = len(search_result['result']['user_list'][0]['user_id']) if num == 0: userid = '1' self.g1.facenum += 1 curusr = self.g1 elif num == 1: userid = '2' self.g2.facenum += 1 curusr = self.g2 elif num == 2: userid = '3' self.g3.facenum += 1 curusr = self.g3 else: rospy.loginfo('wrong with num') # TODO: what's next? # publish guest id which = self.guest_list.index(userid) t = Int8() t.data = which self.guest.publish(t) rospy.loginfo('this is a new face: 0') # 旧人脸 else: userid = search_result['result']['user_list'][0]['user_id'] curusr = self.get_user_facenum(userid) which = self.guest_list.index(userid) t = Int8() t.data = which self.guest.publish(t) rospy.loginfo('this is an old face.') # 加入人脸张数信息 未达到上限20则持续添加,否则不作处理 if curusr.facenum < 20 and curusr.user_id: self.face_add(file_name, image_msg, 'whereisthis_group', curusr.user_id) os.system('rosnode kill /whereisthis_image') # print(curusr.user_id , curusr.facenum) else: print('warning: no face detected. will keep detecting.') print('--------------------------')
class faceAPI(): def __init__(self): """API key information""" self.__APP_ID = 'APP_ID' self.__API_KEY = 'API_KEY' self.__SECRET_KEY = 'SECRET_KEY' self.__faceConnect=AipFace(self.__APP_ID,self.__API_KEY,self.__SECRET_KEY) #Face Feature configure self.feature_get_options = {'max_face_num':1,'face_fields':"race,gender,faceshape,qualities"} self.tempFeaturePath='../TempPicture/FaceFeature/Temp.png' self.tempFeatureDBPath ="../TempPicture/FaceFeature/TempFeatureDB/TempFeature" self.tempFTDBpath="../TempPicture/FaceFeature/TempDB/Temp" #Identify configure self.identify_option= {"ext_fields": "faceliveness","user_top_num":1} self.identify_detect_option={'max_face_num':10,'face_fields':"gender,faceshape,qualities"} self.tempIdentifyPath="../TempPicture/Identify/TempIdentify.png" self.tempIdentifyFeaturePath="../TempPicture/Identify/IdentifyFeature/FaceFeature" self.tempIdentifyDBPath="../TempPicture/Identify/IdentifyRecord/IDT" # image resize def rectangle(self,image,width,height,xloc,yloc,path): frame = cv2.rectangle(image, (xloc, yloc), (xloc+width,yloc+height), (255, 0, 0), 2) cv2.imwrite(path,frame) def resize(self,image,width,height,xloc,yloc,path): frame=cv2.resize(image[yloc:yloc + height, xloc:xloc + width], (width, height)) cv2.imwrite(path,frame) def face_feature_detect(self,image): self.__system_time=time.strftime('%Y_%m_%d_%H_%M_%S') cv2.imwrite(self.tempFeaturePath,image) __faceresult = self.__faceConnect.detect(image=get_file_content(self.tempFeaturePath), options=self.feature_get_options) print(__faceresult) if __faceresult['result_num'] ==1: race=__faceresult['result'][0]['race'] gender=__faceresult['result'][0]['gender'] if gender=='male': gender='男' else: gender='女' face_probability, human_probability = __faceresult['result'][0]['face_probability'],__faceresult['result'][0]['qualities']['type']['human'] if race=='yellow': race='黃色' elif race=='white': race='白色' elif race=='black': race='黑色' if face_probability<=0.9 or human_probability<=0.9: print("低於取樣標準\n再試一次") else: print("性別:{}\n膚色:{}\n臉部置信度:{}\n人臉置信度:{}".format( gender,race, face_probability, human_probability)) xloc, yloc = __faceresult['result'][0]['location']['left'], __faceresult['result'][0]['location']['top'] width, height = __faceresult['result'][0]['location']['width'], __faceresult['result'][0]['location']['height'] # image mark face location self.rectangle(image=image,path=self.tempFeaturePath, width=width,height=height,xloc=xloc,yloc=yloc) self.rectangle(image=image, path=self.tempFTDBpath+self.__system_time+".png", width=width, height=height, xloc=xloc, yloc=yloc) #image cut face location self.resize(image=image,path=self.tempFeatureDBPath + self.__system_time + ".png" ,width=width,height=height,xloc=xloc,yloc=yloc) add_result=self.__faceConnect.addUser(uid="test02",group_id="groupTest", user_info="姓名:{}|性別:{}|膚色:{}".format("宋東儒",gender,race), image=get_file_content("../TempPicture/FaceFeature/Temp.png")) print(add_result) else: print("請勿超過一人或空白畫面") def identify_user(self,image): self.__system_time = time.strftime('%Y_%m_%d_%H_%M_%S') cv2.imwrite(self.tempIdentifyPath,image) identify_detect_result=self.__faceConnect.detect( image=get_file_content(self.tempIdentifyPath),options=self.identify_detect_option) detect_num=identify_detect_result['result_num'] print(detect_num) for index in range(0,detect_num): xloc,yloc=identify_detect_result['result'][index]['location']['left'],identify_detect_result['result'][index]['location']['top'] width,height=identify_detect_result['result'][index]['location']['width'],identify_detect_result['result'][index]['location']['height'] # image mark face location self.rectangle(image=image,width=width,height=height,xloc=xloc,yloc=yloc, path=self.tempIdentifyDBPath+self.__system_time+".png") # image cut face location self.resize(image=image,width=width,height=height,xloc=xloc,yloc=yloc, path=self.tempIdentifyFeaturePath+str(detect_num)+".png") __identify_result = self.__faceConnect.identifyUser( group_id='groupTest',image=get_file_content(self.tempIdentifyPath),options=self.identify_option) print(__identify_result) try: if __identify_result['result'][0]['scores'][-1] >=70: print("使用者ID:{}\n所屬群組:{}\n描述:{}".format( __identify_result['result'][0]['uid'], __identify_result['result'][0]['group_id'], __identify_result['result'][0]['user_info'])) else: print("Faild不通過\n不符合任一用戶") cv2.imshow("Suspect",cv2.imread(self.tempIdentifyDBPath+self.__system_time+".png")) except : print("不存在資料庫") def update_user(self): __update_result = self.__faceConnect.updateUser( uid='test_001', group_id='groupTest', user_info='testPhoto', image=get_file_content('../TempTake/jia.jpg') ) print(__update_result) def delete_user(self): __delete_result = self.__faceConnect.deleteUser(uid='test01') print(__delete_result) def delete_group_user(self): __delete_group_result=self.__faceConnect.deleteGroupUser(uid='test_002',group_id="groupTest") print(__delete_group_result) def all_user_query(self): __all_query_result = self.__faceConnect.getUser('test_001') print(__all_query_result) def user_query(self): query_options={"group_id":'groupTest'} __query_result = self.__faceConnect.getUser(uid='test_001' ,options=query_options) print(__query_result) def version(self): ver=self.__faceConnect.getVersion() print(ver) def get_groupList(self): __list=self.__faceConnect.getGroupList() print(__list) def get_groupUser(self,): __group_user=self.__faceConnect.getGroupUsers(group_id='groupTest') print(__group_user)
""" 调用人脸识别 """ client.identifyUser(groupId, image); """ 调用人脸认证 """ client.verifyUser(uid, groupId, image); """ 调用M:N 识别 """ client.multiIdentify(groupId, image); """ 调用人脸注册 """ client.addUser(uid, userInfo, groupId, image); """ 调用人脸更新 """ client.updateUser(uid, userInfo, groupId, image); """ 调用人脸删除 """ client.deleteUser(uid); """ 调用用户信息查询 """ client.getUser(uid); # BaiDu AI 接口详细说明 ### 人脸检测 ### """ 调用人脸检测 """ client.detect(image); """ 如果有可选参数 """ options = {} options["max_face_num"] = 2 # 最多处理人脸数目,默认值1