| 1 |
#include "colmap/retrieval/visual_index.h" |
| 2 |
|
| 3 |
#include "colmap/feature/types.h" |
| 4 |
|
| 5 |
#include "pycolmap/helpers.h" |
| 6 |
#include "pycolmap/pybind11_extension.h" |
| 7 |
#include "pycolmap/utils.h" |
| 8 |
|
| 9 |
#include <filesystem> |
| 10 |
#include <memory> |
| 11 |
|
| 12 |
#include <pybind11/pybind11.h> |
| 13 |
#include <pybind11/stl.h> |
| 14 |
|
| 15 |
using namespace colmap; |
| 16 |
using VisualIndex = retrieval::VisualIndex; |
| 17 |
using ImageScore = retrieval::ImageScore; |
| 18 |
using namespace pybind11::literals; |
| 19 |
namespace py = pybind11; |
| 20 |
|
| 21 |
namespace { |
| 22 |
|
| 23 |
class PyVisualIndexImpl : public VisualIndex, py::trampoline_self_life_support { |
| 24 |
public: |
| 25 |
size_t NumVisualWords() const override { |
| 26 |
PYBIND11_OVERRIDE_PURE(size_t, VisualIndex, NumVisualWords); |
| 27 |
} |
| 28 |
|
| 29 |
size_t NumImages() const override { |
| 30 |
PYBIND11_OVERRIDE_PURE(size_t, VisualIndex, NumImages); |
| 31 |
} |
| 32 |
|
| 33 |
int DescDim() const override { |
| 34 |
PYBIND11_OVERRIDE_PURE(int, VisualIndex, DescDim); |
| 35 |
} |
| 36 |
|
| 37 |
int EmbeddingDim() const override { |
| 38 |
PYBIND11_OVERRIDE_PURE(int, VisualIndex, EmbeddingDim); |
| 39 |
} |
| 40 |
|
| 41 |
FeatureExtractorType FeatureType() const override { |
| 42 |
PYBIND11_OVERRIDE_PURE(FeatureExtractorType, VisualIndex, FeatureType); |
| 43 |
} |
| 44 |
|
| 45 |
void Add(const IndexOptions& options, |
| 46 |
int image_id, |
| 47 |
const FeatureKeypoints& keypoints, |
| 48 |
const FeatureDescriptorsFloat& descriptors) override { |
| 49 |
PYBIND11_OVERRIDE_PURE( |
| 50 |
void, VisualIndex, Add, options, image_id, keypoints, descriptors); |
| 51 |
} |
| 52 |
|
| 53 |
bool IsImageIndexed(int image_id) const override { |
| 54 |
PYBIND11_OVERRIDE_PURE(bool, VisualIndex, IsImageIndexed, image_id); |
| 55 |
} |
| 56 |
|
| 57 |
void Query(const QueryOptions& options, |
| 58 |
const FeatureDescriptorsFloat& descriptors, |
| 59 |
std::vector<ImageScore>* image_scores) const override { |
| 60 |
PYBIND11_OVERRIDE_PURE( |
| 61 |
void, VisualIndex, Query, options, descriptors, image_scores); |
| 62 |
} |
| 63 |
|
| 64 |
void Query(const QueryOptions& options, |
| 65 |
const FeatureKeypoints& keypoints, |
| 66 |
const FeatureDescriptorsFloat& descriptors, |
| 67 |
std::vector<ImageScore>* image_scores) const override { |
| 68 |
PYBIND11_OVERRIDE_PURE( |
| 69 |
void, VisualIndex, Query, keypoints, descriptors, image_scores); |
| 70 |
} |
| 71 |
|
| 72 |
void Prepare() override { |
| 73 |
PYBIND11_OVERRIDE_PURE(void, VisualIndex, Prepare); |
| 74 |
} |
| 75 |
|
| 76 |
void Build(const BuildOptions& options, |
| 77 |
const FeatureDescriptorsFloat& descriptors) override { |
| 78 |
PYBIND11_OVERRIDE_PURE(void, VisualIndex, Build, options, descriptors); |
| 79 |
} |
| 80 |
|
| 81 |
void Write(const std::filesystem::path& path) const override { |
| 82 |
PYBIND11_OVERRIDE_PURE(void, VisualIndex, Read); |
| 83 |
} |
| 84 |
|
| 85 |
protected: |
| 86 |
void ReadFromFaiss(const std::filesystem::path& path, |
| 87 |
long offset, |
| 88 |
FeatureExtractorType feature_type) override { |
| 89 |
PYBIND11_OVERRIDE_PURE( |
| 90 |
void, VisualIndex, ReadFromFaiss, path, offset, feature_type); |
| 91 |
} |
| 92 |
}; |
| 93 |
|
| 94 |
} // namespace |
| 95 |
|
| 96 |
void BindVisualIndex(py::module& m) { |
| 97 |
auto PyImageScore = py::classh<ImageScore>(m, "ImageScore") |
| 98 |
.def(py::init<>()) |
| 99 |
.def_readonly("image_id", &ImageScore::image_id) |
| 100 |
.def_readonly("score", &ImageScore::score); |
| 101 |
MakeDataclass(PyImageScore); |
| 102 |
|
| 103 |
py::classh<VisualIndex, PyVisualIndexImpl> PyVisualIndex(m, "VisualIndex"); |
| 104 |
|
| 105 |
auto PyIndexOptions = |
| 106 |
py::classh<VisualIndex::IndexOptions>(PyVisualIndex, "IndexOptions") |
| 107 |
.def(py::init<>()) |
| 108 |
.def_readwrite("num_neighbors", |
| 109 |
&VisualIndex::IndexOptions::num_neighbors) |
| 110 |
.def_readwrite("num_checks", &VisualIndex::IndexOptions::num_checks) |
| 111 |
.def_readwrite("num_threads", |
| 112 |
&VisualIndex::IndexOptions::num_threads); |
| 113 |
MakeDataclass(PyIndexOptions); |
| 114 |
|
| 115 |
auto PyQueryOptions = |
| 116 |
py::classh<VisualIndex::QueryOptions>(PyVisualIndex, "QueryOptions") |
| 117 |
.def(py::init<>()) |
| 118 |
.def_readwrite("max_num_images", |
| 119 |
&VisualIndex::QueryOptions::max_num_images) |
| 120 |
.def_readwrite("num_neighbors", |
| 121 |
&VisualIndex::QueryOptions::num_neighbors) |
| 122 |
.def_readwrite("num_checks", &VisualIndex::QueryOptions::num_checks) |
| 123 |
.def_readwrite("num_threads", &VisualIndex::QueryOptions::num_threads) |
| 124 |
.def_readwrite( |
| 125 |
"num_images_after_verification", |
| 126 |
&VisualIndex::QueryOptions::num_images_after_verification); |
| 127 |
MakeDataclass(PyQueryOptions); |
| 128 |
|
| 129 |
auto PyBuildOptions = |
| 130 |
py::classh<VisualIndex::BuildOptions>(PyVisualIndex, "BuildOptions") |
| 131 |
.def(py::init<>()) |
| 132 |
.def_readwrite("num_visual_words", |
| 133 |
&VisualIndex::BuildOptions::num_visual_words) |
| 134 |
.def_readwrite("num_iterations", |
| 135 |
&VisualIndex::BuildOptions::num_iterations) |
| 136 |
.def_readwrite("num_rounds", &VisualIndex::BuildOptions::num_rounds) |
| 137 |
.def_readwrite("num_checks", &VisualIndex::BuildOptions::num_checks) |
| 138 |
.def_readwrite("num_threads", |
| 139 |
&VisualIndex::BuildOptions::num_threads); |
| 140 |
MakeDataclass(PyBuildOptions); |
| 141 |
|
| 142 |
PyVisualIndex.def(py::init<>()) |
| 143 |
.def_static("create", &VisualIndex::Create) |
| 144 |
.def("add", |
| 145 |
static_cast<void (VisualIndex::*)( |
| 146 |
const typename VisualIndex::IndexOptions&, |
| 147 |
int, |
| 148 |
const FeatureKeypoints&, |
| 149 |
const FeatureDescriptorsFloat&)>(&VisualIndex::Add), |
| 150 |
py::call_guard<py::gil_scoped_release>()) |
| 151 |
.def("is_image_indexed", &VisualIndex::IsImageIndexed) |
| 152 |
.def("num_visual_words", &VisualIndex::NumVisualWords) |
| 153 |
.def("num_images", &VisualIndex::NumImages) |
| 154 |
.def("desc_dim", &VisualIndex::DescDim) |
| 155 |
.def("embedding_dim", &VisualIndex::EmbeddingDim) |
| 156 |
.def("feature_type", &VisualIndex::FeatureType) |
| 157 |
.def("query", |
| 158 |
static_cast<void (VisualIndex::*)( |
| 159 |
const typename VisualIndex::QueryOptions&, |
| 160 |
const FeatureDescriptorsFloat&, |
| 161 |
std::vector<ImageScore>*) const>(&VisualIndex::Query), |
| 162 |
py::call_guard<py::gil_scoped_release>()) |
| 163 |
.def("query", |
| 164 |
static_cast<void (VisualIndex::*)( |
| 165 |
const typename VisualIndex::QueryOptions&, |
| 166 |
const FeatureKeypoints&, |
| 167 |
const FeatureDescriptorsFloat&, |
| 168 |
std::vector<ImageScore>*) const>(&VisualIndex::Query), |
| 169 |
py::call_guard<py::gil_scoped_release>()) |
| 170 |
.def("prepare", |
| 171 |
&VisualIndex::Prepare, |
| 172 |
py::call_guard<py::gil_scoped_release>()) |
| 173 |
.def("build", |
| 174 |
&VisualIndex::Build, |
| 175 |
py::call_guard<py::gil_scoped_release>()) |
| 176 |
.def_static( |
| 177 |
"read", &VisualIndex::Read, py::call_guard<py::gil_scoped_release>()) |
| 178 |
.def("write", |
| 179 |
&VisualIndex::Write, |
| 180 |
py::call_guard<py::gil_scoped_release>()) |
| 181 |
.def("__repr__", [](const VisualIndex& self) { |
| 182 |
std::ostringstream ss; |
| 183 |
ss << self; |
| 184 |
return ss.str(); |
| 185 |
}); |
| 186 |
} |