人脸识别服务 FRS-Python SDK:SDK代码解析

时间:2024-10-17 17:48:14

SDK代码解析

  • 人脸检测
    # detect face by base64
    def detectFaceByBase64():
        try:
            request = DetectFaceByBase64Request()
            request.body = FaceDetectBase64Req(
                image_base64="/9j/4AAQSkZJRgABAQAAAQABAAD...",
                attributes="2,4"
            )
            response = client.detect_face_by_base64(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
    # detect face by file
    def detectFaceByFile():
        try:
            request = DetectFaceByFileRequest()
            with open("/root/picture.jpg", "rb") as f:
                request.body = DetectFaceByFileRequestBody(image_file=FormFile(f))
                response = client.detect_face_by_file(request)
                print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
  • 人脸比对
    # compare face by base64
    def compareFaceByBase64():
        try:
            request = CompareFaceByBase64Request()
            request.body = FaceCompareBase64Req(
                image1_base64="/9j/4AAQSkZJRgABAQAAAQABAAD...",
                image2_base64="/9j/4AAQSkZJRgABAQAAAQABAAD..."
            )
            response = client.compare_face_by_base64(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
    # compare face by file
    def compareFaceByFile():
        try:
            request = CompareFaceByFileRequest()
            with open("/root/picture1.jpg", "rb") as f1:
                with open("/root/picture2.jpg", "rb") as f2:
                    request.body = CompareFaceByFileRequestBody(image1_file=FormFile(f1),
                                                                image2_file=FormFile(f2))
                    response = client.compare_face_by_file(request)
                    print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • 人脸搜索
    # search face by base64
    def searchFaceByBase64():
        try:
            request = SearchFaceByBase64Request()
            request.face_set_name = "face_set_name"
            listFaceSearchBase64ReqReturnFieldsbody = [
                "timestamp"
            ]
            request.body = FaceSearchBase64Req(
                return_fields=listFaceSearchBase64ReqReturnFieldsbody,
                image_base64="/9j/4AAQSkZJRgABAQAAAQABAAD..."
            )
            response = client.search_face_by_base64(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
    # search face by file
    def searchFaceByFile():
        try:
            request = SearchFaceByFileRequest()
            request.face_set_name = "face_set_name"
            with open("/root/picture.jpg", "rb") as f:
                    request.body = SearchFaceByFileRequestBody(
                    return_fields="[\"timestamp\"]",
                    filter="timestamp:10",
                    top_n=10,
                    image_file=FormFile(f)
                )
                response = client.search_face_by_file(request)
                print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • 创建人脸库
    def createFaceSet():
        try:
            request = CreateFaceSetRequest()
            request.body = CreateFaceSetReq(
                face_set_name="face_set_name",
                external_fields={"timestamp": {"type": "long"}}
            )
            response = client.create_face_set(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
  • 查询人脸库
    def showFaceSet():
        try:
            request = ShowFaceSetRequest()
            request.face_set_name = "face_set_name"
            response = client.show_face_set(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
  • 查询所有人脸库
    def showAllFaceSet():
        try:
            request = ShowAllFaceSetsRequest()
            response = client.show_all_face_sets(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
  • 删除人脸库
    def deleteFaceSet():
        try:
            request = DeleteFaceSetRequest()
            request.face_set_name = "face_set_name"
            response = client.delete_face_set(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
  • 添加人脸
    # add face by base64
    def addFacesByBase64():
        try:
            request = AddFacesByBase64Request()
            request.face_set_name = "face_set_name"
            request.body = AddFacesBase64Req(
                external_fields="{\"timestamp\":12}",
                image_base64="/9j/4AAQSkZJRgABAQAAAQABAAD..."
            )
            response = client.add_faces_by_base64(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
    # add face by file
    def addFacesByFile():
        try:
            request = AddFacesByFileRequest()
            request.face_set_name = "face_set_name"
            with open("/root/picture.jpg", "rb") as f:
                request.body = AddFacesByFileRequestBody(
                    external_fields="{\"timestamp\":12}",
                    image_file=FormFile(f)
                )
                response = client.add_faces_by_file(request)
                print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
  • 删除人脸
    def deleteFace():
        # Delete Face By FaceId
        try:
            request = DeleteFaceByFaceIdRequest()
            request.face_set_name = "face_set_name"
            request.face_id = "LkPJblq6"
            response = client.delete_face_by_face_id(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
        # Delete Face By ExternalImageId
        try:
            request = DeleteFaceByExternalImageIdRequest()
            request.face_set_name = "face_set_name"
            request.external_image_id = "external_image_id"
            response = client.delete_face_by_external_image_id(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
  • 批量删除人脸
    def batchDeleteFaces():
        try:
            request = BatchDeleteFacesRequest()
            request.face_set_name = "face_set_name"
            request.body = DeleteFacesBatchReq(
                filter="age:[20 TO 30]"
            )
            response = client.batch_delete_faces(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
  • 更新人脸
    def updateFace():
        try:
            request = UpdateFaceRequest()
            request.face_set_name = "face_set_name"
            request.body = UpdateFaceReq(face_id="LkPJblq6")
            response = client.update_face(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
  • 查询人脸
    def showFaces():
        # Show Faces By FaceId
        try:
            request = ShowFacesByFaceIdRequest()
            request.face_set_name = "face_set_name"
            request.face_id = "LkPJblq6"
            response = client.show_faces_by_face_id(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
        # Show Faces By Limit
        try:
            request = ShowFacesByLimitRequest()
            request.face_set_name = "face_set_name"
            request.offset = 0
            request.limit = 10
            response = client.show_faces_by_limit(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
  • 动作活体检测
    # detect live by base64
    def detectLiveByBase64():
        try:
            request = DetectLiveByBase64Request()
            request.body = LiveDetectBase64Req(
                actions="1,2,3,4",
                video_base64="/9j/4AAQSkZJRgABAQAAAQABAAD..."
            )
            response = client.detect_live_by_base64(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
    # detect live by file
    def detectLiveByFile():
        try:
            request = DetectLiveByFileRequest()
            with open("/root/video.mp4", "rb") as f:
                request.body = DetectLiveByFileRequestBody(
                    video_file=FormFile(f),
                    actions="1,2,3,4"
                )
                response = client.detect_live_by_file(request)
                print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
  • 静默活体检测
    # detect live face by base64
    def detectLiveFaceByBase64():
        try:
            request = DetectLiveFaceByBase64Request()
            request.body = LiveDetectFaceBase64Req(
                image_base64="/9j/4AAQSkZJRgABAQAAAQABAAD..."
            )
            response = client.detect_live_face_by_base64(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
    # detect live face by file
    def detectLiveFaceByFile():
        try:
            request = DetectLiveFaceByFileRequest()
            with open("/root/picture.jpg", "rb") as f:
                request.body = DetectLiveFaceByFileRequestBody(
                    image_file=FormFile(f)
                )
                response = client.detect_live_face_by_file(request)
                print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
support.huaweicloud.com/sdkreference-face/face_04_0021.html