protobuf、prototxt的使用/Windows/VS

<a target="_blank" href="https://www.ancii.com/link/v1/y3ed3IFjm07WptvGLstKidxjQIicn80X0b8aUZq1c2c/" rel="nofollow" title="博客已转移至个人网站">博客已转移至个人网站</a>(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提取的头文件置于工程根目录下,并设置包含

 <img src="https://blog.csdn.net/pc1377318286/article/details/50752856" title="protobuf、prototxt的使用/Windows/VS" alt="protobuf、prototxt的使用/Windows/VS"><img src="https://img-blog.csdn.net/20160226224353190?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" title="protobuf、prototxt的使用/Windows/VS" alt="protobuf、prototxt的使用/Windows/VS">

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文档。

相关推荐