Skip to content

Commit

Permalink
display fps in webcam demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Wei Liu committed Jul 6, 2016
1 parent 64306a8 commit 2125bbd
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/caffe/util/bbox_util.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <algorithm>
#include <ctime>
#include <map>
#include <string>
#include <utility>
Expand Down Expand Up @@ -1122,6 +1123,8 @@ vector<cv::Scalar> GetColors(const int n) {
return colors;
}

static clock_t start_clock = clock();

template <typename Dtype>
void VisualizeBBox(const vector<cv::Mat>& images, const Blob<Dtype>* detections,
const float threshold, const vector<cv::Scalar>& colors,
Expand All @@ -1133,6 +1136,10 @@ void VisualizeBBox(const vector<cv::Mat>& images, const Blob<Dtype>* detections,
if (num_det == 0 || num_img == 0) {
return;
}
// Comute FPS.
float fps = num_img / (double(clock() - start_clock) / CLOCKS_PER_SEC);
start_clock = clock();

const Dtype* detections_data = detections->cpu_data();
const int width = images[0].cols;
const int height = images[0].rows;
Expand All @@ -1154,8 +1161,23 @@ void VisualizeBBox(const vector<cv::Mat>& images, const Blob<Dtype>* detections,
all_detections[img_idx][label].push_back(bbox);
}

int fontface = cv::FONT_HERSHEY_SIMPLEX;
double scale = 1;
int thickness = 2;
int baseline = 0;
char buffer[50];
for (int i = 0; i < num_img; ++i) {
cv::Mat image = images[i];
// Show FPS.
snprintf(buffer, sizeof(buffer), "FPS: %.2f", fps);
cv::Size text = cv::getTextSize(buffer, fontface, scale, thickness,
&baseline);
cv::rectangle(image, cv::Point(0, 0),
cv::Point(text.width, text.height + baseline),
CV_RGB(255, 255, 255), CV_FILLED);
cv::putText(image, buffer, cv::Point(0, text.height + baseline / 2.),
fontface, scale, CV_RGB(0, 0, 0), thickness, 8);
// Draw bboxes.
for (map<int, vector<NormalizedBBox> >::iterator it =
all_detections[i].begin(); it != all_detections[i].end(); ++it) {
int label = it->first;
Expand All @@ -1171,11 +1193,6 @@ void VisualizeBBox(const vector<cv::Mat>& images, const Blob<Dtype>* detections,
cv::Point bottom_right_pt(bboxes[j].xmax(), bboxes[j].ymax());
cv::rectangle(image, top_left_pt, bottom_right_pt, color, 4);
cv::Point bottom_left_pt(bboxes[j].xmin(), bboxes[j].ymax());
int fontface = cv::FONT_HERSHEY_SIMPLEX;
double scale = 1;
int thickness = 2;
int baseline = 0;
char buffer[50];
snprintf(buffer, sizeof(buffer), "%s: %.2f", label_name.c_str(),
bboxes[j].score());
cv::Size text = cv::getTextSize(buffer, fontface, scale, thickness,
Expand Down

0 comments on commit 2125bbd

Please sign in to comment.