Skip to content

Commit dbaec9b

Browse files
authored
Update API
1 parent a735205 commit dbaec9b

12 files changed

Lines changed: 202 additions & 24 deletions

.github/workflows/build.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ name: Build
22

33
on:
44
push:
5-
branches:
6-
- master
75
tags:
86
- "v*"
97

CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.27.0)
22

33
set(CMAKE_CXX_STANDARD 17)
44
set(CMAKE_CXX_STANDARD_REQUIRED ON)
5-
set(PYBIND11_TAG v2.13.6)
5+
set(PYBIND11_TAG v3.0.0)
66

77
if (UNIX)
88
set(PYTHON_EXECUTABLE /usr/bin/python3 CACHE STRING "Path to Python executable")
@@ -67,7 +67,7 @@ else()
6767
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
6868

6969
COMMAND ${PYTHON_EXECUTABLE} -m venv .venv
70-
COMMAND .venv/bin/python -m pip install pybind11-stubgen
70+
COMMAND .venv/bin/python -m pip install pybind11-stubgen numpy
7171
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/*.so ${CMAKE_BINARY_DIR}/.venv/lib/python3.${PYTHON_MINOR_VERSION}/site-packages
7272
COMMAND ${CMAKE_BINARY_DIR}/.venv/bin/pybind11-stubgen -o ${CMAKE_BINARY_DIR} ${PROJECT_NAME}
7373
COMMAND ${CMAKE_COMMAND} -E rm -rf .venv
@@ -79,7 +79,7 @@ else()
7979
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
8080

8181
COMMAND ${PYTHON_EXECUTABLE} -m venv .venv
82-
COMMAND .venv/Scripts/python -m pip install pybind11-stubgen
82+
COMMAND .venv/Scripts/python -m pip install pybind11-stubgen numpy
8383
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${PROJECT_NAME}> ${CMAKE_BINARY_DIR}/.venv/Lib/site-packages
8484
COMMAND ${CMAKE_BINARY_DIR}/.venv/Scripts/pybind11-stubgen -o ${CMAKE_BINARY_DIR} ${PROJECT_NAME}
8585
COMMAND ${CMAKE_COMMAND} -E rm -rf .venv

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Face SDK python-api
2+
`face_sdk` package allows you to use FaceSDK in Python language
3+
4+
## Requirements
5+
Python 3.8 or higher is supported. Setuptools package must be installed before starting the installation.
6+
7+
## Install
8+
Go to the processing_block_python_api directory and run `pip3 install .`
9+
10+
## Platforms
11+
You can use `face_sdk` package on Linux, Windows platforms
12+
13+
## Troubleshooting
14+
15+
| Error | Solution |
16+
| ----- | -------- |
17+
| `Assertion failed (Cannot open shared object file libtensorflow.so.2)` | Make sure the library file libtensorflow.so.2 is in the same directory as the libfacerec.so library you are using |
18+
| `Assertion failed (Cannot open shared object file tensorflow.dll)` | Make sure the library file tensorflow.dll is in the same directory as the facerec.dll library you are using |

include/Context.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <pybind11/stl.h>
55

66
#include <pybind11/pybind11.h>
7+
#include <pybind11/numpy.h>
78

89
#include <pbio/Context.h>
910

@@ -33,6 +34,7 @@ namespace face_sdk_3divi
3334

3435
private:
3536
std::shared_ptr<pbio::Context> implementation;
37+
size_t iterationIndex;
3638

3739
private:
3840
static pbio::Context parsePythonBytes(const py::bytes& data);
@@ -51,7 +53,9 @@ namespace face_sdk_3divi
5153

5254
Context(const py::bytes& data);
5355

54-
Context(uint8_t* data, int32_t width, int32_t height, Context::Format format = Context::Format::FORMAT_BGR, int32_t baseAngle = 0);
56+
Context(py::bytes& data, int32_t width, int32_t height, Context::Format format = Context::Format::FORMAT_BGR, int32_t baseAngle = 0);
57+
58+
Context(py::array_t<uint8_t> data, int32_t width, int32_t height, Context::Format format = Context::Format::FORMAT_BGR, int32_t baseAngle = 0);
5559

5660
Context(const std::string& pathToJsonFile);
5761

@@ -121,7 +125,7 @@ namespace face_sdk_3divi
121125

122126
DynamicTemplateIndex getDynamicTemplateIndex() const;
123127

124-
std::variant<py::handle, py::float_, ContextTemplate, DynamicTemplateIndex> getValue() const;
128+
std::variant<std::string, py::handle, py::float_, ContextTemplate, DynamicTemplateIndex> getValue() const;
125129

126130
std::vector<std::string> getKeys();
127131

@@ -133,6 +137,10 @@ namespace face_sdk_3divi
133137

134138
const Context operator[](const std::string& key) const;
135139

140+
Context& startIterate();
141+
142+
Context next();
143+
136144
void operator =(const py::str& value);
137145

138146
void operator =(const py::int_& value);

include/ContextTemplate.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ namespace py = pybind11;
88

99
namespace face_sdk_3divi
1010
{
11+
class Context;
12+
1113
class ContextTemplate
1214
{
1315
private:
@@ -20,6 +22,8 @@ namespace face_sdk_3divi
2022

2123
ContextTemplate(py::object& binaryReadStream);
2224

25+
ContextTemplate(const Context& config);
26+
2327
int32_t size() const;
2428

2529
void save(py::object& binaryWriteStream) const;

include/FacerecService.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
#include <pbio/FacerecService.h>
77

8+
#include "Context.h"
9+
#include "ProcessingBlock.h"
10+
#include "DynamicTemplateIndex.h"
11+
812
namespace face_sdk_3divi
913
{
1014
class FacerecService
@@ -20,7 +24,29 @@ namespace face_sdk_3divi
2024
public:
2125
static pbio::FacerecService::Ptr& get();
2226

23-
static void createService(const std::string& dllPath, const std::string& confDirectory, const std::string& licenseDirectory);
27+
static FacerecService& createService(const std::string& dllPath, const std::string& confDirectory, const std::string& licenseDirectory);
28+
29+
static Context createContext(const py::dict& ctx);
30+
31+
static Context createContext(const py::bytes& data);
32+
33+
static Context createContext(py::array_t<uint8_t> data, int32_t width, int32_t height, Context::Format format = Context::Format::FORMAT_BGR, int32_t baseAngle = 0);
34+
35+
static Context createContext(const std::string& path);
36+
37+
static ProcessingBlock createProcessingBlock(const Context& config);
38+
39+
static ProcessingBlock createProcessingBlock(const py::dict& dict);
40+
41+
static DynamicTemplateIndex createDynamicTemplateIndex(const std::vector<ContextTemplate>& templates, const std::vector<std::string>& uuids, const Context& config);
42+
43+
static DynamicTemplateIndex createDynamicTemplateIndex(const Context& config);
44+
45+
static ContextTemplate convertTemplate(const Context& config);
46+
47+
static ContextTemplate loadContextTemplate(py::object& binaryReadStream);
48+
49+
static std::string getVersion();
2450

2551
friend struct std::default_delete<FacerecService>;
2652
};

include/ProcessingBlock.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ namespace face_sdk_3divi
1616

1717
ProcessingBlock(const py::dict& dict);
1818

19+
ProcessingBlock(ProcessingBlock&& other) = default;
20+
21+
ProcessingBlock& operator =(ProcessingBlock&& other) = default;
22+
1923
void operator ()(Context& config);
2024

2125
~ProcessingBlock() = default;

pyproject.toml

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
[build-system]
2-
requires = ["scikit-build-core[pyproject]"]
2+
requires = [
3+
"scikit-build-core[pyproject]",
4+
"numpy==1.26.4"
5+
]
36
build-backend = "scikit_build_core.build"
47

58
[project]
69
name = "face_sdk_3divi"
7-
version = "1.0.0"
8-
description = "A minimal example package"
10+
version = "3.27.0"
11+
description = "Package allows you to use FaceSDK in python language"
912
readme = "README.md"
1013
requires-python = ">=3.8"
11-
authors = [{ name = "Your Name", email = "your.email@example.com" }]
14+
authors = [{ name = "", email = "" }]
1215
classifiers = [
1316
"Programming Language :: Python :: 3",
1417
"License :: OSI Approved :: MIT License",
1518
"Operating System :: OS Independent",
1619
]
20+
21+
dependencies = [
22+
"numpy==1.26.4"
23+
]
24+
25+
[tool.scikit-build]
26+
sdist.include = [
27+
"FaceSDK/include/*.h"
28+
]

src/Context.cpp

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,22 @@ namespace face_sdk_3divi
5757

5858
}
5959

60-
Context::Context(uint8_t* data, int32_t width, int32_t height, Context::Format format, int32_t baseAngle) :
61-
implementation(std::make_shared<pbio::Context>(Context::createContextFromFrame(data, width, height, format, baseAngle)))
60+
Context::Context(py::bytes& data, int32_t width, int32_t height, Context::Format format, int32_t baseAngle)
6261
{
62+
std::string_view temp = static_cast<std::string_view>(data);
63+
const uint8_t* dataPtr = reinterpret_cast<const uint8_t*>(temp.data());
64+
65+
implementation = std::make_shared<pbio::Context>
66+
(
67+
Context::createContextFromFrame(const_cast<uint8_t*>(dataPtr), width, height, format, baseAngle)
68+
);
69+
}
70+
71+
Context::Context(py::array_t<uint8_t> data, int32_t width, int32_t height, Context::Format format, int32_t baseAngle)
72+
{
73+
py::buffer_info buffer = data.request();
6374

75+
implementation = std::make_shared<pbio::Context>(Context::createContextFromFrame(static_cast<uint8_t*>(buffer.ptr), width, height, format, baseAngle));
6476
}
6577

6678
Context::Context(const std::string& pathToJsonFile) :
@@ -254,7 +266,7 @@ namespace face_sdk_3divi
254266
}
255267
else
256268
{
257-
throw std::runtime_error("Unknown type: " + py::str(value.get_type()).cast<std::string>());
269+
throw std::runtime_error("Unknown type: " + py::str(py::type::of(value)).cast<std::string>());
258270
}
259271
}
260272

@@ -293,14 +305,14 @@ namespace face_sdk_3divi
293305
return (**this).getDynamicTemplateIndex();
294306
}
295307

296-
std::variant<py::handle, py::float_, ContextTemplate, DynamicTemplateIndex> Context::getValue() const
308+
std::variant<std::string, py::handle, py::float_, ContextTemplate, DynamicTemplateIndex> Context::getValue() const
297309
{
298-
std::function<std::variant<py::handle, py::float_, ContextTemplate, DynamicTemplateIndex>()> getter;
310+
std::function<std::variant<std::string, py::handle, py::float_, ContextTemplate, DynamicTemplateIndex>()> getter;
299311
const pbio::Context& value = **this;
300312

301313
if (value.isString())
302314
{
303-
getter = [&value]() { return py::str(value.getString()); };
315+
getter = [&value]() { return value.getString(); };
304316
}
305317
else if (value.isLong())
306318
{
@@ -357,6 +369,23 @@ namespace face_sdk_3divi
357369
return (**this)[key].getContextPtr();
358370
}
359371

372+
Context& Context::startIterate()
373+
{
374+
iterationIndex = 0;
375+
376+
return *this;
377+
}
378+
379+
Context Context::next()
380+
{
381+
if (iterationIndex >= size())
382+
{
383+
throw py::stop_iteration();
384+
}
385+
386+
return (*this)[iterationIndex++];
387+
}
388+
360389
void Context::operator =(const py::str& value)
361390
{
362391
(**this) = static_cast<std::string>(value);

src/ContextTemplate.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ namespace face_sdk_3divi
4141
implementation = FacerecService::get()->loadContextTemplate(is);
4242
}
4343

44+
ContextTemplate::ContextTemplate(const Context& config) :
45+
implementation(FacerecService::get()->convertTemplate(*config))
46+
{
47+
48+
}
49+
4450
ContextTemplate::ContextTemplate(py::object& binaryReadStream)
4551
{
4652
PythonInputStream inputStream(binaryReadStream);

0 commit comments

Comments
 (0)