#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;
}
premature filter
最新推荐文章于 2024-10-17 16:00:45 发布