Upload 17 files
Browse files- .gitattributes +4 -0
- examples/cmdline.hpp +732 -0
- examples/test_ax_api.cpp +42 -0
- examples/test_axcl_api.cpp +35 -0
- examples/test_det.cpp +128 -0
- examples/test_enum_devices.cpp +59 -0
- examples/timer.hpp +61 -0
- include/ax_devices.h +70 -0
- include/libdet.h +89 -0
- lib/aarch64/libdet.so +3 -0
- lib/example.py +74 -0
- lib/gradio_example.py +108 -0
- lib/pyaxdev.py +148 -0
- lib/pydet.py +128 -0
- lib/requirements.txt +3 -0
- lib/result_11pose.jpg +3 -0
- lib/result_v8.jpg +3 -0
- lib/x86_64/libdet.so +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,7 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
lib/aarch64/libdet.so filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
lib/result_11pose.jpg filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
lib/result_v8.jpg filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
lib/x86_64/libdet.so filter=lfs diff=lfs merge=lfs -text
|
examples/cmdline.hpp
ADDED
|
@@ -0,0 +1,732 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
Copyright (c) 2009, Hideyuki Tanaka
|
| 3 |
+
All rights reserved.
|
| 4 |
+
|
| 5 |
+
Redistribution and use in source and binary forms, with or without
|
| 6 |
+
modification, are permitted provided that the following conditions are met:
|
| 7 |
+
* Redistributions of source code must retain the above copyright
|
| 8 |
+
notice, this list of conditions and the following disclaimer.
|
| 9 |
+
* Redistributions in binary form must reproduce the above copyright
|
| 10 |
+
notice, this list of conditions and the following disclaimer in the
|
| 11 |
+
documentation and/or other materials provided with the distribution.
|
| 12 |
+
* Neither the name of the <organization> nor the
|
| 13 |
+
names of its contributors may be used to endorse or promote products
|
| 14 |
+
derived from this software without specific prior written permission.
|
| 15 |
+
|
| 16 |
+
THIS SOFTWARE IS PROVIDED BY <copyright holder> ''AS IS'' AND ANY
|
| 17 |
+
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
| 18 |
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
| 19 |
+
DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
|
| 20 |
+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
| 21 |
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
| 22 |
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
| 23 |
+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
| 24 |
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
| 25 |
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 26 |
+
*/
|
| 27 |
+
|
| 28 |
+
#pragma once
|
| 29 |
+
|
| 30 |
+
#include <cxxabi.h>
|
| 31 |
+
|
| 32 |
+
#include <algorithm>
|
| 33 |
+
#include <cstdlib>
|
| 34 |
+
#include <cstring>
|
| 35 |
+
#include <iostream>
|
| 36 |
+
#include <map>
|
| 37 |
+
#include <sstream>
|
| 38 |
+
#include <stdexcept>
|
| 39 |
+
#include <string>
|
| 40 |
+
#include <typeinfo>
|
| 41 |
+
#include <vector>
|
| 42 |
+
|
| 43 |
+
namespace cmdline {
|
| 44 |
+
|
| 45 |
+
namespace detail {
|
| 46 |
+
|
| 47 |
+
template <typename Target, typename Source, bool Same>
|
| 48 |
+
class lexical_cast_t {
|
| 49 |
+
public:
|
| 50 |
+
static Target cast(const Source &arg) {
|
| 51 |
+
Target ret;
|
| 52 |
+
std::stringstream ss;
|
| 53 |
+
if (!(ss << arg && ss >> ret && ss.eof())) throw std::bad_cast();
|
| 54 |
+
|
| 55 |
+
return ret;
|
| 56 |
+
}
|
| 57 |
+
};
|
| 58 |
+
|
| 59 |
+
template <typename Target, typename Source>
|
| 60 |
+
class lexical_cast_t<Target, Source, true> {
|
| 61 |
+
public:
|
| 62 |
+
static Target cast(const Source &arg) { return arg; }
|
| 63 |
+
};
|
| 64 |
+
|
| 65 |
+
template <typename Source>
|
| 66 |
+
class lexical_cast_t<std::string, Source, false> {
|
| 67 |
+
public:
|
| 68 |
+
static std::string cast(const Source &arg) {
|
| 69 |
+
std::ostringstream ss;
|
| 70 |
+
ss << arg;
|
| 71 |
+
return ss.str();
|
| 72 |
+
}
|
| 73 |
+
};
|
| 74 |
+
|
| 75 |
+
template <typename Target>
|
| 76 |
+
class lexical_cast_t<Target, std::string, false> {
|
| 77 |
+
public:
|
| 78 |
+
static Target cast(const std::string &arg) {
|
| 79 |
+
Target ret;
|
| 80 |
+
std::istringstream ss(arg);
|
| 81 |
+
if (!(ss >> ret && ss.eof())) throw std::bad_cast();
|
| 82 |
+
return ret;
|
| 83 |
+
}
|
| 84 |
+
};
|
| 85 |
+
|
| 86 |
+
template <typename T1, typename T2>
|
| 87 |
+
struct is_same {
|
| 88 |
+
static const bool value = false;
|
| 89 |
+
};
|
| 90 |
+
|
| 91 |
+
template <typename T>
|
| 92 |
+
struct is_same<T, T> {
|
| 93 |
+
static const bool value = true;
|
| 94 |
+
};
|
| 95 |
+
|
| 96 |
+
template <typename Target, typename Source>
|
| 97 |
+
Target lexical_cast(const Source &arg) {
|
| 98 |
+
return lexical_cast_t<Target, Source,
|
| 99 |
+
detail::is_same<Target, Source>::value>::cast(arg);
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
static inline std::string demangle(const std::string &name) {
|
| 103 |
+
int status = 0;
|
| 104 |
+
char *p = abi::__cxa_demangle(name.c_str(), 0, 0, &status);
|
| 105 |
+
std::string ret(p);
|
| 106 |
+
free(p);
|
| 107 |
+
return ret;
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
template <class T>
|
| 111 |
+
std::string readable_typename() {
|
| 112 |
+
return demangle(typeid(T).name());
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
template <class T>
|
| 116 |
+
std::string default_value(T def) {
|
| 117 |
+
return detail::lexical_cast<std::string>(def);
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
template <>
|
| 121 |
+
inline std::string readable_typename<std::string>() {
|
| 122 |
+
return "string";
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
} // namespace detail
|
| 126 |
+
|
| 127 |
+
//-----
|
| 128 |
+
|
| 129 |
+
class cmdline_error : public std::exception {
|
| 130 |
+
public:
|
| 131 |
+
cmdline_error(const std::string &msg) : msg(msg) {}
|
| 132 |
+
~cmdline_error() throw() {}
|
| 133 |
+
const char *what() const throw() { return msg.c_str(); }
|
| 134 |
+
|
| 135 |
+
private:
|
| 136 |
+
std::string msg;
|
| 137 |
+
};
|
| 138 |
+
|
| 139 |
+
template <class T>
|
| 140 |
+
struct default_reader {
|
| 141 |
+
T operator()(const std::string &str) { return detail::lexical_cast<T>(str); }
|
| 142 |
+
};
|
| 143 |
+
|
| 144 |
+
template <class T>
|
| 145 |
+
struct range_reader {
|
| 146 |
+
range_reader(const T &low, const T &high) : low(low), high(high) {}
|
| 147 |
+
T operator()(const std::string &s) const {
|
| 148 |
+
T ret = default_reader<T>()(s);
|
| 149 |
+
if (!(ret >= low && ret <= high))
|
| 150 |
+
throw cmdline::cmdline_error("range_error");
|
| 151 |
+
return ret;
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
private:
|
| 155 |
+
T low, high;
|
| 156 |
+
};
|
| 157 |
+
|
| 158 |
+
template <class T>
|
| 159 |
+
range_reader<T> range(const T &low, const T &high) {
|
| 160 |
+
return range_reader<T>(low, high);
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
template <class T>
|
| 164 |
+
struct oneof_reader {
|
| 165 |
+
T operator()(const std::string &s) {
|
| 166 |
+
T ret = default_reader<T>()(s);
|
| 167 |
+
if (std::find(alt.begin(), alt.end(), ret) == alt.end())
|
| 168 |
+
throw cmdline_error("");
|
| 169 |
+
return ret;
|
| 170 |
+
}
|
| 171 |
+
void add(const T &v) { alt.push_back(v); }
|
| 172 |
+
|
| 173 |
+
private:
|
| 174 |
+
std::vector<T> alt;
|
| 175 |
+
};
|
| 176 |
+
|
| 177 |
+
template <class T>
|
| 178 |
+
oneof_reader<T> oneof(T a1) {
|
| 179 |
+
oneof_reader<T> ret;
|
| 180 |
+
ret.add(a1);
|
| 181 |
+
return ret;
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
template <class T>
|
| 185 |
+
oneof_reader<T> oneof(T a1, T a2) {
|
| 186 |
+
oneof_reader<T> ret;
|
| 187 |
+
ret.add(a1);
|
| 188 |
+
ret.add(a2);
|
| 189 |
+
return ret;
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
template <class T>
|
| 193 |
+
oneof_reader<T> oneof(T a1, T a2, T a3) {
|
| 194 |
+
oneof_reader<T> ret;
|
| 195 |
+
ret.add(a1);
|
| 196 |
+
ret.add(a2);
|
| 197 |
+
ret.add(a3);
|
| 198 |
+
return ret;
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
template <class T>
|
| 202 |
+
oneof_reader<T> oneof(T a1, T a2, T a3, T a4) {
|
| 203 |
+
oneof_reader<T> ret;
|
| 204 |
+
ret.add(a1);
|
| 205 |
+
ret.add(a2);
|
| 206 |
+
ret.add(a3);
|
| 207 |
+
ret.add(a4);
|
| 208 |
+
return ret;
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
template <class T>
|
| 212 |
+
oneof_reader<T> oneof(T a1, T a2, T a3, T a4, T a5) {
|
| 213 |
+
oneof_reader<T> ret;
|
| 214 |
+
ret.add(a1);
|
| 215 |
+
ret.add(a2);
|
| 216 |
+
ret.add(a3);
|
| 217 |
+
ret.add(a4);
|
| 218 |
+
ret.add(a5);
|
| 219 |
+
return ret;
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
template <class T>
|
| 223 |
+
oneof_reader<T> oneof(T a1, T a2, T a3, T a4, T a5, T a6) {
|
| 224 |
+
oneof_reader<T> ret;
|
| 225 |
+
ret.add(a1);
|
| 226 |
+
ret.add(a2);
|
| 227 |
+
ret.add(a3);
|
| 228 |
+
ret.add(a4);
|
| 229 |
+
ret.add(a5);
|
| 230 |
+
ret.add(a6);
|
| 231 |
+
return ret;
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
template <class T>
|
| 235 |
+
oneof_reader<T> oneof(T a1, T a2, T a3, T a4, T a5, T a6, T a7) {
|
| 236 |
+
oneof_reader<T> ret;
|
| 237 |
+
ret.add(a1);
|
| 238 |
+
ret.add(a2);
|
| 239 |
+
ret.add(a3);
|
| 240 |
+
ret.add(a4);
|
| 241 |
+
ret.add(a5);
|
| 242 |
+
ret.add(a6);
|
| 243 |
+
ret.add(a7);
|
| 244 |
+
return ret;
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
template <class T>
|
| 248 |
+
oneof_reader<T> oneof(T a1, T a2, T a3, T a4, T a5, T a6, T a7, T a8) {
|
| 249 |
+
oneof_reader<T> ret;
|
| 250 |
+
ret.add(a1);
|
| 251 |
+
ret.add(a2);
|
| 252 |
+
ret.add(a3);
|
| 253 |
+
ret.add(a4);
|
| 254 |
+
ret.add(a5);
|
| 255 |
+
ret.add(a6);
|
| 256 |
+
ret.add(a7);
|
| 257 |
+
ret.add(a8);
|
| 258 |
+
return ret;
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
template <class T>
|
| 262 |
+
oneof_reader<T> oneof(T a1, T a2, T a3, T a4, T a5, T a6, T a7, T a8, T a9) {
|
| 263 |
+
oneof_reader<T> ret;
|
| 264 |
+
ret.add(a1);
|
| 265 |
+
ret.add(a2);
|
| 266 |
+
ret.add(a3);
|
| 267 |
+
ret.add(a4);
|
| 268 |
+
ret.add(a5);
|
| 269 |
+
ret.add(a6);
|
| 270 |
+
ret.add(a7);
|
| 271 |
+
ret.add(a8);
|
| 272 |
+
ret.add(a9);
|
| 273 |
+
return ret;
|
| 274 |
+
}
|
| 275 |
+
|
| 276 |
+
template <class T>
|
| 277 |
+
oneof_reader<T> oneof(T a1, T a2, T a3, T a4, T a5, T a6, T a7, T a8, T a9,
|
| 278 |
+
T a10) {
|
| 279 |
+
oneof_reader<T> ret;
|
| 280 |
+
ret.add(a1);
|
| 281 |
+
ret.add(a2);
|
| 282 |
+
ret.add(a3);
|
| 283 |
+
ret.add(a4);
|
| 284 |
+
ret.add(a5);
|
| 285 |
+
ret.add(a6);
|
| 286 |
+
ret.add(a7);
|
| 287 |
+
ret.add(a8);
|
| 288 |
+
ret.add(a9);
|
| 289 |
+
ret.add(a10);
|
| 290 |
+
return ret;
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
//-----
|
| 294 |
+
|
| 295 |
+
class parser {
|
| 296 |
+
public:
|
| 297 |
+
parser() {}
|
| 298 |
+
~parser() {
|
| 299 |
+
for (std::map<std::string, option_base *>::iterator p = options.begin();
|
| 300 |
+
p != options.end(); p++)
|
| 301 |
+
delete p->second;
|
| 302 |
+
}
|
| 303 |
+
|
| 304 |
+
void add(const std::string &name, char short_name = 0,
|
| 305 |
+
const std::string &desc = "") {
|
| 306 |
+
if (options.count(name))
|
| 307 |
+
throw cmdline_error("multiple definition: " + name);
|
| 308 |
+
options[name] = new option_without_value(name, short_name, desc);
|
| 309 |
+
ordered.push_back(options[name]);
|
| 310 |
+
}
|
| 311 |
+
|
| 312 |
+
template <class T>
|
| 313 |
+
void add(const std::string &name, char short_name = 0,
|
| 314 |
+
const std::string &desc = "", bool need = true, const T def = T()) {
|
| 315 |
+
add(name, short_name, desc, need, def, default_reader<T>());
|
| 316 |
+
}
|
| 317 |
+
|
| 318 |
+
template <class T, class F>
|
| 319 |
+
void add(const std::string &name, char short_name = 0,
|
| 320 |
+
const std::string &desc = "", bool need = true, const T def = T(),
|
| 321 |
+
F reader = F()) {
|
| 322 |
+
if (options.count(name))
|
| 323 |
+
throw cmdline_error("multiple definition: " + name);
|
| 324 |
+
options[name] = new option_with_value_with_reader<T, F>(
|
| 325 |
+
name, short_name, need, def, desc, reader);
|
| 326 |
+
ordered.push_back(options[name]);
|
| 327 |
+
}
|
| 328 |
+
|
| 329 |
+
void footer(const std::string &f) { ftr = f; }
|
| 330 |
+
|
| 331 |
+
void set_program_name(const std::string &name) { prog_name = name; }
|
| 332 |
+
|
| 333 |
+
bool exist(const std::string &name) const {
|
| 334 |
+
if (options.count(name) == 0)
|
| 335 |
+
throw cmdline_error("there is no flag: --" + name);
|
| 336 |
+
return options.find(name)->second->has_set();
|
| 337 |
+
}
|
| 338 |
+
|
| 339 |
+
template <class T>
|
| 340 |
+
const T &get(const std::string &name) const {
|
| 341 |
+
if (options.count(name) == 0)
|
| 342 |
+
throw cmdline_error("there is no flag: --" + name);
|
| 343 |
+
const option_with_value<T> *p =
|
| 344 |
+
dynamic_cast<const option_with_value<T> *>(options.find(name)->second);
|
| 345 |
+
if (p == NULL) throw cmdline_error("type mismatch flag '" + name + "'");
|
| 346 |
+
return p->get();
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
const std::vector<std::string> &rest() const { return others; }
|
| 350 |
+
|
| 351 |
+
bool parse(const std::string &arg) {
|
| 352 |
+
std::vector<std::string> args;
|
| 353 |
+
|
| 354 |
+
std::string buf;
|
| 355 |
+
bool in_quote = false;
|
| 356 |
+
for (std::string::size_type i = 0; i < arg.length(); i++) {
|
| 357 |
+
if (arg[i] == '\"') {
|
| 358 |
+
in_quote = !in_quote;
|
| 359 |
+
continue;
|
| 360 |
+
}
|
| 361 |
+
|
| 362 |
+
if (arg[i] == ' ' && !in_quote) {
|
| 363 |
+
args.push_back(buf);
|
| 364 |
+
buf = "";
|
| 365 |
+
continue;
|
| 366 |
+
}
|
| 367 |
+
|
| 368 |
+
if (arg[i] == '\\') {
|
| 369 |
+
i++;
|
| 370 |
+
if (i >= arg.length()) {
|
| 371 |
+
errors.push_back("unexpected occurrence of '\\' at end of string");
|
| 372 |
+
return false;
|
| 373 |
+
}
|
| 374 |
+
}
|
| 375 |
+
|
| 376 |
+
buf += arg[i];
|
| 377 |
+
}
|
| 378 |
+
|
| 379 |
+
if (in_quote) {
|
| 380 |
+
errors.push_back("quote is not closed");
|
| 381 |
+
return false;
|
| 382 |
+
}
|
| 383 |
+
|
| 384 |
+
if (buf.length() > 0) args.push_back(buf);
|
| 385 |
+
|
| 386 |
+
for (size_t i = 0; i < args.size(); i++)
|
| 387 |
+
std::cout << "\"" << args[i] << "\"" << std::endl;
|
| 388 |
+
|
| 389 |
+
return parse(args);
|
| 390 |
+
}
|
| 391 |
+
|
| 392 |
+
bool parse(const std::vector<std::string> &args) {
|
| 393 |
+
int argc = static_cast<int>(args.size());
|
| 394 |
+
std::vector<const char *> argv(argc);
|
| 395 |
+
|
| 396 |
+
for (int i = 0; i < argc; i++) argv[i] = args[i].c_str();
|
| 397 |
+
|
| 398 |
+
return parse(argc, &argv[0]);
|
| 399 |
+
}
|
| 400 |
+
|
| 401 |
+
bool parse(int argc, const char *const argv[]) {
|
| 402 |
+
errors.clear();
|
| 403 |
+
others.clear();
|
| 404 |
+
|
| 405 |
+
if (argc < 1) {
|
| 406 |
+
errors.push_back("argument number must be longer than 0");
|
| 407 |
+
return false;
|
| 408 |
+
}
|
| 409 |
+
if (prog_name == "") prog_name = argv[0];
|
| 410 |
+
|
| 411 |
+
std::map<char, std::string> lookup;
|
| 412 |
+
for (std::map<std::string, option_base *>::iterator p = options.begin();
|
| 413 |
+
p != options.end(); p++) {
|
| 414 |
+
if (p->first.length() == 0) continue;
|
| 415 |
+
char initial = p->second->short_name();
|
| 416 |
+
if (initial) {
|
| 417 |
+
if (lookup.count(initial) > 0) {
|
| 418 |
+
lookup[initial] = "";
|
| 419 |
+
errors.push_back(std::string("short option '") + initial +
|
| 420 |
+
"' is ambiguous");
|
| 421 |
+
return false;
|
| 422 |
+
} else
|
| 423 |
+
lookup[initial] = p->first;
|
| 424 |
+
}
|
| 425 |
+
}
|
| 426 |
+
|
| 427 |
+
for (int i = 1; i < argc; i++) {
|
| 428 |
+
if (strncmp(argv[i], "--", 2) == 0) {
|
| 429 |
+
const char *p = strchr(argv[i] + 2, '=');
|
| 430 |
+
if (p) {
|
| 431 |
+
std::string name(argv[i] + 2, p);
|
| 432 |
+
std::string val(p + 1);
|
| 433 |
+
set_option(name, val);
|
| 434 |
+
} else {
|
| 435 |
+
std::string name(argv[i] + 2);
|
| 436 |
+
if (options.count(name) == 0) {
|
| 437 |
+
errors.push_back("undefined option: --" + name);
|
| 438 |
+
continue;
|
| 439 |
+
}
|
| 440 |
+
if (options[name]->has_value()) {
|
| 441 |
+
if (i + 1 >= argc) {
|
| 442 |
+
errors.push_back("option needs value: --" + name);
|
| 443 |
+
continue;
|
| 444 |
+
} else {
|
| 445 |
+
i++;
|
| 446 |
+
set_option(name, argv[i]);
|
| 447 |
+
}
|
| 448 |
+
} else {
|
| 449 |
+
set_option(name);
|
| 450 |
+
}
|
| 451 |
+
}
|
| 452 |
+
} else if (strncmp(argv[i], "-", 1) == 0) {
|
| 453 |
+
if (!argv[i][1]) continue;
|
| 454 |
+
char last = argv[i][1];
|
| 455 |
+
for (int j = 2; argv[i][j]; j++) {
|
| 456 |
+
last = argv[i][j];
|
| 457 |
+
if (lookup.count(argv[i][j - 1]) == 0) {
|
| 458 |
+
errors.push_back(std::string("undefined short option: -") +
|
| 459 |
+
argv[i][j - 1]);
|
| 460 |
+
continue;
|
| 461 |
+
}
|
| 462 |
+
if (lookup[argv[i][j - 1]] == "") {
|
| 463 |
+
errors.push_back(std::string("ambiguous short option: -") +
|
| 464 |
+
argv[i][j - 1]);
|
| 465 |
+
continue;
|
| 466 |
+
}
|
| 467 |
+
set_option(lookup[argv[i][j - 1]]);
|
| 468 |
+
}
|
| 469 |
+
|
| 470 |
+
if (lookup.count(last) == 0) {
|
| 471 |
+
errors.push_back(std::string("undefined short option: -") + last);
|
| 472 |
+
continue;
|
| 473 |
+
}
|
| 474 |
+
if (lookup[last] == "") {
|
| 475 |
+
errors.push_back(std::string("ambiguous short option: -") + last);
|
| 476 |
+
continue;
|
| 477 |
+
}
|
| 478 |
+
|
| 479 |
+
if (i + 1 < argc && options[lookup[last]]->has_value()) {
|
| 480 |
+
set_option(lookup[last], argv[i + 1]);
|
| 481 |
+
i++;
|
| 482 |
+
} else {
|
| 483 |
+
set_option(lookup[last]);
|
| 484 |
+
}
|
| 485 |
+
} else {
|
| 486 |
+
others.push_back(argv[i]);
|
| 487 |
+
}
|
| 488 |
+
}
|
| 489 |
+
|
| 490 |
+
for (std::map<std::string, option_base *>::iterator p = options.begin();
|
| 491 |
+
p != options.end(); p++)
|
| 492 |
+
if (!p->second->valid())
|
| 493 |
+
errors.push_back("need option: --" + std::string(p->first));
|
| 494 |
+
|
| 495 |
+
return errors.size() == 0;
|
| 496 |
+
}
|
| 497 |
+
|
| 498 |
+
void parse_check(const std::string &arg) {
|
| 499 |
+
if (!options.count("help")) add("help", '?', "print this message");
|
| 500 |
+
check(0, parse(arg));
|
| 501 |
+
}
|
| 502 |
+
|
| 503 |
+
void parse_check(const std::vector<std::string> &args) {
|
| 504 |
+
if (!options.count("help")) add("help", '?', "print this message");
|
| 505 |
+
check(args.size(), parse(args));
|
| 506 |
+
}
|
| 507 |
+
|
| 508 |
+
void parse_check(int argc, char *argv[]) {
|
| 509 |
+
if (!options.count("help")) add("help", '?', "print this message");
|
| 510 |
+
check(argc, parse(argc, argv));
|
| 511 |
+
}
|
| 512 |
+
|
| 513 |
+
std::string error() const { return errors.size() > 0 ? errors[0] : ""; }
|
| 514 |
+
|
| 515 |
+
std::string error_full() const {
|
| 516 |
+
std::ostringstream oss;
|
| 517 |
+
for (size_t i = 0; i < errors.size(); i++) oss << errors[i] << std::endl;
|
| 518 |
+
return oss.str();
|
| 519 |
+
}
|
| 520 |
+
|
| 521 |
+
std::string usage() const {
|
| 522 |
+
std::ostringstream oss;
|
| 523 |
+
oss << "usage: " << prog_name << " ";
|
| 524 |
+
for (size_t i = 0; i < ordered.size(); i++) {
|
| 525 |
+
if (ordered[i]->must()) oss << ordered[i]->short_description() << " ";
|
| 526 |
+
}
|
| 527 |
+
|
| 528 |
+
oss << "[options] ... " << ftr << std::endl;
|
| 529 |
+
oss << "options:" << std::endl;
|
| 530 |
+
|
| 531 |
+
size_t max_width = 0;
|
| 532 |
+
for (size_t i = 0; i < ordered.size(); i++) {
|
| 533 |
+
max_width = std::max(max_width, ordered[i]->name().length());
|
| 534 |
+
}
|
| 535 |
+
for (size_t i = 0; i < ordered.size(); i++) {
|
| 536 |
+
if (ordered[i]->short_name()) {
|
| 537 |
+
oss << " -" << ordered[i]->short_name() << ", ";
|
| 538 |
+
} else {
|
| 539 |
+
oss << " ";
|
| 540 |
+
}
|
| 541 |
+
|
| 542 |
+
oss << "--" << ordered[i]->name();
|
| 543 |
+
for (size_t j = ordered[i]->name().length(); j < max_width + 4; j++)
|
| 544 |
+
oss << ' ';
|
| 545 |
+
oss << ordered[i]->description() << std::endl;
|
| 546 |
+
}
|
| 547 |
+
return oss.str();
|
| 548 |
+
}
|
| 549 |
+
|
| 550 |
+
private:
|
| 551 |
+
void check(int argc, bool ok) {
|
| 552 |
+
if ((argc == 1 && !ok) || exist("help")) {
|
| 553 |
+
std::cerr << usage();
|
| 554 |
+
exit(0);
|
| 555 |
+
}
|
| 556 |
+
|
| 557 |
+
if (!ok) {
|
| 558 |
+
std::cerr << error() << std::endl << usage();
|
| 559 |
+
exit(1);
|
| 560 |
+
}
|
| 561 |
+
}
|
| 562 |
+
|
| 563 |
+
void set_option(const std::string &name) {
|
| 564 |
+
if (options.count(name) == 0) {
|
| 565 |
+
errors.push_back("undefined option: --" + name);
|
| 566 |
+
return;
|
| 567 |
+
}
|
| 568 |
+
if (!options[name]->set()) {
|
| 569 |
+
errors.push_back("option needs value: --" + name);
|
| 570 |
+
return;
|
| 571 |
+
}
|
| 572 |
+
}
|
| 573 |
+
|
| 574 |
+
void set_option(const std::string &name, const std::string &value) {
|
| 575 |
+
if (options.count(name) == 0) {
|
| 576 |
+
errors.push_back("undefined option: --" + name);
|
| 577 |
+
return;
|
| 578 |
+
}
|
| 579 |
+
if (!options[name]->set(value)) {
|
| 580 |
+
errors.push_back("option value is invalid: --" + name + "=" + value);
|
| 581 |
+
return;
|
| 582 |
+
}
|
| 583 |
+
}
|
| 584 |
+
|
| 585 |
+
class option_base {
|
| 586 |
+
public:
|
| 587 |
+
virtual ~option_base() {}
|
| 588 |
+
|
| 589 |
+
virtual bool has_value() const = 0;
|
| 590 |
+
virtual bool set() = 0;
|
| 591 |
+
virtual bool set(const std::string &value) = 0;
|
| 592 |
+
virtual bool has_set() const = 0;
|
| 593 |
+
virtual bool valid() const = 0;
|
| 594 |
+
virtual bool must() const = 0;
|
| 595 |
+
|
| 596 |
+
virtual const std::string &name() const = 0;
|
| 597 |
+
virtual char short_name() const = 0;
|
| 598 |
+
virtual const std::string &description() const = 0;
|
| 599 |
+
virtual std::string short_description() const = 0;
|
| 600 |
+
};
|
| 601 |
+
|
| 602 |
+
class option_without_value : public option_base {
|
| 603 |
+
public:
|
| 604 |
+
option_without_value(const std::string &name, char short_name,
|
| 605 |
+
const std::string &desc)
|
| 606 |
+
: nam(name), snam(short_name), desc(desc), has(false) {}
|
| 607 |
+
~option_without_value() {}
|
| 608 |
+
|
| 609 |
+
bool has_value() const { return false; }
|
| 610 |
+
|
| 611 |
+
bool set() {
|
| 612 |
+
has = true;
|
| 613 |
+
return true;
|
| 614 |
+
}
|
| 615 |
+
|
| 616 |
+
bool set(const std::string &) { return false; }
|
| 617 |
+
|
| 618 |
+
bool has_set() const { return has; }
|
| 619 |
+
|
| 620 |
+
bool valid() const { return true; }
|
| 621 |
+
|
| 622 |
+
bool must() const { return false; }
|
| 623 |
+
|
| 624 |
+
const std::string &name() const { return nam; }
|
| 625 |
+
|
| 626 |
+
char short_name() const { return snam; }
|
| 627 |
+
|
| 628 |
+
const std::string &description() const { return desc; }
|
| 629 |
+
|
| 630 |
+
std::string short_description() const { return "--" + nam; }
|
| 631 |
+
|
| 632 |
+
private:
|
| 633 |
+
std::string nam;
|
| 634 |
+
char snam;
|
| 635 |
+
std::string desc;
|
| 636 |
+
bool has;
|
| 637 |
+
};
|
| 638 |
+
|
| 639 |
+
template <class T>
|
| 640 |
+
class option_with_value : public option_base {
|
| 641 |
+
public:
|
| 642 |
+
option_with_value(const std::string &name, char short_name, bool need,
|
| 643 |
+
const T &def, const std::string &desc)
|
| 644 |
+
: nam(name),
|
| 645 |
+
snam(short_name),
|
| 646 |
+
need(need),
|
| 647 |
+
has(false),
|
| 648 |
+
def(def),
|
| 649 |
+
actual(def) {
|
| 650 |
+
this->desc = full_description(desc);
|
| 651 |
+
}
|
| 652 |
+
~option_with_value() {}
|
| 653 |
+
|
| 654 |
+
const T &get() const { return actual; }
|
| 655 |
+
|
| 656 |
+
bool has_value() const { return true; }
|
| 657 |
+
|
| 658 |
+
bool set() { return false; }
|
| 659 |
+
|
| 660 |
+
bool set(const std::string &value) {
|
| 661 |
+
try {
|
| 662 |
+
actual = read(value);
|
| 663 |
+
has = true;
|
| 664 |
+
} catch (const std::exception &e) {
|
| 665 |
+
return false;
|
| 666 |
+
}
|
| 667 |
+
return true;
|
| 668 |
+
}
|
| 669 |
+
|
| 670 |
+
bool has_set() const { return has; }
|
| 671 |
+
|
| 672 |
+
bool valid() const {
|
| 673 |
+
if (need && !has) return false;
|
| 674 |
+
return true;
|
| 675 |
+
}
|
| 676 |
+
|
| 677 |
+
bool must() const { return need; }
|
| 678 |
+
|
| 679 |
+
const std::string &name() const { return nam; }
|
| 680 |
+
|
| 681 |
+
char short_name() const { return snam; }
|
| 682 |
+
|
| 683 |
+
const std::string &description() const { return desc; }
|
| 684 |
+
|
| 685 |
+
std::string short_description() const {
|
| 686 |
+
return "--" + nam + "=" + detail::readable_typename<T>();
|
| 687 |
+
}
|
| 688 |
+
|
| 689 |
+
protected:
|
| 690 |
+
std::string full_description(const std::string &desc) {
|
| 691 |
+
return desc + " (" + detail::readable_typename<T>() +
|
| 692 |
+
(need ? "" : " [=" + detail::default_value<T>(def) + "]") + ")";
|
| 693 |
+
}
|
| 694 |
+
|
| 695 |
+
virtual T read(const std::string &s) = 0;
|
| 696 |
+
|
| 697 |
+
std::string nam;
|
| 698 |
+
char snam;
|
| 699 |
+
bool need;
|
| 700 |
+
std::string desc;
|
| 701 |
+
|
| 702 |
+
bool has;
|
| 703 |
+
T def;
|
| 704 |
+
T actual;
|
| 705 |
+
};
|
| 706 |
+
|
| 707 |
+
template <class T, class F>
|
| 708 |
+
class option_with_value_with_reader : public option_with_value<T> {
|
| 709 |
+
public:
|
| 710 |
+
option_with_value_with_reader(const std::string &name, char short_name,
|
| 711 |
+
bool need, const T def,
|
| 712 |
+
const std::string &desc, F reader)
|
| 713 |
+
: option_with_value<T>(name, short_name, need, def, desc),
|
| 714 |
+
reader(reader) {}
|
| 715 |
+
|
| 716 |
+
private:
|
| 717 |
+
T read(const std::string &s) { return reader(s); }
|
| 718 |
+
|
| 719 |
+
F reader;
|
| 720 |
+
};
|
| 721 |
+
|
| 722 |
+
std::map<std::string, option_base *> options;
|
| 723 |
+
std::vector<option_base *> ordered;
|
| 724 |
+
std::string ftr;
|
| 725 |
+
|
| 726 |
+
std::string prog_name;
|
| 727 |
+
std::vector<std::string> others;
|
| 728 |
+
|
| 729 |
+
std::vector<std::string> errors;
|
| 730 |
+
};
|
| 731 |
+
|
| 732 |
+
} // namespace cmdline
|
examples/test_ax_api.cpp
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include "runner/ax650/ax_api_loader.h"
|
| 2 |
+
#include "runner/ax650/ax_model_runner_ax650.hpp"
|
| 3 |
+
#include "cmdline.hpp"
|
| 4 |
+
|
| 5 |
+
#include <fstream>
|
| 6 |
+
#include <vector>
|
| 7 |
+
#include <cstring>
|
| 8 |
+
|
| 9 |
+
AxSysApiLoader &get_ax_sys_loader();
|
| 10 |
+
|
| 11 |
+
AxEngineApiLoader &get_ax_engine_loader();
|
| 12 |
+
|
| 13 |
+
int main(int argc, char *argv[])
|
| 14 |
+
{
|
| 15 |
+
cmdline::parser parser;
|
| 16 |
+
parser.add<std::string>("model", 'm', "model", true);
|
| 17 |
+
parser.parse_check(argc, argv);
|
| 18 |
+
|
| 19 |
+
AxSysApiLoader &ax_sys_loader = get_ax_sys_loader();
|
| 20 |
+
|
| 21 |
+
AxEngineApiLoader &ax_engine_loader = get_ax_engine_loader();
|
| 22 |
+
|
| 23 |
+
ax_sys_loader.AX_SYS_Init();
|
| 24 |
+
AX_ENGINE_NPU_ATTR_T npu_attr;
|
| 25 |
+
memset(&npu_attr, 0, sizeof(AX_ENGINE_NPU_ATTR_T));
|
| 26 |
+
npu_attr.eHardMode = AX_ENGINE_VIRTUAL_NPU_DISABLE;
|
| 27 |
+
ax_engine_loader.AX_ENGINE_Init(&npu_attr);
|
| 28 |
+
|
| 29 |
+
ax_runner_ax650 runner;
|
| 30 |
+
std::ifstream file(parser.get<std::string>("model"), std::ios::binary);
|
| 31 |
+
if (!file.is_open())
|
| 32 |
+
{
|
| 33 |
+
printf("open file failed\n");
|
| 34 |
+
return -1;
|
| 35 |
+
}
|
| 36 |
+
std::vector<uint8_t> model_data((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
| 37 |
+
runner.init(model_data.data(), model_data.size(), 0);
|
| 38 |
+
|
| 39 |
+
ax_engine_loader.AX_ENGINE_Deinit();
|
| 40 |
+
ax_sys_loader.AX_SYS_Deinit();
|
| 41 |
+
return 0;
|
| 42 |
+
}
|
examples/test_axcl_api.cpp
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include "runner/axcl/axcl_manager.h"
|
| 2 |
+
#include "runner/axcl/ax_model_runner_axcl.hpp"
|
| 3 |
+
#include "cmdline.hpp"
|
| 4 |
+
#include <fstream>
|
| 5 |
+
|
| 6 |
+
int main(int argc, char *argv[])
|
| 7 |
+
{
|
| 8 |
+
auto ret = axclInit();
|
| 9 |
+
if (ret != 0)
|
| 10 |
+
{
|
| 11 |
+
printf("axclInit failed\n");
|
| 12 |
+
return -1;
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
axcl_Dev_Init(0);
|
| 16 |
+
cmdline::parser parser;
|
| 17 |
+
parser.add<std::string>("model", 'm', "model", true);
|
| 18 |
+
parser.parse_check(argc, argv);
|
| 19 |
+
|
| 20 |
+
ax_runner_axcl runner;
|
| 21 |
+
std::ifstream file(parser.get<std::string>("model"), std::ios::binary);
|
| 22 |
+
if (!file.is_open())
|
| 23 |
+
{
|
| 24 |
+
printf("open file failed\n");
|
| 25 |
+
return -1;
|
| 26 |
+
}
|
| 27 |
+
std::vector<uint8_t> model_data((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
| 28 |
+
runner.init(model_data.data(), model_data.size(), 0);
|
| 29 |
+
|
| 30 |
+
runner.deinit();
|
| 31 |
+
|
| 32 |
+
axcl_Dev_Exit(0);
|
| 33 |
+
axclFinalize();
|
| 34 |
+
return 0;
|
| 35 |
+
}
|
examples/test_det.cpp
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include "libdet.h"
|
| 2 |
+
#include "cmdline.hpp"
|
| 3 |
+
#include "timer.hpp"
|
| 4 |
+
#include <fstream>
|
| 5 |
+
#include <cstring>
|
| 6 |
+
#include <opencv2/opencv.hpp>
|
| 7 |
+
|
| 8 |
+
int main(int argc, char *argv[])
|
| 9 |
+
{
|
| 10 |
+
ax_devices_t ax_devices;
|
| 11 |
+
memset(&ax_devices, 0, sizeof(ax_devices_t));
|
| 12 |
+
if (ax_dev_enum_devices(&ax_devices) != 0)
|
| 13 |
+
{
|
| 14 |
+
printf("enum devices failed\n");
|
| 15 |
+
return -1;
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
if (ax_devices.host.available)
|
| 19 |
+
{
|
| 20 |
+
ax_dev_sys_init(host_device, -1);
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
if (ax_devices.devices.count > 0)
|
| 24 |
+
{
|
| 25 |
+
ax_dev_sys_init(axcl_device, 0);
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
if (!ax_devices.host.available && ax_devices.devices.count == 0)
|
| 29 |
+
{
|
| 30 |
+
printf("no device available\n");
|
| 31 |
+
return -1;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
ax_det_init_t init_info;
|
| 35 |
+
memset(&init_info, 0, sizeof(init_info));
|
| 36 |
+
|
| 37 |
+
cmdline::parser parser;
|
| 38 |
+
parser.add<std::string>("model", 'm', "model", true);
|
| 39 |
+
parser.add<int>("model_type", 't', "model type", true);
|
| 40 |
+
parser.add<std::string>("image", 'i', "image folder(jpg png etc....)", true);
|
| 41 |
+
parser.add<float>("threshold", 0, "threshold", false, 0.25);
|
| 42 |
+
parser.add<int>("num_classes", 'c', "num classes", false, 80);
|
| 43 |
+
parser.add<int>("num_kpt", 0, "num kpt", false, 0);
|
| 44 |
+
parser.add<std::string>("output", 'o', "output", false, "results.jpg");
|
| 45 |
+
parser.parse_check(argc, argv);
|
| 46 |
+
|
| 47 |
+
if (ax_devices.host.available)
|
| 48 |
+
{
|
| 49 |
+
init_info.dev_type = host_device;
|
| 50 |
+
}
|
| 51 |
+
else if (ax_devices.devices.count > 0)
|
| 52 |
+
{
|
| 53 |
+
init_info.dev_type = axcl_device;
|
| 54 |
+
init_info.devid = 0;
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
init_info.num_classes = parser.get<int>("num_classes");
|
| 58 |
+
init_info.num_kpt = parser.get<int>("num_kpt");
|
| 59 |
+
|
| 60 |
+
init_info.model_type = ax_det_model_type_e(parser.get<int>("model_type"));
|
| 61 |
+
|
| 62 |
+
sprintf(init_info.model_path, "%s", parser.get<std::string>("model").c_str());
|
| 63 |
+
init_info.threshold = parser.get<float>("threshold");
|
| 64 |
+
|
| 65 |
+
ax_det_handle_t handle;
|
| 66 |
+
int ret = ax_det_init(&init_info, &handle);
|
| 67 |
+
if (ret != ax_det_errcode_success)
|
| 68 |
+
{
|
| 69 |
+
printf("ax_det_init failed\n");
|
| 70 |
+
return -1;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
std::string image_src = parser.get<std::string>("image");
|
| 74 |
+
cv::Mat src = cv::imread(image_src);
|
| 75 |
+
if (src.empty())
|
| 76 |
+
{
|
| 77 |
+
printf("imread %s failed\n", image_src.c_str());
|
| 78 |
+
return -1;
|
| 79 |
+
}
|
| 80 |
+
cv::cvtColor(src, src, cv::COLOR_BGR2RGB);
|
| 81 |
+
ax_det_img_t img;
|
| 82 |
+
img.data = src.data;
|
| 83 |
+
img.width = src.cols;
|
| 84 |
+
img.height = src.rows;
|
| 85 |
+
img.channels = src.channels();
|
| 86 |
+
img.stride = src.step;
|
| 87 |
+
ax_det_result_t result;
|
| 88 |
+
memset(&result, 0, sizeof(result));
|
| 89 |
+
ret = ax_det(handle, &img, &result);
|
| 90 |
+
if (ret != ax_det_errcode_success)
|
| 91 |
+
{
|
| 92 |
+
printf("ax_det failed\n");
|
| 93 |
+
return -1;
|
| 94 |
+
}
|
| 95 |
+
printf("num_objs: %d\n", result.num_objs);
|
| 96 |
+
|
| 97 |
+
cv::cvtColor(src, src, cv::COLOR_RGB2BGR);
|
| 98 |
+
|
| 99 |
+
for (int i = 0; i < result.num_objs; i++)
|
| 100 |
+
{
|
| 101 |
+
ax_det_obj_t &obj = result.objects[i];
|
| 102 |
+
cv::Rect rect(obj.box.x, obj.box.y, obj.box.w, obj.box.h);
|
| 103 |
+
cv::rectangle(src, rect, cv::Scalar(0, 255, 0), 2);
|
| 104 |
+
char label_info[128];
|
| 105 |
+
sprintf(label_info, "%d %5.2f", obj.label, obj.score);
|
| 106 |
+
cv::putText(src, label_info, cv::Point(obj.box.x, obj.box.y + 25), cv::FONT_HERSHEY_SIMPLEX, 1.5, cv::Scalar(0, 255, 0), 2);
|
| 107 |
+
|
| 108 |
+
for (int j = 0; j < obj.num_kpt; j++)
|
| 109 |
+
{
|
| 110 |
+
cv::circle(src, cv::Point(obj.kpts[j].x, obj.kpts[j].y), 10, cv::Scalar(0, 255, 0), -1);
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
cv::imwrite(parser.get<std::string>("output"), src);
|
| 115 |
+
|
| 116 |
+
ax_det_deinit(handle);
|
| 117 |
+
|
| 118 |
+
if (ax_devices.host.available)
|
| 119 |
+
{
|
| 120 |
+
ax_dev_sys_deinit(host_device, -1);
|
| 121 |
+
}
|
| 122 |
+
else if (ax_devices.devices.count > 0)
|
| 123 |
+
{
|
| 124 |
+
ax_dev_sys_deinit(axcl_device, 0);
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
return 0;
|
| 128 |
+
}
|
examples/test_enum_devices.cpp
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include "ax_devices.h"
|
| 2 |
+
#include <iostream>
|
| 3 |
+
#include <cstring>
|
| 4 |
+
|
| 5 |
+
int main()
|
| 6 |
+
{
|
| 7 |
+
ax_devices_t ax_devices;
|
| 8 |
+
memset(&ax_devices, 0, sizeof(ax_devices_t));
|
| 9 |
+
if (ax_dev_enum_devices(&ax_devices) != 0)
|
| 10 |
+
{
|
| 11 |
+
printf("enum devices failed\n");
|
| 12 |
+
return -1;
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
std::cout << "host npu avaiable:" << static_cast<int>(ax_devices.host.available) << " version:" << ax_devices.host.version << std::endl;
|
| 16 |
+
std::cout << "host mem total:" << ax_devices.host.mem_info.total << " MiB remain:" << ax_devices.host.mem_info.remain << " MiB" << std::endl;
|
| 17 |
+
|
| 18 |
+
std::cout << "Host Version: " << ax_devices.devices.host_version << std::endl;
|
| 19 |
+
std::cout << "Dev Version: " << ax_devices.devices.dev_version << std::endl;
|
| 20 |
+
std::cout << "Detected Devices Count: " << static_cast<int>(ax_devices.devices.count) << std::endl;
|
| 21 |
+
|
| 22 |
+
for (unsigned char i = 0; i < ax_devices.devices.count; ++i)
|
| 23 |
+
{
|
| 24 |
+
std::cout << " Device " << static_cast<int>(i) << ":" << std::endl;
|
| 25 |
+
std::cout << " Temperature: " << ax_devices.devices.devices_info[i].temp << "C" << std::endl;
|
| 26 |
+
std::cout << " CPU Usage: " << ax_devices.devices.devices_info[i].cpu_usage << "%" << std::endl;
|
| 27 |
+
std::cout << " NPU Usage: " << ax_devices.devices.devices_info[i].npu_usage << "%" << std::endl;
|
| 28 |
+
std::cout << " Memory Remaining: " << ax_devices.devices.devices_info[i].mem_info.remain << " MiB" << std::endl;
|
| 29 |
+
std::cout << " Memory Total: " << ax_devices.devices.devices_info[i].mem_info.total << " MiB" << std::endl;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
if (ax_devices.host.available)
|
| 33 |
+
{
|
| 34 |
+
ax_dev_sys_init(host_device, -1);
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
if (ax_devices.devices.count > 0)
|
| 38 |
+
{
|
| 39 |
+
for (unsigned char i = 0; i < ax_devices.devices.count; ++i)
|
| 40 |
+
{
|
| 41 |
+
ax_dev_sys_init(axcl_device, i);
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
if (ax_devices.host.available)
|
| 46 |
+
{
|
| 47 |
+
ax_dev_sys_deinit(host_device, -1);
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
if (ax_devices.devices.count > 0)
|
| 51 |
+
{
|
| 52 |
+
for (unsigned char i = 0; i < ax_devices.devices.count; ++i)
|
| 53 |
+
{
|
| 54 |
+
ax_dev_sys_deinit(axcl_device, i);
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
return 0;
|
| 59 |
+
}
|
examples/timer.hpp
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
* AXERA is pleased to support the open source community by making ax-samples available.
|
| 3 |
+
*
|
| 4 |
+
* Copyright (c) 2022, AXERA Semiconductor (Shanghai) Co., Ltd. All rights reserved.
|
| 5 |
+
*
|
| 6 |
+
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
|
| 7 |
+
* in compliance with the License. You may obtain a copy of the License at
|
| 8 |
+
*
|
| 9 |
+
* https://opensource.org/licenses/BSD-3-Clause
|
| 10 |
+
*
|
| 11 |
+
* Unless required by applicable law or agreed to in writing, software distributed
|
| 12 |
+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
| 13 |
+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
| 14 |
+
* specific language governing permissions and limitations under the License.
|
| 15 |
+
*/
|
| 16 |
+
|
| 17 |
+
/*
|
| 18 |
+
* Author: ls.wang
|
| 19 |
+
*/
|
| 20 |
+
|
| 21 |
+
#pragma once
|
| 22 |
+
|
| 23 |
+
#include <chrono>
|
| 24 |
+
|
| 25 |
+
class timer
|
| 26 |
+
{
|
| 27 |
+
private:
|
| 28 |
+
std::chrono::system_clock::time_point start_time, end_time;
|
| 29 |
+
|
| 30 |
+
public:
|
| 31 |
+
timer()
|
| 32 |
+
{
|
| 33 |
+
start();
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
void start()
|
| 37 |
+
{
|
| 38 |
+
stop();
|
| 39 |
+
this->start_time = this->end_time;
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
void stop()
|
| 43 |
+
{
|
| 44 |
+
#ifdef _MSC_VER
|
| 45 |
+
this->end_time = std::chrono::system_clock::now();
|
| 46 |
+
#else
|
| 47 |
+
this->end_time = std::chrono::high_resolution_clock::now();
|
| 48 |
+
#endif
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
float cost()
|
| 52 |
+
{
|
| 53 |
+
if (this->end_time <= this->start_time)
|
| 54 |
+
{
|
| 55 |
+
this->stop();
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
auto ms = std::chrono::duration_cast<std::chrono::microseconds>(this->end_time - this->start_time).count();
|
| 59 |
+
return static_cast<float>(ms) / 1000.f;
|
| 60 |
+
}
|
| 61 |
+
};
|
include/ax_devices.h
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#ifndef __AX_DEVICES_H__
|
| 2 |
+
#define __AX_DEVICES_H__
|
| 3 |
+
|
| 4 |
+
#if defined(__cplusplus)
|
| 5 |
+
extern "C"
|
| 6 |
+
{
|
| 7 |
+
#endif
|
| 8 |
+
#define AX_DEVICES_COUNT 16
|
| 9 |
+
#define AX_VERSION_LEN 32
|
| 10 |
+
|
| 11 |
+
typedef enum
|
| 12 |
+
{
|
| 13 |
+
ax_dev_errcode_failed = -1,
|
| 14 |
+
ax_dev_errcode_success = 0,
|
| 15 |
+
|
| 16 |
+
ax_dev_errcode_sysinit_failed,
|
| 17 |
+
ax_dev_errcode_sysdeinit_failed,
|
| 18 |
+
ax_dev_errcode_axcl_sysinit_failed,
|
| 19 |
+
ax_dev_errcode_axcl_sysdeinit_failed,
|
| 20 |
+
} ax_dev_errcode_e;
|
| 21 |
+
|
| 22 |
+
typedef enum
|
| 23 |
+
{
|
| 24 |
+
unknown_device = 0,
|
| 25 |
+
host_device = 1,
|
| 26 |
+
axcl_device = 2
|
| 27 |
+
} ax_devive_e;
|
| 28 |
+
|
| 29 |
+
typedef struct
|
| 30 |
+
{
|
| 31 |
+
struct
|
| 32 |
+
{
|
| 33 |
+
char available;
|
| 34 |
+
char version[AX_VERSION_LEN];
|
| 35 |
+
struct
|
| 36 |
+
{
|
| 37 |
+
int remain;
|
| 38 |
+
int total;
|
| 39 |
+
} mem_info;
|
| 40 |
+
} host;
|
| 41 |
+
|
| 42 |
+
struct
|
| 43 |
+
{
|
| 44 |
+
char host_version[AX_VERSION_LEN];
|
| 45 |
+
char dev_version[AX_VERSION_LEN];
|
| 46 |
+
unsigned char count;
|
| 47 |
+
struct
|
| 48 |
+
{
|
| 49 |
+
int temp;
|
| 50 |
+
int cpu_usage;
|
| 51 |
+
int npu_usage;
|
| 52 |
+
struct
|
| 53 |
+
{
|
| 54 |
+
int remain;
|
| 55 |
+
int total;
|
| 56 |
+
} mem_info;
|
| 57 |
+
} devices_info[AX_DEVICES_COUNT];
|
| 58 |
+
|
| 59 |
+
} devices;
|
| 60 |
+
} ax_devices_t;
|
| 61 |
+
|
| 62 |
+
int ax_dev_enum_devices(ax_devices_t *devices);
|
| 63 |
+
int ax_dev_sys_init(ax_devive_e dev_type, char devid);
|
| 64 |
+
int ax_dev_sys_deinit(ax_devive_e dev_type, char devid);
|
| 65 |
+
|
| 66 |
+
#if defined(__cplusplus)
|
| 67 |
+
}
|
| 68 |
+
#endif
|
| 69 |
+
|
| 70 |
+
#endif // __AX_DEVICES_H__
|
include/libdet.h
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#ifndef __LIBDET_H__
|
| 2 |
+
#define __LIBDET_H__
|
| 3 |
+
#include "ax_devices.h"
|
| 4 |
+
|
| 5 |
+
#if defined(__cplusplus)
|
| 6 |
+
extern "C"
|
| 7 |
+
{
|
| 8 |
+
#endif
|
| 9 |
+
#define AX_DET_MAX_OBJ_NUM 64
|
| 10 |
+
#define AX_DET_MAX_KPT_NUM 32
|
| 11 |
+
typedef enum
|
| 12 |
+
{
|
| 13 |
+
ax_det_errcode_failed = -1,
|
| 14 |
+
ax_det_errcode_success = 0,
|
| 15 |
+
} ax_det_errcode_e;
|
| 16 |
+
|
| 17 |
+
typedef enum
|
| 18 |
+
{
|
| 19 |
+
ax_det_model_type_unknown = -1,
|
| 20 |
+
ax_det_model_type_yolov5,
|
| 21 |
+
ax_det_model_type_yolov8,
|
| 22 |
+
ax_det_model_type_yolov8_pose,
|
| 23 |
+
ax_det_model_type_yolo11,
|
| 24 |
+
ax_det_model_type_yolo11_pose,
|
| 25 |
+
} ax_det_model_type_e;
|
| 26 |
+
|
| 27 |
+
typedef struct
|
| 28 |
+
{
|
| 29 |
+
int width;
|
| 30 |
+
int height;
|
| 31 |
+
int channels;
|
| 32 |
+
int stride;
|
| 33 |
+
void *data;
|
| 34 |
+
} ax_det_img_t;
|
| 35 |
+
|
| 36 |
+
typedef struct
|
| 37 |
+
{
|
| 38 |
+
struct
|
| 39 |
+
{
|
| 40 |
+
int x, y, w, h;
|
| 41 |
+
} box;
|
| 42 |
+
|
| 43 |
+
struct
|
| 44 |
+
{
|
| 45 |
+
int x, y;
|
| 46 |
+
} kpts[AX_DET_MAX_KPT_NUM];
|
| 47 |
+
int num_kpt;
|
| 48 |
+
|
| 49 |
+
float score;
|
| 50 |
+
int label;
|
| 51 |
+
} ax_det_obj_t;
|
| 52 |
+
|
| 53 |
+
typedef struct
|
| 54 |
+
{
|
| 55 |
+
ax_det_obj_t objects[AX_DET_MAX_OBJ_NUM];
|
| 56 |
+
int num_objs;
|
| 57 |
+
} ax_det_result_t;
|
| 58 |
+
|
| 59 |
+
typedef struct
|
| 60 |
+
{
|
| 61 |
+
ax_devive_e dev_type; // Device type
|
| 62 |
+
char devid; // axcl device ID
|
| 63 |
+
|
| 64 |
+
ax_det_model_type_e model_type;
|
| 65 |
+
char model_path[256];
|
| 66 |
+
|
| 67 |
+
int num_classes;
|
| 68 |
+
int num_kpt; // for face/pose
|
| 69 |
+
|
| 70 |
+
float threshold;
|
| 71 |
+
|
| 72 |
+
// int anchors[18]; // for yolov5
|
| 73 |
+
// int strides[3];
|
| 74 |
+
|
| 75 |
+
float mean[3]; // for nchw float input model, not suggestion
|
| 76 |
+
float std[3]; // for nchw float input model, not suggestion
|
| 77 |
+
} ax_det_init_t;
|
| 78 |
+
|
| 79 |
+
typedef void *ax_det_handle_t;
|
| 80 |
+
|
| 81 |
+
int ax_det_init(ax_det_init_t *init, ax_det_handle_t *handle);
|
| 82 |
+
int ax_det_deinit(ax_det_handle_t handle);
|
| 83 |
+
|
| 84 |
+
int ax_det(ax_det_handle_t handle, ax_det_img_t *img, ax_det_result_t *result);
|
| 85 |
+
|
| 86 |
+
#if defined(__cplusplus)
|
| 87 |
+
}
|
| 88 |
+
#endif
|
| 89 |
+
#endif
|
lib/aarch64/libdet.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2d4c794896dee3be2c0291a0119e5dd3dcf2fc5459a750f3148ae2473f8b5d7d
|
| 3 |
+
size 4361264
|
lib/example.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from pydet import AXDet, ModelType
|
| 3 |
+
from pyaxdev import enum_devices, sys_init, sys_deinit, AxDeviceType
|
| 4 |
+
import cv2
|
| 5 |
+
import glob
|
| 6 |
+
import argparse
|
| 7 |
+
import tqdm
|
| 8 |
+
|
| 9 |
+
if __name__ == '__main__':
|
| 10 |
+
parser = argparse.ArgumentParser()
|
| 11 |
+
parser.add_argument('--model', type=str)
|
| 12 |
+
parser.add_argument('--model_type', type=int)
|
| 13 |
+
parser.add_argument('--image', type=str)
|
| 14 |
+
parser.add_argument('--threshold', type=float, default=0.25)
|
| 15 |
+
parser.add_argument('--num_classes', type=int, default=80)
|
| 16 |
+
parser.add_argument('--num_kpt', type=int, default=0)
|
| 17 |
+
parser.add_argument('--output', type=str, default='results.jpg')
|
| 18 |
+
|
| 19 |
+
args = parser.parse_args()
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# 枚举设备
|
| 23 |
+
dev_type = AxDeviceType.unknown_device
|
| 24 |
+
dev_id = -1
|
| 25 |
+
devices_info = enum_devices()
|
| 26 |
+
print("可用设备:", devices_info)
|
| 27 |
+
if devices_info['host']['available']:
|
| 28 |
+
print("host device available")
|
| 29 |
+
sys_init(AxDeviceType.host_device, -1)
|
| 30 |
+
dev_type = AxDeviceType.host_device
|
| 31 |
+
dev_id = -1
|
| 32 |
+
elif devices_info['devices']['count'] > 0:
|
| 33 |
+
print("axcl device available, use device-0")
|
| 34 |
+
sys_init(AxDeviceType.axcl_device, 0)
|
| 35 |
+
dev_type = AxDeviceType.axcl_device
|
| 36 |
+
dev_id = 0
|
| 37 |
+
else:
|
| 38 |
+
raise Exception("No available device")
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
det = AXDet(
|
| 42 |
+
model_path=args.model,
|
| 43 |
+
model_type=args.model_type,
|
| 44 |
+
threshold=args.threshold,
|
| 45 |
+
num_classes=args.num_classes,
|
| 46 |
+
num_kpt=args.num_kpt,
|
| 47 |
+
dev_type=dev_type,
|
| 48 |
+
devid=dev_id,
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# 加载图像
|
| 52 |
+
img = cv2.imread(args.image)
|
| 53 |
+
img_infer = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 54 |
+
|
| 55 |
+
# 推理
|
| 56 |
+
results = det.detect(img_infer)
|
| 57 |
+
for obj in results:
|
| 58 |
+
box = obj.box
|
| 59 |
+
score = obj.score
|
| 60 |
+
label = obj.label
|
| 61 |
+
kpts = obj.kpts
|
| 62 |
+
|
| 63 |
+
cv2.rectangle(img, box, (0, 255, 0), 2)
|
| 64 |
+
cv2.putText(img, f"{label}: {score:.2f}", (int(box[0]), int(box[1])-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
| 65 |
+
if args.num_kpt > 0:
|
| 66 |
+
for i in range(args.num_kpt):
|
| 67 |
+
cv2.circle(img, kpts[i], 5, (0, 0, 255), -1)
|
| 68 |
+
cv2.imwrite(args.output, img)
|
| 69 |
+
del det
|
| 70 |
+
|
| 71 |
+
if devices_info['host']['available']:
|
| 72 |
+
sys_deinit(AxDeviceType.host_device, -1)
|
| 73 |
+
elif devices_info['devices']['count'] > 0:
|
| 74 |
+
sys_deinit(AxDeviceType.axcl_device, 0)
|
lib/gradio_example.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from pydet import AXDet, ModelType
|
| 4 |
+
from pyaxdev import enum_devices, sys_init, sys_deinit, AxDeviceType
|
| 5 |
+
import cv2
|
| 6 |
+
import glob
|
| 7 |
+
import argparse
|
| 8 |
+
import subprocess
|
| 9 |
+
import re
|
| 10 |
+
|
| 11 |
+
def get_all_local_ips():
|
| 12 |
+
result = subprocess.run(['ip', 'a'], capture_output=True, text=True)
|
| 13 |
+
output = result.stdout
|
| 14 |
+
|
| 15 |
+
# 匹配所有IPv4
|
| 16 |
+
ips = re.findall(r'inet (\d+\.\d+\.\d+\.\d+)', output)
|
| 17 |
+
|
| 18 |
+
# 过滤掉回环地址
|
| 19 |
+
real_ips = [ip for ip in ips if not ip.startswith('127.')]
|
| 20 |
+
|
| 21 |
+
return real_ips
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
if __name__ == '__main__':
|
| 25 |
+
parser = argparse.ArgumentParser()
|
| 26 |
+
parser.add_argument('--model', type=str)
|
| 27 |
+
parser.add_argument('--model_type', type=int)
|
| 28 |
+
parser.add_argument('--image', type=str)
|
| 29 |
+
parser.add_argument('--threshold', type=float, default=0.25)
|
| 30 |
+
parser.add_argument('--num_classes', type=int, default=80)
|
| 31 |
+
parser.add_argument('--num_kpt', type=int, default=0)
|
| 32 |
+
args = parser.parse_args()
|
| 33 |
+
|
| 34 |
+
# 初始化
|
| 35 |
+
dev_type = AxDeviceType.unknown_device
|
| 36 |
+
dev_id = -1
|
| 37 |
+
devices_info = enum_devices()
|
| 38 |
+
print("可用设备:", devices_info)
|
| 39 |
+
if devices_info['host']['available']:
|
| 40 |
+
print("host device available")
|
| 41 |
+
sys_init(AxDeviceType.host_device, -1)
|
| 42 |
+
dev_type = AxDeviceType.host_device
|
| 43 |
+
dev_id = -1
|
| 44 |
+
elif devices_info['devices']['count'] > 0:
|
| 45 |
+
print("axcl device available, use device-0")
|
| 46 |
+
sys_init(AxDeviceType.axcl_device, 0)
|
| 47 |
+
dev_type = AxDeviceType.axcl_device
|
| 48 |
+
dev_id = 0
|
| 49 |
+
else:
|
| 50 |
+
raise Exception("No available device")
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
det = AXDet(
|
| 54 |
+
model_path=args.model,
|
| 55 |
+
model_type=args.model_type,
|
| 56 |
+
threshold=args.threshold,
|
| 57 |
+
num_classes=args.num_classes,
|
| 58 |
+
num_kpt=args.num_kpt,
|
| 59 |
+
dev_type=dev_type,
|
| 60 |
+
devid=dev_id,
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
def detect_image(img):
|
| 64 |
+
results = det.detect(img)
|
| 65 |
+
print(results)
|
| 66 |
+
for obj in results:
|
| 67 |
+
box = obj.box
|
| 68 |
+
score = obj.score
|
| 69 |
+
label = obj.label
|
| 70 |
+
kpts = obj.kpts
|
| 71 |
+
|
| 72 |
+
cv2.rectangle(img, box, (0, 255, 0), 2)
|
| 73 |
+
cv2.putText(img, f"{label}: {score:.2f}", (int(box[0]), int(box[1])-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
| 74 |
+
if args.num_kpt > 0:
|
| 75 |
+
for i in range(args.num_kpt):
|
| 76 |
+
cv2.circle(img, kpts[i], 15, (0, 0, 255), -1)
|
| 77 |
+
return img
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
# Gradio界面
|
| 81 |
+
with gr.Blocks() as demo:
|
| 82 |
+
gr.Markdown("# 🔍 Det Demo")
|
| 83 |
+
|
| 84 |
+
with gr.Row():
|
| 85 |
+
input_image = gr.Image(label="输入图像")
|
| 86 |
+
output_image = gr.Image(label="输出图像")
|
| 87 |
+
det_btn = gr.Button("Detect")
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
det_btn.click(fn=detect_image, inputs=[input_image], outputs=[output_image])
|
| 91 |
+
|
| 92 |
+
# 启动
|
| 93 |
+
ips = get_all_local_ips()
|
| 94 |
+
for ip in ips:
|
| 95 |
+
print(f"* Running on local URL: http://{ip}:7860")
|
| 96 |
+
ip = "0.0.0.0"
|
| 97 |
+
demo.launch(server_name=ip, server_port=7860)
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
del det
|
| 101 |
+
|
| 102 |
+
import atexit
|
| 103 |
+
if devices_info['host']['available']:
|
| 104 |
+
atexit.register(lambda: sys_deinit(AxDeviceType.host_device, -1))
|
| 105 |
+
elif devices_info['devices']['count'] > 0:
|
| 106 |
+
atexit.register(lambda: sys_deinit(AxDeviceType.axcl_device, 0))
|
| 107 |
+
|
| 108 |
+
|
lib/pyaxdev.py
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import ctypes
|
| 2 |
+
import os
|
| 3 |
+
import platform
|
| 4 |
+
|
| 5 |
+
def check_error(code: int) -> None:
|
| 6 |
+
if code != 0:
|
| 7 |
+
raise Exception(f"API错误: {code}")
|
| 8 |
+
|
| 9 |
+
base_dir = os.path.dirname(__file__)
|
| 10 |
+
arch = platform.machine()
|
| 11 |
+
|
| 12 |
+
if arch == 'x86_64':
|
| 13 |
+
arch_dir = 'x86_64'
|
| 14 |
+
elif arch in ('aarch64', 'arm64'):
|
| 15 |
+
arch_dir = 'aarch64'
|
| 16 |
+
else:
|
| 17 |
+
raise RuntimeError(f"Unsupported architecture: {arch}")
|
| 18 |
+
so_name = 'libdet.so'
|
| 19 |
+
lib_paths = [
|
| 20 |
+
"/home/axera/libdet.axera/build/" + so_name,
|
| 21 |
+
os.path.join(base_dir, arch_dir, so_name),
|
| 22 |
+
os.path.join(base_dir, so_name)
|
| 23 |
+
]
|
| 24 |
+
|
| 25 |
+
last_error = None
|
| 26 |
+
diagnostic_shown = set()
|
| 27 |
+
|
| 28 |
+
for lib_path in lib_paths:
|
| 29 |
+
try:
|
| 30 |
+
print(f"Trying to load: {lib_path}")
|
| 31 |
+
_lib = ctypes.CDLL(lib_path)
|
| 32 |
+
print(f"✅ Successfully loaded: {lib_path}")
|
| 33 |
+
break
|
| 34 |
+
except OSError as e:
|
| 35 |
+
last_error = e
|
| 36 |
+
err_str = str(e)
|
| 37 |
+
print(f"\n❌ Failed to load: {lib_path}")
|
| 38 |
+
print(f" {err_str}")
|
| 39 |
+
|
| 40 |
+
# Only show GLIBCXX tip once
|
| 41 |
+
if "GLIBCXX" in err_str and "not found" in err_str:
|
| 42 |
+
if "missing_glibcxx" not in diagnostic_shown:
|
| 43 |
+
diagnostic_shown.add("missing_glibcxx")
|
| 44 |
+
print("🔍 Detected missing GLIBCXX version in libstdc++.so.6")
|
| 45 |
+
print("💡 This usually happens when your environment (like Conda) uses an older libstdc++")
|
| 46 |
+
print(f"👉 Try running with system libstdc++ preloaded:")
|
| 47 |
+
print(f" export LD_PRELOAD=/usr/lib/{arch_dir}-linux-gnu/libstdc++.so.6\n")
|
| 48 |
+
elif "No such file" in err_str:
|
| 49 |
+
if "file_not_found" not in diagnostic_shown:
|
| 50 |
+
diagnostic_shown.add("file_not_found")
|
| 51 |
+
print(f"🔍 File not found. Please verify that {so_name} exists and the path is correct.\n")
|
| 52 |
+
elif "wrong ELF class" in err_str:
|
| 53 |
+
if "elf_mismatch" not in diagnostic_shown:
|
| 54 |
+
diagnostic_shown.add("elf_mismatch")
|
| 55 |
+
print("🔍 ELF class mismatch — likely due to architecture conflict (e.g., loading x86_64 .so on aarch64).")
|
| 56 |
+
print(f"👉 Run `file {lib_path}` to verify the binary architecture.\n")
|
| 57 |
+
else:
|
| 58 |
+
if "generic_error" not in diagnostic_shown:
|
| 59 |
+
diagnostic_shown.add("generic_error")
|
| 60 |
+
print("📎 Tip: Use `ldd` to inspect missing dependencies:")
|
| 61 |
+
print(f" ldd {lib_path}\n")
|
| 62 |
+
else:
|
| 63 |
+
raise RuntimeError(f"\n❗ Failed to load {so_name}.\nLast error:\n{last_error}")
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
# 定义枚举类型
|
| 67 |
+
class AxDeviceType(ctypes.c_int):
|
| 68 |
+
unknown_device = 0
|
| 69 |
+
host_device = 1
|
| 70 |
+
axcl_device = 2
|
| 71 |
+
|
| 72 |
+
# 定义结构体
|
| 73 |
+
class AxMemInfo(ctypes.Structure):
|
| 74 |
+
_fields_ = [
|
| 75 |
+
('remain', ctypes.c_int),
|
| 76 |
+
('total', ctypes.c_int)
|
| 77 |
+
]
|
| 78 |
+
|
| 79 |
+
class AxHostInfo(ctypes.Structure):
|
| 80 |
+
_fields_ = [
|
| 81 |
+
('available', ctypes.c_char),
|
| 82 |
+
('version', ctypes.c_char * 32),
|
| 83 |
+
('mem_info', AxMemInfo)
|
| 84 |
+
]
|
| 85 |
+
|
| 86 |
+
class AxDeviceInfo(ctypes.Structure):
|
| 87 |
+
_fields_ = [
|
| 88 |
+
('temp', ctypes.c_int),
|
| 89 |
+
('cpu_usage', ctypes.c_int),
|
| 90 |
+
('npu_usage', ctypes.c_int),
|
| 91 |
+
('mem_info', AxMemInfo)
|
| 92 |
+
]
|
| 93 |
+
|
| 94 |
+
class AxDevices(ctypes.Structure):
|
| 95 |
+
_fields_ = [
|
| 96 |
+
('host', AxHostInfo),
|
| 97 |
+
('host_version', ctypes.c_char * 32),
|
| 98 |
+
('dev_version', ctypes.c_char * 32),
|
| 99 |
+
('count', ctypes.c_ubyte),
|
| 100 |
+
('devices_info', AxDeviceInfo * 16)
|
| 101 |
+
]
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
_lib.ax_dev_enum_devices.argtypes = [ctypes.POINTER(AxDevices)]
|
| 105 |
+
_lib.ax_dev_enum_devices.restype = ctypes.c_int
|
| 106 |
+
|
| 107 |
+
_lib.ax_dev_sys_init.argtypes = [AxDeviceType, ctypes.c_char]
|
| 108 |
+
_lib.ax_dev_sys_init.restype = ctypes.c_int
|
| 109 |
+
|
| 110 |
+
_lib.ax_dev_sys_deinit.argtypes = [AxDeviceType, ctypes.c_char]
|
| 111 |
+
_lib.ax_dev_sys_deinit.restype = ctypes.c_int
|
| 112 |
+
|
| 113 |
+
def enum_devices() -> dict:
|
| 114 |
+
devices = AxDevices()
|
| 115 |
+
check_error(_lib.ax_dev_enum_devices(ctypes.byref(devices)))
|
| 116 |
+
|
| 117 |
+
return {
|
| 118 |
+
'host': {
|
| 119 |
+
'available': bool(devices.host.available[0]),
|
| 120 |
+
'version': devices.host.version.decode('utf-8'),
|
| 121 |
+
'mem_info': {
|
| 122 |
+
'remain': devices.host.mem_info.remain,
|
| 123 |
+
'total': devices.host.mem_info.total
|
| 124 |
+
}
|
| 125 |
+
},
|
| 126 |
+
'devices': {
|
| 127 |
+
'host_version': devices.host_version.decode('utf-8'),
|
| 128 |
+
'dev_version': devices.dev_version.decode('utf-8'),
|
| 129 |
+
'count': devices.count,
|
| 130 |
+
'devices_info': [{
|
| 131 |
+
'temp': dev.temp,
|
| 132 |
+
'cpu_usage': dev.cpu_usage,
|
| 133 |
+
'npu_usage': dev.npu_usage,
|
| 134 |
+
'mem_info': {
|
| 135 |
+
'remain': dev.mem_info.remain,
|
| 136 |
+
'total': dev.mem_info.total
|
| 137 |
+
}
|
| 138 |
+
} for dev in devices.devices_info[:devices.count]]
|
| 139 |
+
}
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def sys_init(dev_type: AxDeviceType = AxDeviceType.axcl_device, devid: int = 0) -> None:
|
| 144 |
+
check_error(_lib.ax_dev_sys_init(dev_type, devid))
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
def sys_deinit(dev_type: AxDeviceType = AxDeviceType.axcl_device, devid: int = 0) -> None:
|
| 148 |
+
check_error(_lib.ax_dev_sys_deinit(dev_type, devid))
|
lib/pydet.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import ctypes
|
| 2 |
+
import os
|
| 3 |
+
from typing import List, Tuple
|
| 4 |
+
import numpy as np
|
| 5 |
+
import platform
|
| 6 |
+
from pyaxdev import _lib, AxDeviceType, AxDevices, check_error
|
| 7 |
+
|
| 8 |
+
class ModelType(ctypes.c_int):
|
| 9 |
+
ax_det_model_type_unknown = -1
|
| 10 |
+
ax_det_model_type_yolov5 = 0
|
| 11 |
+
ax_det_model_type_yolov8 = 1
|
| 12 |
+
ax_det_model_type_yolov8_pose = 2
|
| 13 |
+
ax_det_model_type_yolo11 = 3
|
| 14 |
+
ax_det_model_type_yolo11_pose = 4
|
| 15 |
+
|
| 16 |
+
class DetInit(ctypes.Structure):
|
| 17 |
+
_fields_ = [
|
| 18 |
+
('dev_type', AxDeviceType),
|
| 19 |
+
('devid', ctypes.c_char),
|
| 20 |
+
('model_type', ModelType),
|
| 21 |
+
('model_path', ctypes.c_char * 256),
|
| 22 |
+
('num_classes', ctypes.c_int),
|
| 23 |
+
('num_kpt', ctypes.c_int),
|
| 24 |
+
('threshold', ctypes.c_float),
|
| 25 |
+
('mean', ctypes.c_float * 3),
|
| 26 |
+
('std', ctypes.c_float * 3),
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
class DetImage(ctypes.Structure):
|
| 30 |
+
_fields_ = [
|
| 31 |
+
('width', ctypes.c_int),
|
| 32 |
+
('height', ctypes.c_int),
|
| 33 |
+
('channels', ctypes.c_int),
|
| 34 |
+
('stride', ctypes.c_int),
|
| 35 |
+
('data', ctypes.POINTER(ctypes.c_ubyte)),
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
class ObjectItem(ctypes.Structure):
|
| 40 |
+
_fields_ = [
|
| 41 |
+
('box', ctypes.c_int * 4),
|
| 42 |
+
('kpts', ctypes.c_int * 2 * 32),
|
| 43 |
+
('num_kpt', ctypes.c_int),
|
| 44 |
+
('score', ctypes.c_float),
|
| 45 |
+
('label', ctypes.c_int),
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
class ObjectResult(ctypes.Structure):
|
| 49 |
+
_fields_ = [
|
| 50 |
+
('objects', ObjectItem * 64),
|
| 51 |
+
('num_objs', ctypes.c_int),
|
| 52 |
+
]
|
| 53 |
+
|
| 54 |
+
_lib.ax_det_init.argtypes = [ctypes.POINTER(DetInit), ctypes.POINTER(ctypes.c_void_p)]
|
| 55 |
+
_lib.ax_det_init.restype = ctypes.c_int
|
| 56 |
+
|
| 57 |
+
_lib.ax_det_deinit.argtypes = [ctypes.c_void_p]
|
| 58 |
+
_lib.ax_det_deinit.restype = ctypes.c_int
|
| 59 |
+
|
| 60 |
+
_lib.ax_det.argtypes = [ctypes.c_void_p, ctypes.POINTER(DetImage), ctypes.POINTER(ObjectResult)]
|
| 61 |
+
_lib.ax_det.restype = ctypes.c_int
|
| 62 |
+
|
| 63 |
+
class Object:
|
| 64 |
+
def __init__(self, box: List[int], score: float, label: int, kpts: List[int] = []):
|
| 65 |
+
self.box = box
|
| 66 |
+
self.score = score
|
| 67 |
+
self.label = label
|
| 68 |
+
self.kpts = kpts
|
| 69 |
+
|
| 70 |
+
def __repr__(self):
|
| 71 |
+
return f"Object(box={self.box}, score={self.score:.2f}, label={self.label}, kpts={self.kpts})"
|
| 72 |
+
|
| 73 |
+
class AXDet:
|
| 74 |
+
def __init__(self, model_path: str, model_type: ModelType, num_classes: int,
|
| 75 |
+
num_kpt: int = 0,
|
| 76 |
+
threshold: float= 0.25,
|
| 77 |
+
mean: List[float] = [0,0,0], std: List[float] = [1,1,1],
|
| 78 |
+
dev_type: AxDeviceType = AxDeviceType.axcl_device,
|
| 79 |
+
devid: int = 0):
|
| 80 |
+
self.handle = None
|
| 81 |
+
self.init_info = DetInit()
|
| 82 |
+
|
| 83 |
+
# 设置初始化参数
|
| 84 |
+
self.init_info.dev_type = dev_type
|
| 85 |
+
self.init_info.devid = devid
|
| 86 |
+
|
| 87 |
+
# 设置路径
|
| 88 |
+
self.init_info.model_type = model_type
|
| 89 |
+
self.init_info.model_path = model_path.encode('utf-8')
|
| 90 |
+
self.init_info.num_classes = num_classes
|
| 91 |
+
self.init_info.num_kpt = num_kpt
|
| 92 |
+
self.init_info.threshold = threshold
|
| 93 |
+
for i in range(3):
|
| 94 |
+
self.init_info.mean[i] = mean[i]
|
| 95 |
+
self.init_info.std[i] = std[i]
|
| 96 |
+
|
| 97 |
+
# 创建CLIP实例
|
| 98 |
+
handle = ctypes.c_void_p()
|
| 99 |
+
check_error(_lib.ax_det_init(ctypes.byref(self.init_info), ctypes.byref(handle)))
|
| 100 |
+
self.handle = handle
|
| 101 |
+
|
| 102 |
+
def __del__(self):
|
| 103 |
+
if self.handle:
|
| 104 |
+
_lib.ax_det_deinit(self.handle)
|
| 105 |
+
|
| 106 |
+
def detect(self, image_data: np.ndarray):
|
| 107 |
+
|
| 108 |
+
image = DetImage()
|
| 109 |
+
image.data = ctypes.cast(image_data.ctypes.data, ctypes.POINTER(ctypes.c_ubyte))
|
| 110 |
+
image.width = image_data.shape[1]
|
| 111 |
+
image.height = image_data.shape[0]
|
| 112 |
+
image.channels = image_data.shape[2]
|
| 113 |
+
image.stride = image_data.shape[1] * image_data.shape[2]
|
| 114 |
+
result = ObjectResult()
|
| 115 |
+
check_error(_lib.ax_det(self.handle, ctypes.byref(image), ctypes.byref(result)))
|
| 116 |
+
objects = []
|
| 117 |
+
for i in range(result.num_objs):
|
| 118 |
+
_obj = result.objects[i]
|
| 119 |
+
|
| 120 |
+
obj = Object(
|
| 121 |
+
box=[_obj.box[0], _obj.box[1], _obj.box[2], _obj.box[3]],
|
| 122 |
+
score=_obj.score,
|
| 123 |
+
label=_obj.label,
|
| 124 |
+
kpts=[(_obj.kpts[j][0], _obj.kpts[j][1]) for j in range(_obj.num_kpt)],
|
| 125 |
+
)
|
| 126 |
+
objects.append(obj)
|
| 127 |
+
return objects
|
| 128 |
+
|
lib/requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
opencv-python
|
| 3 |
+
Pillow
|
lib/result_11pose.jpg
ADDED
|
Git LFS Details
|
lib/result_v8.jpg
ADDED
|
Git LFS Details
|
lib/x86_64/libdet.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:996c6be093d56653c9a380f53e224a7415f3162bf701ea993c2d6cfc678a8219
|
| 3 |
+
size 5757008
|