赞
踩
- import tkinter as tk
- from tkinter import *
- from tkinter import filedialog, messagebox
- from PIL import Image, ImageTk
- import base64
- import requests
- import os
-
- '''
- 步骤:
- 1.创建所需要的窗口和按钮
- 2.对两张图片进行b64处理
- 3.与百度人脸融合接口交互
- 4.返回的数据处理显示到相应的位置上
- 入口编写
- '''
- #百度数据接口准备,数据初始化
-
-
- facesImg1s = ''
- facesImg2s = ''
-
-
- apiKey = '自己申请的'
- secretKey = '自己申请的'
- def getToken():
- getTokenUrl = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+apiKey+'&client_secret='+secretKey
- response = requests.get(getTokenUrl)
- data = response.json()
- token = data.get('access_token')
- return token
-
-
- def resize(w,h,newW,newH,pilPicture):
- f1 = 1.0 * newW / w
- f2 = 1.0 * newH / h
- factor = min([f1, f2])
- width = int(w * factor)
- height = int(h * factor)
- return pilPicture.resize((width, height), Image.ANTIALIAS)
-
- def showPhoto(route,xPos,yPos):
- image = Image.open(route)
- w, h = image.size
- if w == 260 and h == 260:
- resized = image
- else:
- resized = resize(w,h,270,270,image)
- photo = ImageTk.PhotoImage(resized)
- photoLabel = tk.Label(window,image=photo,width=270,height=270)
- photoLabel.place(x=xPos,y=yPos)
- window.mainloop()
- #设置选择图片方法
- def setPhoto(number):
- global facesImg1s
- global facesImg2s
- tmpImg = filedialog.askopenfilename(initialdir="./images/getImages")
- if number == 1:
- facesImg1s = tmpImg
- showPhoto(facesImg1s,0,0)
- if number == 2:
- facesImg2s = tmpImg
- showPhoto(facesImg2s,300,0)
- #两张照片的融合处理
- def twoPhotosFuse(facesImg1s,facesImg2s):
- #判断2张照片是否都有否则提示
- if len(facesImg1s)== 0 or len(facesImg2s) == 0:
- messagebox.showwarning(title='请注意',message='请选择2张图片')
- else:
- #与百度进行交互处理
- access_token = getToken()
- request_url = 'https://aip.baidubce.com/rest/2.0/face/v1/merge'
- with open(facesImg1s,'rb') as f1:
- image1 = f1.read()
- b64Image1 = base64.b64encode(image1)
- with open(facesImg2s,'rb') as f2:
- image2 = f2.read()
- b64Image2 = base64.b64encode(image2)
- IMAGE_TYPE = 'BASE64'
- 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\"}}"
- request_url = request_url+"?access_token="+access_token
- headers = {'content-type': 'application/json'}
- response = requests.post(request_url, data=params, headers=headers)
- if response:
- data = response.json()
- tmpImg = base64.b64decode(data['result']['merge_image'])
- #写入本地
- #根据文件目录创建一个名字
- nu = len(os.listdir('./images/createImages'))
- with open('./images/createImages/%s.jpg'%nu,'wb')as newF:
- newF.write(tmpImg)
- showPhoto("./images/createImages/%s.jpg"%nu,320,320)
- #方法分发器
- def chooseMethod(type):
- #选择第一张照片
- if type == 'chooseOnePhtoto':
- setPhoto(1)
- #选择第二张照片
- elif type == 'chooseTwoPhtoto':
- setPhoto(2)
- #人脸融合操作
- elif type == 'twoPhotosFuse':
- twoPhotosFuse(facesImg1s,facesImg2s)
- window = tk.Tk()
- window.geometry('1000x660')
- window.resizable(0,0)
- window.title('人脸操作入口')
- bgImg = ImageTk.PhotoImage(file="./images/baseImages/bg1.jpg")
- bg = tk.Label(window,width=1000,height=600,image=bgImg)
- bg.pack()
- #加载选择按钮的图片
- faceImage = ImageTk.PhotoImage(file='./images/baseImages/face1.png')
- #添加选择位置图片
- face1Button = tk.Button(window,image=faceImage,width=192,height=63,command=lambda:chooseMethod('chooseOnePhtoto'))
- face1Button.place(x=750,y=100)
- #加载选择按钮的图片
- face2Image = ImageTk.PhotoImage(file='./images/baseImages/face2.png')
- #添加选择位置图片
- face2Button = tk.Button(window,image=face2Image,width=192,height=63,command=lambda:chooseMethod('chooseTwoPhtoto'))
- face2Button.place(x=750,y=180)
- #加载选择按钮的图片
- selectImg = ImageTk.PhotoImage(file='./images/baseImages/2.png')
- select = tk.Button(window,image=selectImg,width=192,height=63,command=lambda:chooseMethod('twoPhotosFuse'))
- select.place(x=750,y=260)
- window.mainloop()
-
-
-
-
-
-
-
-
-
-
-
效果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。