Skip to content

Commit 6c991a8

Browse files
moved tracking code from internal repo
1 parent 5b14a35 commit 6c991a8

File tree

2 files changed

+63
-13
lines changed

2 files changed

+63
-13
lines changed

source/camera.cpp

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,12 @@ void resize_and_crop(cv::Mat *in_frame, cv::Mat *out_frame) {
5454

5555
float largest_factor = factor_w > factor_h ? factor_w : factor_h;
5656

57-
cv::Size resize_size(static_cast<int>(largest_factor * static_cast<float>(in_frame->cols)),
58-
static_cast<int>(largest_factor * static_cast<float>(in_frame->rows)));
57+
cv::Size resize_size(ceil(largest_factor * static_cast<float>(in_frame->cols)),
58+
ceil(largest_factor * static_cast<float>(in_frame->rows)));
59+
60+
if (use_debug) {
61+
printf("resize_size width=%d height=%d\n", resize_size.width, resize_size.height);
62+
}
5963

6064
cv::Mat resized;
6165
cv::resize(*in_frame, resized, resize_size);
@@ -86,7 +90,7 @@ int main(int argc, char** argv) {
8690
printf("You can find these via `v4l2-ctl --list-devices`.\n");
8791
printf("E.g. for:\n");
8892
printf(" C922 Pro Stream Webcam (usb-70090000.xusb-2.1):\n");
89-
printf(" /dev/video0\n");
93+
printf(" /dev/video0\n");
9094
printf("The ID of the webcam is 0\n");
9195
exit(1);
9296
}
@@ -105,13 +109,21 @@ int main(int argc, char** argv) {
105109
return 1;
106110
}
107111

112+
// print camera properties
113+
printf("");
114+
printf("Camera properties:\n");
115+
printf(" width: %d\n", (int)camera.get(cv::CAP_PROP_FRAME_WIDTH));
116+
printf(" height: %d\n", (int)camera.get(cv::CAP_PROP_FRAME_HEIGHT));
117+
printf(" fps: %d\n", (int)camera.get(cv::CAP_PROP_FPS));
118+
108119
if (use_debug) {
109120
// create a window to display the images from the webcam
110121
cv::namedWindow("Webcam", cv::WINDOW_AUTOSIZE);
111122
}
112123

113124
// this will contain the image from the webcam
114125
cv::Mat frame;
126+
run_classifier_init();
115127

116128
// display the frame until you press a key
117129
while (1) {
@@ -187,9 +199,31 @@ int main(int argc, char** argv) {
187199
printf("Visual anomaly values: Mean %.3f Max %.3f\n", result.visual_ad_result.mean_value, result.visual_ad_result.max_value);
188200
#endif
189201

202+
// print the open traces
203+
ei_printf("Open traces:\r\n");
204+
for (uint32_t i = 0; i < result.postprocessed_output.object_tracking_output.open_traces_count; i++) {
205+
ei_object_tracking_trace_t trace = result.postprocessed_output.object_tracking_output.open_traces[i];
206+
ei_printf(" Trace %d: %s [ x: %u, y: %u, width: %u, height: %u, value: %f ]\r\n",
207+
trace.id,
208+
trace.label,
209+
trace.x,
210+
trace.y,
211+
trace.width,
212+
trace.height,
213+
trace.value);
214+
}
215+
190216
// show the image on the window
191217
if (use_debug) {
192-
cv::imshow("Webcam", cropped);
218+
// draw the bounding boxes of the traces
219+
for (uint32_t i = 0; i < result.postprocessed_output.object_tracking_output.open_traces_count; i++) {
220+
ei_object_tracking_trace_t trace = result.postprocessed_output.object_tracking_output.open_traces[i];
221+
cv::rectangle(cropped, cv::Rect(trace.x, trace.y, trace.width, trace.height), cv::Scalar(0, 255, 0), 2);
222+
// add the label and ID
223+
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);
224+
}
225+
cv::imshow("File", cropped);
226+
output_file.write(cropped);
193227
// wait (10ms) for a key to be pressed
194228
if (cv::waitKey(10) >= 0)
195229
break;
@@ -200,6 +234,7 @@ int main(int argc, char** argv) {
200234
usleep(sleep_ms * 1000);
201235
}
202236
}
237+
run_classifier_deinit();
203238
return 0;
204239
}
205240

source/video.cpp

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ int main(int argc, char** argv) {
124124

125125
// this will contain the image from the file
126126
cv::Mat frame;
127+
run_classifier_init();
127128

128129
// display the frames until the end of the file
129130
while (true) {
@@ -165,6 +166,7 @@ int main(int argc, char** argv) {
165166
return 1;
166167
}
167168

169+
168170
// print the predictions
169171
printf("Predictions (DSP: %d ms., Classification: %d ms., Anomaly: %d ms.): \n",
170172
result.timing.dsp, result.timing.classification, result.timing.anomaly);
@@ -204,17 +206,28 @@ int main(int argc, char** argv) {
204206
printf("Visual anomaly values: Mean %.3f Max %.3f\n", result.visual_ad_result.mean_value, result.visual_ad_result.max_value);
205207
#endif
206208

209+
// print the open traces
210+
ei_printf("Open traces:\r\n");
211+
for (uint32_t i = 0; i < result.postprocessed_output.object_tracking_output.open_traces_count; i++) {
212+
ei_object_tracking_trace_t trace = result.postprocessed_output.object_tracking_output.open_traces[i];
213+
ei_printf(" Trace %d: %s [ x: %u, y: %u, width: %u, height: %u, value: %f ]\r\n",
214+
trace.id,
215+
trace.label,
216+
trace.x,
217+
trace.y,
218+
trace.width,
219+
trace.height,
220+
trace.value);
221+
}
222+
207223
// show the image on the window
208224
if (use_debug) {
209-
// draw the bounding boxes
210-
for (size_t ix = 0; ix < result.bounding_boxes_count; ix++) {
211-
auto bb = result.bounding_boxes[ix];
212-
if (bb.value == 0) {
213-
continue;
214-
}
215-
216-
cv::rectangle(cropped, cv::Rect(bb.x, bb.y, bb.width, bb.height), cv::Scalar(0, 255, 0), 2);
217-
cv::putText(cropped, bb.label, cv::Point(bb.x, bb.y - 10), cv::FONT_HERSHEY_SIMPLEX, 0.9, cv::Scalar(0, 255, 0), 2);
225+
// draw the bounding boxes of the traces
226+
for (uint32_t i = 0; i < result.postprocessed_output.object_tracking_output.open_traces_count; i++) {
227+
ei_object_tracking_trace_t trace = result.postprocessed_output.object_tracking_output.open_traces[i];
228+
cv::rectangle(cropped, cv::Rect(trace.x, trace.y, trace.width, trace.height), cv::Scalar(0, 255, 0), 2);
229+
// add the label and ID
230+
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);
218231
}
219232

220233
cv::imshow("File", cropped);
@@ -229,6 +242,8 @@ int main(int argc, char** argv) {
229242
usleep(sleep_ms * 1000);
230243
}
231244
}
245+
246+
run_classifier_deinit();
232247
output_file.release();
233248
file.release();
234249
return 0;

0 commit comments

Comments
 (0)