caffe 显示AnnotatedDatum代码

 static bool show_rt(cv::Mat &show, cv::Rect &rt, int label)
    {

        static std::string label_map[] =
                {
                        "bg",
                        "Pedestrian",
                        "Cyclist",
                        "car"
                };
//        cv::Mat show = img.clone();
        std::map<int,cv::Scalar> lable_colors = { {-1, cv::Scalar(0,255,255)}, {1, cv::Scalar(0,0,255)}, {2, cv::Scalar(0,255,0)}, {3, cv::Scalar(255,0,0)}};

        int thickness = int(0.002 * (show.rows + show.cols) / 2) + 1;
        cv::rectangle(show,rt,lable_colors[label],thickness,cv::LINE_AA);
        int tf = std::max(thickness-1,1);
        int baseline = 0;
        cv::Size textSize = cv::getTextSize(label_map[label],0,thickness/3.,tf,&baseline);
        cv::Point pt_tl = rt.tl();
        cv::Point pt_2 = cv::Point(int(pt_tl.x+textSize.width),int(pt_tl.y-textSize.height-3));
        if (-1 != label) cv::rectangle(show,pt_tl,pt_2,lable_colors[label],-1,cv::LINE_AA);//  filled
        if (-1 != label) {cv::putText(show,label_map[label],cv::Point(pt_tl.x,pt_tl.y-2),0,thickness/3.,cv::Scalar(255,255,255),tf,cv::LINE_AA);}
        return true;
    }


    void show_sample_box(const AnnotatedDatum & annot, const vector<NormalizedBBox> &sampled_bboxes)
    {
        const int img_height = annot.datum().height();
        const int img_width = annot.datum().width();

        const string& data = annot.datum().data();
        std::vector<char> vec_data(data.c_str(), data.c_str() + data.size());
        cv::Mat cv_img = cv::imdecode(vec_data, -1);

        for(int i=0; i<sampled_bboxes.size(); i++)
        {
            const NormalizedBBox &roi_crop = sampled_bboxes[i];
            int xmin = roi_crop.xmin() * img_width + 0.5f;
            int xmax = roi_crop.xmax() * img_width + 0.5f;
            int ymin = roi_crop.ymin() * img_height + 0.5f;
            int ymax = roi_crop.ymax() * img_height + 0.5f;

            cv::Rect rt = cv::Rect(cv::Point(xmin, ymin), cv::Point(xmax, ymax));
            show_rt(cv_img, rt, -1);


            cv::namedWindow("roi-bbox", 0);
            cv::imshow("roi-bbox",cv_img);
            cv::waitKey(0);
        }

    }



void show_AnnotatedDatum(const AnnotatedDatum & annot, string name = "img")
{
    const int img_height = annot.datum().height();
    const int img_width = annot.datum().width();

    const string& data = annot.datum().data();
    std::vector<char> vec_data(data.c_str(), data.c_str() + data.size());
    cv::Mat cv_img = cv::imdecode(vec_data, -1);
    if (!cv_img.data) {
        LOG(ERROR) << "Could not decode datum ";
    }

//	cv::namedWindow("cv_img", 0);
//	cv::imshow("cv_img",cv_img);
//	cv::waitKey(0);



    if (annot.type() == AnnotatedDatum_AnnotationType_BBOX) {
        // Go through each AnnotationGroup.
        for (int g = 0; g < annot.annotation_group_size(); ++g) {
            const AnnotationGroup &anno_group = annot.annotation_group(g);
            const int group_label = anno_group.group_label();

            // Go through each Annotation.
            for (int a = 0; a < anno_group.annotation_size(); ++a) {
                const Annotation &anno = anno_group.annotation(a);
                const NormalizedBBox &bbox = anno.bbox();
                int xmin = bbox.xmin() * img_width + 0.5f;
                int xmax = bbox.xmax() * img_width + 0.5f;
                int ymin = bbox.ymin() * img_height + 0.5f;
                int ymax = bbox.ymax() * img_height + 0.5f;

                xmin = std::max(0, std::min(xmin, img_width - 1));
                xmax = std::max(0, std::min(xmax, img_width - 1));
                ymin = std::max(0, std::min(ymin, img_height - 1));
                ymax = std::max(0, std::min(ymax, img_height - 1));
                if (xmax <= xmin || ymax <= ymin)
                    continue;

                cv::Rect rt = cv::Rect(cv::Point(xmin, ymin), cv::Point(xmax, ymax));
                show_rt(cv_img, rt, group_label);
            }
        }
    }
    //cv::resize(cv_img, cv_img, cv::Size(1000,500));
    cv::namedWindow(name, 0);
    cv::imshow(name,cv_img);
    cv::waitKey(0);

}
posted @ 2023-03-24 14:32  无左无右  阅读(13)  评论(0编辑  收藏  举报