Skip to content

Reidentification example#

What it does#

This example demonstrates how to:

  • detect a human body in an image,

  • extract data of that body (so called human descriptor),

  • compare descriptors received from different images.

Prerequisites#

This example assumes that you have already read the FaceEngine Handbook (or at least have it somewhere nearby for reference) and know some core concepts, like memory management, object ownership, and life-time control. This sample will not explain these aspects in detail.

Example walkthrough#

We present a rather rich human processing pipeline. In fact, it is a bit too rich for this sample, but it was made so intentionally to give you a clue of some real world usage scenarios.

Preparations#

SDK initialization. It is pretty straightforward and self-explanatory, so we will not discuss it here.

Human detection#

At this stage we have an image. Presumably, there is a human somewhere in it and we would like to find it. For that purpose, a human detector is used. We use it like so:

fsdk::IHumanDetectorPtr humanDetector = fsdk::acquire(faceEngine->createHumanDetector());
if (!humanDetector) {
    ...
}
...
// Detect no more than 10 humans in the image.
const int maxDetectionCount = 10;

// Detect humans in images.
fsdk::ResultValue<fsdk::FSDKError, fsdk::Ref<fsdk::IResultBatch<fsdk::Human>>> detectorResult = humanDetector->detect(
    fsdk::Span<const fsdk::Image>(images.data(), images.size()),
    fsdk::Span<const fsdk::Rect>(rects.data(), rects.size()),
    maxDetectionCount);

Extraction#

When we have a human detection, we need to extract data from it. The data is contained in descriptor object can be used for comparison or matching. A descriptor may be extracted from an image using human detection. Later, one or multiple descriptors can be matched to determine human similarity.

// Create human descriptor.
fsdk::IDescriptorPtr descriptor = fsdk::acquire(faceEngine->createDescriptor(fsdk::DV_MIN_HUMAN_DESCRIPTOR_VERSION));
if (!descriptor) {
    ...
}

// Create descriptor extractor. For human descriptor extractor pass the version DV_MIN_HUMAN_DESCRIPTOR_VERSION or 101
fsdk::IDescriptorExtractorPtr descriptorExtractor = fsdk::acquire(engine->createExtractor(fsdk::DV_MIN_HUMAN_DESCRIPTOR_VERSION));

// Extract human descriptor.
fsdk::ResultValue<fsdk::FSDKError, float> descriptorExtractorResult = descriptorExtractor->extractFromWarpedImage(
    humanWarpedImage,
    descriptor
);
if(descriptorExtractorResult.isError()) {
    ...
}

Descriptor matching#

For example, we take only first descriptor in the first image and all detections in other images for matching. For that we use a descriptor matcher object.

// Create descriptor matcher.
fsdk::IDescriptorMatcherPtr descriptorMatcher = fsdk::acquire(engine->createMatcher(fsdk::DV_MIN_HUMAN_DESCRIPTOR_VERSION));

// Match descriptors.
// Returns similarity in range (0..1],
// where: 0 means totally different.
//        1 means totally the same.
fsdk::ResultValue<fsdk::FSDKError, fsdk::MatchingResult> descriptorMatcherResult =
    descriptorMatcher->match(descriptors[0][0], descriptors[i][j]);
if (descriptorMatcherResult.isError()) {
    ...
}

float similarity = descriptorMatcherResult.getValue().similarity;

Similarity score tells how similar these descriptors are. Its value is in (0..1] range. Values near 1 tell us that the descriptors are very similar.

How to run#

Use the following command to run the example.

./example_human_extraction <some_image1> <some_image2>

Example output#

Similarity of current descriptor and all human descriptors in other images in descent order:
0.920459 :: image number is 1, detection number is 6
0.63473 :: image number is 1, detection number is 1
0.572745 :: image number is 1, detection number is 5
0.562269 :: image number is 1, detection number is 2
0.55178 :: image number is 1, detection number is 7
0.539377 :: image number is 1, detection number is 3
0.528099 :: image number is 1, detection number is 4
0.511602 :: image number is 1, detection number is 8
0.307247 :: image number is 1, detection number is 9
Back to top