caffe textboxes++测试(c++版本)-mainlib.cpp

这里是测试主函数mainlib.cpp

#include <stdio.h>  // for snprintf
#include <string>
#include <vector>
#include <unistd.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <strstream>
#include <signal.h>
#include <iconv.h>
#include "boost/algorithm/string.hpp"
#include "google/protobuf/text_format.h"
#include "leveldb/db.h"
#include "leveldb/write_batch.h"

#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/net.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/io.hpp"

#include "caffe/layers/memory_data_layer.hpp"
#include "caffe/caffe.hpp"

#include "opencv2/opencv.hpp"

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include <boost/thread.hpp>
#include <iosfwd>
#include <utility>
#include "caffe/Cut2Image.h"
#include "caffe/ocr.h"
#include "caffe/boxDetect.h"
#include "caffe/cutImage.h"
#include "caffe/ctc_ocr.h"
#include "classification.hpp"

#include "caffe/nms.h"

#include  <dirent.h>
#include  <sys/types.h>

#define K 4//4

using namespace caffe;
using namespace std;

extern "C" {
    Classifier *classifier[K];
    Classifier *classifier_isCWB;
    Detector *detector_textbox;
    ICNNPredict* pCNN;

    void getBoxImg(IplImage* img,vector<IplImage*> &box_imgs, int num,const char* imgfolder)
    {
        if(img==NULL)
        {
            cout<< "Unable to decode image "<<endl;
            return;
        }
        vector<vector<float> > detections = detector_textbox->Detect(cv::Mat(img));
        // vector<vector<float> > detections = detector_textbox->Multi_Scale_Detect(cv::Mat(img));  
        vector<proposal_type> proposals;
        // vector<Rect> proposals;//nms
        //std::vector<float> scores;

        /* Print the detection results. */
        for (int i = 0; i < detections.size(); ++i)
        {
            const vector<float>& d = detections[i];
            // Detection format: [det_conf, det_x1, det_y2, det_x2, det_y2, det_x3, det_y3, det_x4, det_y4].
            CHECK_EQ(d.size(), 9);
            const float score = d[0];
            float confidence_threshold=0.6;//0.01;
            //cout<<"score="<<score<<endl;
            if (score >= confidence_threshold)
            {
                int det_x1=static_cast<int>(d[1] * cv::Mat(img).cols);
                int det_y1=static_cast<int>(d[2] * cv::Mat(img).rows);
                int det_x2=static_cast<int>(d[3] * cv::Mat(img).cols);
                int det_y2=static_cast<int>(d[4] * cv::Mat(img).rows);
                int det_x3=static_cast<int>(d[5] * cv::Mat(img).cols);
                int det_y3=static_cast<int>(d[6] * cv::Mat(img).rows);
                int det_x4=static_cast<int>(d[7] * cv::Mat(img).cols);
                int det_y4=static_cast<int>(d[8] * cv::Mat(img).rows);
                det_x1 = max(1, min(det_x1, img->width-1));
                det_x2 = max(1, min(det_x2, img->width-1));
                det_x3 = max(1, min(det_x3, img->width-1));
                det_x4 = max(1, min(det_x4, img->width-1));
                det_y1 = max(1, min(det_y1, img->height-1));
                det_y2 = max(1, min(det_y2, img->height-1));
                det_y3 = max(1, min(det_y3, img->height-1));
                det_y4 = max(1, min(det_y4, img->height-1));
               
                 
                proposal_type pro;
                pro.x1=det_x1;
                pro.x2=det_x2;
                pro.x3=det_x3;
                pro.x4=det_x4;
                pro.y1=det_y1;
                pro.y2=det_y2;
                pro.y3=det_y3;
                pro.y4=det_y4;
                pro.score=score;
                proposals.push_back(pro);
            }
        }
        //cout<<"detection"<<endl;
        //nms(proposals, scores, 0.3);
        nms(proposals,0.2);

        cv::Mat image4(img,false);

        for(int i=0; i<proposals.size(); i++)
        {
            line(image4, cvPoint(proposals[i].x1, proposals[i].y1), cvPoint(proposals[i].x2, proposals[i].y2), Scalar(255, 0, 0),3, 1, 0);
            line(image4, cvPoint(proposals[i].x2, proposals[i].y2), cvPoint(proposals[i].x3, proposals[i].y3), Scalar(255, 0, 0),3, 1, 0);
            line(image4, cvPoint(proposals[i].x3, proposals[i].y3), cvPoint(proposals[i].x4, proposals[i].y4), Scalar(255, 0, 0),3, 1, 0);
            line(image4, cvPoint(proposals[i].x1, proposals[i].y1), cvPoint(proposals[i].x4, proposals[i].y4), Scalar(255, 0, 0),3, 1, 0);
            // cvRectangle(img,cvPoint(xmin, ymin),cvPoint(xmin+width, ymin+height),Scalar(255, 0, 0),3, 1, 0);
        }
        //cout<<"rect"<<endl;
        char img_name[2000];

        sprintf(img_name, "/data2/myfile/TextBoxes_plusplus/test_images/result_allimages/%s",imgfolder);
        cvSaveImage(img_name, img);
        printf("img_name:%s\n",img_name);
    }

// If image is CWB, cutting rows as text lines.
// Img: source image.
// BoxImg: vector to store cutted images.
    void CWB2BoxImg(IplImage* Img, vector<IplImage*>& BoxImg)
    {
        if(Img == NULL)
        {
            cout << "Image is NULL" << endl;
            return;
        }
        Cut2ImagesByBlank(Img, BoxImg);
        for(int i=0; i<BoxImg.size(); i++)
        {
            IplImage* tmp=BoxImg[i];
            char box_img_name[100];
            sprintf(box_img_name, "/root/ydb/ocr/box/%d.jpg", i);
            cvSaveImage(box_img_name, tmp);
            tmp=NULL;
        }
        char img_name[100];
        sprintf(img_name, "/root/workplace/iieproject_v1/media/ocr.jpg");
        cvSaveImage(img_name, Img);

    }

    void modelInit()
    {

        string model_file_box="/data2/myfile/TextBoxes_plusplus/models/deploy.prototxt";
        string trained_file_box="/data2/myfile/TextBoxes_plusplus/models/VGGNet/all/text_polygon_precise_fix_order_384x384/VGG_text_text_polygon_precise_fix_order_384x384_iter_35000.caffemodel";
        
        string mean_file_box="";
        string mean_value_box="104,117,123";

        Caffe::set_mode(Caffe::GPU);
        Caffe::SetDevice(1);

        detector_textbox = new Detector(model_file_box, trained_file_box, mean_file_box, mean_value_box);
    }

    void mainGetBoxes(char* rootname,char* imgfolder)
    {
        //用来存储图片路径
        vector<string> ImgNames;
        char str3[1000];
        //strcpy(str3, str1);
        //strcat(str3, str2);
        //得到图片路径
        sprintf(str3,"%s%s",rootname,imgfolder);
        cout<<str3<<endl;
        ImgNames.push_back(str3);
        Caffe::set_mode(Caffe::GPU);
        Caffe::SetDevice(1);
        int joasize = ImgNames.size();
        cout<<"joasize:"<<joasize<<endl;
        for(int i=0; i<joasize; i++)
        {
            string sImgPath = ImgNames[i];
            printf("img path of ocr is:%s %d\n",sImgPath.c_str(),i);
            //载入图片
            IplImage *Img_ipl=cvLoadImage(sImgPath.c_str());
            if(Img_ipl==NULL)
            {
                cout<<"can not open img!"<<endl;
                continue;
                //answer.append(string("]end"));
                //return answer;
            }
           
            vector<IplImage*> box_imgs;
            getBoxImg(Img_ipl,box_imgs, i,imgfolder);

        }
    }

}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,366评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,521评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,689评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,925评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,942评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,727评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,447评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,349评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,820评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,990评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,127评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,812评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,471评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,017评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,142评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,388评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,066评论 2 355

推荐阅读更多精彩内容