人脸识别 百度api python demo

 get_token.py:

import urllib, urllib2, sys
import ssl

host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=apikey&client_secret=secretkey'
request = urllib2.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib2.urlopen(request)
content = response.read()
if (content):
    print(content)

faceDetect.py:

# encoding:utf-8
import cv2
import urllib
import urllib2
import urllib2, base64
import time
try:
    from urllib import urlencode
except ImportError:
    from urllib.parse import urlencode
import shutil
import os
import os.path


rootDir = "/home/gaoxiaochuan/Downloads/gaopic/set11"

request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"
i = 0
j = 0

for lists in os.listdir(rootDir):
    i = i + 1
    #time.sleep(0.7)#gao
    time.sleep(0.2)#yunsen
    path = os.path.join(rootDir, lists)
    #print path
    f = open(path,'rb')
    img = base64.b64encode(f.read())
    params={'image':img,'image_type': 'BASE64','face_fields':'age,beauty,faceshape,gender,glasses,race,qualities'}
    params=urlencode(params)
    access_token = 'token' #通过执行get_token.py获取的token值
    request_url = request_url + "?access_token=" + access_token
    request = urllib2.Request(url=request_url, data=params)
    request.add_header('Content-Type', 'application/json')
    response = urllib2.urlopen(request)
    content = response.read()
    if content:
        #print (content)
        success = content.find("SUCCESS")
        if (-1 != success):
            j = j + 1
            #print("hahahh")
    print('\r\n')
    print("i = ", i)
    print("j = ", j)
       

 

你可能感兴趣的:(百度API)