赞
踩
目录
人脸造假技术最近几年很流行,利用生成对抗网络实现人脸生成等技术层出不穷,生成造假人脸和利用神经网络识别造假的人脸也是必要的。
详细的创建步骤详见下面链接
client_id为官网获取的API Key,client_secret为官网获取的Secret Key.将下行client_id=后的....换为你的API Key,client_secret=后的....换为你的Secret Key
下面是调用百度API接口实现人脸融合
-
- import requests
- import base64
- import json
-
- # 获取token
- def get_token(client_id, client_secret):
- # client_id为官网获取的API Key,client_secret为官网获取的Secret Key.将下行client_id=后的....换为你的API Key,client_secret=后的....换为你的Secret Key
- url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=aFRkmkiCGpGZxUKfj8zY7Yak&client_secret=Ht9NmCICXtnfCVjfIkYneSPo8z9qaeGg"
- response = requests.get(url)
- resultJson = response.json()
- return resultJson['access_token']
-
- # 根据图片名读取图片,并转换成base64
- def read_photo(name):
- with open('%s' % name, 'rb') as f:
- base64_data = base64.b64encode(f.read())
- bd = base64_data.decode()
- return bd
-
-
- # 调用百度的接口,实现融合图片
- def face_fusion(token, template, target):
- url = 'https://aip.baidubce.com/rest/2.0/face/v1/merge'
- request_url = url + '?access_token=' + token
- params = {
- "image_template": {
- "image": template,
- "image_type": "BASE64",
- "quality_control": "NORMAL"
- },
- "image_target": {
- "image": target,
- "image_type": "BASE64",
- "quality_control": "NORMAL"
- },
- "merge_degree": "HIGH"
- }
- params = json.dumps(params)
- headers = {'content-type': 'application/json'}
- result = requests.post(request_url, data=params, headers=headers).json()
- if result['error_code'] == 0:
- res = result["result"]["merge_image"]
- down_photo(res)
- else:
- print(str(result['error_code'])+result['error_msg'])
-
- # 下载融合后图片
- def down_photo(data):
- imagedata = base64.b64decode(data)
- file = open(r'C:\Users\Administrator\Desktop\fake_image\\102.jpg', "wb") #修改为自己的路径,'wb'不改
- file.write(imagedata)
-
- # 主程序
- if __name__ == '__main__':
- # 这里的融合用一个男一个女的效果比较不错,所以用boy和girl命名
- # 路径按照自己的图片路径来
- boy = read_photo('results/1.jpg')
- girl = read_photo('results/2.jpg')
- token = get_token('aFRkmkiCGpGZxUKfj8zY7Yak', 'Ht9NmCICXtnfCVjfIkYneSPo8z9qaeGg') # 第一个改为API Key,第二个改为Secret Key
- face_fusion(token, boy, girl)

- '''批量修改图片的尺寸(像素大小)'''
-
- from PIL import Image
- import os
- path="C:/Users/Administrator/Desktop/real" #图片所在的文件夹路径
- for maindir, subdir,file_name_list in os.walk(path):
- print(file_name_list)
- for file_name in file_name_list:
- image=os.path.join(maindir,file_name) #获取每张图片的路径
- file=Image.open(image)
- out=file.resize((600,600),Image.ANTIALIAS) #以高质量修改图片尺寸为(400,48)
- out.save(image)
- """批量改变图片的类型"""
-
-
- import os
-
- class BatchRename():
- def __init__(self):
- self.path = 'C:/Users/Administrator/Desktop/data-1' #表示需要命名处理的文件夹目录,复制地址后注意反斜杠
-
- def rename(self):
- filelist = os.listdir(self.path) #获取文件路径
- total_num = len(filelist) #获取文件长度(文件夹下图片个数)
- i = 1 #表示文件的命名是从1开始的
- for item in filelist:
- if item.endswith('.jpg') or item.endswith('.jfif'): #初始的图片的格式为jpg格式的(或者源文件是png格式及其他格式,后面的转换格式就可以调整为自己需要的格式即可,我习惯转成.jpg)
- src = os.path.join(os.path.abspath(self.path), item)
- dst = os.path.join(os.path.abspath(self.path), format(str(i)) + '.jpg')#处理后的格式也为jpg格式的,当然这里可以改成png格式
- # 这种情况下的命名格式为000xxxx.jpg形式,可以自主定义想要的格式
- try:
- os.rename(src, dst)
- print ('converting %s to %s ...' % (src, dst))
- i = i + 1
- except:
- continue
- print ('total %d to rename & converted %d jpgs' % (total_num, i))
-
- if __name__ == '__main__':
- demo = BatchRename()
- demo.rename()

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。