一.设计流程
1.导入模块
import time
import base64
import cv2
import requests
time模块:计时
base64模块:由于某些系统中只能使用ASCII字符。Base64就是用来将非ASCII字符的数据转换成 ASCII字符的一种方法。
cv2模块:本程序中要用到opencv的框选模块
requests模块:使得python进行网络请求时,变得人性化,使用Requests可以轻而易举的完成浏览器可有的任何操作
2.获取token
这里首先要在百度智能云获取到自己的APP_ID、API_KEY、SECRET_KEY,具体方法:获取token
def gettoken():
token = ""
if token == "":
APP_ID = '*****' # 你的APP_ID
API_KEY = '*****' # 你的API_KEY
SECRET_KEY = '*****' # 你的SECRET_KEY
# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oaut/2.0/token?grant_type=client_credentials' + '&client_id=' + API_KEY + '&client_secret=' + SECRET_KEY
# print(host)
response = requests.get(host)
# if response:
# for item in response.json().items(): # 逐项遍历response.json()----字典
# print(item)
token = response.json()["access_token"]
print(">>成功获取到token")
return token
需要注意的是:这里获取的token你可以自己输出一下,如果是24.*****(一长串)就说明对了
3.图片读取
name为图片名称,以“rb”方式打开图片
几种文件打开方式的用法和区别:python文件打开方式用法
def read_pic(name):
f = open(name, "rb")
base64_data = base64.b64encode(f.read())
s = base64_data.decode()
print(">>图片读取成功")
return s
4.人脸检测与属性分析
首先调用我们获取到的token,将我们需要访问的url通过字符串拼接完整,通过post方式访问url,并通过json()函数转换成python支持的dict。最后返回response 供后面我们框选人脸使用。
# 人脸检测与属性分析
def face_nums(picture):
token = gettoken()
request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"
params = {
"image": picture,</