教你用python制作人脸卡通画(附源码) 效果展示: 让我们开始学习之路: 原理:利用第三方人脸接口将图像人脸化 第三方接口注册地址:https://ai.minivision.cn/#/login 注册成功后-进入控制台-创建APP Key 记录好 APP Key 和App Secret 参见API文档开始调用吧
from PySide2.QtWidgets import QApplication
from PySide2.QtUiTools import QUiLoader
from PySide2.QtCore import QFile
from PySide2.QtWidgets import QFileDialog
from PySide2 import QtCore
from PySide2 import QtGui
import os
import hashlib
import time
import json
import requests
import base64
QtCore.QCoreApplication.addLibraryPath(os.path.join(os.path.dirname(QtCore.__file__), "plugins"))
class Stats:
def __init__(self):
qfle_stats=QFile('xs.ui')
qfle_stats.open(QFile.ReadOnly)
qfle_stats.close()
self.ui=QUiLoader().load(qfle_stats)
self.ui.pushButton.clicked.connect(self.openFileNameDialog)
self.ui.pushButton_2.clicked.connect(self.KT)
self.ui.comboBox.currentIndexChanged.connect(self.handleSelectionChange)
global ii
ii="baixi"
def handleSelectionChange(self):
method = self.ui.comboBox.currentText()
print(method)
global ii
if method=='白皙':
ii='baixi'
elif method=='怀旧':
ii = 'huaijiu'
elif method == '年代':
ii = 'niandai'
elif method == '咖啡':
ii = 'kafei'
elif method == '浪漫':
ii = 'langman'
elif method == '青葱':
ii = 'qingcong'
elif method == '想象':
ii = 'xiangxiang'
elif method == '梦幻':
ii = 'menghuan'
elif method == '浪漫':
ii = 'langman'
elif method == '字云':
ii = 'ziyun'
elif method == '素描':
ii = 'sumiao'
elif method == '剪纸':
ii = 'jianzhi'
elif method == '琉璃':
ii = 'liuli'
elif method == '皮革':
ii = 'pige'
elif method == '羊皮纸':
ii = 'yangpizhi'
elif method == '油画':
ii = 'youhua'
elif method == '豹纹':
ii = 'baowen'
elif method == '海洋':
ii = 'haiyang'
elif method == '金属':
ii = 'jinshu'
elif method == '香槟':
ii = 'xiangbin'
elif method == '大爆炸':
ii = 'dabaozha'
elif method == '星空':
ii = 'xingkong'
elif method == '牛仔':
ii = 'niuzai'
elif method == '星轨':
ii = 'xinggui'
elif method == '渲染':
ii = 'xuanran'
elif method == '意识流':
ii = 'yishiliu'
print(ii)
def openFileNameDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getOpenFileName(self.ui, "QFileDialog.getOpenFileName()", "",
"All Files (*);;Python Files (*.py)", options=options)
print(fileName)
self.ui.label.setText(fileName)
句柄 = self.ui.label_2.winId()
print('我的句柄是'+str(句柄))
pixmap = QtGui.QPixmap(fileName)
self.ui.label_2.setPixmap(pixmap)
self.ui.label_2.setScaledContents(True)
def KT(self):
app_key = '你的app key'
app_secret = '你的app secret'
timestamp = int(round(time.time() * 1000))
i = generate_token(timestamp, app_key, app_secret)
with open(self.ui.label.text(), 'rb')as f:
base64data = base64.b64encode(f.read())
s = base64data.decode()
params = {
"imageBase64": s,
"appKey": "你的app key",
"timestamp": timestamp,
"needFilter": "true",
"filterName": ii
}
params1 = json.dumps(params)
print(timestamp)
headers = {
'Content-Type': 'application/json',
'Token': i,
'cache-control': 'no-cache'
}
response = requests.post('https://ai.minivision.cn/apiagw/api/v1/cartoon/self_cartoon', headers=headers,
data=params1).text
response = json.loads(response)
print(response)
imagedata = base64.b64decode(response.get('data').get('base64'))
with open('abc.jpg', 'wb')as f:
f.write(imagedata)
句柄 = self.ui.label_3.winId()
print('我的句柄是'+str(句柄))
pixmap = QtGui.QPixmap('abc.jpg')
self.ui.label_3.setPixmap(pixmap)
self.ui.label_3.setScaledContents(True)
def generate_token(timestamp, app_key, app_secret):
text_array = [app_key, '{', app_secret, ':', str(timestamp), '}']
text = ''.join(text_array)
print(text)
m = hashlib.md5(text.encode('utf-8'))
return m.hexdigest()
if "__main__" == __name__:
app = QApplication([])
Stats = Stats()
Stats.ui.show()
app.exec_()
|