Fix encode related fullframe body face images

This commit is contained in:
Barzan Hayati 2025-09-13 00:05:14 +00:00
parent 434f3c79ec
commit c0aeaec9c2
3 changed files with 72 additions and 168 deletions

View File

@ -1038,91 +1038,50 @@ NvOSD_RectParams *FaceNvInferServerManager::allign_postprocess(
float y0 = parent_bbox.top;
float w0 = parent_bbox.width;
float h0 = parent_bbox.height;
// std::cout << "x = " << x0
// << " y = " << y0
// << " w = " << w0
// << " h = " << h0
// << std::endl;
float scale = std::min(target_w / w0, target_h / h0);
int new_w = int(w0 * scale);
int new_h = int(h0 * scale);
int pad_w = target_w - new_w;
int pad_h = target_h - new_h;
// std::cout << "scale = " << scale
// << " new_w = " << new_w
// << " new_h = " << new_h
// << " pad_w = " << pad_w
// << " pad_h = " << pad_h
// << std::endl;
int left = (int)pad_w / 2;
// int right = pad_w - left;
int top = (int)pad_h / 2;
// int bottom = pad_h - top;
// int _pad =(left, top);
// int _scale = scale;
// std::cout << "left = " << left
// << " top = " << top
// << std::endl;
float x1n = numpy_clip(face_location[0], 0.0f, 1.0f);
float y1n = numpy_clip(face_location[1], 0.0f, 1.0f);
float x2n = numpy_clip(face_location[2], 0.0f, 1.0f);
float y2n = numpy_clip(face_location[3], 0.0f, 1.0f);
// std::cout << "x1n = " << x1n
// << " y1n = " << y1n
// << " x2n = " << x2n
// << " y2n = " << y2n
// << std::endl;
float x1_p = x1n * target_w;
float y1_p = y1n * target_h;
float x2_p = x2n * target_w;
float y2_p = y2n * target_h;
// std::cout << "x1_p = " << x1_p
// << " y1_p = " << y1_p
// << " x2_p = " << x2_p
// << " y2_p = " << y2_p
// << std::endl;
int pad_x = left;
int pad_y = top;
// std::cout << "pad_x = " << pad_x
// << " pad_y = " << pad_y
// << std::endl;
float x1 = (x1_p - pad_x) / scale;
float y1 = (y1_p - pad_y) / scale;
float x2 = (x2_p - pad_x) / scale;
float y2 = (y2_p - pad_y) / scale;
// std::cout << "x1 = " << x1
// << " y1 = " << y1
// << " x2 = " << x2
// << " y2 = " << y2
// << std::endl;
x1 += x0;
y1 += y0;
x2 += x0;
y2 += y0;
// std::cout << "x1 = " << x1
// << " y1 = " << y1
// << " x2 = " << x2
// << " y2 = " << y2
// << std::endl;
NvOSD_RectParams *updated_bbox = new NvOSD_RectParams();
updated_bbox->left = x1;
updated_bbox->top = y1;
updated_bbox->width = x2 - x1;
updated_bbox->height = y2 - y1;
// std::cout << "x = " << updated_bbox->left
// << " y = " << updated_bbox->top
// << " w = " << updated_bbox->width
// << " h = " << updated_bbox->height
// << std::endl;
// box = [int(x1), int(y1), int(x2), int(y2)];
// std::quick_exit(0);
return updated_bbox;
}
@ -1135,7 +1094,6 @@ void FaceNvInferServerManager::add_face_body(int object_id, float face_score) {
if (((*iter).face_score > face_score)) {
// std::cout<<(*iter).face_score;
(*iter).face_score = face_score;
// std::cout<<" "<<(*iter).face_score<<std::endl;
return;
}
}
@ -1172,19 +1130,8 @@ void FaceNvInferServerManager::encode_objects_attach_meta(
// }
/*Main Function Call */
// bool nvds_obj_enc_process (NvDsObjEncCtxHandle, NvDsObjEncUsrArgs *,
// NvBufSurface *, NvDsObjectMeta *, NvDsFrameMeta *);
nvds_obj_enc_process(static_cast<NvDsObjEncCtxHandle>(ctx), &objData,
ip_surf, obj_meta, frame_meta);
if (obj_meta->unique_component_id == 1) {
std::cout << "obj_meta->unique_component_id = "
<< obj_meta->unique_component_id
<< " obj_meta->rect_params.width = "
<< obj_meta->rect_params.width
<< " obj_meta->rect_params.height = "
<< obj_meta->rect_params.height << std::endl;
std::quick_exit(0);
}
}
void FaceNvInferServerManager::encode_full_frame_attach_meta(
@ -1262,6 +1209,29 @@ bool FaceNvInferServerManager::all_zero(const float *data, size_t size) {
return true;
}
/**
* @brief Collect all BODY objects in the frame and return a map
* from object_id to NvDsObjectMeta*.
*
* @param frame_meta Frame metadata containing objects
* @param body_component_id unique_component_id used for BODY objects (e.g., 1)
* @return std::unordered_map<guint, NvDsObjectMeta*> map of body objects
*/
std::unordered_map<guint, NvDsObjectMeta *>
FaceNvInferServerManager::collect_body_objects(NvDsFrameMeta *frame_meta,
gint body_component_id = 1) {
std::unordered_map<guint, NvDsObjectMeta *> body_map;
for (NvDsMetaList *l_obj = frame_meta->obj_meta_list; l_obj != nullptr;
l_obj = l_obj->next) {
NvDsObjectMeta *obj = static_cast<NvDsObjectMeta *>(l_obj->data);
if (!obj) continue;
if (obj->unique_component_id == body_component_id) {
body_map[obj->object_id] = obj;
}
}
return body_map;
}
/* This is the buffer probe function that we have registered on the sink pad
* of the tiler element. All SGIE infer elements in the pipeline shall attach
* their NvDsInferTensorMeta to each object's metadata of each frame, here we
@ -1294,52 +1264,26 @@ GstPadProbeReturn FaceNvInferServerManager::sgie_pad_buffer_probe(
gst_buffer_get_nvds_batch_meta(GST_BUFFER(info->data));
if (!batch_meta) return GST_PAD_PROBE_OK;
bool is_zero_embedding_vector = false;
bool current_full_frame_is_captured = false;
// bool current_full_frame_is_captured = false;
bool find_body = false;
/* Iterate each frame metadata in batch */
for (NvDsMetaList *l_frame = batch_meta->frame_meta_list; l_frame != NULL;
l_frame = l_frame->next) {
NvDsFrameMeta *frame_meta = (NvDsFrameMeta *)l_frame->data;
current_full_frame_is_captured = false;
// current_full_frame_is_captured = false;
std::unordered_map<guint, NvDsObjectMeta *> body_map =
collect_body_objects(frame_meta);
/* Iterate object metadata in frame */
for (NvDsMetaList *l_obj = frame_meta->obj_meta_list; l_obj != NULL;
l_obj = l_obj->next) {
NvDsObjectMeta *obj_meta = (NvDsObjectMeta *)l_obj->data;
if (obj_meta->unique_component_id == 1 &&
obj_meta->obj_user_meta_list != NULL) {
// body object obj_user_meta_list list is empty so
// for loop : Iterate user metadata in object to search SGIE's
// tensor data could not execute.
std::cout << "obj_meta->unique_component_id = "
<< obj_meta->unique_component_id
<< " obj_meta->rect_params.width = "
<< obj_meta->rect_params.width
<< " obj_meta->rect_params.height = "
<< obj_meta->rect_params.height << std::endl;
}
// if (obj_meta->unique_component_id == 2) {
// body = obj_meta->parent;
if (!obj_meta) continue;
// // if(body)
// // {
// // std::cout<<"body->class_id = "<<body->class_id
// // << " body->unique_component_id =
// "<<body->unique_component_id<<std::endl;
// // if (body->unique_component_id==1){
// // std::quick_exit(0);
// // }
// // }
// }
// (void)parent;
// if (obj_meta->unique_component_id !=
// IMPRECISE_FACE_COMPONENT_ID){
// continue;
// }
// 1 , 41 for face
// 0 for body
// 1 , 41 for face 0 for body
if (obj_meta->class_id == 1) {
if (obj_meta->rect_params.width >
obj_meta->rect_params.height) {
@ -1362,13 +1306,7 @@ GstPadProbeReturn FaceNvInferServerManager::sgie_pad_buffer_probe(
// body_obj->rect_params.height);
}
}
// else{
// // std::cout<<"obj_meta->class_id ="
// <<obj_meta->class_id<<std::endl; continue;
// }
// std::cout<<"obj_meta->class_id = "<<obj_meta->class_id
// << " obj_meta->unique_component_id =
// "<<obj_meta->unique_component_id<<std::endl;
/* Iterate user metadata in object to search SGIE's tensor data */
for (NvDsMetaList *l_user = obj_meta->obj_user_meta_list;
l_user != NULL; l_user = l_user->next) {
@ -1381,7 +1319,6 @@ GstPadProbeReturn FaceNvInferServerManager::sgie_pad_buffer_probe(
NvDsInferTensorMeta *meta =
(NvDsInferTensorMeta *)user_meta->user_meta_data;
// from here
// std::cout << "meta->num_output_layers: "
// << meta->num_output_layers << std::endl;
for (unsigned int i = 0; i < meta->num_output_layers; i++) {
@ -1498,25 +1435,15 @@ GstPadProbeReturn FaceNvInferServerManager::sgie_pad_buffer_probe(
}
if (score_face > threshold_face_detection) {
// std::cout<<"obj_meta->rect_params left =
// "<<obj_meta->rect_params.left
// <<" top = "<<obj_meta->rect_params.top
// <<" width = "<<obj_meta->rect_params.width
// <<" height =
// "<<obj_meta->rect_params.height<<std::endl;
NvOSD_RectParams *updated_bbox = allign_postprocess(
obj_meta->rect_params, face_location);
// std::cout<<"updated_bbox left = "<<updated_bbox->left
// <<" top = "<<updated_bbox->top
// <<" width = "<<updated_bbox->width
// <<" height = "<<updated_bbox->height<<std::endl;
// (void)updated_bbox;
NvDsObjectMeta *final_face_obj =
nvds_acquire_obj_meta_from_pool(batch_meta);
strncpy(final_face_obj->obj_label, "FINAL_FACE",
sizeof(final_face_obj->obj_label) - 1);
strncpy(
final_face_obj->obj_label, sgie_class_str[0],
sizeof(final_face_obj->obj_label) - 1); //"FINAL_FACE"
final_face_obj
->obj_label[sizeof(final_face_obj->obj_label) - 1] =
'\0'; // Ensure null termination
@ -1525,12 +1452,7 @@ GstPadProbeReturn FaceNvInferServerManager::sgie_pad_buffer_probe(
final_face_obj->confidence = score_face;
add_face_body(obj_meta->object_id, score_face);
// std::cout << "body_obj->object_id = " <<
// body_obj->object_id
// << " score_face= "<<score_face<<std::endl;
// std::cout<< " size face_body_list = " <<
// face_body_list.size() << std::endl; obj_meta->object_id =
// UNTRACKED_OBJECT_ID;
// obj_meta->object_id = UNTRACKED_OBJECT_ID;
final_face_obj->class_id = 1;
final_face_obj->object_id = obj_meta->object_id;
@ -1550,14 +1472,6 @@ GstPadProbeReturn FaceNvInferServerManager::sgie_pad_buffer_probe(
// int(obj_meta->rect_params.height *
// MUXER_OUTPUT_HEIGHT / FACE_NET_HEIGHT);
// std::cout << "nvinferserver second for x = " <<
// rect_params.left
// << " y = " << rect_params.top
// << " w = " << rect_params.width
// << " h = " << rect_params.height
// << " score = " << obj_meta->confidence <<
// std::endl;
/* Border of width 3. */
rect_params.border_width = 3;
rect_params.has_bg_color = 0;
@ -1596,15 +1510,37 @@ GstPadProbeReturn FaceNvInferServerManager::sgie_pad_buffer_probe(
// nvds_add_obj_meta_to_frame(frame_meta, final_face_obj,
// NULL);
if (is_zero_embedding_vector == false) {
if (current_full_frame_is_captured == false) {
// encode_objects_attach_meta(ctx, ip_surf, frame_meta,
// obj_meta);
// // collect all body objects
// auto body_map = collect_body_objects(frame_meta);
// // encode body corresponding to this object
find_body = false;
auto it = body_map.find(obj_meta->object_id);
if (it != body_map.end()) {
NvDsObjectMeta *body_meta = it->second;
encode_objects_attach_meta(ctx, ip_surf, frame_meta,
body_meta);
find_body = true;
} else {
// body object not found for this object_id
// std::cout << "No body object found for object_id
// = " << obj_meta->object_id << std::endl;
}
if (find_body == true) {
// if (current_full_frame_is_captured == false) {
// encode_full_frame_attach_meta(ctx, ip_surf,
// frame_meta);
// current_full_frame_is_captured = true;
// }
encode_full_frame_attach_meta(ctx, ip_surf,
frame_meta);
current_full_frame_is_captured = true;
encode_objects_attach_meta(ctx, ip_surf, frame_meta,
final_face_obj);
}
encode_objects_attach_meta(ctx, ip_surf, frame_meta,
obj_meta);
encode_objects_attach_meta(ctx, ip_surf, frame_meta,
final_face_obj);
}
nvds_add_obj_meta_to_frame(frame_meta, final_face_obj,
obj_meta);
@ -1615,27 +1551,13 @@ GstPadProbeReturn FaceNvInferServerManager::sgie_pad_buffer_probe(
// unsigned int numDims = layer.inferDims.numDims;
// unsigned int numElements = layer.inferDims.numElements;
// std::cout << "Layer " << jkl << " (" << layer.layerName
// <<
// "):\n"; std::cout << " Num Dims: " << numDims << "\n";
// std::cout << " Num Elements: " << numElements << "\n";
// std::cout << " Dims: [";
// for (unsigned int mno = 0; mno < numDims; ++mno) {
// std::cout << layer.inferDims.d[mno];
// if (mno < numDims - 1)
// std::cout << ", ";
// }
// std::cout << "]\n";
// float *data_face = static_cast<float *>(layer.buffer);
// }
// std::quick_exit(0);
// const NvDsInferLayerInfo &layer =
// outputLayersInfo[0]; // or loop over all
// std::cout<<"meta->num_output_layers: "<<
// meta->num_output_layers<<std::endl; for (unsigned int i = 0;
// for (unsigned int i = 0;
// i < meta->num_output_layers; i++) {
// NvDsInferLayerInfo *info = &meta->output_layers_info[i];
// info->buffer = meta->out_buf_ptrs_host[i];
@ -1652,7 +1574,6 @@ GstPadProbeReturn FaceNvInferServerManager::sgie_pad_buffer_probe(
getDimsCHWFromDims(dims_chw,
meta->output_layers_info[0].inferDims);
unsigned int numClasses = dims_chw.c;
// std::cout << "numClasses: " << numClasses << std::endl;
(void)numClasses;
// float *outputCoverageBuffer =
// (float *) meta->output_layers_info[0].buffer;

View File

@ -9,6 +9,8 @@
// #include "nvdsinfer_custom_impl.h"
#include <immintrin.h> // for AVX intrinsics
#include <unordered_map>
#include "config_manager.hpp"
#include "custom_gstnvdsinfer.hpp"
#include "nvdsmeta.h"
@ -72,4 +74,6 @@ class FaceNvInferServerManager {
NvDsFrameMeta *);
static void encode_objects_attach_meta(gpointer, NvBufSurface *,
NvDsFrameMeta *, NvDsObjectMeta *);
static std::unordered_map<guint, NvDsObjectMeta *> collect_body_objects(
NvDsFrameMeta *, gint);
};

View File

@ -63,8 +63,6 @@ void NvOsdManager::save_full_frame(NvDsFrameMeta *frame_meta) {
int stream_num = 0;
while (usrMetaList != NULL) {
NvDsUserMeta *usrMetaData = (NvDsUserMeta *)usrMetaList->data;
// std::cout<<"usrMetaData->base_meta.meta_type frame =
// "<<usrMetaData->base_meta.meta_type<<std::endl;
if (usrMetaData->base_meta.meta_type == NVDS_CROP_IMAGE_META) {
snprintf(fileFrameNameString, FILE_NAME_SIZE, "%s_frame_%d_%d.jpg",
osd_string, frame_number, stream_num++);
@ -75,8 +73,6 @@ void NvOsdManager::save_full_frame(NvDsFrameMeta *frame_meta) {
fwrite(enc_jpeg_image->outBuffer, sizeof(uint8_t),
enc_jpeg_image->outLen, file);
fclose(file);
// std::cout<<"fileFrameNameString =
// "<<fileFrameNameString<<std::endl;
}
usrMetaList = usrMetaList->next;
}
@ -86,21 +82,15 @@ void NvOsdManager::save_cropped_objects(NvDsFrameMeta *frame_meta,
NvDsObjectMeta *obj_meta,
guint num_rects) {
const char *osd_string = "OSD";
// std::cout<<"obj_meta->unique_component_id =
// "<<obj_meta->unique_component_id<<std::endl;
char fileObjNameString[FILE_NAME_SIZE];
/* For Demonstration Purposes we are writing metadata to jpeg images of
* vehicles or persons for the first 100 frames only.
* The files generated have a 'OSD' prefix. */
// std::cout<<"in obj_meta_list NvOsdManager"<<std::endl;
// std::quick_exit(0);
NvDsUserMetaList *usrMetaList = obj_meta->obj_user_meta_list;
FILE *file;
while (usrMetaList != NULL) {
NvDsUserMeta *usrMetaData = (NvDsUserMeta *)usrMetaList->data;
// std::cout<<"usrMetaData->base_meta.meta_type object =
// "<<usrMetaData->base_meta.meta_type<<std::endl;
if (usrMetaData->base_meta.meta_type == NVDS_CROP_IMAGE_META) {
NvDsObjEncOutParams *enc_jpeg_image =
(NvDsObjEncOutParams *)usrMetaData->user_meta_data;
@ -113,9 +103,6 @@ void NvOsdManager::save_cropped_objects(NvDsFrameMeta *frame_meta,
fwrite(enc_jpeg_image->outBuffer, sizeof(uint8_t),
enc_jpeg_image->outLen, file);
fclose(file);
std::cout << "fileObjNameString = " << fileObjNameString
<< " obj_meta->class_id =" << obj_meta->class_id
<< std::endl;
usrMetaList = NULL;
} else {
usrMetaList = usrMetaList->next;
@ -153,14 +140,6 @@ GstPadProbeReturn NvOsdManager::osd_src_pad_buffer_probe(GstPad *pad,
if (obj_meta->class_id == PGIE_CLASS_ID_PERSON) {
person_count++;
num_rects++;
// std::cout << "In OSD sink "
// << "x = " << obj_meta->rect_params.left
// << " y = " << obj_meta->rect_params.top
// << " w = " << obj_meta->rect_params.width
// << " h = " << obj_meta->rect_params.height
// << " score = " << obj_meta->confidence
// << " Object ID: " << obj_meta->object_id
// << std::endl;
}
if (write_cropped_objects_to_disk == true)
save_cropped_objects(frame_meta, obj_meta, num_rects);