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'))
# coding=utf-8 from aip import AipFace APP_ID = '10522678' API_KEY = 'V21FC4N90bTuMxk0qMixX5o1' SECRET_KEY = 'OqMDdmsFIXcHpA7wN5I6aeej2wZq8PIF' #初始化AipSpeech对象 aipface = AipFace(APP_ID, API_KEY, SECRET_KEY) def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() dilireba = '../face/di1.jpg' wulei = '../face/wulei1.jpg' zhangyishan = "../face/zhangyishan1.jpg" # aipface.addUser(uid='uid1',groupId='group1',image=get_file_content(dilireba),userInfo='迪丽热巴') # aipface.updateUser(uid='uid1',groupId='group1',image=get_file_content(dilireba),userInfo='dilireba') # print aipface.addUser(uid='uid2',groupId='group1',image=get_file_content(wulei),userInfo='吴磊') print aipface.addUser(uid='uid3', groupId='group1', image=get_file_content(zhangyishan), userInfo='张一山') print aipface.getGroupUsers('group1')['result'][2]['user_info']
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 Face_finder: #类变量 #picture 输入的图片 #app_id #aip_id #secret_key #client #tpye #result_list def __init__(self, picture): self.picture = img_change_to_BASE64(picture) self.type = "BASE64" self.result_list = [] def set_parameter_of_aip(self, groupId, app_id, aip_id, secret_key): self.groupId = groupId self.app_id = app_id self.aip_id = aip_id self.secret_key = secret_key #print('The app_id is {}'.format(self.app_id)) #print('The aip_id is {}'.format(self.aip_id)) #print('The secret_key is {}'.format(self.secret_key)) def start_server(self): self.client = AipFace(self.app_id, self.aip_id, self.secret_key) #print('start server successfully') def search_face(self, groupIdlist, picture): search_result = self.client.search(img_change_to_BASE64(picture), "BASE64", groupIdlist) return search_result def return_user_list(self, groupid): user_list = self.client.getGroupUsers(groupid) return user_list def detect_face(self): option = {} option['max_face_num'] = 10 option['face_type'] = 'LIVE' search_result = self.client.detect(self.picture, self.type, option) return search_result def process_the_face_detected(self, result, picture): face_num = result['result']['face_num'] face_locations = [] #获得脸的位置信息 for i in range(face_num): single_face_location = sub_picture( result['result']['face_list'][i]['location']['left'], result['result']['face_list'][i]['location']['top'], result['result']['face_list'][i]['location']['height'], result['result']['face_list'][i]['location']['width']) face_locations.append(single_face_location) #在原图中截取 face_list = [] for i in range(face_num): face = picture[face_locations[i].top:(face_locations[i].top + face_locations[i].height), face_locations[i].left:(face_locations[i].left + face_locations[i].width)] face_list.append(face) #展示截取的图片 for i in range(face_num): cv2.rectangle(picture, (face_locations[i].left, face_locations[i].top), (face_locations[i].left + face_locations[i].width, face_locations[i].top + face_locations[i].height), (255, 255, 0), 1) #cv2.namedWindow('show_face') #cv2.imshow('show_face',picture) #cv2.waitKey(0) return face_list def thread_for_detect_face(self, picture, locker, num): result = self.search_face(self.groupId, picture) #进入临界区 locker.acquire() #print("detect {}".format(result['result']['user_list'][0]['user_id'])) people = detected_people(result['result']['user_list'][0]['user_id'], num) self.result_list.append(people) locker.release()
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)