当前位置:   article > 正文

NO.56——Face Swapping with Face++ API(换脸)_swapface 官网 api

swapface 官网 api

在上篇博客中,介绍了应用dlib和openCV进行人脸融合的方法,这里介绍一个更简单、效果更好的方法,就是调用旷世科技的人脸融合接口:https://api-cn.faceplusplus.com/imagepp/v1/mergeface。

在实际运用过程中,还调用了人脸检测接口:https://api-cn.faceplusplus.com/facepp/v3/detect。

效果如图,可见,比前一种方法效果好很多:

 

具体代码如下:

  1. import requests
  2. import simplejson
  3. import json
  4. #接口的报文是通过base64加密传输的,这种加密方式是可逆的。所以在进行接口自动化时,需要对所传的参数进行base64编码
  5. import base64
  6. #Face++网址:[url]https://console.faceplusplus.com.cn/dashboard[/url]
  7. #第一步,获取人脸关键点
  8. def find_face(imgpath):
  9. print("正在查找……")
  10. http_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'
  11. data = {"api_key": 'api_key',
  12. "api_secret": 'api_secret',
  13. "image_url": imgpath, "return_landmark": 1}
  14. files = {"image_file": open(imgpath, "rb")}
  15. response = requests.post(http_url, data=data, files=files)
  16. req_con = response.content.decode('utf-8')
  17. req_dict = json.JSONDecoder().decode(req_con)
  18. this_json = simplejson.dumps(req_dict)
  19. this_json2 = simplejson.loads(this_json)
  20. print(this_json2)
  21. faces = this_json2['faces']
  22. list0 = faces[0]
  23. rectangle = list0['face_rectangle']
  24. #print(rectangle)
  25. return rectangle
  26. #第二步,换脸,其中图片的大小应不超过2M
  27. # number表示换脸的相似度
  28. def merge_face(image_url1, image_url2, image_url, number):
  29. ff1 = find_face(image_url1)
  30. ff2 = find_face(image_url2)
  31. rectangle1 = str(str(ff1['top']) + "," + str(ff1['left']) + "," + str(ff1['width']) + "," + str(ff1['height']))
  32. rectangle2 = str(ff2['top']) + "," + str(ff2['left']) + "," + str(ff2['width']) + "," + str(ff2['height'])
  33. print(rectangle2)
  34. url_add = "https://api-cn.faceplusplus.com/imagepp/v1/mergeface"
  35. f1 = open(image_url1, 'rb')
  36. f1_64 = base64.b64encode(f1.read())
  37. f1.close()
  38. f2 = open(image_url2, 'rb')
  39. f2_64 = base64.b64encode(f2.read())
  40. f2.close()
  41. data = {"api_key": 'api_key',
  42. "api_secret": 'api_secret',
  43. "template_base64": f1_64, "template_rectangle": rectangle1,
  44. "merge_base64": f2_64, "merge_rectangle": rectangle2, "merge_rate": number}
  45. response = requests.post(url_add, data=data)
  46. req_con1 = response.content.decode('utf-8')
  47. #print(req_con1)
  48. req_dict = json.JSONDecoder().decode(req_con1)
  49. result = req_dict['result']
  50. imgdata = base64.b64decode(result)
  51. file = open(image_url, 'wb')
  52. file.write(imgdata)
  53. file.close()
  54. image1 = '/Users/chenyan/Desktop/AI/googleAI/faceswap_Face++/11.jpg'
  55. image2 = '/Users/chenyan/Desktop/AI/googleAI/faceswap_Face++/bb.jpg'
  56. image = '/Users/chenyan/Desktop/AI/googleAI/faceswap_Face++/testbb.jpg'
  57. merge_face(image2, image1, image, 80)

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/123622
推荐阅读
相关标签
  

闽ICP备14008679号