# 图片识别
import base64
from PIL import Image
from aip import AipFace
from PIL import Image, ImageDraw, ImageFont
APP_ID = '23157414'
API_KEY = 'WhceDbN1GR7OpsYGguhvpgiu'
SECRET_KEY = 'NA6N6UxpsfSlQ6GgnAMuf9jvZ1UtTZHX'
client = AipFace(APP_ID, API_KEY, SECRET_KEY)
filename = './图片/1.jpg'
fo = open(filename, 'rb')
image = fo.read()
fo.close()
image = str(base64.b64encode(image), 'utf-8')
image_type = 'BASE64'
options = {}
options['face_field'] = 'age,gender,beauty,glasses'
options['max_face_num'] = 10
options['face_type'] = 'LIVE'
result = client.detect(image, image_type, options)
print(result)
img = Image.open(filename)
img.show()
for face in result['result']['face_list']:
if face['gender']['type'] == 'male':
gender = '男'
else:
gender = '女'
age = face['age']
beauty = face['beauty']
if face['glasses']['type'] == 'none':
glasses = '没戴眼镜'
else:
glasses = '戴眼镜'
print('性别:' + gender)
print('年龄:' + str(age))
print('颜值:' + str(beauty))
print(glasses + '\n')
img =Image.open(filename)
draw = ImageDraw.Draw(img)
ttfont = ImageFont.truetype('C:\Windows\Fonts\Deng.ttf', 12)
for face in result['result']['face_list']:
if face['gender']['type'] == 'male':
gender = '男'
else:
gender = '女'
age = face['age']
beauty = face['beauty']
if face['glasses']['type'] == 'none':
glasses = '没戴眼镜'
else:
glasses = '戴眼镜'
x1 = face['location']['left']
y1 = face['location']['top']
x2 = x1+face['location']['width']
y2 = y1+face['location']['height']
draw.rectangle((x1, y1, x2, y2), outline='blue')
x = x2+5
draw.text([x, y1], '性别:'+gender, 'white', font=ttfont)
draw.text([x, y1+15], '年龄'+str(age), 'white', font=ttfont)
draw.text([x, y1+30], '颜值'+str(beauty), 'white', font=ttfont)
draw.text([x, y1+45], glasses, 'white', font=ttfont)
img.show()
老师,我想在这个代码里加一个就是当识别的图片中没有人物是在图片中显示‘’此图片中没有人物‘’这样的代码,老师你帮我看看怎么加