调用百度人脸识别api检测颜值和识别车牌

一、平台接入

1.打开https://ai.baidu.com/
进入控制台,选择文字识别服务。
2.创建应用,如图示:
在这里插入图片描述
创建完毕,点击“返回应用列表”
在这里插入图片描述
此处显示AK,SK,后面程序中会用到

二、人脸识别调用步骤

调用主要有三步:

获取access_token
将图片处理成base64编码格式
post请求访问接口得到结果

1.获取access_token
在这里插入图片描述
官方给的python示例代码,不过这个是python2的代码,python3里已经没有了urllib2,而且很繁琐
给出博主自己编写的py3利用requests的demo:

# -*- coding: utf-8 -*-
__author__ = 'fff_zrx'
import requests
#获取access_token
#client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=your ak&client_secret=your sk'
header={'Content-Type': 'application/json; charset=UTF-8'}
response1=requests.post(url=host,headers=header)#<class 'requests.models.Response'>
json1 = response1.json()#<class 'dict'>
access_token=json1['access_token']

2.将图片处理成base64编码格式流程大致是将图片读取为二进制格式,再利用二进制到base64格式的函数转换
https://www.jianshu.com/p/570c1acdd236
在这里插入图片描述
转换代码:

import base64
filepath='zrx.jpg'
f = open(r'%s' % filepath, 'rb')
pic = base64.b64encode(f.read())
f.close()
base64=str(pic,'utf-8')
print(base64)

3.post请求访问接口得到结果

request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"
params = {"image":base64,"image_type":"BASE64","face_field":"faceshape,facetype,beauty,"}
header={'Content-Type': 'application/json'}
request_url = request_url + "?access_token=" + access_token
response1=requests.post(url=request_url,data=params,headers=header)#<class 'requests.models.Response'>
json1 = response1.json()#<class 'dict'>
print(json1)
print("颜值评分为")
print (json1["result"]["face_list"][0]['beauty'],'分/100分')

4、完整代码:

# -*- coding: utf-8 -*-
__author__ = 'fff_zrx'
import requests
import base64
#获取access_token
#client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=your ak&client_secret=your sk'
header={'Content-Type': 'application/json; charset=UTF-8'}
response1=requests.post(url=host,headers=header)#<class 'requests.models.Response'>
json1 = response1.json()#<class 'dict'>
access_token=json1['access_token']
#转换图片格式
filepath='zrx.jpg'
f = open(r'%s' % filepath, 'rb')
pic = base64.b64encode(f.read())
f.close()
base64=str(pic,'utf-8')
print(base64)
#访问人脸检测api
request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"
params = {"image":base64,"image_type":"BASE64","face_field":"faceshape,facetype,beauty,"}
header={'Content-Type': 'application/json'}
request_url = request_url + "?access_token=" + access_token
response1=requests.post(url=request_url,data=params,headers=header)#<class 'requests.models.Response'>
json1 = response1.json()#<class 'dict'>
print(json1)
print("颜值评分为")
print (json1["result"]["face_list"][0]['beauty'],'分/100分')

结果:
在这里插入图片描述

三、车牌识别

重新生成AK和SK
完整代码:

# -*- coding: utf-8 -*-
# !/usr/bin/env python
import urllib
import urllib.parse
import urllib.request
import base64
import json

# client_id 为官网获取的AK, client_secret 为官网获取的SK
client_id = 'GRRG0QUHaxBLB2jS8Giru9fT'
client_secret = 'GVBHHeCvQDwmSr8pSWpXGddFYh3thQO7'

# 获取token
def get_token():
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret
    request = urllib.request.Request(host)
    request.add_header('Content-Type', 'application/json; charset=UTF-8')
    response = urllib.request.urlopen(request)
    token_content = response.read()
    if token_content:
        token_info = json.loads(token_content.decode("utf-8"))
        token_key = token_info['access_token']
    return token_key

    # 读取图片
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()
    # 获取车牌号信息
def get_license_plate(path):
    request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate"

    f = get_file_content(path)
    access_token = get_token()
    img = base64.b64encode(f)
    params = {"custom_lib": False, "image": img}
    params = urllib.parse.urlencode(params).encode('utf-8')
    request_url = request_url + "?access_token=" + access_token
    request = urllib.request.Request(url=request_url, data=params)
    request.add_header('Content-Type', 'application/x-www-form-urlencoded')
    response = urllib.request.urlopen(request)
    content = response.read()
    if content:
        license_plates = json.loads(content.decode("utf-8"))
        strover = '识别结果:'
        words_result = license_plates['words_result']
        number = words_result['number']
        strover += '  车牌号:{} \n '.format(number)
        #        print (content)
        print(strover)
        return content
    else:
        return ''

image_path = 'chepai.jpg'
get_license_plate(image_path)

结果:
在这里插入图片描述
参考博客:
https://www.csdn.net/article/a/2019-07-04/15977001
https://blog.csdn.net/qq_38412868/article/details/92394766

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值