diff --git a/source/camera.cpp b/source/camera.cpp index b9b0a5a..1db0ca9 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) { + ei_printf("resize_size width=%d height=%d\n", resize_size.width, resize_size.height); + } cv::Mat resized; cv::resize(*in_frame, resized, resize_size); @@ -70,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); @@ -82,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); } @@ -105,6 +109,13 @@ int main(int argc, char** argv) { return 1; } + // print camera properties + 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 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) { @@ -148,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]; @@ -162,34 +174,55 @@ 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); + } + 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"); + 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); } - printf("Visual anomaly values: Mean %.3f Max %.3f\n", result.visual_ad_result.mean_value, result.visual_ad_result.max_value); - #endif // 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); // wait (10ms) for a key to be pressed if (cv::waitKey(10) >= 0) break; @@ -200,6 +233,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;