百度人脸识别离线C++SDK与GRPC结合

准备文件

  • GRPC编译参考参考:查看
  • 下载最新的Opencv:下载 并解压
  • 登录百度云平台,下载百度人脸识别SDK C++ 并解压,然后绑定序列号
  • 安装好VS2022,必须是这个版本,并且安装组件“用于Windows的C++CMake工具”“用于Linux的C++CMake工具”,“C++桌面开发”“VS扩展开发”
  • 文件目录如下
-- FaceOfflineSdk
-- opencv
-- grpc
-- pb_proto_file
-- src
  • 把以下代码保存到pb_proto_file目录下,文件名:baidu_face_proto_c.proto
\\ 这里主要是百度人脸识别的功能
syntax = "proto3";
import "google\protobuf\empty.proto";

\\option go_package = "..\pb_service";

package pb_service;

message FaceIsLiveness{
    float ScoreRGB=1;
}
\\
message FaceBase64Img{
  string Base64Img = 1; \\传入的图片
}
message FaceEmployeeId{
  string EmployeeId = 1;
}
\\
message FaceUserInfo{
  string EmployeeId=1;
  string UserId = 2;
  string GroupId = 3;
  string UserInfo = 4; \\ 用户信息可填中文,表示用户信息
  string Base64Img = 5;
  string Feature = 6;
}
message FaceUserAddReturn{
  string Res=1;
}
message FaceUserDelReturn{
  string Res=1;
}

service FaceService{
  \\ GetFaceIsLiveness 获取照片活体的阈值
  rpc GetFaceIsLiveness(FaceBase64Img)returns(FaceIsLiveness){}
  \\ GetFaceUser 获取识别到的人
  rpc GetFaceUser(FaceBase64Img)returns(FaceEmployeeId){}
  \\ FaceManagerUserAdd 人脸注册示例
  rpc  FaceManagerUserAdd(FaceUserInfo)returns(FaceUserAddReturn){};
  \\ FaceManagerUserUpdate 人脸更新示例
  rpc FaceManagerUserUpdate(FaceUserInfo)returns(FaceUserAddReturn){};
  \\ FaceManagerUserDelete 用户删除示例
  rpc FaceManagerUserDelete(FaceEmployeeId)returns(FaceUserDelReturn){};
  \\ FaceManagerGetUserImage 获取用户图片示例
  rpc FaceManagerGetUserImage(FaceEmployeeId)returns(FaceUserInfo){};
}

Windows编译 GRPC

git clone  https:\\github.com\grpc\grpc
cd grpc
git submodule update --init
mkdir "cmake\build"
cd "cmake\build"
cmake -DgRPC_INSTALL=ON -DgRPC_BUILD_TESTS=OFF -DCMAKE_CXX_STANDARD=17 -DCMAKE_INSTALL_PREFIX=.\MY_INSTALL_DIR ..\..
cmake --build . --config Release --target install -j 4

将GRPC和百度SDK合并

 cd grpc
 mkdir "..\src\BaiduFaceProtoC\grpc_install"
 mkdir "..\src\BaiduFaceProtoC\baidu_face\include"
 # 将编译好的GRPC复制到项目中
 xcopy cmake\build\MY_INSTALL_DIR\ ..\src\BaiduFaceProtoC\grpc_install\ /e /y
 cd ..\src\BaiduFaceProtoC
 md cmake\build
 md cmake\bin
 md cmake\bin\Release
 # 导入百度人脸识别SDK 
 xcopy ..\..\FaceOfflineSdk\x64\ baidu_face /e /y
 xcopy ..\..\FaceOfflineSdk\x64\ cmake\bin\Release\ /e /y
 xcopy ..\..\FaceOfflineSdk\FaceOfflineSdk\include\ baidu_face\include\  /e /y
 xcopy ..\..\FaceOfflineSdk\FaceOfflineSdk\lib\x64\ baidu_face\ /e /y
 xcopy ..\..\FaceOfflineSdk\FaceOfflineSdk\lib\x64\ cmake\bin\Release\
 xcopy ..\..\FaceOfflineSdk\FaceOfflineSdk\third_party\opencv-win\include\opencv\ baidu_face\include\opencv\ /e /y
 xcopy ..\..\FaceOfflineSdk\FaceOfflineSdk\face_scene\include\ baidu_face\include\ /e /y
 xcopy ..\..\FaceOfflineSdk\images\ cmake\bin\images\ /e /y
 xcopy ..\..\FaceOfflineSdk\license\ cmake\bin\license\ /e /y
 xcopy ..\..\FaceOfflineSdk\models\ cmake\bin\models\ /e /y
 xcopy ..\..\FaceOfflineSdk\conf\ cmake\bin\conf\ /e /y
 # 导入Opencv
 xcopy ..\..\opencv\build\include\opencv2\ baidu_face\include\opencv2\  /e /y
 xcopy ..\..\opencv\build\bin\ baidu_face\  /e /y
 xcopy ..\..\opencv\build\x64\vc16\bin\ baidu_face\ /e /y
 xcopy ..\..\opencv\build\x64\vc16\lib\ baidu_face\ /e /y
 xcopy ..\..\opencv\build\bin\ cmake\bin\Release\  /e /y
 xcopy ..\..\opencv\build\x64\vc16\bin\ cmake\bin\Release\ /e /y
 xcopy ..\..\opencv\build\x64\vc16\lib\ cmake\bin\Release\ /e /y
  • 添加CMakeLists.txt
vim src\BaiduFaceProtoC\CMakeLists.txt
cmake_minimum_required(VERSION 3.16)

project(BaiduFaceProtoC C CXX)

include("../../grpc/examples/cpp/cmake/common.cmake")

# Proto file
get_filename_component(hw_proto "../../pb_proto_file/baidu_face_proto_c.proto" ABSOLUTE)
get_filename_component(hw_proto_path "${hw_proto}" PATH)

# 设置可执行文件的输出目录
set(EXECUTABLE_OUTPUT_PATH "${CMAKE_CURRENT_BINARY_DIR}/../bin")

# 设置库文件的输出目录
set(LIBRARY_OUTPUT_PATH "${CMAKE_CURRENT_BINARY_DIR}/../bin")

# Generated sources
set(hw_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/baidu_face_proto_c.pb.cc")
set(hw_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/baidu_face_proto_c.pb.h")
set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/baidu_face_proto_c.grpc.pb.cc")
set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/baidu_face_proto_c.grpc.pb.h")
add_custom_command(
      OUTPUT "${hw_proto_srcs}" "${hw_proto_hdrs}" "${hw_grpc_srcs}" "${hw_grpc_hdrs}"
      COMMAND ${_PROTOBUF_PROTOC}
      ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}"
        --cpp_out "${CMAKE_CURRENT_BINARY_DIR}"
        -I "${hw_proto_path}"
        --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
        "${hw_proto}"
      DEPENDS "${hw_proto}")

# Include generated *.pb.h files
include_directories(
  "${CMAKE_CURRENT_BINARY_DIR}"
  "${CMAKE_CURRENT_BINARY_DIR}/../../baidu_face/include"
)

# hw_grpc_proto
add_library(hw_grpc_proto
  ${hw_grpc_srcs}
  ${hw_grpc_hdrs}
  ${hw_proto_srcs}
  ${hw_proto_hdrs})
target_link_libraries(hw_grpc_proto
  absl::check
  ${_REFLECTION}
  ${_GRPC_GRPCPP}
  ${_PROTOBUF_LIBPROTOBUF})

set(BaiduFaceApi_DIR "${CMAKE_CURRENT_BINARY_DIR}/../../baidu_face")
find_library(BAIDUFACEAPI_LIBRARY NAMES BaiduFaceApi PATHS ${BaiduFaceApi_DIR})
target_link_libraries(hw_grpc_proto ${BAIDUFACEAPI_LIBRARY})

# Targets greeter_[async_](client|server)
foreach(_target greeter_server)
  add_executable(${_target} "${_target}.cc")
  target_link_libraries(${_target}
    hw_grpc_proto
    absl::check
    absl::flags
    absl::flags_parse
    absl::log
    ${_REFLECTION}
    ${_GRPC_GRPCPP}
    ${_PROTOBUF_LIBPROTOBUF})
endforeach()
  • 添加服务端代码 greeter_server.cc
vim src\BaiduFaceProtoC\greeter_server.cc
#include <grpcpp/ext/proto_server_reflection_plugin.h>
#include <grpcpp/grpcpp.h>
#include <grpcpp/health_check_service_interface.h>

#include <iostream>
#include <memory>
#include <string>

#include "absl/flags/flag.h"
#include "absl/flags/parse.h"
#include "absl/strings/str_format.h"

#include "cmake/build/baidu_face_proto_c.grpc.pb.h"

#include "opencv2/opencv.hpp"
#include "baidu_face_api.h"

using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using pb_service::FaceService;
using pb_service::FaceIsLiveness;
using pb_service::FaceBase64Img;
using pb_service::FaceEmployeeId;
using pb_service::FaceUserInfo;
using pb_service::FaceUserAddReturn;
using pb_service::FaceUserDelReturn;

ABSL_FLAG(uint16_t, port, 50051, "Server port for the service");

// Logic and data behind the server's behavior.
class FaceServiceServiceImpl final : public FaceService::Service {

    // GetFaceIsLiveness 获取照片活体的阈值
    Status GetFaceIsLiveness(::grpc::ServerContext* context, const ::pb_service::FaceBase64Img* request, ::pb_service::FaceIsLiveness* response)override {
        return Status::OK;
    }
    // GetFaceUser 获取识别到的人
    Status GetFaceUser(::grpc::ServerContext* context, const ::pb_service::FaceBase64Img* request, ::pb_service::FaceEmployeeId* response)override {
        //response->set_employeeid(3);
        return Status::OK;
    }
    // FaceManagerUserAdd 人脸注册示例
    Status FaceManagerUserAdd(::grpc::ServerContext* context, const ::pb_service::FaceUserInfo* request, ::pb_service::FaceUserAddReturn* response)override {
        //response->set_res("true");
        return Status::OK;
    }
    // FaceManagerUserUpdate 人脸更新示例
    Status FaceManagerUserUpdate(::grpc::ServerContext* context, const FaceUserInfo* request, FaceUserAddReturn* response)override {
        //response->set_res("true");
        return Status::OK;
    }
    // FaceManagerUserDelete 用户删除示例
    Status FaceManagerUserDelete(::grpc::ServerContext* context, const FaceEmployeeId* request, FaceUserDelReturn* response)override {
        //response->set_res("true");
        return Status::OK;
    }
    // FaceManagerGetUserImage 获取用户图片示例
    Status FaceManagerGetUserImage(::grpc::ServerContext* context, const FaceEmployeeId* request, FaceUserInfo* response)override {
        //response->set_userinfo("true");
        return Status::OK;
    }
};

void RunServer(uint16_t port) {
  std::string server_address = absl::StrFormat("0.0.0.0:%d", port);
  FaceServiceServiceImpl service;

  grpc::EnableDefaultHealthCheckService(true);
  grpc::reflection::InitProtoReflectionServerBuilderPlugin();
  ServerBuilder builder;
  // Listen on the given address without any authentication mechanism.
  builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  // Register "service" as the instance through which we'll communicate with
  // clients. In this case it corresponds to an *synchronous* service.
  builder.RegisterService(&service);
  // Finally assemble the server.
  std::unique_ptr<Server> server(builder.BuildAndStart());
  std::cout << "Server listening on " << server_address << std::endl;

  // Wait for the server to shutdown. Note that some other thread must be
  // responsible for shutting down the server for this call to ever return.
  server->Wait();
}
BaiduFaceApi* baiduFaceapi;
int main(int argc, char** argv) {
 // api实例指针,可作为您项目中的全局变量,项目初始化实例化该指针,项目结束再释放
 baiduFaceapi = new BaiduFaceApi();
 std::string device_id;
  //// 获取设备指纹
  baiduFaceapi->get_device_id(device_id);
  std::cout << "device id is:" << device_id << std::endl;
  //// 获取sdk版本号
  std::string version;
  baiduFaceapi->sdk_version(version);
  std::cout << "sdk version:" << version << std::endl;
  //// 初始化sdk
  int res = baiduFaceapi->sdk_init(nullptr);
  std::cout << "sdk init res:" << res << std::endl;
  if (res != 0) {
    std::cout << "初始化百度人脸识别失败:sdk init res:" << res << std::endl;
    return 0;
  }
  std::cout << "初始化百度人脸识别成功" << std::endl;

  absl::ParseCommandLine(argc, argv);
  RunServer(absl::GetFlag(FLAGS_port));
  return 0;
}

编译

cd cmake/bin/Release
cmake -DCMAKE_INSTALL_PREFIX="../../../grpc_install;../../../baidu_face"  -DCMAKE_CXX_STANDARD=17 -S ../../../ -B ../../build
# 注意!!重要!! 到这一步,在build目录下生成的所有cpp文件如`*.cpp`,`*.h`都需要设置为`utf-8 bom`,默认没有bom,需要添加,然后重新执行就好。不然下面的编译命令会失败!
cmake --build ../../build --config Release -j 4
./greeter_server.exe
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天道酬勤~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值