Ejemplo n.º 1
0
def estimate(
    warp: Union[HumanWarp, HumanWarpedImage, FaceWarp, FaceWarpedImage],
    descriptorFactory: BaseDescriptorFactory,
    coreEstimator: IDescriptorExtractorPtr,
    descriptor: Optional[BaseDescriptor] = None,
    asyncEstimate: bool = False,
) -> Union[BaseDescriptor, AsyncTask[BaseDescriptor]]:
    """
    Estimate a face descriptor or a human descriptor from the warped image.

    Args:
        warp: warped image
        descriptor: descriptor for saving extract result
        descriptorFactory: descriptor factory
        coreEstimator: descriptor extractor
        asyncEstimate: estimate or run estimation in background
    Returns:
        estimated descriptor if asyncEstimate is false otherwise async task
    Raises:
        LunaSDKException: if estimation failed
    """
    if descriptor is None:
        descriptor = descriptorFactory.generateDescriptor()
        coreDescriptor = descriptor.coreEstimation
    else:
        coreDescriptor = descriptor.coreEstimation
    if asyncEstimate:
        task = coreEstimator.asyncExtractFromWarpedImage(
            warp.warpedImage.coreImage, coreDescriptor)
        return AsyncTask(task, partial(postProcessing, descriptor=descriptor))
    error, optionalGS = coreEstimator.extractFromWarpedImage(
        warp.warpedImage.coreImage, coreDescriptor)
    return postProcessing(error, optionalGS, descriptor)
Ejemplo n.º 2
0
def estimate(
    warp: Union[HumanWarp, HumanWarpedImage, FaceWarp, FaceWarpedImage],
    descriptorFactory: BaseDescriptorFactory,
    coreEstimator: IDescriptorExtractorPtr,
    descriptor: Optional[BaseDescriptor] = None,
) -> BaseDescriptor:
    """
    Estimate a face descriptor or a human descriptor from the warped image.

    Args:
        warp: warped image
        descriptor: descriptor for saving extract result
        descriptorFactory: descriptor factory
        coreEstimator: descriptor extractor
    Returns:
        estimated descriptor
    Raises:
        LunaSDKException: if estimation failed
    """
    if descriptor is None:
        descriptor = descriptorFactory.generateDescriptor()
        coreDescriptor = descriptor.coreEstimation
    else:
        coreDescriptor = descriptor.coreEstimation

    error, optionalGS = coreEstimator.extractFromWarpedImage(
        warp.warpedImage.coreImage, coreDescriptor)
    if error.isError:
        raise LunaSDKException(LunaVLError.fromSDKError(error))
    descriptor.garbageScore = optionalGS
    return descriptor
Ejemplo n.º 3
0
def estimateDescriptorsBatch(
    warps: Union[List[Union[HumanWarp, HumanWarpedImage]],
                 List[Union[FaceWarp, FaceWarpedImage]]],
    descriptorFactory: Type[BaseDescriptorFactory],
    coreEstimator: IDescriptorExtractorPtr,
    aggregate: bool = False,
    descriptorBatch: Optional[BaseDescriptorBatch] = None,
) -> Tuple[BaseDescriptorBatch, Union[BaseDescriptor, None]]:
    """
    Estimate a batch of descriptors from warped images.

    Args:
        warps: warped images
        aggregate:  whether to estimate  aggregate descriptor or not
        descriptorBatch: optional batch for saving descriptors
        descriptorFactory: descriptor factory
        coreEstimator: descriptor extractor
    Returns:
        tuple of batch and the aggregate descriptors (or None)
    Raises:
        LunaSDKException: if estimation failed

    """
    if descriptorBatch is None:
        descriptorBatch = descriptorFactory.generateDescriptorsBatch(
            len(warps))
    if aggregate:
        aggregatedDescriptor = descriptorFactory.generateDescriptor()

        error, optionalGSAggregateDescriptor, scores = coreEstimator.extractFromWarpedImageBatch(
            [warp.warpedImage.coreImage for warp in warps],
            descriptorBatch.coreEstimation,
            aggregatedDescriptor.coreEstimation,
            len(warps),
        )
        if error.isError:
            raise LunaSDKException(LunaVLError.fromSDKError(error))
        aggregatedDescriptor.garbageScore = optionalGSAggregateDescriptor
    else:
        aggregatedDescriptor = None
        error, scores = coreEstimator.extractFromWarpedImageBatch(
            [warp.warpedImage.coreImage for warp in warps],
            descriptorBatch.coreEstimation, len(warps))
        if error.isError:
            raise LunaSDKException(LunaVLError.fromSDKError(error))
        descriptorBatch.scores = scores
    return descriptorBatch, aggregatedDescriptor
Ejemplo n.º 4
0
def estimateDescriptorsBatch(
    warps: Union[List[Union[HumanWarp, HumanWarpedImage]],
                 List[Union[FaceWarp, FaceWarpedImage]]],
    descriptorFactory: BaseDescriptorFactory,
    coreEstimator: IDescriptorExtractorPtr,
    aggregate: bool = False,
    descriptorBatch: Optional[BaseDescriptorBatch] = None,
    asyncEstimate: bool = False,
) -> Union[Tuple[BaseDescriptorBatch, Union[BaseDescriptor, None]],
           AsyncTask[Tuple[BaseDescriptorBatch, Union[BaseDescriptor,
                                                      None]]], ]:
    """
    Estimate a batch of descriptors from warped images.

    Args:
        warps: warped images
        aggregate:  whether to estimate  aggregate descriptor or not
        descriptorBatch: optional batch for saving descriptors
        descriptorFactory: descriptor factory
        coreEstimator: descriptor extractor
        asyncEstimate: estimate or run estimation in background
    Returns:
        tuple of batch and the aggregate descriptors (or None) if asyncEstimate is false otherwise async task
    Raises:
        LunaSDKException: if estimation failed
    """

    if descriptorBatch is None:
        descriptorBatch = descriptorFactory.generateDescriptorsBatch(
            len(warps))
    coreImages = [warp.warpedImage.coreImage for warp in warps]
    validateInputByBatchEstimator(coreEstimator, coreImages)
    if aggregate:
        aggregatedDescriptor = descriptorFactory.generateDescriptor()
        if asyncEstimate:
            task = coreEstimator.asyncExtractFromWarpedImageBatch(
                coreImages, descriptorBatch.coreEstimation,
                aggregatedDescriptor.coreEstimation)
            return AsyncTask(
                task,
                postProcessing=partial(
                    postProcessingBatchWithAggregation,
                    descriptorBatch=descriptorBatch,
                    aggregatedDescriptor=aggregatedDescriptor,
                ),
            )
        error, optionalGSAggregateDescriptor, scores = coreEstimator.extractFromWarpedImageBatch(
            coreImages, descriptorBatch.coreEstimation,
            aggregatedDescriptor.coreEstimation)
        return postProcessingBatchWithAggregation(
            error,
            optionalGSAggregateDescriptor,
            scores,
            descriptorBatch=descriptorBatch,
            aggregatedDescriptor=aggregatedDescriptor,
        )
    if asyncEstimate:
        task = coreEstimator.asyncExtractFromWarpedImageBatch(
            coreImages, descriptorBatch.coreEstimation)
        return AsyncTask(task,
                         postProcessing=partial(
                             postProcessingBatch,
                             descriptorBatch=descriptorBatch))
    error, scores = coreEstimator.extractFromWarpedImageBatch(
        coreImages, descriptorBatch.coreEstimation)
    return postProcessingBatch(error, scores, descriptorBatch=descriptorBatch)
Ejemplo n.º 5
0
def estimateDescriptorsBatch(
    warps: Union[List[Union[HumanWarp, HumanWarpedImage]],
                 List[Union[FaceWarp, FaceWarpedImage]]],
    descriptorFactory: Type[BaseDescriptorFactory],
    coreEstimator: IDescriptorExtractorPtr,
    aggregate: bool = False,
    descriptorBatch: Optional[BaseDescriptorBatch] = None,
) -> Tuple[BaseDescriptorBatch, Union[BaseDescriptor, None]]:
    """
    Estimate a batch of descriptors from warped images.

    Args:
        warps: warped images
        aggregate:  whether to estimate  aggregate descriptor or not
        descriptorBatch: optional batch for saving descriptors
        descriptorFactory: descriptor factory
        coreEstimator: descriptor extractor
    Returns:
        tuple of batch and the aggregate descriptors (or None)
    Raises:
        LunaSDKException: if estimation failed
    """
    def getErrorsExtractingOneByOne() -> List[LunaVLError]:
        """
        Extract descriptor without batching and collect errors
        Returns:
            list with errors
        """
        errors = []
        for idx, warp in enumerate(warps):
            if len(descriptorBatch):
                coreDescriptor = descriptorBatch[idx].coreEstimation
            else:
                descriptor = descriptorFactory.generateDescriptor()
                coreDescriptor = descriptor.coreEstimation

            errorOne, _ = coreEstimator.extractFromWarpedImage(
                warp.warpedImage.coreImage, coreDescriptor)
            if errorOne.isError:
                errors.append(LunaVLError.fromSDKError(errorOne))
            else:
                errors.append(LunaVLError.Ok.format(
                    LunaVLError.Ok.description))

            return errors

    if descriptorBatch is None:
        descriptorBatch = descriptorFactory.generateDescriptorsBatch(
            len(warps))
    if aggregate:
        aggregatedDescriptor = descriptorFactory.generateDescriptor()

        error, optionalGSAggregateDescriptor, scores = coreEstimator.extractFromWarpedImageBatch(
            [warp.warpedImage.coreImage for warp in warps],
            descriptorBatch.coreEstimation,
            aggregatedDescriptor.coreEstimation,
            len(warps),
        )
        if error.isError:
            errors = getErrorsExtractingOneByOne()
            raise LunaSDKException(
                LunaVLError.BatchedInternalError.format(
                    LunaVLError.fromSDKError(error).detail), errors)

        aggregatedDescriptor.garbageScore = optionalGSAggregateDescriptor
    else:
        aggregatedDescriptor = None
        error, scores = coreEstimator.extractFromWarpedImageBatch(
            [warp.warpedImage.coreImage for warp in warps],
            descriptorBatch.coreEstimation, len(warps))
        if error.isError:
            errors = getErrorsExtractingOneByOne()
            raise LunaSDKException(
                LunaVLError.BatchedInternalError.format(
                    LunaVLError.fromSDKError(error).detail), errors)

        descriptorBatch.scores = scores
    return descriptorBatch, aggregatedDescriptor