python3调用旷视人脸识别

1.到旷视官网上注册申请key和secret

https://www.faceplusplus.com.cn/

2.人脸库图像准备,我的人脸库用图片如下:(图片文件名不能有中文,否则会调用错误)

3.根据官方api文档写几个post相关的函数

def detect_face(filepath):#检测图片里人脸信息
    http_url ="https://api-cn.faceplusplus.com/facepp/v3/detect"
    files = {"image_file": open(filepath, "rb")}
    data = {"api_key":key, "api_secret": secret}
    response = requests.post(http_url, data=data, files=files)
    req_dict = response.json()
    print(req_dict)
    return req_dict

def set_face(outer_id):#创建face_set人脸库
    url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'
    params = {
            'api_key':key,
            'api_secret':secret,
            'outer_id':outer_id
            }
    response = requests.post(url,data = params)
    req_dict = response.json()
    print(req_dict)
    return req_dict

def addface(outer_id,facetokens):#将face加入到faceset
    url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/addface'
    params = {
            'api_key':key,
            'api_secret':secret,
            #'faceset_token':faceset,
            'outer_id':outer_id,
            'face_tokens':facetokens
            }
    r = requests.post(url,data = params)
    req_dict = r.json()
    print(req_dict)
    return req_dict

def face_search(image_file1,outer_id):#查找face库里有无对应人脸,默认返回result是最大脸结果
    url = 'https://api-cn.faceplusplus.com/facepp/v3/search'
    files = {"image_file": open(image_file1, "rb")}
    params = {
            'api_key':key,
            'api_secret':secret,
            #'faceset_token':faceset_token,
            'outer_id':outer_id
            }
    r = requests.post(url,files = files,data = params)
    req_dict = r.json()
    print(req_dict)
    return req_dict

def face_search2(facetoken,outer_id):#根据facetoken来返回结果
    url = 'https://api-cn.faceplusplus.com/facepp/v3/search'
    params = {
            'api_key':key,
            'api_secret':secret,
            'face_token':facetoken,
            'outer_id':outer_id
            }
    r = requests.post(url,data = params)
    req_dict = r.json()
    print(req_dict)
    return req_dict

def getfaceset():#获取faceset信息
    url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets'
    params = {
            'api_key':key,
            'api_secret':secret,
            }
    response = requests.post(url,data = params)
    req_dict = response.json()
    print(req_dict)
    return req_dict

def deletefaceset(outer_id):#删除faceset
    url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/delete'
    params = {
            'api_key':key,
            'api_secret':secret,
            'outer_id':outer_id,
            'check_empty':0
            }
    response = requests.post(url,data = params)
    req_dict = response.json()
    print(req_dict)
    return req_dict

def face_SetUserID(face_token,user_id):#为检测出的某一个人脸添加标识信息,该信息会在Search接口结果中返回,用来确定用户身份。
    url = 'https://api-cn.faceplusplus.com/facepp/v3/face/setuserid'
    params = {
            'api_key':key,
            'api_secret':secret,
            'face_token':face_token,
            'user_id':user_id
            }
    r = requests.post(url,data = params)
    req_dict = r.json()
    print(req_dict)
    return req_dict

4.调用旷视api

1)创建人脸库faceset:创建faceset要用到Faceset的Face detect的api,

   首先,使用FaceSet Create API创建人脸库,然后使用Detect API历遍人脸图片目录,并把对应的facetoken保存在人脸库里并附加1个对应的‘userid’方便后续调用。

def makedataset(dataset_path='./database',outer_id='人脸库'):
    img_paths=os.listdir(dataset_path)
    user_ids=[img_path.split('.')[0] for img_path in img_paths] #解析人名
    img_paths=[os.path.join(dataset_path,img_path) for img_path in img_paths]#解析图片地址
    #1.创建Faceset,outerid为’人脸库‘
    set_face(outer_id)#创建人脸库faceset
    #2.人脸库中加入人脸token
    for (user_id,img_path) in zip(user_ids,img_paths):
        face_json = detect_face(img_path)#检测图片中人脸
        face_token= face_json['faces'][0]['face_token']#获取人脸face_token
        addface(outer_id,face_token)#把face_token追加到人脸库faceset
        face_SetUserID(face_token,user_id)#给face_token绑定1个user_id

2)人脸检测和识别 :原face search的api只返回最大脸的识别结果,如果图片中所有的都要识别,则需要把图片里所有的检测出的face_token送入search api

def find_all_faces(img_path,outer_id='人脸库'):#找到图片里所有脸并标出
    face_json=detect_face(img_path)
    faces=face_json['faces']
    for face in faces:
        face['result']=face_search2(face['face_token'],outer_id)['results'][0]
    return faces

def find_biggest_face(img_path,outer_id='人脸库'):#找到图片里脸最大的并标出
    face_json=face_search(img_path,outer_id='人脸库')
    faces=[face_json['faces'][0]]
    faces[0]['result']=face_json['results'][0]
    return faces

3)检测结果绘制:由于opencv不支持中文显示,所以这里要使用pillow的库来处理

def draw_result(img,faces):#绘制结果(PIL库可以显示中文)
    textSize=20
    if (isinstance(img, np.ndarray)):  #判断是否OpenCV图片类型
        img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    draw = ImageDraw.Draw(img)
    fontText = ImageFont.truetype("/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc", textSize, encoding="utf-8")
    for face in faces:
        loc=face['face_rectangle']
        result=face['result']
        if loc['width']>35:#只处理宽度大于40的人脸
            draw.rectangle((loc['left'], loc['top'], loc['left']+loc['width'], loc['top']+loc['height']), None, (0,255,0),width=4)
            if result['confidence']>60:#根据置信率贴标
                text_show=name_dict[result['user_id']]+' '+str(result['confidence'])+'%'
                draw.text((loc['left'], loc['top']-textSize-4), text_show, (0,255,0), font=fontText)
            else:
                draw.text((loc['left'], loc['top']-textSize-4), '其他人', (0,255,0), font=fontText)
    return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)

检测效果图:

完整代码:

#--------------------利用旷视api进行人脸检测和识别---------------------
import requests
from json import JSONDecoder
import os
import cv2
from PIL import Image,ImageDraw,ImageFont
import numpy as np

#定义key和sercet,需要到旷视网上申请注册
key = ""
secret = ""
#定义userid和人名的关系
name_dict={"HuangXiaoming":"黄晓明","DengChao":"邓超","HeJiong":"何炅","TongDawei":"佟大为","WangYuan":"王源","YangYing":"杨颖","YiYangqianxi":"易烊千玺","WangJunkai":"王俊凯"}

def detect_face(filepath):#传入图片文件
    http_url ="https://api-cn.faceplusplus.com/facepp/v3/detect"
    files = {"image_file": open(filepath, "rb")}
    data = {"api_key":key, "api_secret": secret}
    response = requests.post(http_url, data=data, files=files)
    req_dict = response.json()
    print(req_dict)
    return req_dict

def set_face(outer_id):#创建face_set
    url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'
    params = {
            'api_key':key,
            'api_secret':secret,
            'outer_id':outer_id
            }
    response = requests.post(url,data = params)
    req_dict = response.json()
    print(req_dict)
    return req_dict

def addface(outer_id,facetokens):#将face加入到faceset
    url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/addface'
    params = {
            'api_key':key,
            'api_secret':secret,
            #'faceset_token':faceset,
            'outer_id':outer_id,
            'face_tokens':facetokens
            }
    r = requests.post(url,data = params)
    req_dict = r.json()
    print(req_dict)
    return req_dict

def face_search(image_file1,outer_id):#查找face库里有无对应人脸,默认返回result是最大脸结果
    url = 'https://api-cn.faceplusplus.com/facepp/v3/search'
    files = {"image_file": open(image_file1, "rb")}
    params = {
            'api_key':key,
            'api_secret':secret,
            #'faceset_token':faceset_token,
            'outer_id':outer_id
            }
    r = requests.post(url,files = files,data = params)
    req_dict = r.json()
    print(req_dict)
    return req_dict

def face_search2(facetoken,outer_id):#根据facetoken来返回结果
    url = 'https://api-cn.faceplusplus.com/facepp/v3/search'
    params = {
            'api_key':key,
            'api_secret':secret,
            'face_token':facetoken,
            'outer_id':outer_id
            }
    r = requests.post(url,data = params)
    req_dict = r.json()
    print(req_dict)
    return req_dict

def getfaceset():#获取faceset信息
    url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets'
    params = {
            'api_key':key,
            'api_secret':secret,
            }
    response = requests.post(url,data = params)
    req_dict = response.json()
    print(req_dict)
    return req_dict

def deletefaceset(outer_id):#删除faceset
    url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/delete'
    params = {
            'api_key':key,
            'api_secret':secret,
            'outer_id':outer_id,
            'check_empty':0
            }
    response = requests.post(url,data = params)
    req_dict = response.json()
    print(req_dict)
    return req_dict

def face_SetUserID(face_token,user_id):#为检测出的某一个人脸添加标识信息,该信息会在Search接口结果中返回,用来确定用户身份。
    url = 'https://api-cn.faceplusplus.com/facepp/v3/face/setuserid'
    params = {
            'api_key':key,
            'api_secret':secret,
            'face_token':face_token,
            'user_id':user_id
            }
    r = requests.post(url,data = params)
    req_dict = r.json()
    print(req_dict)
    return req_dict

def makedataset(dataset_path='./database',outer_id='人脸库'):
    img_paths=os.listdir(dataset_path)
    user_ids=[img_path.split('.')[0] for img_path in img_paths] #解析人名
    img_paths=[os.path.join(dataset_path,img_path) for img_path in img_paths]#解析图片地址
    #1.创建Faceset,outerid为’人脸库‘
    set_face(outer_id)
    #2.人脸库中加入人脸token
    for (user_id,img_path) in zip(user_ids,img_paths):
        face_json = detect_face(img_path)
        face_token= face_json['faces'][0]['face_token']
        addface(outer_id,face_token)
        face_SetUserID(face_token,user_id)

def draw_result(img,faces):#绘制结果(PIL库可以显示中文)
    textSize=20
    if (isinstance(img, np.ndarray)):  #判断是否OpenCV图片类型
        img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    draw = ImageDraw.Draw(img)
    fontText = ImageFont.truetype("/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc", textSize, encoding="utf-8")
    for face in faces:
        loc=face['face_rectangle']
        result=face['result']
        if loc['width']>35:#只处理宽度大于40的人脸
            draw.rectangle((loc['left'], loc['top'], loc['left']+loc['width'], loc['top']+loc['height']), None, (0,255,0),width=4)
            if result['confidence']>60:#根据置信率贴标
                text_show=name_dict[result['user_id']]+' '+str(result['confidence'])+'%'
                draw.text((loc['left'], loc['top']-textSize-4), text_show, (0,255,0), font=fontText)
            else:
                draw.text((loc['left'], loc['top']-textSize-4), '其他人', (0,255,0), font=fontText)
    return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)

def find_all_faces(img_path,outer_id='人脸库'):#找到图片里所有脸并标出
    face_json=detect_face(img_path)
    faces=face_json['faces']
    for face in faces:
        face['result']=face_search2(face['face_token'],outer_id)['results'][0]
    return faces

def find_biggest_face(img_path,outer_id='人脸库'):#找到图片里脸最大的并标出
    face_json=face_search(img_path,outer_id='人脸库')
    faces=[face_json['faces'][0]]
    faces[0]['result']=face_json['results'][0]
    return faces



if __name__ == "__main__":
    #makedataset(outer_id='人脸库')
    #dataset=getfaceset()
    #deletefaceset('人脸库')
    img_paths=os.listdir('./face_test')
    img_paths=[os.path.join('./face_test',img_path) for img_path in img_paths]
    index=0
    for img_path in img_paths:
        faces=find_all_faces(img_path,outer_id='人脸库')
        #faces=find_biggest_face(img_path,outer_id='人脸库')
        img=cv2.imread(img_path)
        img_result=draw_result(img,faces)
        cv2.imwrite('./result/'+str(index)+'.jpg',img_result)
        index=index+1
        #cv2.imshow('',img_result)
        #cv2.waitKey(0)

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Python调用旷视科技的人脸识别API实现人脸识别的步骤如下: 1. 在旷视科技平台注册账号,创建应用,获取App ID和API Key。 2. 安装Python的requests库,用于发送HTTP请求和接收响应。 3. 使用requests库发送HTTP POST请求到旷视科技的人脸识别API,需要传递相应的参数,包括App ID、API Key、待识别的图片等。例如: ``` import requests api_key = 'YOUR_API_KEY' api_secret = 'YOUR_API_SECRET' image_file = 'YOUR_IMAGE_FILE' url = 'https://api-cn.faceplusplus.com/facepp/v3/detect' data = { 'api_key': api_key, 'api_secret': api_secret, 'image_file': open(image_file, 'rb'), 'return_attributes': 'gender,age,smiling,emotion,ethnicity,beauty,skinstatus' } response = requests.post(url, files=data) ``` 其中,YOUR_API_KEY和YOUR_API_SECRET是通过旷视科技平台获取的,YOUR_IMAGE_FILE是待识别的图片的文件路径。 4. 解析返回的JSON格式响应,得到识别结果。例如: ``` import json result = json.loads(response.text) faces = result['faces'] for face in faces: attributes = face['attributes'] gender = attributes['gender']['value'] age = attributes['age']['value'] smiling = attributes['smile']['value'] emotion = attributes['emotion']['value'] ethnicity = attributes['ethnicity']['value'] beauty = attributes['beauty'] skinstatus = attributes['skinstatus'] print('Gender:', gender) print('Age:', age) print('Smiling:', smiling) print('Emotion:', emotion) print('Ethnicity:', ethnicity) print('Beauty:', beauty) print('Skin Status:', skinstatus) ``` 其中,通过faces获取到识别出来的人脸信息,通过attributes获取到人脸的属性信息,包括性别、年龄、微笑程度、情绪、人种、美丑分值、皮肤状态等。 需要注意的是,使用旷视科技平台的人脸识别API需要申请开通并获取相应权限,同时需要遵循相关法律法规和隐私保护原则。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值