Python之人脸识别api

本来是今天说好了要认真复习考研高数的,但是电脑在我的眼前,没有忍住就写了一个人脸识别(当然是通过百度的api)

识别出的结果大于80表示就是一个人

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import re
import requests
import os
import numpy
from bs4 import BeautifulSoup
import base64
import json
api1 = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=lVfov6E1oaWZR9f4qIhd9Hjy&client_secret=Gubrc6RnMTdA3Eb8WumHIGrz4vHgCTdy"
api2 = "https://aip.baidubce.com/rest/2.0/face/v3/match?access_token="
#1.读取图片数据
def get_img(img1,img2):
with open(img1,"rb") as f:
pic1=f.read()
with open(img2,"rb") as f:
pic2=f.read()
params=json.dumps([
{"image": str(base64.b64encode(pic1), "utf-8"), "image_type": "BASE64", "face_type": "LIVE",
"quality_control": "LOW"},
{"image": str(base64.b64encode(pic2), "utf-8"), "image_type": "BASE64", "face_type": "IDCARD",
"quality_control": "LOW"},
])
print(params)
pass
return params
#2.获取token值,拼接api
def get_token():
response=requests.get(url=api1)
print(response.text)
access_token=eval(response.text)['access_token']
api_url=api2+access_token
return api_url
#3.请求api拿到最终结果
def than_img(img1,img2):
api_url=get_token()
print(api_url)
params=get_img(img1,img2)
print(params)
content=requests.post(url=api_url,data=params)
print(content.text)
than_img("1.jpg","2.jpg")

Fork me on GitHub