9#include <initializer_list>
11#include <tensorflow/lite/interpreter.h>
12#include <tensorflow/lite/interpreter_builder.h>
13#include <tensorflow/lite/model_builder.h>
14#include <tensorflow/lite/kernels/register.h>
16#include <opencv2/opencv.hpp>
18#include "cpp-tools/src/resources.h"
22void loadObjectLabels(
const std::string& file, std::vector<std::string>& objs);
23inline size_t tpusAvailable() {
return edgetpu::EdgeTpuManager::GetSingleton()->EnumerateEdgeTpu().size(); }
43 inline operator bool()
const {
return this->
isValid(); }
49 std::unique_ptr<tflite::FlatBufferModel>
map;
50 std::unique_ptr<tflite::Interpreter>
model;
52 tflite::ops::builtin::BuiltinOpResolver
resolver;
70 inline static const char
78template<
class derived_t =
void>
93 virtual void process(cv::Mat& io_frame)
override;
96 inline float getX1(
size_t idx)
const {
return this->
coords[idx * 4 + 1]; }
97 inline float getY1(
size_t idx)
const {
return this->
coords[idx * 4]; }
98 inline float getX2(
size_t idx)
const {
return this->
coords[idx * 4 + 3]; }
99 inline float getY2(
size_t idx)
const {
return this->
coords[idx * 4 + 2]; }
131 inline static const float
133 inline static const char*
134 default_model =
"lite-model_movenet_singlepose_lightning_tflite_int8_4.tflite";
135 inline static const std::array<
const char*, (size_t)
Output::TOTAL>
138 "left eye",
"right eye",
139 "left ear",
"right ear",
140 "left shoulder",
"right shoulder",
141 "left elbow",
"right elbow",
142 "left wrist",
"right wrist",
143 "left hip",
"right hip",
144 "left knee",
"right knee",
145 "left ankle",
"right ankle"
147 inline static const std::array<std::pair<Output, Output>, 12>
168template<
class derived_t =
void>
180 virtual void process(cv::Mat& io_frame)
override;
184 inline float getX(
size_t idx)
const {
return this->
outputs[idx * 3 + 1]; }
185 inline float getY(
size_t idx)
const {
return this->
outputs[idx * 3]; }
189 std::array<cv::Point2f, ~Output::TOTAL>
points;
206template<
class derived_t>
219template<
class derived_t>
223 this->
model->Invoke();
225 size_t detected = this->getDetections();
227 for(
size_t i = 0; i < detected; i++) {
228 bb = std::move(cv::Rect2f(
230 getX1(i) * io_frame.size().width,
231 getY1(i) * io_frame.size().height
234 getX2(i) * io_frame.size().width,
235 getY2(i) * io_frame.size().height
238 cv::rectangle(io_frame, bb, {0, 100, 255}, 4);
240 io_frame, this->getLabel(i) +
": " + std::to_string(this->
getConfidence(i)) +
"%",
241 cv::Point(bb.tl().x, bb.tl().y - 10), cv::FONT_HERSHEY_DUPLEX, 0.55, {0, 100, 255}, 1, cv::LINE_AA
247template<
class derived_t>
251 if(this->
model->outputs().size() == 1) {
252 this->
outputs =
reinterpret_cast<float*
>(this->
model->output_tensor(0)->data.data);
256template<
class derived_t>
260 this->
model->Invoke();
262 for(
size_t i = 0; i <
~Output::TOTAL; i++) {
263 this->
points[i] = std::move(cv::Point2f(
264 this->
getX(i) * io_frame.size().width,
265 this->getY(i) * io_frame.size().height
268 cv::circle(io_frame, this->
points[i], 1, {25, 255, 0}, 2, cv::LINE_AA);
280#define __TFMODEL_UNSUPPORTED
std::vector< std::string > obj_labels
float getX2(size_t idx) const
bool isValidIdx(size_t idx) const
const std::string & getLabel(size_t idx) const
bool isValidOutput() const
virtual void process(cv::Mat &io_frame) override
AxonRunner_(size_t th=default_threading)
AxonRunner_(const char *model, Optimization opt=Optimization::DEFAULT, const char *map=default_labels, size_t th=default_threading)
size_t getDetections() const
float getY2(size_t idx) const
float getX1(size_t idx) const
float getY1(size_t idx) const
uint32_t getConfidence(size_t idx) const
std::array< cv::Point2f, ~Output::TOTAL > points
bool isValidOutput() const
virtual void process(cv::Mat &io_frame) override
float getX(size_t idx) const
MoveNet_(const char *model, Optimization opt=Optimization::DEFAULT, size_t th=default_threading)
float getConfidence(size_t idx) const
static bool isValidIdx(size_t idx)
MoveNet_(size_t th=default_threading)
float getY(size_t idx) const
TfModel(const char *def, const char *edge, size_t th=default_threading)
bool isValidBuffer() const
std::unique_ptr< tflite::FlatBufferModel > map
TfModel(const char *m, size_t t=default_threading)
std::unique_ptr< tflite::Interpreter > model
static size_t default_threading
TfModel(const TfModel &)=delete
tflite::ops::builtin::BuiltinOpResolver resolver
std::shared_ptr< edgetpu::EdgeTpuContext > edgetpu_context
static const char * default_labels
static const char * default_model
static const char * edgetpu_model
static const char * default_model
static const std::array< std::pair< Output, Output >, 12 > connections
static const std::array< const char *,(size_t) Output::TOTAL > names
static const float confid_thresh
constexpr size_t operator~(MoveNet_B::Output e)
void loadObjectLabels(const std::string &file, std::vector< std::string > &objs)