昨夜寒风刺骨入魂 TM徐love
关注数: 7 粉丝数: 4 发帖数: 154 关注贴吧数: 36
大佬们看看这怎么办 这是在网上找的利用api接口 来实现ai换脸的 但这个第一行和第二行为什么会报错呢 大概意思是没有此模块 删了后也会报错 是在下面 api啥的都换了 文件路径也试着换了 还是不行 0import requests import simplejson import json import base64 #Face++网址:[url]http://tieba.baidu.com/mo/q/checkurl?url=https%3A%2F%2Fconsole.faceplusplus.com.cn%2Fdashboard&urlrefer=92458258c0987ecdaade804c78fc419e[/url] #第一步,获取人脸关键点 def find_face(imgpath): print("正在查找……") http_url = 'http://tieba.baidu.com/mo/q/checkurl?url=https%3A%2F%2Fapi-cn.faceplusplus.com%2Ffacepp%2Fv3%2Fdetect&urlrefer=3fdc5ec07f921153799d878f4e8c5157' data = {"api_key": 'nxp6gJ7E7sJH_p35j8fE4zdqV4S6_8JL', "api_secret": 'zKRFFIOLAOjFU1KUKReS49pboXUXPPRC', "image_url": imgpath, "return_landmark": 1} files = {"image_file": open(imgpath, "rb")} response = requests.post(http_url, data=data, files=files) req_con = response.content.decode('utf-8') req_dict = json.JSONDecoder().decode(req_con) this_json = simplejson.dumps(req_dict) this_json2 = simplejson.loads(this_json) print(this_json2) faces = this_json2['faces'] list0 = faces[0] rectangle = list0['face_rectangle'] #print(rectangle) return rectangle #第二步,换脸,其中图片的大小应不超过2M # number表示换脸的相似度 def merge_face(image_url1, image_url2, image_url, number): ff1 = find_face(image_url1) ff2 = find_face(image_url2) rectangle1 = str(str(ff1['top']) + "," + str(ff1['left']) + "," + str(ff1['width']) + "," + str(ff1['height'])) rectangle2 = str(ff2['top']) + "," + str(ff2['left']) + "," + str(ff2['width']) + "," + str(ff2['height']) print(rectangle2) url_add = "http://tieba.baidu.com/mo/q/checkurl?url=https%3A%2F%2Fapi-cn.faceplusplus.com%2Fimagepp%2Fv1%2Fmergeface&urlrefer=abd8bd49aa05ad540584cf2f0afdc641" f1 = open(image_url1, 'rb') f1_64 = base64.b64encode(f1.read()) f1.close() f2 = open(image_url2, 'rb') f2_64 = base64.b64encode(f2.read()) f2.close() data = {"api_key": 'nxp6gJ7E7sJH_p35j8fE4zdqV4S6_8JL', "api_secret": 'zKRFFIOLAOjFU1KUKReS49pboXUXPPRC', "template_base64": f1_64, "template_rectangle": rectangle1, "merge_base64": f2_64, "merge_rectangle": rectangle2, "merge_rate": number} response = requests.post(url_add, data=data) req_con1 = response.content.decode('utf-8') #print(req_con1) req_dict = json.JSONDecoder().decode(req_con1) result = req_dict['result'] imgdata = base64.b64decode(result) file = open(image_url, 'wb') file.write(imgdata) file.close() image1 = r"C:\Users\31056\Desktop\ai\1.jpg" image2 = r"C:\Users\31056\Desktop\ai\2.jpg" image = r"C:\Users\31056\Desktop\ai\igh.jpg" merge_face(image2, image1, image, 90)
1 下一页