Skip to content

Adding tracking code to the camera/video #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions source/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(largest_factor * static_cast<float>(in_frame->cols)),
static_cast<int>(largest_factor * static_cast<float>(in_frame->rows)));
cv::Size resize_size(ceil(largest_factor * static_cast<float>(in_frame->cols)),
ceil(largest_factor * static_cast<float>(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);
Expand Down Expand Up @@ -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);
}
Expand All @@ -105,13 +109,21 @@ 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);
}

// this will contain the image from the webcam
cv::Mat frame;
run_classifier_init();

// display the frame until you press a key
while (1) {
Expand Down Expand Up @@ -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;
Expand All @@ -200,6 +234,7 @@ int main(int argc, char** argv) {
usleep(sleep_ms * 1000);
}
}
run_classifier_deinit();
return 0;
}

Expand Down
33 changes: 24 additions & 9 deletions source/video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -229,6 +242,8 @@ int main(int argc, char** argv) {
usleep(sleep_ms * 1000);
}
}

run_classifier_deinit();
output_file.release();
file.release();
return 0;
Expand Down