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 savelandmarks_facepp(filename): api = API() video = loadvideo(filename) landmarks = [] print('Number of Frames: %d' % (len(video))) cnt = 0 for image in video: cv2.imwrite('temp.jpg', image) landmarks.append(get_landmarks_facepp(File('./temp.jpg'), api)) cnt = cnt + 1 print('%f%%' % (cnt / len(video) * 100)) # Dump landmarks to file with open(filename.split('.')[0] + '.facepp', 'wb') as f: pickle.dump(landmarks, f)
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'))
# 此方法专用来打印api返回的信息 def print_result(hit, result): with open("result.json", 'w+') as file: file.write('\n'.join(" " + i 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 # 人脸搜索步骤
import os import pymysql from pprint import pformat # import PythonSDK from PythonSDK.facepp import API, File # 图片暂存 img_transmit = './static/transmit.jpg' img_register = './static/register.jpg' basedir = os.path.abspath(os.path.dirname(__file__)) # 定义一个根目录 用于保存图片用 # api api = API() app = Flask(__name__) # 此方法专用来打印api返回的信息 def print_result(hit, result): print(hit) print('\n'.join(" " + i for i in pformat(result, width=75).split('\n'))) def print_function_title(title): return "\n" + "-" * 60 + title + "-" * 60 # 接受上传图片并据此进行人脸比对,返回检测到人的user_id
@ GitHub : https://github.com/JackyPJB @ Contact : [email protected] ------------------------------------------------- Description : ------------------------------------------------- """ import base64 import requests import cv2 import numpy as np # import PythonSDK from PythonSDK.facepp import API # 初始化对象,进行api的调用工作 api = API() __author__ = 'Max_Pengjb' # 融合图片,这是一个有双眼皮的人 merge_url = 'https://cdn.faceplusplus.com.cn/mc-official/scripts/demoScript/images/demo-pic114.jpg' # 这是没有双眼皮的人 template_url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574981557927&di=484eae05e3ed0c0f4d30914862a012a0&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20181110%2F2063daae7ad94d3294d21fda4d604a6b.jpeg' origin_img_res = requests.get(template_url) # text一般用于返回的文本 # content的一般用于对返回的其他数据类型 img_data_str = origin_img_res.content # file.content 是读取的远程文件的字节流 origin_img_np_array = np.frombuffer(img_data_str, np.uint8) # print(origin_img_np_array) origin_img = cv2.imdecode(origin_img_np_array, cv2.IMREAD_COLOR)
luke_bowsher = './KnightbookScraping/Grade-12/Male/Luke Bowsher_male_Grade-12.png' matt_phua = './KnightbookScraping/Grade-12/Male/Matthew Phua_male_Grade-12.png' abby_doll = './KnightbookScraping/Grade-12/Female/Abigail Doll_female_Grade-12.png' neha_tarkad = './KnightbookScraping/Grade-12/Female/Neha Tarakad Juneja_female_Grade-12.png' max_dostart = './KnightbookScraping/Grade-12/Male/Max Dostart-Meers Dostart Meers_male_Grade-12.png' paras_arora = './KnightbookScraping/Grade-12/Male/Paras Arora_male_Grade-12.png' joseph_ma = './KnightbookScraping/Grade-12/Male/Joseph Ma Chou_male_Grade-12.png' #此方法专用来打印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/20813963 #template_rectangle参数中的数据要通过人脸检测api来获取 mergeFace_res = api.mergeface(template_file=File(matt_phua), merge_file=File(santa)) print_result("mergeFace", mergeFace_res) # Start Fusion PythonSDK.ImagePro.ImageProCls.getMergeImg(mergeFace_res["result"])
# "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的调用工作 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)):
double_fold_eyelid = './imgResource/double_fold_eyelid5.jpg' # face++人脸融合后的双眼皮照片 double_fold_merge = './imgResource/double_fold_merge.jpg' # face++ 双眼皮照片 # 此方法专用来打印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,eyegaze") # 人脸分析 https://console.faceplusplus.com.cn/documents/4888383 # res = api.face.analyze(face_tokens="f914dfbca818c802acc07840dae19214", # return_landmark=2, image_url=detech_img_url, # return_attributes="gender,age,smiling,headpose,facequality," # "blur,eyestatus,emotion,ethnicity,beauty," # "mouthstatus,skinstatus,eyegaze" # ) # 皮肤分析API https://console.faceplusplus.com.cn/documents/119745378 # res = api.skinanalyze(image_url=detech_img_url)
segment_img = './imgResource/segment.jpg' # 用于人体抠像 merge_img = './imgResource/merge.jpg' # 用于人脸融合 # 此方法专用来打印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) img = cv2.imread('./imgResource/img.jpg') cv2.namedWindow('raw pic') cv2.imshow('raw pic', img) cv2.waitKey(0) cv2.destroyAllWindows() faceStr = [] face_string = ''
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('请输入名字', '删除人脸')
segment_img = './imgResource/segment.jpg' # 用于人体抠像 merge_img = './imgResource/merge.jpg' # 用于人脸融合 # 此方法专用来打印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)
faceSet_img = './imgResource/demo.jpeg' # 用于创建faceSet face_search_img = './imgResource/search.png' # 用于人脸搜索 segment_img = './imgResource/segment.jpg' # 用于人体抠像 merge_img = './imgResource/merge.jpg' # 用于人脸融合 # 此方法专用来打印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 # 人脸搜索步骤