Skip to content

Rust API Minimal Examples

Video Processing

This example shows how to initialize the video intelligence class and process a video frame.

use plumerai_video_intelligence as plumerai;

// Settings, to be changed as needed
let input_resolution = plumerai::Size { width: 1600, height: 1200 };

// Initialize the video intelligence algorithm
let mut pvi = plumerai::VideoIntelligence::new(input_resolution);

// Set whether the video stream is night mode (IR) or not.
pvi.set_night_mode(false);

// Loop over frames in a video stream (example: 10 frames)
for _t in 0..10 {
    // Some example input here, normally this is where camera data is acquired
    let image_data = vec![0; 1600 * 1200 * 3]; // RGB888
    let image = plumerai::ImagePointer::PackedRGB888(&image_data);

    // The time between two video frames in seconds
    // In this example we assume a constant frame rate of 30 fps, but variable
    // rates are supported.
    // None means that the system clock will be used.
    let delta_t = Some(1. / 30.);

    // Process the frame
    pvi.process_frame(&image, delta_t)?;

    let predictions = pvi.object_detection().get_detections();

    // Display the results to stdout
    for p in predictions {
        println!("prediction: {:}", p);
    }
}

Automatic Face Enrollment:

This example extends the code above and shows how to use the automatic face enrollment functionality.

use plumerai_video_intelligence as plumerai;

// Settings, to be changed as needed
let input_resolution = plumerai::Size { width: 1600, height: 1200 };

// Initialize the video intelligence algorithm
let mut pvi = plumerai::VideoIntelligence::new(input_resolution);
let mut face_enrollment_automatic = pvi.face_enrollment_automatic()?;
let mut face_identification = pvi.face_identification()?;

// Set whether the video stream is night mode (IR) or not.
pvi.set_night_mode(false);

// Loop over frames in a video stream (example: 10 frames)
for _t in 0..10 {
    // Some example input here, normally this is where camera data is acquired
    let image_data = vec![0; 1600 * 1200 * 3]; // RGB888
    let image = plumerai::ImagePointer::PackedRGB888(&image_data);

    // The time between two video frames in seconds
    // In this example we assume a constant frame rate of 30 fps, but variable
    // rates are supported.
    // None means that the system clock will be used.
    let delta_t = Some(1. / 30.);

    // Process the frame
    pvi.process_frame(&image, delta_t)?;

    // Report the number of faces in the library so far. At first the library
    // will be empty, but as soon as a face is well visible for a while, it
    // will be added to the library with a new unique face ID. The library
    // will grow over time, unless `remove_face_embedding` is called.
    let face_ids = face_enrollment_automatic.get_face_ids();
    println!("Total of {:} people in the familiar face-ID library", face_ids.len());

    let predictions = pvi.object_detection().get_detections();

    // Display the results to stdout
    for p in predictions {
        // After a few frames of showing a clear face in view, the face ID should
        // become non-negative as the face is automatically enrolled.
        if p.class_id() == plumerai::DetectionClass::Face {
            let face_id = face_identification.get_face_id(&p)?;
            println!("Person box with face id {:}: {:}", face_id, p);
        } else {
            println!("prediction: {:}", p);
        }
    }
}

Manual Face Enrollment:

This example shows how to use the manual face enrollment functionality. It consists of two main loops: 1. An example enrollment loop, which runs for a fixed number of frames and computes a face embedding vector to enroll one person in the face library. 2. An example video processing loop, similar to the first example.

use plumerai_video_intelligence as plumerai;

// Settings, to be changed as needed
let input_resolution = plumerai::Size { width: 1600, height: 1200 };

// Initialize the video intelligence algorithm
let mut pvi = plumerai::VideoIntelligence::new(input_resolution);
let mut face_enrollment_manual = pvi.face_enrollment_manual()?;
let mut face_identification = pvi.face_identification()?;

// Set whether the video stream is night mode (IR) or not.
pvi.set_night_mode(false);

// Set an area of the frame to be used for face enrollment.
let enrollment_region = plumerai::RelativeCoords::all();

// Start the manual enrollment
face_enrollment_manual.start_enrollment(&enrollment_region)?;

// Enroll for 10 frames (just an example, more is better)
for _t in 0..10 {
    // Some example input here, normally this is where camera data is acquired
    let image_data = vec![0; 1600 * 1200 * 3]; // RGB888
    let image = plumerai::ImagePointer::PackedRGB888(&image_data);

    // Process the frame. If the enrollment frames come from a video source,
    // then use `process_frame` instead.
    // For single images, the size can be different to the input resolution.
    let image_size = plumerai::ImageSize::VideoSize;
    pvi.single_image(&image, image_size)?;
}

// Finish enrollment
let embedding = face_enrollment_manual.finish_enrollment()?;

// Add the embedding to the library with face ID `1`
let face_id = plumerai::FaceID::Identified(1);
face_enrollment_manual.add_embedding(embedding, &face_id)?;

// Loop over frames in a video stream (example: 10 frames)
for _t in 0..10 {
    // Some example input here, normally this is where camera data is acquired
    let image_data = vec![0; 1600 * 1200 * 3]; // RGB888
    let image = plumerai::ImagePointer::PackedRGB888(&image_data);

    // The time between two video frames in seconds
    // In this example we assume a constant frame rate of 30 fps, but variable
    // rates are supported.
    // None means that the system clock will be used.
    let delta_t = Some(1. / 30.);

    // Process the frame
    pvi.process_frame(&image, delta_t)?;

    let predictions = pvi.object_detection().get_detections();

    // Display the results to stdout
    for p in predictions {
        // After a few frames of showing a clear face in view, the face ID should
        // become non-negative as the face is automatically enrolled.
        if p.class_id() == plumerai::DetectionClass::Person {
            let face_id = face_identification.get_face_id(&p)?;
            println!("Person box with face id {:}: {:}", face_id, p);
        } else {
            println!("prediction: {:}", p);
        }
    }
}