-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_detector.h
More file actions
56 lines (45 loc) · 1.73 KB
/
object_detector.h
File metadata and controls
56 lines (45 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//
// Some code is lent from: https://github.com/freshtechyy/ONNX-Runtime-GPU-image-classifciation-example at the point of writing licensed with the MIT license.
// The rest is written by me: https://github.com/SiBensberg
//
#ifndef ZED_INFERENCE_OBJECT_DETECTOR_H
#define ZED_INFERENCE_OBJECT_DETECTOR_H
#include <memory>
#include <onnxruntime_cxx_api.h>
#include <opencv2/core/mat.hpp>
#include <opencv2/dnn/dnn.hpp>
#include <opencv2/opencv.hpp>
#include <algorithm>
#include <iostream>
#include <chrono>
#include <vector>
#include <cmath>
#include <sstream>
using clock_time = std::chrono::system_clock;
using sec = std::chrono::duration<double>;
class ObjectDetector {
public:
explicit ObjectDetector(const std::string& modelPath);
std::vector<std::vector<float>> inference(const cv::Mat &imageBGR) const;
bool hwc = true; // whether input to model is HWC or CHW
private:
// ORT Environment
Ort::Env mEnv;
// Session
mutable Ort::Session mSession = Ort::Session(nullptr);
// Memory Info
Ort::MemoryInfo memoryInfo = Ort::MemoryInfo::CreateCpu(OrtAllocatorType::OrtArenaAllocator, OrtMemType::OrtMemTypeDefault);
// Inputs
char* mInputName;
std::vector<int64_t> mInputDims; // b x h x w x c
static inline std::vector<int64_t> mDefaultInputDims = {1, 512, 512, 3};
// Outputs
char* mOutputName;
std::vector<int64_t> mOutputDims; // b x h x w x c
// Camera input
mutable std::vector<int64_t> cameraInputDims; // h x w
void createTensorFromImage(const cv::Mat& img,
std::vector<uint8_t>& inputTensorValues) const;
std::vector<std::vector<float>> calculateBoxes(const Ort::Value &outputTensor) const;
};
#endif //ZED_INFERENCE_OBJECT_DETECTOR_H