当前位置:   article > 正文

百度ai人脸融合_百度智能云人像融合

百度智能云人像融合
  1. import tkinter as tk
  2. from tkinter import *
  3. from tkinter import filedialog, messagebox
  4. from PIL import Image, ImageTk
  5. import base64
  6. import requests
  7. import os
  8. '''
  9. 步骤:
  10. 1.创建所需要的窗口和按钮
  11. 2.对两张图片进行b64处理
  12. 3.与百度人脸融合接口交互
  13. 4.返回的数据处理显示到相应的位置上
  14. 入口编写
  15. '''
  16. #百度数据接口准备,数据初始化
  17. facesImg1s = ''
  18. facesImg2s = ''
  19. apiKey = '自己申请的'
  20. secretKey = '自己申请的'
  21. def getToken():
  22. getTokenUrl = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+apiKey+'&client_secret='+secretKey
  23. response = requests.get(getTokenUrl)
  24. data = response.json()
  25. token = data.get('access_token')
  26. return token
  27. def resize(w,h,newW,newH,pilPicture):
  28. f1 = 1.0 * newW / w
  29. f2 = 1.0 * newH / h
  30. factor = min([f1, f2])
  31. width = int(w * factor)
  32. height = int(h * factor)
  33. return pilPicture.resize((width, height), Image.ANTIALIAS)
  34. def showPhoto(route,xPos,yPos):
  35. image = Image.open(route)
  36. w, h = image.size
  37. if w == 260 and h == 260:
  38. resized = image
  39. else:
  40. resized = resize(w,h,270,270,image)
  41. photo = ImageTk.PhotoImage(resized)
  42. photoLabel = tk.Label(window,image=photo,width=270,height=270)
  43. photoLabel.place(x=xPos,y=yPos)
  44. window.mainloop()
  45. #设置选择图片方法
  46. def setPhoto(number):
  47. global facesImg1s
  48. global facesImg2s
  49. tmpImg = filedialog.askopenfilename(initialdir="./images/getImages")
  50. if number == 1:
  51. facesImg1s = tmpImg
  52. showPhoto(facesImg1s,0,0)
  53. if number == 2:
  54. facesImg2s = tmpImg
  55. showPhoto(facesImg2s,300,0)
  56. #两张照片的融合处理
  57. def twoPhotosFuse(facesImg1s,facesImg2s):
  58. #判断2张照片是否都有否则提示
  59. if len(facesImg1s)== 0 or len(facesImg2s) == 0:
  60. messagebox.showwarning(title='请注意',message='请选择2张图片')
  61. else:
  62. #与百度进行交互处理
  63. access_token = getToken()
  64. request_url = 'https://aip.baidubce.com/rest/2.0/face/v1/merge'
  65. with open(facesImg1s,'rb') as f1:
  66. image1 = f1.read()
  67. b64Image1 = base64.b64encode(image1)
  68. with open(facesImg2s,'rb') as f2:
  69. image2 = f2.read()
  70. b64Image2 = base64.b64encode(image2)
  71. IMAGE_TYPE = 'BASE64'
  72. params = "{\"image_template\":{\"image\":\""+str(b64Image1,'utf-8')+"\",\"image_type\":\"BASE64\",\"quality_control\":\"NONE\"},\"image_target\":{\"image\":\""+str(b64Image2,'utf-8')+"\",\"image_type\":\"BASE64\",\"quality_control\":\"NONE\"}}"
  73. request_url = request_url+"?access_token="+access_token
  74. headers = {'content-type': 'application/json'}
  75. response = requests.post(request_url, data=params, headers=headers)
  76. if response:
  77. data = response.json()
  78. tmpImg = base64.b64decode(data['result']['merge_image'])
  79. #写入本地
  80. #根据文件目录创建一个名字
  81. nu = len(os.listdir('./images/createImages'))
  82. with open('./images/createImages/%s.jpg'%nu,'wb')as newF:
  83. newF.write(tmpImg)
  84. showPhoto("./images/createImages/%s.jpg"%nu,320,320)
  85. #方法分发器
  86. def chooseMethod(type):
  87. #选择第一张照片
  88. if type == 'chooseOnePhtoto':
  89. setPhoto(1)
  90. #选择第二张照片
  91. elif type == 'chooseTwoPhtoto':
  92. setPhoto(2)
  93. #人脸融合操作
  94. elif type == 'twoPhotosFuse':
  95. twoPhotosFuse(facesImg1s,facesImg2s)
  96. window = tk.Tk()
  97. window.geometry('1000x660')
  98. window.resizable(0,0)
  99. window.title('人脸操作入口')
  100. bgImg = ImageTk.PhotoImage(file="./images/baseImages/bg1.jpg")
  101. bg = tk.Label(window,width=1000,height=600,image=bgImg)
  102. bg.pack()
  103. #加载选择按钮的图片
  104. faceImage = ImageTk.PhotoImage(file='./images/baseImages/face1.png')
  105. #添加选择位置图片
  106. face1Button = tk.Button(window,image=faceImage,width=192,height=63,command=lambda:chooseMethod('chooseOnePhtoto'))
  107. face1Button.place(x=750,y=100)
  108. #加载选择按钮的图片
  109. face2Image = ImageTk.PhotoImage(file='./images/baseImages/face2.png')
  110. #添加选择位置图片
  111. face2Button = tk.Button(window,image=face2Image,width=192,height=63,command=lambda:chooseMethod('chooseTwoPhtoto'))
  112. face2Button.place(x=750,y=180)
  113. #加载选择按钮的图片
  114. selectImg = ImageTk.PhotoImage(file='./images/baseImages/2.png')
  115. select = tk.Button(window,image=selectImg,width=192,height=63,command=lambda:chooseMethod('twoPhotosFuse'))
  116. select.place(x=750,y=260)
  117. window.mainloop()

 效果:

 

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

闽ICP备14008679号