def Merge(api_key, api_secret): """ 可以对模板图和融合图中的人脸进行融合操作。融合后的图片中将包含融合图中的人脸特征,以及模板图中的其他外貌特征与内容。 :param api_key: :param api_secret: :return: """ if not os.path.exists("merge"): os.mkdir("merge") url = "https://api-cn.faceplusplus.com/imagepp/v1/mergeface" data = {"api_key": api_key, "api_secret": api_secret} img1 = input("请输入 脸部图片 磁盘路径(支持相对路径):") img2 = input("请输入 模板图片 磁盘路径(支持相对路径):") if img1 and img1.strip() != "" and img2 and img2.strip( ) != "" and os.path.exists(img1) and os.path.exists(img2): data["merge_base64"] = get_image(img1) data["template_base64"] = get_image(img2) result = requests.post(url, data).json() if result != None and "error_message" not in result: time_used = result["time_used"] # 整个请求所花费的时间,单位为毫秒。 request_id = result["request_id"] # 用于区分每一次请求的唯一的字符串。 print("网络耗时:{}毫秒".format(time_used)) print("请求ID:{}".format(request_id)) imgdata = result["result"] Base64ToObj(imgdata, "merge/merge_{}".format(img1)) print("成功生成 merge/merge_{} 图片".format(img1)) input("\n按下Enter键回到主菜单") return result else: print("网络错误!") else: print("输入错误!")
def Face_3d(api_key, api_secret): """ 可根据单张或多张单人人脸图片,重建3D人脸效果。 :param api_key: :param api_secret: :return: """ url = "https://api-cn.faceplusplus.com/facepp/v1/3dface" data = { "api_key": api_key, "api_secret": api_secret, "texture": 1, "mtl": 1 } if not os.path.exists("3d"): os.mkdir("3d") value = 1 while True: type = input("你想使用几张人脸图片合成3D模型?【1~3】:") try: value = int(type) break except: print("输入错误") if value > 3: value = 3 for i in range(value): v = i + 1 img = input("请输入图片 {} 磁盘路径(支持相对路径):".format(v)) data["image_base64_{}".format(v)] = get_image(img) result = requests.post(url, data).json() if result != None and "error_message" not in result: request_id = result["request_id"] time_used = result["time_used"] print("请求ID:{}".format(request_id)) print("请求耗时:{}毫秒".format(time_used)) obj_file = result["obj_file"] Base64ToObj(obj_file, "3d/face.obj") texture_img = result["texture_img"] Base64ToObj(texture_img, "3d/tex.jpg") mtl_file = result["mtl_file"] Base64ToObj(mtl_file, "3d/face.mtl") print("3d文件夹下已生成 face.obj【模型】,face.mtl【材质】,tex.jpg【纹理】 文件") print("请用C4D、3Dmax、Maya等建模工具打开 obj 文件") transfer_matrix = result["transfer_matrix"] print("3D模型视角变换矩阵:") print(transfer_matrix) input("按下Enter键回到主菜单") return result else: print("网络请求错误") return
def Search(api_key, api_secret): """ 在一个已有的 FaceSet 中找出与目标人脸最相似的一张或多张人脸 :param api_key: :param api_secret: :return: """ url = "https://api-cn.faceplusplus.com/facepp/v3/search" data = {"api_key": api_key, "api_secret": api_secret} while True: type = input(""" \n请选择搜寻模式(输入数字): 1.利用图片进行搜寻 2.利用 face_token 进行比对 3.退出 """) aim = input("请输入目标人脸的 face_token :") if aim != None and type != None and aim.strip() != "" and type.strip( ) != "": if "1" in type: file = "" while file == None or file.strip() == "": file = input("请输入被搜寻图片的磁盘路径(支持相对路径):") data["image_base64"] = get_image(file) elif "2" in type: token = "" while token == None or token.strip() == "": token = input("请输入被搜寻图片的 face_token:") data["faceset_token"] = token elif "3" in type: break else: print("输入错误!") continue result = requests.post(url, data).json() else: print("输入错误!")
def FacialFeatures(api_key,api_secret): """ 根据单张正面人脸图片,分析人脸面部特征。 :param api_key: :param api_secret: :return:json """ url = "https://api-cn.faceplusplus.com/facepp/v1/facialfeatures" data = {"api_key": api_key, "api_secret": api_secret} type = input("请输入数字选择类型:\n1->本地图片\n2->网络图片\n") if type!=None and type.strip()!="": if "2" in type: img = input("请输入图片直链网址(记得要加前缀):") if img != None and img.strip()!="": data["image_url"] = img else: print("错误") return else: img = input("请输入需要检测面容的图片磁盘路径(支持相对路径):") if img != None and img.strip()!="": data["image_base64"] = get_image(img) else: print("错误") return # 返回人脸矫正结果 data["return_imagereset"] = 1 result = requests.post(url, data).json() if result != None and "error_message" not in result: #请求ID request_id = result["request_id"] #图片ID image_id = result["image_id"] #矫正后图片 image_reset = result["image_reset"] Base64ToObj(image_reset,"reset_{}".format(img)) #人脸矩形框的位置 top = result["face_rectangle"]["top"] left = result["face_rectangle"]["left"] width = result["face_rectangle"]["width"] height = result["face_rectangle"]["height"] #人脸姿势分析结果 pitch_angle = result["headpose"]["pitch_angle"] roll_angle = result["headpose"]["roll_angle"] yaw_angle = result["headpose"]["yaw_angle"] #整个请求所花费的时间,单位为毫秒 time_used = result["time_used"] #人脸特征分析的结果 it = result["result"] #三庭 #三庭比例 three_parts = it["three_parts"] parts_ratio = three_parts["parts_ratio"] #上庭 one_part = three_parts["one_part"] faceup_value = one_part["faceup_ratio"] faceup_result = one_part["faceup_result"] faceup = {"faceup_normal":"上庭标准","faceup_long":"上庭偏长","faceup_short":"上庭偏短"}.get(faceup_result) #中庭 two_part = three_parts["two_part"] facemid_value = two_part["facemid_ratio"] facemid_result = two_part["facemid_result"] facemid = {"facemid_normal":"中庭标准","facemid_long":"中庭偏长","facemid_short":"中庭偏短"}.get(facemid_result) #下庭 three_part = three_parts["three_part"] facedown_value = three_part["facedown_ratio"] facedown_result = three_part["facedown_result"] facedown = {"facedown_normal":"下庭标准","facedown_long":"下庭偏长","facedown_short":"下庭偏短"}.get(facedown_result) #五眼 five_eyes = it["five_eyes"] #五眼比例 eyes_ratio = five_eyes["eyes_ratio"] #眼部宽度 righteye = five_eyes["righteye"] lefteye = five_eyes["lefteye"] #五眼右侧分析结果 righteye_empty_result = five_eyes["one_eye"]["righteye_empty_result"] one_eye = {"righteye_empty_normal":"右眼外侧适中","righteye_empty_short":"右眼外侧偏窄","righteye_empty_long":"右眼外侧偏宽"}.get(righteye_empty_result) #内眼角间距分析结果 eyein_result = five_eyes["three_eye"]["eyein_result"] three_eye = {"eyein_normal":"内眼角间距适中","eyein_short":"内眼角间距偏窄","eyein_long":"内眼角间距偏宽"}.get(eyein_result) #五眼左侧分析结果 lefteye_empty_result = five_eyes["five_eye"]["lefteye_empty_result"] five_eye = {"lefteye_empty_normal":"左眼外侧适中","lefteye_empty_short":"左外外侧偏窄","lefteye_empty_long":"左眼外侧偏宽"}.get(lefteye_empty_result) #黄金三角 golden_triangle = it["golden_triangle"] #脸型分析结果 face = it["face"] #颞部宽度 tempus_length = face["tempus_length"] #颧骨宽度 zygoma_length = face["zygoma_length"] #脸部长度 face_length = face["face_length"] #下颌角宽度 mandible_length = face["mandible_length"] #下颌角度数 E = face["E"] #颞部宽度、颧部宽度(固定颧部为1)、下颌角宽度比(若为0则返回null) ABD_ratio = face["ABD_ratio"] #脸型判断结果 face_type = {"pointed_face":"瓜子脸","oval_face":"椭圆脸","diamond_face":"菱形脸","round_face":'圆形脸',"long_face":"长形脸","square_face":"方形脸","normal_face":'标准脸' }.get(face["face_type"],"未知") #下巴分析结果 jaw = it["jaw"] jaw_width = jaw["jaw_width"] jaw_length = jaw["jaw_length"] jaw_angle = jaw["jaw_angle"] jaw_type ={"flat_jaw":"圆下巴","sharp_jaw":"尖下巴","square_jaw":"方下巴"}.get(jaw["jaw_type"],"未知") #眉毛分析结果 eyebrow = it["eyebrow"] brow_width = eyebrow["brow_width"] brow_height = eyebrow["brow_height"] brow_uptrend_angle = eyebrow["brow_uptrend_angle"] brow_camber_angle = eyebrow["brow_camber_angle"] brow_thick = eyebrow["brow_thick"] eyebrow_type = {"bushy_eyebrows":"粗眉","eight_eyebrows":"八字眉","raise_eyebrows":'上挑眉',"straight_eyebrows":"一字眉","round_eyebrows":"拱形眉","arch_eyebrows":"柳叶眉","thin_eyebrows":"细眉" }.get(eyebrow["eyebrow_type"],"未知") #眼睛分析结果 eyes = it["eyes"] eye_width = eyes["eye_width"] eye_height = eyes["eye_height"] angulus_oculi_medialis = eyes["angulus_oculi_medialis"] eyes_type = {"round_eyes":"圆眼","thin_eyes":"细长眼","big_eyes":'大眼',"small_eyes":"小眼",'normal_eyes':'标准眼' }.get(eyes["eyes_type"],"未知") #鼻子分析结果 nose = it["nose"] nose_width = nose["nose_width"] nose_type = {"normal_nose":"标准鼻","thick_nose":"宽鼻","thin_nose":'窄鼻' }.get(nose["nose_type"],"未知") #嘴唇分析结果 mouth = it["mouth"] mouth_height = mouth["mouth_height"] mouth_width = mouth["mouth_width"] lip_thickness = mouth["lip_thickness"] angulus_oris = mouth["angulus_oris"] mouth_type = {"thin_lip":"薄唇","thick_lip":"厚唇","smile_lip":"微笑唇","upset_lip":'态度唇','normal_lip':'标准唇' }.get(mouth["mouth_type"],"未知") txt=""" 本次请求用时:{time_used} 毫秒 图片ID:{image_id} 矫正图片 ai_{img} 已生成! 人脸姿势分析:抬头 {pitch_angle}° 转头 {roll_angle}° 摇头 {yaw_angle}° 人脸矩形框: 宽度:{width} 像素 高度:{height} 像素 左上角横坐标:{left} 左上角纵坐标:{top} 人脸特征分析的结果: 三庭比例:{parts_ratio} 上庭比例:{faceup_value}% {faceup} 中庭比例:{facemid_value}% {facemid} 下庭比例:{facedown_value}% {facedown} 五眼比例:{eyes_ratio} {one_eye} {three_eye} {five_eye} 右眼宽度:{righteye}mm 左眼宽度:{lefteye}mm 眼型:{eyes_type} 宽度:{eye_width}mm 高度:{eye_height}mm 内眦角度数:{angulus_oculi_medialis}° 黄金三角度数:{golden_triangle}° 脸型:{face_type} 颞部宽度:{tempus_length}mm 颧骨宽度:{zygoma_length}mm 脸部长度:{face_length}mm 下颌角宽度:{mandible_length}mm 下颌角度数:{E}° 颞部宽度、颧部宽度(固定颧部为1)、下颌角宽度比:{ABD_ratio} 下巴:{jaw_type} 宽度:{jaw_width}mm 长度:{jaw_length}mm 角度:{jaw_angle}° 眉毛:{eyebrow_type} 粗细:{brow_thick}mm 宽度:{brow_width}mm 高度:{brow_height}mm 挑度:{brow_uptrend_angle}° 弯度:{brow_camber_angle}° 鼻型:{nose_type} 鼻翼宽度:{nose_width}mm 唇型判断:{mouth_type} 高度:{mouth_height}mm 宽度:{mouth_width}mm 厚度:{lip_thickness}mm 嘴角弯曲度:{angulus_oris}° """.format( time_used=time_used,image_id=image_id,img=img, pitch_angle=pitch_angle,roll_angle=roll_angle,yaw_angle=yaw_angle, width=width,height=height,left=left,top=top, parts_ratio=parts_ratio,faceup_value=faceup_value,faceup=faceup, facemid_value=facemid_value,facemid=facemid,facedown_value=facedown_value,facedown=facedown, eyes_ratio=eyes_ratio, one_eye=one_eye, three_eye=three_eye, five_eye=five_eye, righteye=righteye, lefteye=lefteye, eyes_type=eyes_type, eye_width=eye_width, eye_height=eye_height, angulus_oculi_medialis=angulus_oculi_medialis, golden_triangle=golden_triangle, face_type=face_type, tempus_length=tempus_length, zygoma_length=zygoma_length, face_length=face_length, mandible_length=mandible_length, E=E, ABD_ratio=ABD_ratio, jaw_type=jaw_type, jaw_width=jaw_width, jaw_length=jaw_length, jaw_angle=jaw_angle, eyebrow_type=eyebrow_type, brow_thick=brow_thick, brow_width=brow_width, brow_height=brow_height, brow_uptrend_angle=brow_uptrend_angle, brow_camber_angle=brow_camber_angle, nose_type=nose_type, nose_width=nose_width, mouth_type=mouth_type, mouth_height=mouth_height, mouth_width=mouth_width, lip_thickness=lip_thickness, angulus_oris=angulus_oris ) print(txt) f = open("log/{}.txt".format(logTime()),"w") f.write(txt) f.close() input("按下Enter键回到主菜单") return result else: print("网络请求错误!") return else: print("错误") return
def Compare(api_key, api_secret): """ 将两个人脸进行比对,来判断是否为同一个人,返回比对结果置信度和不同误识率下的阈值。 支持传入图片或 face_token 进行比对。使用图片时会自动选取图片中检测到人脸尺寸最大的一个人脸。 :param api_key: :param api_secret: :return: json[result],confidence """ url = "https://api-cn.faceplusplus.com/facepp/v3/compare" data = {"api_key": api_key, "api_secret": api_secret} while True: type = input(""" \n请选择对比模式(输入数字): 1.利用图片进行比对 2.利用 face_token 进行比对 3.退出 """) if type != None and "1" in type: img1 = input("请输入图片 1 磁盘路径(支持相对路径):") img2 = input("请输入图片 2 磁盘路径(支持相对路径):") if img1 and img1.strip() != "" and img2 and img2.strip( ) != "" and os.path.exists(img1) and os.path.exists(img2): data["image_base64_1"] = get_image(img1) data["image_base64_2"] = get_image(img2) else: print("图片路径有错误!") elif type != None and "2" in type: img1 = input("请输入图片 1 的 face_token:") img2 = input("请输入图片 2 的 face_token:") if img1 and img1.strip() != "" and img2 and img2.strip() != "": data["face_token1"] = img1.strip() data["face_token2"] = img2.strip() else: print("图片 face_token 有错误!") elif type != None and "3" in type: break else: print("输入错误!") continue result = requests.post(url, data).json() if result != None and "error_message" not in result: #用于区分每一次请求的唯一的字符串。 request_id = result["request_id"] #比对结果置信度,范围 [0,100],小数点后3位有效数字,数字越大表示两个人脸越可能是同一个人。 confidence = result["confidence"] #一组用于参考的置信度阈值,包含以下三个字段。每个字段的值为一个 [0,100] 的浮点数,小数点后 3 位有效数字。 thresholds = result["thresholds"] #1e-3:误识率为千分之一的置信度阈值; value_1e_3 = thresholds["1e-3"] #1e-4:误识率为万分之一的置信度阈值; value_1e_4 = thresholds["1e-4"] #1e-5:误识率为十万分之一的置信度阈值; value_1e_5 = thresholds["1e-5"] if float(confidence) > float(value_1e_5): confidence_rank = "十万分之一的误识率" elif float(confidence) > float(value_1e_4): confidence_rank = "万分之一的误识率" else: confidence_rank = "千分之一的误识率" #图片在系统中的标识 if "image_id1" in result and "image_id2" in result: image_id1 = result["image_id1"] image_id2 = result["image_id2"] else: image_id1 = "无" image_id2 = "无" #检测出的人脸数组,采用数组中的第一个人脸(最凸显的人脸)进行人脸比对 if "faces1" in result and "faces2" in result: faces1 = result["faces1"] faces2 = result["faces2"] else: faces1 = "1" faces2 = "1" #整个请求所花费的时间 time_used = result["time_used"] txt = """ \n本次网络请求用时: {time_used} 毫秒 请求ID : {request_id} 图片 1 ID : {image_id1} 图片 1 共检测出 {faces1} 张人脸(默认采用最凸显的人脸) 图片 2 ID : {image_id2} 图片 2 共检测出 {faces2} 张人脸(默认采用最凸显的人脸) 【{confidence_rank}】 2 张人脸拥有 {confidence}% 相似度 """.format(time_used=time_used, request_id=request_id, image_id1=image_id1, image_id2=image_id2, faces1=len(faces1), faces2=len(faces2), confidence_rank=confidence_rank, confidence=confidence) print(txt) f = open("log/{}.txt".format(logTime()), "w") f.write(txt) f.close() input("按下Enter键回到主菜单") return result, confidence else: print("网络请求错误!")
def Detect(api_key, api_secret): """ 检测图片中的人脸(支持一至多张人脸),并标记出边框。您也可以对尺寸最大的5张人脸进行分析,获得面部关键点、年龄、性别、头部姿态、微笑检测、眼镜检测以及人脸质量等信息。 :param api_key: :param api_secret: :return: json,[face_token] """ img = input("请输入图片磁盘路径(支持相对路径):") if img and img.strip() != "" and os.path.exists(img): image_base64 = get_image(img) url = "https://api-cn.faceplusplus.com/facepp/v3/detect" data = { "api_key": api_key, "api_secret": api_secret, "image_base64": image_base64, "return_attributes": "gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,ethnicity,beauty,mouthstatus,eyegaze,skinstatus", "beauty_score_min": 0, "beauty_score_max": 100 } result = requests.post(url, data).json() if result != None and "error_message" not in result: time_used = result["time_used"] # 整个请求所花费的时间,单位为毫秒。 face_num = result["face_num"] # 检测出的人脸个数 image_id = result["image_id"] # 被检测的图片在系统中的标识。 request_id = result["request_id"] # 用于区分每一次请求的唯一的字符串。 print(""" \n本次网络请求用时:{time_used}毫秒 请求ID:{request_id} 检测图片路径为:{img} 检测图片Base64:{image_base64} 检测图片ID:{image_id} 共检测出 {face_num} 张人脸 """.format(time_used=time_used, img=img, image_id=image_id, request_id=request_id, face_num=face_num, image_base64=image_base64)) face_id = [] for index in range(0, len(result["faces"])): # 人脸反馈内容 it = result["faces"][index] # 人脸的标识 face_token = it["face_token"] face_id.append(face_token) # 人脸矩形框的位置 face_rectangle = it["face_rectangle"] width = face_rectangle["width"] # 矩形框的宽度 height = face_rectangle["height"] # 矩形框的高度 face_left = face_rectangle["left"] # 矩形框左上角像素点的横坐标 face_top = face_rectangle["top"] # 矩形框左上角像素点的纵坐标 # 人脸属性内容 attr = result["faces"][index]["attributes"] # 性别分析结果 if attr["gender"]["value"] == "Male": gender = "男性" elif attr["gender"]["value"] == "Female": gender = "女性" # 年龄分析结果 age = attr["age"]["value"] # 笑容分析结果 smile_value = attr["smile"]["value"] if int(float(smile_value)) > 50: smile = "【有】 程度{}%".format(smile_value) else: smile = "【无】 程度{}%".format(smile_value) # 人脸姿势分析结果 headpose = attr["headpose"] # 抬头角度 pitch_angle = headpose["pitch_angle"] # 旋转(平面旋转)角度 roll_angle = headpose["roll_angle"] # 摇头角度 yaw_angle = headpose["yaw_angle"] # 人脸模糊分析结果 blur = attr["blur"] # 人脸移动模糊度分析结果 motionblur = blur["motionblur"]["value"] # 人脸高斯模糊度分析结果 gaussianblur = blur["gaussianblur"]["value"] # 新型人脸模糊分析结果 blurness = blur["blurness"]["value"] # 眼睛状态信息 eyestatus = attr["eyestatus"] left_eye_status = eyestatus["left_eye_status"] right_eye_status = eyestatus["right_eye_status"] left_eye_max = max(left_eye_status, key=lambda k: left_eye_status[k]) right_eye_max = max(right_eye_status, key=lambda k: right_eye_status[k]) eye = { "occlusion": "眼睛被遮挡", "no_glass_eye_open": "不戴眼镜且睁眼", "normal_glass_eye_close": "佩戴普通眼镜且闭眼", "normal_glass_eye_open": "佩戴普通眼镜且睁眼", "dark_glasses": "佩戴墨镜", "no_glass_eye_close": "不戴眼镜且闭眼" } left_eye = eye.get(left_eye_max, "不戴眼镜且睁眼") # 文字表示 left_eye_value = left_eye_status[left_eye_max] # 置信度 right_eye = eye.get(right_eye_max, "不戴眼镜且睁眼") right_eye_value = right_eye_status[right_eye_max] # 情绪识别结果 emotion = attr["emotion"] anger = emotion["anger"] # 愤怒 disgust = emotion["disgust"] # 厌恶 fear = emotion["fear"] # 恐惧 happiness = emotion["happiness"] # 高兴 neutral = emotion["neutral"] # 平静 sadness = emotion["sadness"] # 伤心 surprise = emotion["surprise"] # 惊讶 # 人脸照片质量 facequality = attr["facequality"]["value"] # 人种分析结果 ethnicity = attr["ethnicity"]["value"] if str(ethnicity).strip() == "": ethnicity = "未知" # 颜值识别结果 beauty = attr["beauty"] male_score = beauty["male_score"] # 男性认为的此人脸颜值分数 female_score = beauty["female_score"] # 女性认为的此人脸颜值分数 # 嘴部状态信息 mouthstatus = attr["mouthstatus"] mouth_max = max(mouthstatus, key=lambda k: mouthstatus[k]) mouth = { "surgical_mask_or_respirator": "嘴部被医用口罩或呼吸面罩遮挡", "other_occlusion": "嘴部被其他物体遮挡", "close": "嘴部没有遮挡且闭上", "open": "嘴部没有遮挡且张开" }.get(mouth_max, "嘴部没有遮挡且闭上") mouth_value = mouthstatus[mouth_max] # 眼球位置与视线方向信息 eyegaze = attr["eyegaze"] left_eye_gaze = eyegaze["left_eye_gaze"] right_eye_gaze = eyegaze["right_eye_gaze"] # 面部特征识别结果 skinstatus = attr["skinstatus"] health = skinstatus["health"] # 健康 stain = skinstatus["stain"] # 色斑 acne = skinstatus["acne"] # 青春痘 dark_circle = skinstatus["dark_circle"] # 黑眼圈 txt = """ \n人脸{index} 检测结果如下: 人脸标识:{face_token} 照片质量:{facequality} 分 人脸模糊度:{blur}% 人脸矩形框: 宽度:{width} 像素 高度:{height} 像素 左上角横坐标:{face_left} 左上角纵坐标:{face_top} 性别:{gender} 年龄:{age} 岁 人种分析:{ethnicity} 人脸姿势:抬头 {pitch_angle}° 转头 {roll_angle}° 摇头 {yaw_angle}° 笑容:{smile} 眼睛状态: 【左眼】{left_eye} 置信度 {left_eye_value}% 【右眼】{right_eye} 置信度 {right_eye_value}% 嘴部状态:{mouth} 置信度 {mouth_value}% 情绪识别: {anger}% 愤怒 {disgust}% 厌恶 {fear}% 恐惧 {happiness}% 高兴 {neutral}% 平静 {sadness}% 伤心 {surprise}% 惊讶 颜值识别:男性评分:{male_score}分 女性评分:{female_score}分 面部特征:健康 {health}% 色斑 {stain}% 青春痘 {acne}% 黑眼圈 {dark_circle}% """.format(index=index + 1, facequality=facequality, blur=int((float(blurness) + float(gaussianblur) + float(motionblur)) / 3), gender=gender, age=age, ethnicity=ethnicity, pitch_angle=pitch_angle, roll_angle=roll_angle, yaw_angle=yaw_angle, smile=smile, left_eye=left_eye, left_eye_value=left_eye_value, right_eye=right_eye, right_eye_value=right_eye_value, mouth=mouth, mouth_value=mouth_value, anger=anger, disgust=disgust, fear=fear, happiness=happiness, neutral=neutral, face_token=face_token, sadness=sadness, surprise=surprise, male_score=float(male_score), female_score=float(female_score), health=health, stain=stain, acne=acne, dark_circle=dark_circle, width=width, height=height, face_left=face_left, face_top=face_top) f = open("log/{}.txt".format(logTime()), "w") f.write(txt) f.close() print(txt) for index in range(0, len(face_id)): print("人脸{index} face_token : {id}".format(index=index + 1, id=face_id[index])) input("按下Enter键回到主菜单") return result, face_id else: print("网络请求错误!") else: print("图片路径错误或不存在")
def Gesture(api_key, api_secret): """ 检测图片中出现的所有的手部,并返回其在图片中的矩形框位置与相应的手势含义。目前可以识别 19 种手势。 :param api_key: :param api_secret: :return: """ url = "https://api-cn.faceplusplus.com/humanbodypp/v1/gesture" data = {"api_key": api_key, "api_secret": api_secret} img = input("请输入手势图片磁盘路径(支持相对路径):") if img and img.strip() != "" and os.path.exists(img): data["image_base64"] = get_image(img) result = requests.post(url, data).json() if result != None and "error_message" not in result: time_used = result["time_used"] # 整个请求所花费的时间,单位为毫秒。 image_id = result["image_id"] # 被检测的图片在系统中的标识。 request_id = result["request_id"] # 用于区分每一次请求的唯一的字符串。 hands = result["hands"] hand_value = len(hands) print("""\n本次网络请求用时:{time_used}毫秒 请求ID:{request_id} 检测图片路径为:{img} 检测图片ID:{image_id} 共检测出 {hand_value} 个手势""".format(time_used=time_used, request_id=request_id, img=img, image_id=image_id, hand_value=hand_value)) dict = { "unknown": "未知", "heart_a": "比心 A:手背朝画面,心尖向下,拇指指尖接触", "heart_b": "比心 B:手指第二关节接触,心尖向下,拇指指尖接触", "heart_c": "比心 C:手腕接触,心尖向下,剩下四指左右两手接触", "heart_d": "比心 D:食指大拇指交叉,食指朝上,其余手指折叠", "ok": "OK:食指拇指尖接触,剩余手指摊开", "hand_open": "手张开:五指打开,手心面向画面", "thumb_up": "点赞:竖大拇指,方向向上", "thumb_down": "差评:竖大拇指,方向向下", "rock": "ROCK:小拇指、食指、大拇指伸直,无名指、中指折起,手心对外", "namaste": "合十:双手合十", "palm_up": "手心向上:摊开手,手心朝上", "fist": "握拳:握拳,手心对外", "index_finger_up": "食指朝上:伸出食指,其余手指折起,手心对外", "double_finger_up": "双指朝上:伸出食指和中止,并拢,其余手指折起,手心对外", "victory": "胜利 :伸出食指和中止,张开,其余手指折起,手心对外", "big_v": "大V:伸出食指和大拇指,其余手指折起,手背朝外", "phonecall": "打电话:伸出大拇指和小指,其余手指折叠,手背对外", "beg": "作揖:一手握拳,另一手覆盖在其之上", "thanks": "感谢:一手握拳,另一手张开,手心覆盖在其之上" } count = 0 for item in hands: count += 1 width = item["hand_rectangle"]["width"] top = item["hand_rectangle"]["top"] height = item["hand_rectangle"]["height"] left = item["hand_rectangle"]["left"] print("\n手势{count}:".format(count=count)) ges = item["gesture"] ges_max = max(ges, key=lambda k: ges[k]) print(dict.get(ges_max, "未知")) print( """宽度:{width} 像素 高度:{height} 像素 左上角横坐标:{left} 左上角纵坐标:{top}""" .format(count=count, width=width, height=height, left=left, top=top)) input("\n按下Enter键回到主菜单") return result else: print("网络错误!") else: print("输入错误!") return
def Beautify(api_key, api_secret): """ 支持对图片中人像进行对美颜美型处理,以及对图像增加滤镜等。美颜包括:美白和磨皮;美型包括:大眼、瘦脸、小脸和去眉毛等处理。 :param api_key: :param api_secret: :return: """ url = "https://api-cn.faceplusplus.com/facepp/v2/beautify" data = {"api_key": api_key, "api_secret": api_secret} while True: img = input("请输入需要美化的图片磁盘路径(支持相对路径):") if img and img.strip() != "" and os.path.exists(img): img_base = get_image(img) data["image_base64"] = img_base print("请输入美颜参数:【默认为50】") whitening = input("请输入美白程度0~100:") if whitening == None or whitening.strip() == "": whitening = 50 else: whitening = int(float(whitening)) data["whitening"] = whitening smoothing = input("请输入磨皮程度0~100:") if smoothing == None or smoothing.strip() == "": smoothing = 50 else: smoothing = int(float(smoothing)) data["smoothing"] = smoothing thinface = input("请输入瘦脸程度0~100:") if thinface == None or thinface.strip() == "": thinface = 50 else: thinface = int(float(thinface)) data["thinface"] = thinface shrink_face = input("请输入小脸程度0~100:") if shrink_face == None or shrink_face.strip() == "": shrink_face = 50 else: shrink_face = int(float(shrink_face)) data["shrink_face"] = shrink_face enlarge_eye = input("请输入大眼程度0~100:") if shrink_face == None or enlarge_eye.strip() == "": enlarge_eye = 50 else: enlarge_eye = int(float(enlarge_eye)) data["enlarge_eye"] = enlarge_eye remove_eyebrow = input("请输入去眉毛程度0~100:") if remove_eyebrow == None or remove_eyebrow.strip() == "": remove_eyebrow = 50 else: remove_eyebrow = int(float(remove_eyebrow)) data["remove_eyebrow"] = remove_eyebrow filter_txt = """ 滤镜类型: 0 无滤镜 1 black_white 黑白 2 calm 平静 3 sunny 晴天 4 trip 旅程 5 beautify 美肤 6 wangjiawei 王家卫 7 cutie 唯美 8 macaron 可人儿 9 new_york 纽约 10 sakura 樱花 11 17_years_old 十七岁 12 clight 柔光灯 13 tea_time 下午茶 14 whiten 亮肤 15 chaplin 卓别林 16 flowers 花香 17 memory 回忆 18 ice_lady 冰美人 19 paris 巴黎 20 times 时光 21 lomo LOMO 22 old_times 旧时光 23 spring 早春 24 story 故事 25 abao 阿宝色 26 wlight 补光灯 27 warm 暖暖 28 glitter 绚烂 29 lavender 薰衣草 30 chanel 香奈儿 31 prague 布拉格 32 old_dream 旧梦 33 blossom 桃花 34 pink 粉黛 35 jiang_nan 江南 请输入滤镜类型数字: """ f_type = "" f_type = input(filter_txt) type_list = { "1": "black_white", "2": "calm", "3": "sunny", "4": "trip", "5": "beautify", "6": "wangjiawei", "7": "cutie", "8": "macaron", "9": "new_york", "10": "sakura", "11": "17_years_old", "12": "clight", "13": "tea_time", "14": "whiten", "15": "chaplin", "16": "flowers", "17": "memory", "18": "ice_lady", "19": "paris", "20": "times", "21": "lomo", "22": "old_times", "23": "spring", "24": "story", "25": "abao", "26": "wlight", "27": "warm", "28": "glitter", "29": "lavender", "30": "chanel", "31": "prague", "32": "old_dream", "33": "blossom", '34': "pink", '35': "jiang_nan" } if f_type == None and f_type.strip() == "": if int(f_type.strip()) in range(1, 36): data["filter_type"] = type_list[f_type.strip()] result = requests.post(url, data).json() if result != None and "error_message" not in result: print("本次请求ID:{}".format(result["request_id"])) print("本次请求用时 {} 毫秒".format(result["time_used"])) Base64ToObj(result["result"], 'ai_{}'.format(img)) print("成功生成 ai_{} 图片文件".format(img)) input("按Enter键回到主菜单") return result else: print("网络请求错误!")
def SkinAnalyze(api_key, api_secret): """ 对人脸图片,进行面部皮肤状态检测分析 :param api_key: :param api_secret: :return: """ url = "https://api-cn.faceplusplus.com/facepp/v1/skinanalyze_advanced" data = {"api_key": api_key, "api_secret": api_secret} while True: img = input("请输入需要检测面容的图片磁盘路径(支持相对路径):") if img != None and img.strip() != "" and os.path.exists(img): data["image_base64"] = get_image(img) result = requests.post(url, data).json() if result != None and "error_message" not in result: # 请求ID request_id = result["request_id"] # 人脸矩形框的位置 top = result["face_rectangle"]["top"] left = result["face_rectangle"]["left"] width = result["face_rectangle"]["width"] height = result["face_rectangle"]["height"] # 整个请求所花费的时间,单位为毫秒 time_used = result["time_used"] it = result["result"] #肤色 skin_color = { "0": "透白", "1": "白皙", "2": "自然", "3": "小麦", "4": "黝黑" }.get(str(it["skin_color"]["value"]), "未知") skin_color_c = it["skin_color"]["confidence"] #肤龄 skin_age = it["skin_age"]["value"] #右眼双眼皮检测 right_eyelids = { "0": "单眼皮", "1": "平行双眼皮", "2": "扇形双眼皮" }.get(str(it["right_eyelids"]["value"]), "未知") right_eyelids_c = it["right_eyelids"]["confidence"] #左眼双眼皮检测 left_eyelids = { "0": "单眼皮", "1": "平行双眼皮", "2": "扇形双眼皮" }.get(str(it["left_eyelids"]["value"]), "未知") left_eyelids_c = it["left_eyelids"]["confidence"] #眼袋检测 eye_pouch = { "0": "无眼袋", "1": "有眼袋" }.get(str(it["eye_pouch"]["value"]), "未知") eye_pouch_c = it["eye_pouch"]["confidence"] #黑眼圈检测 dark_circle = { "0": "无黑眼圈", "1": "色素型黑眼圈", "2": "血管型黑眼圈", "3": "阴影型黑眼圈" }.get(str(it["dark_circle"]["value"]), "未知") dark_circle_c = it["dark_circle"]["confidence"] #抬头纹检测 forehead_wrinkle = { "0": "无抬头纹", "1": "有抬头纹" }.get(str(it["forehead_wrinkle"]["value"]), "未知") forehead_wrinkle_c = it["forehead_wrinkle"]["confidence"] #鱼尾纹检测 crows_feet = { "0": "无鱼尾纹", "1": "有鱼尾纹" }.get(str(it["crows_feet"]["value"]), "未知") crows_feet_c = it["crows_feet"]["confidence"] #眼部细纹检测 eye_finelines = { "0": "无眼部细纹", "1": "有眼部细纹" }.get(str(it["eye_finelines"]["value"]), "未知") eye_finelines_c = it["eye_finelines"]["confidence"] #眉间纹检测 glabella_wrinkle = { "0": "无眉间纹", "1": "有眉间纹" }.get(str(it["glabella_wrinkle"]["value"]), "未知") glabella_wrinkle_c = it["glabella_wrinkle"]["confidence"] #法令纹检测 nasolabial_fold = { "0": "无法令纹", "1": "有法令纹" }.get(str(it["nasolabial_fold"]["value"]), "未知") nasolabial_fold_c = it["nasolabial_fold"]["confidence"] #肤质检测 types = it["skin_type"]["details"] type = 0 value = 0 for k in types: i = types[k] if float(i["confidence"]) > type: type = float(i["confidence"]) value = i["value"] skin_type = { "0": "油性皮肤", "1": "干性皮肤", "2": "中性皮肤", "3": "混合性皮肤" }.get(str(value), "未知") skin_type_c = type #前额毛孔检测 pores_forehead = { "0": "前额无毛孔粗大", "1": "前额有毛孔粗大" }.get(str(it["pores_forehead"]["value"]), "未知") pores_forehead_c = it["pores_forehead"]["confidence"] #左脸颊毛孔检测 pores_left_cheek = { "0": "左脸颊无毛孔粗大", "1": "左脸颊有毛孔粗大" }.get(str(it["pores_left_cheek"]["value"]), "未知") pores_left_cheek_c = it["pores_left_cheek"]["confidence"] #右脸颊毛孔检测 pores_right_cheek = { "0": "右脸颊无毛孔粗大", "1": "右脸颊有毛孔粗大" }.get(str(it["pores_right_cheek"]["value"]), "未知") pores_right_cheek_c = it["pores_right_cheek"]["confidence"] #下巴毛孔检测结果 pores_jaw = { "0": "下巴无毛孔粗大", "1": "下巴有毛孔粗大" }.get(str(it["pores_jaw"]["value"]), "未知") pores_jaw_c = it["pores_jaw"]["confidence"] #黑头检测结果 blackhead = { "0": "无黑头", "1": "轻度", "2": "中度", "3": "重度" }.get(str(it["blackhead"]["value"]), "未知") blackhead_c = it["blackhead"]["confidence"] #痘痘检测结果 acne = len(it["acne"]["rectangle"]) #痣检测结果 mole = len(it["mole"]["rectangle"]) # 斑点检测结果 skin_spot = len(it["skin_spot"]["rectangle"]) txt = """ 本次请求用时:{time_used} 毫秒 人脸矩形框: 宽度:{width} 像素 高度:{height} 像素 左上角横坐标:{left} 左上角纵坐标:{top} 肤色{skin_color} 置信度:{skin_color_c}% 肤龄:{skin_age} 岁 右眼{right_eyelids} 置信度:{right_eyelids_c}% 左眼{left_eyelids} 置信度:{left_eyelids_c}% 眼袋{eye_pouch} 置信度:{eye_pouch_c}% {dark_circle} 置信度:{dark_circle_c}% {skin_type} 置信度:{skin_type_c}% {forehead_wrinkle} 置信度:{forehead_wrinkle_c}% {crows_feet} 置信度:{crows_feet_c}% {eye_finelines} 置信度:{eye_finelines_c}% {glabella_wrinkle} 置信度:{glabella_wrinkle_c}% {nasolabial_fold} 置信度:{nasolabial_fold_c}% {pores_forehead} 置信度:{pores_forehead_c}% {pores_left_cheek} 置信度:{pores_left_cheek_c}% {pores_right_cheek} 置信度:{pores_right_cheek_c}% {pores_jaw} 置信度:{pores_jaw_c}% 黑头检测:{blackhead} 置信度:{blackhead_c}% 痘痘检测:{acne} 痣检测:{mole} 斑点检测:{skin_spot} """.format(nasolabial_fold=nasolabial_fold, time_used=time_used, width=width, height=height, left=left, top=top, skin_color=skin_color, skin_color_c=skin_color_c, skin_age=skin_age, right_eyelids=right_eyelids, right_eyelids_c=right_eyelids_c, left_eyelids=left_eyelids, left_eyelids_c=left_eyelids_c, eye_pouch=eye_pouch, eye_pouch_c=eye_pouch_c, dark_circle=dark_circle, dark_circle_c=dark_circle_c, forehead_wrinkle=forehead_wrinkle, forehead_wrinkle_c=forehead_wrinkle_c, crows_feet=crows_feet, crows_feet_c=crows_feet_c, eye_finelines=eye_finelines, eye_finelines_c=eye_finelines_c, glabella_wrinkle=glabella_wrinkle, glabella_wrinkle_c=glabella_wrinkle_c, nasolabial_fold_c=nasolabial_fold_c, skin_type=skin_type, skin_type_c=skin_type_c, pores_forehead=pores_forehead, pores_forehead_c=pores_forehead_c, pores_left_cheek=pores_left_cheek, pores_left_cheek_c=pores_left_cheek_c, pores_right_cheek=pores_right_cheek, pores_right_cheek_c=pores_right_cheek_c, pores_jaw=pores_jaw, pores_jaw_c=pores_jaw_c, blackhead=blackhead, blackhead_c=blackhead_c, acne=acne, mole=mole, skin_spot=skin_spot) print(txt) f = open("log/{}.txt".format(logTime()), "w") f.write(txt) f.close() input("按下Enter键回到主菜单") return result else: print("网络请求错误!") else: print("输入错误!")