当前位置:   article > 正文

利用百度图像处理API接口实现人脸融合_百度智能云人像融合

百度智能云人像融合

目录

前言

一.百度智能云平台账号的创建

二.python调用百度API接口实现人脸融合

1.在百度智能云上获取token

 2.调用API接口实现人脸融合

 3.主程序

三.完整项目代码

四.对生成图片的后续处理

1.批量修改图片大小

2.批量修改图片的类型


前言

人脸造假技术最近几年很流行,利用生成对抗网络实现人脸生成等技术层出不穷,生成造假人脸和利用神经网络识别造假的人脸也是必要的。

一.百度智能云平台账号的创建

详细的创建步骤详见下面链接

https://blog.csdn.net/Yhen1/article/details/109409575https://blog.csdn.net/Yhen1/article/details/109409575

二.python调用百度API接口实现人脸融合

1.在百度智能云上获取token

client_id为官网获取的API Key,client_secret为官网获取的Secret Key.将下行client_id=后的....换为你的API Key,client_secret=后的....换为你的Secret Key

 2.调用API接口实现人脸融合

 3.主程序

三.完整项目代码

下面是调用百度API接口实现人脸融合

  1. import requests
  2. import base64
  3. import json
  4. # 获取token
  5. def get_token(client_id, client_secret):
  6. # client_id为官网获取的API Key,client_secret为官网获取的Secret Key.将下行client_id=后的....换为你的API Key,client_secret=后的....换为你的Secret Key
  7. url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=aFRkmkiCGpGZxUKfj8zY7Yak&client_secret=Ht9NmCICXtnfCVjfIkYneSPo8z9qaeGg"
  8. response = requests.get(url)
  9. resultJson = response.json()
  10. return resultJson['access_token']
  11. # 根据图片名读取图片,并转换成base64
  12. def read_photo(name):
  13. with open('%s' % name, 'rb') as f:
  14. base64_data = base64.b64encode(f.read())
  15. bd = base64_data.decode()
  16. return bd
  17. # 调用百度的接口,实现融合图片
  18. def face_fusion(token, template, target):
  19. url = 'https://aip.baidubce.com/rest/2.0/face/v1/merge'
  20. request_url = url + '?access_token=' + token
  21. params = {
  22. "image_template": {
  23. "image": template,
  24. "image_type": "BASE64",
  25. "quality_control": "NORMAL"
  26. },
  27. "image_target": {
  28. "image": target,
  29. "image_type": "BASE64",
  30. "quality_control": "NORMAL"
  31. },
  32. "merge_degree": "HIGH"
  33. }
  34. params = json.dumps(params)
  35. headers = {'content-type': 'application/json'}
  36. result = requests.post(request_url, data=params, headers=headers).json()
  37. if result['error_code'] == 0:
  38. res = result["result"]["merge_image"]
  39. down_photo(res)
  40. else:
  41. print(str(result['error_code'])+result['error_msg'])
  42. # 下载融合后图片
  43. def down_photo(data):
  44. imagedata = base64.b64decode(data)
  45. file = open(r'C:\Users\Administrator\Desktop\fake_image\\102.jpg', "wb") #修改为自己的路径,'wb'不改
  46. file.write(imagedata)
  47. # 主程序
  48. if __name__ == '__main__':
  49. # 这里的融合用一个男一个女的效果比较不错,所以用boy和girl命名
  50. # 路径按照自己的图片路径来
  51. boy = read_photo('results/1.jpg')
  52. girl = read_photo('results/2.jpg')
  53. token = get_token('aFRkmkiCGpGZxUKfj8zY7Yak', 'Ht9NmCICXtnfCVjfIkYneSPo8z9qaeGg') # 第一个改为API Key,第二个改为Secret Key
  54. face_fusion(token, boy, girl)

四.对生成图片的后续处理

1.批量修改图片大小

  1. '''批量修改图片的尺寸(像素大小)'''
  2. from PIL import Image
  3. import os
  4. path="C:/Users/Administrator/Desktop/real" #图片所在的文件夹路径
  5. for maindir, subdir,file_name_list in os.walk(path):
  6. print(file_name_list)
  7. for file_name in file_name_list:
  8. image=os.path.join(maindir,file_name) #获取每张图片的路径
  9. file=Image.open(image)
  10. out=file.resize((600,600),Image.ANTIALIAS) #以高质量修改图片尺寸为(400,48)
  11. out.save(image)

2.批量修改图片的类型

  1. """批量改变图片的类型"""
  2. import os
  3. class BatchRename():
  4. def __init__(self):
  5. self.path = 'C:/Users/Administrator/Desktop/data-1' #表示需要命名处理的文件夹目录,复制地址后注意反斜杠
  6. def rename(self):
  7. filelist = os.listdir(self.path) #获取文件路径
  8. total_num = len(filelist) #获取文件长度(文件夹下图片个数)
  9. i = 1 #表示文件的命名是从1开始的
  10. for item in filelist:
  11. if item.endswith('.jpg') or item.endswith('.jfif'): #初始的图片的格式为jpg格式的(或者源文件是png格式及其他格式,后面的转换格式就可以调整为自己需要的格式即可,我习惯转成.jpg)
  12. src = os.path.join(os.path.abspath(self.path), item)
  13. dst = os.path.join(os.path.abspath(self.path), format(str(i)) + '.jpg')#处理后的格式也为jpg格式的,当然这里可以改成png格式
  14. # 这种情况下的命名格式为000xxxx.jpg形式,可以自主定义想要的格式
  15. try:
  16. os.rename(src, dst)
  17. print ('converting %s to %s ...' % (src, dst))
  18. i = i + 1
  19. except:
  20. continue
  21. print ('total %d to rename & converted %d jpgs' % (total_num, i))
  22. if __name__ == '__main__':
  23. demo = BatchRename()
  24. demo.rename()

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

闽ICP备14008679号