6#include <sfe_toolkit/sfe_core.h>
7#include <sfe_toolkit/sfe_object.h>
8#include <sfe_toolkit/sfe_person.h>
12#include "annotate.hpp"
20std::string
image_path =
"assets/person/person_attributes.jpg";
31 auto encoded_image = utils::readFile(
image_path);
32 error =
sfeImageDecode(encoded_image.data(), encoded_image.size(), &image);
33 utils::checkError(error);
35 std::cout <<
"Image size: " << image.width <<
"x" << image.height << std::endl;
38 std::vector<SFEDetection> detections(10);
44 utils::checkError(error);
47 size_t detection_count = detections.size();
52 detections.data(), &detection_count);
53 utils::checkError(error);
54 detections.resize(detection_count);
56 std::cout <<
"Detected " << detection_count <<
" objects" << std::endl;
59 std::vector<size_t> detection_classes;
61 for (
size_t i = 0; i < detections.size(); i++) {
62 auto& detection = detections[i];
63 size_t best_class_index = 0;
65 auto object = detection.modality_data.object.objects[c];
66 if (
object.confidence < 0.01)
continue;
67 if (
object.confidence > detection.modality_data.object.objects[best_class_index].confidence) {
71 std::cout <<
"Detection " << i <<
" : " << OBJECT_CLASS_NAMES[c] <<
" confidence: " << std::fixed << std::setprecision(2) << detection.modality_data.object.objects[c].confidence << std::endl;
73 detection_classes.push_back(best_class_index);
80 if (person_iter == detection_classes.end()) {
81 std::cout <<
"No person detected" << std::endl;
84 person_index = std::distance(detection_classes.begin(), person_iter);
86 std::cout <<
"Person detected at index " << person_index << std::endl;
89 std::array<SFEPersonAttribute, SFE_PERSON_ATTRIBUTE_COUNT> person_attributes;
94 utils::checkError(error);
97 error = sfePersonAttributes(attribute_solver, image, &detections[person_index], person_attributes.data());
98 utils::checkError(error);
100 for (
auto& attribute : person_attributes) {
101 if (attribute.confidence < 0.1)
continue;
102 std::cout <<
"Attribute " << PERSON_ATTRIBUTE_TYPE[attribute.attribute_type] <<
" confidence: " << std::fixed << std::setprecision(2) << attribute.confidence << std::endl;
106 std::array<SFEPersonPoseKeypoint, SFE_PERSON_POSE_KEYPOINT_COUNT> person_pose;
111 utils::checkError(error);
114 error = sfePersonPose(pose_solver, image, &detections[person_index], person_pose.data());
115 utils::checkError(error);
117 for (
auto& keypoint : person_pose) {
118 if (keypoint.confidence < 0.1)
continue;
119 std::cout <<
"Keypoint " << keypoint.keypoint_type <<
" confidence: " << std::fixed << std::setprecision(2) << keypoint.confidence << std::endl;
124 for (
size_t i = 0; i < detections.size(); i++) {
125 auto& detection = detections[i];
126 auto& class_index = detection_classes[i];
127 auto color = i == person_index ? annotate::GREEN : annotate::RED;
129 std::stringstream label;
130 label << i <<
": " << OBJECT_CLASS_NAMES[class_index] << std::fixed << std::setprecision(2)
131 <<
" c=" << detection.modality_data.object.objects[class_index].confidence
132 <<
" d=" << detection.confidence;
134 annotate::labelBox(image, label.str(), detection.bounding_box, annotate::WHITE, color);
138 for (
auto& keypoint : person_pose) {
139 annotate::circle(image, keypoint.x * image.width, keypoint.y * image.height, 3, annotate::YELLOW);
143 size_t size = image.width * image.height * 3;
144 auto png_file = std::vector<unsigned char>(size);
146 utils::checkError(error);
147 png_file.resize(size);
148 utils::saveFile(
"person_attributes.png", png_file);
float detection_threshold
std::string solver_person_pose
std::string solver_object_detect
std::string solver_person_attributes
const auto SOLVER_PARAMETERS
void sfeSolverFree(SFESolver solver)
Free memory associated with SFESolver.
void * SFESolver
Solver provides an abstract interface over inference models and engines.
void * SFEError
Error type used to hold optional error message.
void sfeImageFree(SFEImage image)
Free memory associated with SFEImage.
@ SFE_IMAGE_FORMAT_PNG
Portable Network Graphics format.
SFEError sfeImageEncode(SFEImageView image, SFEImageFormat image_format, unsigned char *out_data, size_t *in_out_data_len)
Encode SFEImage into a buffer with specified image_format.
SFEError sfeSolverCreate(const char *solver_file, const SFESolverParameter *solver_parameters, size_t solver_parameters_count, SFESolver *out_solver)
Create new solver from solver file.
SFEError sfeImageDecode(const unsigned char *data, size_t data_len, SFEImage *out_image)
Decode SFEImage from raw image data of various formats. Eg. PNG, JPEG ..
SFEError sfeDetect(SFESolver solver, SFEImageView image, float threshold, SFEDetection *out_detections, size_t *in_out_detection_count)
Detect objects in the source image using unified detection API.
#define SFE_OBJECT_DETECT
Object detection count.
Raw owned raster image representation, HWC|BGR order.
Solver parameter used to configure solvers.