Python3使用微软人脸API-1.1人脸检测

在微软服务器中申请了人脸识别API后会得到两个key,随便使用其中一个即可

上传图像文件大小小于等于于4MB

subscription_key='77c80281372a4d75852905b3f0d72b71'
uri_base = 'https://api.cognitive.azure.cn'
#识别脸
def selectLocalFace(imagesUrl):
    try:
        Request='POST'
        #Content_Type1传递类型
        Content_Type1='application/octet-stream'
        headers = {
         'Content-Type': Content_Type1,
         'Ocp-Apim-Subscription-Key': subscription_key,
            }
        #设定是否返回FaceID
        returnFaceId='true'
        #设定是否返回详细的面部细节位置信息(包括鼻子、眼睛、嘴等)
        returnFaceLandmarks='false'
        #设定进行面部智能分析
        #可选:age, gender, smile intensity, facial hair, head pose, glasses, emotion, hair, makeup, occlusion, accessories, blur, exposure and noise
        returnFaceAttributes='age,gender,facialHair,glasses,hair'
        params = { 'returnFaceId': returnFaceId,
                  'returnFaceLandmarks': returnFaceLandmarks,
                    'returnFaceAttributes':returnFaceAttributes,}
        #上传本地图片
        f=open(imagesUrl,"rb")
        body = {'url': imagesUrl}
        response = requests.request(Request, uri_base + '/face/v1.0/detect', json=body, data=f, headers=headers, params=params)
        print ('Response:')
        parsed = json.loads(response.text)
        print(parsed) 
        facenums=0;
        faceAll=[]
        #提取出faceId和人脸的矩形框定位
        for a in parsed:
            for b in a:
                if b=="faceId":
                    faceId=a[b]
                    print(faceId)
                if b=="faceRectangle":
                    top=a[b]["top"]
                    left=a[b]["left"]
                    width=a[b]["width"]
                    height=a[b]["height"] 
                if b=="faceAttributes":
                    gender=a[b]["gender"]
                    age=a[b]["age"]
            imagesFace=[faceId,left,top,width,height,gender,age]
            faceAll.append(imagesFace)
        print(faceAll)
        return faceAll
    except Exception as e:
        print("Error")
        print(e)
        return -1



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值