腾讯优图AI:https://open.youtu.qq.com/#/open
#功能:微信好友头像下载,并识别人脸,测出颜值,画出饼状图。
#PS:头像识别输出的第一列为好友备注名,没备注名的是数字。
#头像识别,首先申请腾讯优图,得到密钥。
import os
import itchat
import math
import PIL.Image as Image
import TencentYoutuyun
from pyecharts import Pie
#上述导入库自行安装
#itchat.auto_login(hotReload=True)#登录
#friends = itchat.get_friends()
#获取好友图像
def headimg():
base_path="img"
if not os.path.exists(base_path):
os.mkdir(base_path)
for count,f in enumerate(friends):
img=itchat.get_head_img(userName=f["UserName"])
img_name=f['RemarkName'] if f['RemarkName']!="" else f['RemarkName']
img_file=os.path.join(base_path,img_name+str(count)+".jpg")
imgFile=open(img_file,"wb")
imgFile.write(img)
imgFile.close()
#头像拼接
def createImg():
x=0
y=0
imgs=os.listdir("img")
newImg=Image.new("RGBA",(640,640))
width=int(math.sqrt(640*640/len(imgs)))
numLine=int(640/width)
for i in imgs:
try:
img=Image.open("img/"+i)
img=img.resize((width,width),Image.ANTIALIAS)
newImg.paste(img,(x*width,y*width))
x+=1
if x>numLine:
x=0
y=y+1
except IOError:
print("img/%s can't open"%(i))
newImg.save("all.png")
headimg()#下载好友头像,头像图片名为备注名,没有就是数字
createImg()#拼接好友头像
appid='10174346'
secret_id='AKIDVAPtvHdZUBmHYfu6h9QWRI3A4qPkFlV8'
secret_key='vluyrRo8GAr8G8oFWakfQc0C1d1A7vY3'
userid='237314158'
end_point=TencentYoutuyun.conf.API_YOUTU_END_POINT#优图开放平台
youtu=TencentYoutuyun.YouTu(appid,secret_id,secret_key,userid,end_point)
use_face=0
not_use_face=0
base_path='img'
#好友头像的识别
for file_name in os.listdir(base_path):
result=youtu.DetectFace(os.path.join(base_path,file_name))
#print(result)
if result["errorcode"]==0:
use_face+=1
gender="男" if result['face'][0]['gender']>=50 else "女"
age=result['face'][0]['age']
beauty=result['face'][0]['beauty']
glass="戴眼镜" if result['face'][0]['glass']==True else "不戴眼镜"
hat='有帽子' if result['face'][0]['hat']==1 else "没有帽子"
expression='大笑' if result['face'][0]['expression']>=80 else '正常'
print(file_name[:-4],gender,age,beauty,glass,hat,expression,sep=',')
elif result["errorcode"]!=0:
not_use_face+=1
#好友头像画出饼状图
attr = ["humanface", "not_use_face"]
value = [use_face,not_use_face]
pie = Pie("好友头像识别", "好友总数:%d" % int(use_face+not_use_face), title_pos='center')
pie.add('', attr, value, radius=[15, 35], rosetype='area', is_label_show=True,
is_legend_show=True, legend_top='bottom') # radius的两个参数:前者是饼状图的内半径,后者是外半径
pie.show_config() # 打印输出图标的所有配置项
pie.render('好友头像识别.html') # 在根目录下生成一个微信好友性别例.html 的文件