python 使用百度API实现人脸对比识别

系统 1726 0
  • 要求

    1.安装百度python API
    2.到百度智能云创建应用
    3.调用API,代码

1.安装百度python API

使用pip安装
pip3 install baidu-api

2.到 百度智能云创建应用

-如下图
python 使用百度API实现人脸对比识别_第1张图片
创建应用得到
APP_ID
API_KEY
SECRET_KEY
python 使用百度API实现人脸对比识别_第2张图片

3.调用API,代码

            
              from aip import AipFace
import base64
import urllib
import cv2
""" 你的 APPID AK SK """
APP_ID = '你的 APP_ID   '
API_KEY = '你的 API_KEY'
SECRET_KEY = '你的SECRET_KEY '
client = AipFace(APP_ID, API_KEY, SECRET_KEY)

def fileopen(filepath): #打开图片
    with open(filepath, 'rb') as f:
        data = base64.b64encode(f.read())
    image = str(data,'utf-8')
    return image

def CatchUsbVideo(window_name,camera_idx):
    cv2.namedWindow(window_name)
    #视频来源,可以来自一段已存好的视频,也可以直接来自USB摄像头
    cap = cv2.VideoCapture(camera_idx)
    #识别出人脸后要画的边框的颜色,RGB格式
    color = (0, 255, 0)
    while cap.isOpened():
        ok, frame = cap.read() #读取一帧数据
        if not ok:            
            break
        c = cv2.waitKey(10)#按q退出
        cv2.imshow(window_name, frame)
        if c == ord('p'):
            cv2.imwrite('p1.jpg',frame)#保存图片
            print('图片保存成功')
            break
        if c == ord('o'):
            cv2.imwrite('p2.jpg',frame)#保存图片
            break
        if c  == ord('q'):
            break        
    #释放摄像头并销毁所有窗口
    cap.release()
    cv2.destroyAllWindows()  
    
def face_detect(filepath1,filepath2):  #人脸对比
    image1 = fileopen(filepath1)
    image2 = fileopen(filepath2)
    result = client.match([
    {
        'image': image1,
        'image_type': 'BASE64',
    },
    {
        'image': image2,
        'image_type': 'BASE64',
    }])
    print(result)              #打印出所有的信息
    
def face_add(filepath,groupid,userid):  #人脸库增加 地址 组 用户
    image = fileopen(filepath)
    imageType="BASE64"
    result=client.addUser(image,imageType,groupid,userid)
    if result['error_code']==0:
        print("增加人脸成功")
    else:
        print("增加人脸失败")
        
def face_search(filepath,groupIdList):  #人脸库搜索  222207 groupIdList="你的用户组名称"
    image = fileopen(filepath)
    imageType="BASE64"
    result=client.search(image,imageType,groupIdList)
    print(result)            #打印出所有信息

if __name__ == '__main__':
    #face_comper()
    #face_detect('p1.jpg','p2.jpg')
    #face_add('p1.jpg','test','one')
    #face_search('p2.jpg','test')
    CatchUsbVideo('window_name',0)
 

            
          

我是使用opencv打开摄像头来拍取照片并上传的
注意要上传的图片格式要经过 base64编码并str强制类型转换才能使用,因为可能base64编码+,-,=这些符号还是ascll码,不然可能会报错,代码如下

            
              def fileopen(filepath): #打开图片
    with open(filepath, 'rb') as f:
        data = base64.b64encode(f.read())
    image = str(data,'utf-8')
    return image

            
          

这里是一些子函数

            
              def test1():
    with open("./p1.jpg", 'rb') as f:
        data = base64.b64encode(f.read())
    #image = "取决于image_type参数,传入BASE64字符串或URL字符串或FACE_TOKEN字符串"
    #image = urllib.parse.urlencode(data)
    image = str(data,'utf-8')
    imageType = "BASE64"

    """ 调用人脸检测 """
    result = client.detect(image, imageType);
    print(result)
    
def face_detect(filepath1,filepath2):  #人脸对比
    image1 = fileopen(filepath1)
    image2 = fileopen(filepath2)
    result = client.match([
    {
        'image': image1,
        'image_type': 'BASE64',
    },
    {
        'image': image2,
        'image_type': 'BASE64',
    }])
    print(result)              #打印出所有的信息
    
def face_add(filepath,groupid,userid):  #人脸库增加 地址 组 用户
    image = fileopen(filepath)
    imageType="BASE64"
    result=client.addUser(image,imageType,groupid,userid)
    if result['error_code']==0:
        print("增加人脸成功")
    else:
        print("增加人脸失败")
        
def face_search(filepath,groupIdList):  #人脸库搜索  groupIdList="你的用户组名称"
    image = fileopen(filepath)
    imageType="BASE64"
    data=client.search(image,imageType,groupIdList)
    result = data.get('result')
    print(result)            #打印出所有信息
    if result == None:
        return None
    else:#分数大于70
        if result['user_list'][0]['score'] >70 :
            print(result['user_list'][0]['user_id'])
            return result['user_list'][0]['user_id']
        else :
            return None

            
          

更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论