premature filter

#include <string>
#include <vector>
#include <sstream>
#include <dmlc/base.h>
#include <dmlc/io.h>
#include <dmlc/timer.h>
#include <dmlc/logging.h>
#include <dmlc/recordio.h>
#include <opencv2/opencv.hpp>
#include <fstream>
#include <iostream>
#include <time.h>
#include <pthread.h>

#define NUM_THREADS 2

struct param {
    int tid;
    std::vector<std::string> img_lst;
    std::vector<std::string> img_dst;
};

void *worker(void *args){
    struct param *p = (param *)args;
    std::vector<std::string> &img_lst = p->img_lst;
    std::vector<std::string> &img_dst = p->img_dst;
    std::vector<std::string>::iterator iter;
    const static size_t kBufferSize = 1 << 20UL;
    std::vector<unsigned char> decode_buf;
    for (iter=img_lst.begin(); iter!=img_lst.end(); ++iter){
        //std::cout<<(*iter).c_str()<<std::endl;
        try {
            dmlc::Stream *fi = dmlc::Stream::Create((*iter).c_str(), "r");
            decode_buf.clear();
            size_t imsize = 0;
            while (true) {
                decode_buf.resize(imsize + kBufferSize);
                size_t nread = fi->Read(dmlc::BeginPtr(decode_buf) + imsize, kBufferSize);
                imsize += nread;
                decode_buf.resize(imsize);
                if (nread != kBufferSize) break;
            }
            cv::Mat img = cv::imdecode(decode_buf, CV_LOAD_IMAGE_COLOR);
            if (img.data) img_dst.push_back(*iter);
            //else std::cout<<"premature image: "<<std::endl;
            delete fi;
        }catch(dmlc::Error) {
            //std::cout<<"invalid image: "<<*iter<<std::endl;
        }
    }
    //write into the file
    //transfer tid to a string
    std::stringstream ss;
    std::string str;
    ss<<p->tid;
    ss>>str;

    std::ofstream ofile;
    ofile.open("tmp_process"+str);
    std::cout<<"tmp_process" + str<<std::endl;
    for (iter=img_dst.begin(); iter!=img_dst.end(); ++iter) {
        ofile<<*iter<<"\n";
    }
    ofile.close();
    pthread_exit(NULL);
    std::cout<<"processing ending"<<std::endl;
    return NULL;
}

int main(int argc, char *argv[]) {
    time_t start, end;
    time(&start);
    //src: the source img.lst
    //des: the destination img.lst
    std::string src = argv[1];
    std::string des = argv[2];
    std::ifstream fin;
    fin.open(src);
    pthread_t threads[NUM_THREADS];

    param **td = new param*[NUM_THREADS];
    for (int i=0; i<NUM_THREADS; ++i) td[i]=new param;

    std::string tmp;
    while (!fin.eof()){
        for (int i=0; i<NUM_THREADS; ++i){
            if(getline(fin, tmp)) {
                (td[i]->img_lst).push_back(tmp);
            }
        }
    }
    for (int i=0; i<NUM_THREADS; ++i)   (td[i]->tid) = i;


    fin.close();

    for (int i=0; i<NUM_THREADS; ++i) {
        std::cout<<"creating threads: "<<i<<std::endl;
        int rc = pthread_create(&threads[i], NULL, worker, (void *)td[i]);

        if (rc){
            std::cout<<"unable to create thread: "<<rc<<std::endl;
            exit(-1);
        }
    }
    pthread_exit(NULL);
    std::cout<<"break point";

    time(&end);
    std::cout<<"running time: "<<difftime(end, start)<<std::endl;

    for (int i = 0; i<NUM_THREADS; ++i) {
        delete td[i];
    }
    delete [] td;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值