会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132359个问题
Python 全系列/第一阶段:Python入门/编程基本概念 4396楼
Python 全系列/第一阶段:Python入门/序列 4397楼
Python 全系列/第一阶段:Python入门/序列 4398楼

# 图片识别
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()

老师,我想在这个代码里加一个就是当识别的图片中没有人物是在图片中显示‘’此图片中没有人物‘’这样的代码,老师你帮我看看怎么加




Python 全系列/第一阶段:Python入门/面向对象 4399楼
Python 全系列/第一阶段:Python入门/面向对象 4400楼

111111111.PNG

222222222.PNG

老师我这结果和您视频里的有点出入,想问一下为什么

Python 全系列/第一阶段:Python入门/面向对象 4401楼
Python 全系列/第一阶段:Python入门/Python入门(动画版) 4403楼
Python 全系列/第一阶段:Python入门/编程基本概念 4404楼
Python 全系列/第一阶段:Python入门/函数和内存分析 4405楼
Python 全系列/第一阶段:Python入门/Python入门(动画版) 4407楼
Python 全系列/第一阶段:Python入门/编程基本概念 4410楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637