Skip to content

Commit

Permalink
add opencv resize
Browse files Browse the repository at this point in the history
  • Loading branch information
antinucleon committed May 2, 2015
1 parent a1fb4ce commit e42c5f5
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions tools/im2rec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,49 @@
* Image Record Format: zeropad[64bit] imid[64bit] img-binary-content
* The 64bit zero pad was reserved for future purposes
*
* Image List Format: unique-image-index label path-to-image
* Image List Format: unique-image-index label[s] path-to-image
* \sa dmlc/recordio.h
*/
#include <cctype>
#include <cstring>
#include <vector>
#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 "../src/io/image_recordio.h"

int main(int argc, char *argv[]) {
if (argc < 4) {
fprintf(stderr, "Usage: <image.lst> <image_root_dir> <output_file> [label_width=1]\n");
fprintf(stderr, "Usage: <image.lst> <image_root_dir> <output_file> <new_img_size/-1(do nothing)]> [label_width=1]\n");
return 0;
}
int label_width = 1;
if (argc > 4) label_width = atoi(argv[4]);
int new_size = atoi(argv[4]);
if (new_size > 0) {
LOG(INFO) << "New Image Size: " << new_size << "x" << new_size;
} else {
LOG(INFO) << "Keep origin image size";
}
if (argc > 5) label_width = atoi(argv[5]);
using namespace dmlc;
const static size_t kBufferSize = 1 << 20UL;
std::string root = argv[2];
cxxnet::ImageRecordIO rec;
size_t imcnt = 0;
double tstart = dmlc::GetTime();
double tstart = dmlc::GetTime();
dmlc::Stream *flist = dmlc::Stream::Create(argv[1], "r");
dmlc::istream is(flist);
dmlc::Stream *fo = dmlc::Stream::Create(argv[3], "w");
dmlc::RecordIOWriter writer(fo);
std::string fname, path, blob;
std::vector<unsigned char> decode_buf;
std::vector<unsigned char> encode_buf;
std::vector<int> encode_params;
encode_params.push_back(CV_IMWRITE_JPEG_QUALITY);
encode_params.push_back(100);
while (is >> rec.header.image_id[0] >> rec.header.label) {
for (int k = 1; k < label_width; ++ k) {
float tmp;
Expand All @@ -44,21 +58,33 @@ int main(int argc, char *argv[]) {
const char *p = fname.c_str();
while (isspace(*p)) ++p;
path = root + p;
dmlc::Stream *fi = dmlc::Stream::Create(path.c_str(), "r");
dmlc::Stream *fi = dmlc::Stream::Create(path.c_str(), "rb");
rec.SaveHeader(&blob);
size_t size = blob.length();
while (true) {
blob.resize(size + kBufferSize);
size_t nread = fi->Read(BeginPtr(blob) + size, kBufferSize);
size += nread;
decode_buf.clear();
decode_buf.resize(kBufferSize);
size_t nread = fi->Read(BeginPtr(decode_buf), kBufferSize);
decode_buf.resize(nread);
cv::Mat img = cv::imdecode(decode_buf, CV_LOAD_IMAGE_COLOR);
cv::Mat res;
if (new_size > 0) {
cv::resize(img, res, cv::Size(new_size, new_size), 0, 0, CV_INTER_CUBIC);
} else {
res = img;
}
encode_buf.clear();
CHECK(cv::imencode(".jpg", res, encode_buf, encode_params));
blob.resize(size + encode_buf.size());
memcpy(BeginPtr(blob) + size, BeginPtr(encode_buf), encode_buf.size());
if (nread != kBufferSize) break;
}
delete fi;
writer.WriteRecord(BeginPtr(blob), size);
writer.WriteRecord(BeginPtr(blob), blob.size());
// write header
++imcnt;
if (imcnt % 1000 == 0) {
LOG(INFO) << imcnt << " images processed, " << GetTime() - tstart << " sec elapsed";
LOG(INFO) << imcnt << " images processed, " << GetTime() - tstart << " sec elapsed";
}
}
delete fo;
Expand Down

0 comments on commit e42c5f5

Please sign in to comment.