使用python+百度ai实现人脸识别

在这里插入代码片
#!/usr/bin/python
# -*- coding: utf-8 -*-
import base64
import cv2 as cv
import json
from aip import AipFace


APP_ID = '23575141'
API_KEY = 'GQmcZTPiqLU1I3bXHuqMgKNP'
SECRET_KEY = '4oiUxWaqC0whKKhGg65VkOdMv4GtfqyA'
client = AipFace(APP_ID, API_KEY, SECRET_KEY)

def photo_take():
    cap = cv.VideoCapture(0)
    while True:
        success, frame = cap.read()
        cv.imshow('video', frame)
        Key = cv.waitKey(1) & 0xFF
        # cv2.waitKey(1)与0xFF(11111111)进行相与运算,是因为cv2.waitKey(1)的返回值不止8位,
        # 但是只有后8位实际有效,为避免产干扰,通过‘与’操作将其余位设置为0
        if Key == ord(' '):  # 按下空格键
            cv.imwrite('D:\\GraduateFrom_XCU\\CODE\\fingerPrint\\templates\\photo\\photo1.jpg', frame)  # 保存图片
            break
    cap.release()  # 释放占用资源
    cv.destroyAllWindows()  # 释放opencv创建的所有窗口


def face_detect(filepath1,filepath2):  #人脸对比
   result = client.match([
    {
        'image': base64.b64encode(open(filepath1, 'rb').read()),
        'image_type': 'BASE64',
    },
    {
        'image': base64.b64encode(open(filepath2, 'rb').read()),
        'image_type': 'BASE64',
    }
   ])
  # print(result)              #打印出所有的信息
   a=result['result']['score']
   print('相似度为:')
   print(a) ,                  #单独显示出相似度 其他的类似
   print('%')
   if a>80:
       print('验证成功')
       return 1
   else:
       print ('验证失败')
       return 0
def face_search(filepath):  #人脸库搜索  222207
    with open(filepath, 'rb') as fp:
        image=base64.b64encode(fp.read())
    imageType="BASE64"
    groupIdList="face1"
    result=client.search(image,imageType,groupIdList)
    print (result['result'])        ##########result['result']如果拍照有人脸,显示人脸信息。无人脸返回None
    if(result['result']==None):
        print ('验证失败')
        return 0
    print(result)            #打印出所有信息
    num=result['result']['user_list'][0]['score']
    if num > 80:
        print ('验证成功')
       # render_template('passenger.html')
        return 1

    else:
        print ('验证失败')
        return 0
    print(result['result']['user_list'][0]['score'])   #打印出相似度其他信息类似
    print(result['result']['face_token'])
    print(result['error_code'])
def face_add(filepath,unit,num):  #人脸库增加 地址 组 用户
    with open(filepath,'rb') as fp:
        image=image=base64.b64encode(fp.read())
    imageType="BASE64"
    groupid=unit
    userid=num
    result=client.addUser(image,imageType,groupid,userid)
    if result['error_code']==0:
        print("增加人脸成功")
    else:
        print("增加人脸失败")
#    print(result)
def face_delete(filepath):  #删除人脸库
    userId = "huge"#######用户名称
    groupId = "face1"###用户组名称
    result = client.deleteUser(groupId, userId);  #其实这里不用按照官方的demo三个参数 每张照片单独的token不用也可以的!
    print(result)
    if result['error_code']==0:
        print("删除人脸成功")
    else:
        print("删除人脸失败")

if __name__=='__main__':
 #   photo_take()
#    face_detect('photo1.jpg','5.png')
    face_search('D:\\CODE\\photo\\9.jpg')
#    face_delete('1.jpg')
 #   face_add('photo/66.jpg','face1','yjh')

你可能感兴趣的:(OpenCV,python,百度,opencv)