先上效果图 多个人在同一个图上可以一起识别,可以获取颜值评分、岁数、性别、表情、是否佩戴眼镜等一系列信息。有想要尝试的小伙伴可以边看边尝试呦! 有想要提前尝试的小伙伴可下载尝试呦 首先,需要去百度开放平台申请token,后续识别需要使用。 然后需要用到四个python包
from wxpy import *
import requests
import base64
import datetime
from PIL import Image, ImageDraw
wxpy 用来获取微信好友发来的信息 requests 请求获取token base64 图片转base64 datetime 获取日期 PIL 图像处理工具
引入之后,初始化程序
self.bot = Bot()
self.myself = self.bot.self
self.friends = self.bot.friends()
self.token = ''
self.img = []
初始化微信
@grade.bot.register()
def reply (msg):
if msg.type == 'Picture':
if str(msg.member) == 'None':
state = 0
name = str(msg.sender)[9:-1]
member = ''
print('收到%s发来的图片...' % name)
else:
name = msg.sender.name
member = str(msg.member)[9:-1]
state = 1
print('收到群%s里%s发来的图片...' %(name, member))
获取百度开放平台token
def getToken (self):
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=client_id&client_secret=client_secret'
res = requests.get(host)
if res:
self.token = res.json()['access_token']
请求打分接口
def faceLevel (self):
print('尝试请求百度接口...')
params = "{\"image\":\"%s\",\"image_type\":\"BASE64\",\"face_field\":\"age,beauty,expression,face_shape,gender,glasses,landmark,landmark150,race,quality,eye_status,emotion,face_type\",\"max_face_num\":\"9\"}" % self.img[0]['base64']
url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"
request_url = url + "?access_token=" + self.token
headers = {'content-type': 'application/json'}
res = requests.post(request_url,data=params,headers=headers)
// 请求成功 获取结果
if res.status_code == 200 and res.json()['error_code'] == 0:
faceList = res.json()['result']['face_list']
else:
del self.img[0]
print('检测失败')
画出脸所在位置
def selectFace (self, img, data):
saveUrl = img[:-4]+ '_select' +img[-4:]
base = Image.open(img)
d = ImageDraw.Draw(base)
for i in range(len(data)):
left = data[i]['location']['left']
top = data[i]['location']['top']
width = data[i]['location']['width']
height = data[i]['location']['height']
age = str(data[i]['age'])
beauty = str(data[i]['beauty'])
d.rectangle([left,top,left + width,top + height], outline="red")
d.text((left + 10, top + 10), "NO.%s\nage:%s\nbeauty:%s" %(str(i + 1), age, beauty), fill='red')
base.save(saveUrl)
base.close()
完成!!!
|