protobuf、prototxt的使用/Windows/VS

博客已转移至个人网站(http://www.p-chao.com)

分为:

一、配置protobuf

二、使用prototxt

下载源码,编译出protoc.exe文件和libprotobuf.lib libprotoc.lib库文件

    注意版本一致,debug出来的给debug用...

使用方法:

protoc.exe 用来根据 proto文件 生成对应的 头文件 和 信息源文件

    用法:    protoc --cpp_out=./ person.proto

                    注意命令行中的空格

libproto ..lib在自己的程序中,配合生成的头和源文件

#pragma(lib,...)  ...

然后还要注意,需要将源中extract_includes.bat提取的头文件置于工程根目录下,并设置包含

protobuf具体的生成方法见博客  http://www.2cto.com/kf/201401/270281.html  

不谢!

关于配置文件,prototxt(caffe中的格式,事实上可以是任何文本文件,二进制格式或者文本格式都可以)

读写prototxt的配置文件需要以下头,用到了google的glogs,自行下载编译,linux下直接使用apt-get 就可以

下面是windows的头(linux稍微修改下include),不多废话,上代码

#include <fcntl.h>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/text_format.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/highgui/highgui_c.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <stdint.h>
#include <stdio.h>

#include <algorithm>
#include <fstream>  // NOLINT(readability/streams)
#include <string>
#include <vector>

#include <fcntl.h>
#include <Windows.h>

#ifdef USE_LEVELDB
#include <leveldb/db.h>
#endif

//#include "caffe/common.hpp"
#include "glog\logging.h"
#include "acf.pb.h"
#include "io.hpp"


using namespace std;

// port for Win32
#ifdef _MSC_VER
#define open _open
#define close _close
#endif

namespace acf {

using google::protobuf::io::FileInputStream;
using google::protobuf::io::FileOutputStream;
using google::protobuf::io::ZeroCopyInputStream;
using google::protobuf::io::CodedInputStream;
using google::protobuf::io::ZeroCopyOutputStream;
using google::protobuf::io::CodedOutputStream;
using google::protobuf::Message;

bool ReadProtoFromTextFile(const char* filename, Message* proto) {
  int fd = open(filename, O_RDONLY);
  CHECK_NE(fd, -1) << "File not found: " << filename;
  FileInputStream* input = new FileInputStream(fd);
  bool success = google::protobuf::TextFormat::Parse(input, proto);
  delete input;
  close(fd);
  return success;
}

void WriteProtoToTextFile(const Message& proto, const char* filename) {
  int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  FileOutputStream* output = new FileOutputStream(fd);
  CHECK(google::protobuf::TextFormat::Print(proto, output));
  delete output;
  close(fd);
}

bool ReadProtoFromBinaryFile(const char* filename, Message* proto) {
  int fd = open(filename, O_RDONLY | O_BINARY);
  CHECK_NE(fd, -1) << "File not found: " << filename;
  ZeroCopyInputStream* raw_input = new FileInputStream(fd);
  CodedInputStream* coded_input = new CodedInputStream(raw_input);
  coded_input->SetTotalBytesLimit(1073741824, 536870912);

  bool success = proto->ParseFromCodedStream(coded_input);

  delete coded_input;
  delete raw_input;
  close(fd);
  return success;
}

void WriteProtoToBinaryFile(const Message& proto, const char* filename) {
  fstream output(filename, ios::out | ios::trunc | ios::binary);
  CHECK(proto.SerializeToOstream(&output));
}

}  // namespace caffe



然后读取prototxt中的配置来初始化参数

Opt opt;
	string filename = "./default.prototxt";
	ReadProtoFromTextFileOrDie(filename, &opt);



这里解释一下,proto文件只是帮助生成一个类、而prototxt文件中存储有配置信息


例如本例中

proto文件


// global configuration of the afc
package acf;

message Opt{
	optional string name = 1;
	optional string posGtDir = 2;
	optional string posImgDir = 3;
	optional string negImgDir = 4;
	optional string posWinDir = 5;
	optional string negWinDir = 6;
	optional int32	nWeak = 7;
	optional int32	modelDs = 8;
	optional int32	modelDsPad = 9;
	optional int32	stride = 10;
}

// it's a size type
message Size{
	optional int32	height = 1;
	optional int32	weidth = 2;
}



prototxt文件


# configure text
#the name of saved detector
name: "models/AcfDetector"
#the director of the database
posGtDir: "database/posGt"
posImgDir: "database/positive"
negImgDir: "database/negative"
posWinDir: "1"
negWinDir: "1"
#the number of weak classifier
nWeak: 32
nWeak: 128
nWeak: 512
nWeak: 2048
#the size of windows
modelDs{
	height: 48
	weidth: 48
}
modelDs{
	height: 50
	weidth: 50
}
#the stride in image
stride: 4



Tips:注释可以使用 // 也可以使用 #


文件也可以选择成二进制,效率会高许多,如果还不明白,参见google的指导和API文档








  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值