python 百度人脸识别

  import requests,urllib,json, base64
from paizhao import imgr



def Getr():
    baidu_server = "https://openapi.baidu.com/oauth/2.0/token?"
    grant_type = "client_credentials"
    #API Key
    client_id = ""
    #Secret Key
    client_secret = "yvs0aPe7XDhQVqhbuGj8w7vkbIHCtcrG"

    #拼url
    url = 'https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}'.format(client_id, client_secret)
    #print(url)
    #获取token
    res = requests.post(url)
    #print(res.text)
    token = json.loads(res.text)["access_token"]
    return token


def ren():  #人脸注册
    request_url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add"
    f = open('1.jpg', 'rb')
    image = base64.b64encode(f.read())
    image64 = str(image, 'utf-8')
    image_type = "BASE64"
    user_id = "2"
    user_info =""
    params = {'image': image64, 'image_type': "BASE64", "group_id":"jincc", "user_id": user_id, "user_info": user_info}
    params = urllib.parse.urlencode(params).encode("utf-8")

    access_token = Getr()
    request_url = request_url + "?access_token=" + access_token

    request = urllib.request.urlopen(url=request_url, data=params)  # 发送请求

    content = request.read()  # 将返回结果读取出来
    print(content)



def renshibie():
    request_url = "https://aip.baidubce.com/rest/2.0/face/v3/search"
    f = open('1.jpg', 'rb')
    image = base64.b64encode(f.read())
    image64 = str(image, 'utf-8')

    params = {'image': image64, 'image_type': "BASE64", "group_id_list": "jincc", "quality_control":"HIGH"}
    params = urllib.parse.urlencode(params).encode("utf-8")

    access_token = Getr()
    request_url = request_url + "?access_token=" + access_token

    request = urllib.request.urlopen(url=request_url, data=params)  # 发送请求

    content = request.read()  # 将返回结果读取出来

    tr1 = str(dict(json.loads(content))['result'])
    print(tr1)

    if tr1 == "None":
        return 0
    else:
        tr = str(list(dict(json.loads(content))["result"]["user_list"])[0]["user_info"])
        return tr




if __name__ == "__main__" :
    # ren()

    while 1:

        imgr()
        if renshibie():
            print("识别成功")
            break
        else:
            print("失败")



拍照
paizhao.py

import cv2
import time
cap = cv2.VideoCapture(0)
def imgr():

        # time.sleep(3)
    while 1:
        ret ,frame = cap.read()
        cv2.imwrite('1.jpg', frame)
        print("保存成功")
        cv2.imshow("capture", frame)
        cv2.waitKey(1)
        break


评论

刷新

友情链接