caffe icon indicating copy to clipboard operation
caffe copied to clipboard

How to extract min, max of X, Y in VisualizeBBox?

Open mintlatte opened this issue 9 years ago • 2 comments

I want to extract min, max of x, y, so I modified bbox_util.cpp (VisualizeBBox) like this --- void VisualizeBBox(const vectorcv::Mat& images, const Blob* detections, const float threshold, const vectorcv::Scalar& colors, const map<int, string>& label_to_display_name, const string& save_file) { // Retrieve detections. CHECK_EQ(detections->width(), 7); const int num_det = detections->height(); const int num_img = images.size(); if (num_det == 0 || num_img == 0) { return; } // Comute FPS. float fps = num_img / (static_cast(clock() - start_clock) / CLOCKS_PER_SEC);

const Dtype* detections_data = detections->cpu_data(); const int width = images[0].cols; const int height = images[0].rows; vector all_detections(num_img); for (int i = 0; i < num_det; ++i) { const int img_idx = detections_data[i * 7]; CHECK_LT(img_idx, num_img); const int label = detections_data[i * 7 + 1]; const float score = detections_data[i * 7 + 2]; if (score < threshold) { continue; } NormalizedBBox bbox; bbox.set_xmin(detections_data[i * 7 + 3] * width); bbox.set_ymin(detections_data[i * 7 + 4] * height); bbox.set_xmax(detections_data[i * 7 + 5] * width); bbox.set_ymax(detections_data[i * 7 + 6] * height); bbox.set_score(score); all_detections[img_idx][label].push_back(bbox); }

int fontface = cv::FONT_HERSHEY_SIMPLEX; double scale = 0.5; int thickness = 1; int baseline = 0; char buffer[50];

/* // file for recoding x,y of object FILE *fp; if( (fp = fopen("XnY_Point.txt", "w")) == NULL) { printf("error\n"); return 0; } */

ofstream outFile("object_infor.txt");

// ************

for (int i = 0; i < num_img; ++i) { // ----- ajb outFile << "image num : " << i << "-----" << std::endl; 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, 4); // Draw bboxes. for (map<int, vector >::iterator it = all_detections[i].begin(); it != all_detections[i].end(); ++it) { int label = it->first; string label_name = "Unknown"; if (label_to_display_name.find(label) != label_to_display_name.end()) { label_name = label_to_display_name.find(label)->second; } CHECK_LT(label, colors.size()); const cv::Scalar& color = colors[label]; const vector& bboxes = it->second; for (int j = 0; j < bboxes.size(); ++j) {
// write infor to txt file // outFile << "detected object num : " << label << " // "; // label = label index outFile << "detected object class : " << label_name << " // "; outFile << "X max : " << bboxes[j].xmax() << ", "; outFile << "X min : " << bboxes[j].xmin() << ", "; outFile << "Y max : " << bboxes[j].ymax() << ", "; outFile << "Y min : " << bboxes[j].ymin(); outFile << std::endl;

    cv::Point top_left_pt(bboxes[j].xmin(), bboxes[j].ymin());
    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());
    snprintf(buffer, sizeof(buffer), "%s : %0.2f", label_name.c_str(),
             bboxes[j].score());
    cv::Size text = cv::getTextSize(buffer, fontface, scale, thickness,
                                    &baseline);
    cv::rectangle(
        image, bottom_left_pt + cv::Point(0, 0),
        bottom_left_pt + cv::Point(text.width, -text.height-baseline),
        color, CV_FILLED);
    cv::putText(image, buffer, bottom_left_pt - cv::Point(0, baseline),
                fontface, scale, CV_RGB(0, 0, 0), thickness, 4);
  }
}

// -----ajb
outFile << "-----" << std::endl;

// Save result if required.
if (!save_file.empty()) {
  if (!cap_out.isOpened()) {
    cv::Size size(image.size().width, image.size().height);
    cv::VideoWriter outputVideo(save_file, CV_FOURCC('D', 'I', 'V', 'X'),
        30, size, true);
    cap_out = outputVideo;
  }
  cap_out.write(image);
}

/* ------------ */
/* Added for saving video */
if(!cap_out.isOpened()) {
    cv::Size S(image.size().width,image.size().height);
    cv::VideoWriter outputVideo("output.avi", CV_FOURCC('D','I','V','X'), 24, S, true);
    cap_out = outputVideo;
}
else {
    cap_out.write(image);
}
/* ----------- */

cv::imshow("detections", image);
if (cv::waitKey(1) == 27) {
  raise(SIGINT);
}

}

outFile.close(); start_clock = clock(); }

But, i even extracted num = 0 (i = 0) image information. And another images infors were not extracted. So, Could you let me know how to extract all infor of images? And What file and function use VisualizeBBox func? I did my best to find that things but... I couldn't.. Please tell me about that... Thank You!

mintlatte avatar Mar 02 '17 09:03 mintlatte

The DetectionOutputLayer.c* produces detection output and called that function.

weiliu89 avatar Mar 03 '17 17:03 weiliu89

Thank you so much! :D

mintlatte avatar Mar 06 '17 04:03 mintlatte