From 6c991a890a190447d5eeb747c272961f0be0098d Mon Sep 17 00:00:00 2001 From: Dmitry Maslov Date: Thu, 20 Mar 2025 15:01:09 +0100 Subject: [PATCH 1/3] moved tracking code from internal repo --- source/camera.cpp | 43 +++++++++++++++++++++++++++++++++++++++---- source/video.cpp | 33 ++++++++++++++++++++++++--------- 2 files changed, 63 insertions(+), 13 deletions(-) diff --git a/source/camera.cpp b/source/camera.cpp index b9b0a5a..7ea0ecc 100644 --- a/source/camera.cpp +++ b/source/camera.cpp @@ -54,8 +54,12 @@ void resize_and_crop(cv::Mat *in_frame, cv::Mat *out_frame) { float largest_factor = factor_w > factor_h ? factor_w : factor_h; - cv::Size resize_size(static_cast(largest_factor * static_cast(in_frame->cols)), - static_cast(largest_factor * static_cast(in_frame->rows))); + cv::Size resize_size(ceil(largest_factor * static_cast(in_frame->cols)), + ceil(largest_factor * static_cast(in_frame->rows))); + + if (use_debug) { + printf("resize_size width=%d height=%d\n", resize_size.width, resize_size.height); + } cv::Mat resized; cv::resize(*in_frame, resized, resize_size); @@ -86,7 +90,7 @@ int main(int argc, char** argv) { printf("You can find these via `v4l2-ctl --list-devices`.\n"); printf("E.g. for:\n"); printf(" C922 Pro Stream Webcam (usb-70090000.xusb-2.1):\n"); - printf(" /dev/video0\n"); + printf(" /dev/video0\n"); printf("The ID of the webcam is 0\n"); exit(1); } @@ -105,6 +109,13 @@ int main(int argc, char** argv) { return 1; } + // print camera properties + printf(""); + printf("Camera properties:\n"); + printf(" width: %d\n", (int)camera.get(cv::CAP_PROP_FRAME_WIDTH)); + printf(" height: %d\n", (int)camera.get(cv::CAP_PROP_FRAME_HEIGHT)); + printf(" fps: %d\n", (int)camera.get(cv::CAP_PROP_FPS)); + if (use_debug) { // create a window to display the images from the webcam cv::namedWindow("Webcam", cv::WINDOW_AUTOSIZE); @@ -112,6 +123,7 @@ int main(int argc, char** argv) { // this will contain the image from the webcam cv::Mat frame; + run_classifier_init(); // display the frame until you press a key while (1) { @@ -187,9 +199,31 @@ int main(int argc, char** argv) { printf("Visual anomaly values: Mean %.3f Max %.3f\n", result.visual_ad_result.mean_value, result.visual_ad_result.max_value); #endif + // print the open traces + ei_printf("Open traces:\r\n"); + for (uint32_t i = 0; i < result.postprocessed_output.object_tracking_output.open_traces_count; i++) { + ei_object_tracking_trace_t trace = result.postprocessed_output.object_tracking_output.open_traces[i]; + ei_printf(" Trace %d: %s [ x: %u, y: %u, width: %u, height: %u, value: %f ]\r\n", + trace.id, + trace.label, + trace.x, + trace.y, + trace.width, + trace.height, + trace.value); + } + // show the image on the window if (use_debug) { - cv::imshow("Webcam", cropped); + // draw the bounding boxes of the traces + for (uint32_t i = 0; i < result.postprocessed_output.object_tracking_output.open_traces_count; i++) { + ei_object_tracking_trace_t trace = result.postprocessed_output.object_tracking_output.open_traces[i]; + cv::rectangle(cropped, cv::Rect(trace.x, trace.y, trace.width, trace.height), cv::Scalar(0, 255, 0), 2); + // add the label and ID + cv::putText(cropped, std::to_string(trace.id) + ": " + trace.label, cv::Point(trace.x, trace.y - 10), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 255, 0), 2); + } + cv::imshow("File", cropped); + output_file.write(cropped); // wait (10ms) for a key to be pressed if (cv::waitKey(10) >= 0) break; @@ -200,6 +234,7 @@ int main(int argc, char** argv) { usleep(sleep_ms * 1000); } } + run_classifier_deinit(); return 0; } diff --git a/source/video.cpp b/source/video.cpp index 2008d04..7c86f9c 100644 --- a/source/video.cpp +++ b/source/video.cpp @@ -124,6 +124,7 @@ int main(int argc, char** argv) { // this will contain the image from the file cv::Mat frame; + run_classifier_init(); // display the frames until the end of the file while (true) { @@ -165,6 +166,7 @@ int main(int argc, char** argv) { return 1; } + // print the predictions printf("Predictions (DSP: %d ms., Classification: %d ms., Anomaly: %d ms.): \n", result.timing.dsp, result.timing.classification, result.timing.anomaly); @@ -204,17 +206,28 @@ int main(int argc, char** argv) { printf("Visual anomaly values: Mean %.3f Max %.3f\n", result.visual_ad_result.mean_value, result.visual_ad_result.max_value); #endif + // print the open traces + ei_printf("Open traces:\r\n"); + for (uint32_t i = 0; i < result.postprocessed_output.object_tracking_output.open_traces_count; i++) { + ei_object_tracking_trace_t trace = result.postprocessed_output.object_tracking_output.open_traces[i]; + ei_printf(" Trace %d: %s [ x: %u, y: %u, width: %u, height: %u, value: %f ]\r\n", + trace.id, + trace.label, + trace.x, + trace.y, + trace.width, + trace.height, + trace.value); + } + // show the image on the window if (use_debug) { - // draw the bounding boxes - for (size_t ix = 0; ix < result.bounding_boxes_count; ix++) { - auto bb = result.bounding_boxes[ix]; - if (bb.value == 0) { - continue; - } - - cv::rectangle(cropped, cv::Rect(bb.x, bb.y, bb.width, bb.height), cv::Scalar(0, 255, 0), 2); - cv::putText(cropped, bb.label, cv::Point(bb.x, bb.y - 10), cv::FONT_HERSHEY_SIMPLEX, 0.9, cv::Scalar(0, 255, 0), 2); + // draw the bounding boxes of the traces + for (uint32_t i = 0; i < result.postprocessed_output.object_tracking_output.open_traces_count; i++) { + ei_object_tracking_trace_t trace = result.postprocessed_output.object_tracking_output.open_traces[i]; + cv::rectangle(cropped, cv::Rect(trace.x, trace.y, trace.width, trace.height), cv::Scalar(0, 255, 0), 2); + // add the label and ID + cv::putText(cropped, std::to_string(trace.id) + ": " + trace.label, cv::Point(trace.x, trace.y - 10), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 255, 0), 2); } cv::imshow("File", cropped); @@ -229,6 +242,8 @@ int main(int argc, char** argv) { usleep(sleep_ms * 1000); } } + + run_classifier_deinit(); output_file.release(); file.release(); return 0; From 682afafd573c74834ff6f4e8866f49556f4848da Mon Sep 17 00:00:00 2001 From: Dmitry Maslov Date: Tue, 1 Apr 2025 15:53:56 +0200 Subject: [PATCH 2/3] removed leftover from video.cpp --- source/camera.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/source/camera.cpp b/source/camera.cpp index 7ea0ecc..6d73688 100644 --- a/source/camera.cpp +++ b/source/camera.cpp @@ -223,7 +223,6 @@ int main(int argc, char** argv) { cv::putText(cropped, std::to_string(trace.id) + ": " + trace.label, cv::Point(trace.x, trace.y - 10), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 255, 0), 2); } cv::imshow("File", cropped); - output_file.write(cropped); // wait (10ms) for a key to be pressed if (cv::waitKey(10) >= 0) break; From 18d71116e63ebed70e1eb090e8b3a5915763cbc2 Mon Sep 17 00:00:00 2001 From: automatiek Date: Thu, 3 Apr 2025 13:32:10 +0200 Subject: [PATCH 3/3] Use ei_printf and alignment --- source/camera.cpp | 58 +++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/source/camera.cpp b/source/camera.cpp index 6d73688..1db0ca9 100644 --- a/source/camera.cpp +++ b/source/camera.cpp @@ -58,7 +58,7 @@ void resize_and_crop(cv::Mat *in_frame, cv::Mat *out_frame) { ceil(largest_factor * static_cast(in_frame->rows))); if (use_debug) { - printf("resize_size width=%d height=%d\n", resize_size.width, resize_size.height); + ei_printf("resize_size width=%d height=%d\n", resize_size.width, resize_size.height); } cv::Mat resized; @@ -74,7 +74,7 @@ void resize_and_crop(cv::Mat *in_frame, cv::Mat *out_frame) { cv::Rect crop_region(crop_x, crop_y, EI_CLASSIFIER_INPUT_WIDTH, EI_CLASSIFIER_INPUT_HEIGHT); if (use_debug) { - printf("crop_region x=%d y=%d width=%d height=%d\n", crop_x, crop_y, EI_CLASSIFIER_INPUT_WIDTH, EI_CLASSIFIER_INPUT_HEIGHT); + ei_printf("crop_region x=%d y=%d width=%d height=%d\n", crop_x, crop_y, EI_CLASSIFIER_INPUT_WIDTH, EI_CLASSIFIER_INPUT_HEIGHT); } *out_frame = resized(crop_region); @@ -86,12 +86,12 @@ int main(int argc, char** argv) { // Try it from a real terminal. if (argc < 2) { - printf("Requires one parameter (ID of the webcam).\n"); - printf("You can find these via `v4l2-ctl --list-devices`.\n"); - printf("E.g. for:\n"); - printf(" C922 Pro Stream Webcam (usb-70090000.xusb-2.1):\n"); - printf(" /dev/video0\n"); - printf("The ID of the webcam is 0\n"); + ei_printf("Requires one parameter (ID of the webcam).\n"); + ei_printf("You can find these via `v4l2-ctl --list-devices`.\n"); + ei_printf("E.g. for:\n"); + ei_printf(" C922 Pro Stream Webcam (usb-70090000.xusb-2.1):\n"); + ei_printf(" /dev/video0\n"); + ei_printf("The ID of the webcam is 0\n"); exit(1); } @@ -110,11 +110,11 @@ int main(int argc, char** argv) { } // print camera properties - printf(""); - printf("Camera properties:\n"); - printf(" width: %d\n", (int)camera.get(cv::CAP_PROP_FRAME_WIDTH)); - printf(" height: %d\n", (int)camera.get(cv::CAP_PROP_FRAME_HEIGHT)); - printf(" fps: %d\n", (int)camera.get(cv::CAP_PROP_FPS)); + ei_printf(""); + ei_printf("Camera properties:\n"); + ei_printf(" width: %d\n", (int)camera.get(cv::CAP_PROP_FRAME_WIDTH)); + ei_printf(" height: %d\n", (int)camera.get(cv::CAP_PROP_FRAME_HEIGHT)); + ei_printf(" fps: %d\n", (int)camera.get(cv::CAP_PROP_FPS)); if (use_debug) { // create a window to display the images from the webcam @@ -160,12 +160,12 @@ int main(int argc, char** argv) { return 1; } - // print the predictions - printf("Predictions (DSP: %d ms., Classification: %d ms., Anomaly: %d ms.): \n", - result.timing.dsp, result.timing.classification, result.timing.anomaly); + // print the predictions + ei_printf("Predictions (DSP: %d ms., Classification: %d ms., Anomaly: %d ms.): \n", + result.timing.dsp, result.timing.classification, result.timing.anomaly); - #if EI_CLASSIFIER_OBJECT_DETECTION == 1 - printf("#Object detection results:\n"); + #if EI_CLASSIFIER_OBJECT_DETECTION == 1 + ei_printf("#Object detection results:\n"); bool found_bb = false; for (size_t ix = 0; ix < result.bounding_boxes_count; ix++) { auto bb = result.bounding_boxes[ix]; @@ -174,30 +174,30 @@ int main(int argc, char** argv) { } found_bb = true; - printf(" %s (%f) [ x: %u, y: %u, width: %u, height: %u ]\n", bb.label, bb.value, bb.x, bb.y, bb.width, bb.height); + ei_printf(" %s (%f) [ x: %u, y: %u, width: %u, height: %u ]\n", bb.label, bb.value, bb.x, bb.y, bb.width, bb.height); } if (!found_bb) { - printf(" no objects found\n"); + ei_printf(" no objects found\n"); } - #else - printf("#Classification results:\n"); + #else + ei_printf("#Classification results:\n"); for (size_t ix = 0; ix < EI_CLASSIFIER_LABEL_COUNT; ix++) { - printf("%s: %.05f\n", result.classification[ix].label, result.classification[ix].value); + ei_printf("%s: %.05f\n", result.classification[ix].label, result.classification[ix].value); } - #endif + #endif - #if EI_CLASSIFIER_HAS_ANOMALY == 3 // visual AD - printf("#Visual anomaly grid results:\n"); + #if EI_CLASSIFIER_HAS_ANOMALY == 3 // visual AD + ei_printf("#Visual anomaly grid results:\n"); for (uint32_t i = 0; i < result.visual_ad_count; i++) { ei_impulse_result_bounding_box_t bb = result.visual_ad_grid_cells[i]; if (bb.value == 0) { continue; } - printf(" %s (%f) [ x: %u, y: %u, width: %u, height: %u ]\n", bb.label, bb.value, bb.x, bb.y, bb.width, bb.height); + ei_printf(" %s (%f) [ x: %u, y: %u, width: %u, height: %u ]\n", bb.label, bb.value, bb.x, bb.y, bb.width, bb.height); } - printf("Visual anomaly values: Mean %.3f Max %.3f\n", result.visual_ad_result.mean_value, result.visual_ad_result.max_value); - #endif + ei_printf("Visual anomaly values: Mean %.3f Max %.3f\n", result.visual_ad_result.mean_value, result.visual_ad_result.max_value); + #endif // print the open traces ei_printf("Open traces:\r\n");