Debian Codegraph @

src/pycolmap/pipeline/extract_features.cc

raw
1 #include "colmap/controllers/feature_extraction.h"
2 #include "colmap/controllers/image_reader.h"
3 #include "colmap/exe/feature.h"
4 #include "colmap/exe/sfm.h"
5 #include "colmap/feature/sift.h"
6 #include "colmap/util/file.h"
7 #include "colmap/util/logging.h"
8 #include "colmap/util/misc.h"
9
10 #include "pycolmap/helpers.h"
11 #include "pycolmap/pybind11_extension.h"
12 #include "pycolmap/utils.h"
13
14 #include <filesystem>
15 #include <memory>
16
17 #include <pybind11/pybind11.h>
18 #include <pybind11/stl.h>
19
20 using namespace colmap;
21 using namespace pybind11::literals;
22 namespace py = pybind11;
23
24 void ExtractFeatures(
25 const std::filesystem::path& database_path,
26 const std::filesystem::path& image_path,
27 const std::vector<std::string>& image_names,
28 const CameraMode camera_mode,
29 ImageReaderOptions reader_options,
30 FeatureExtractionOptions extraction_options,
31 const Device device,
32 const std::shared_ptr<CancellationToken>& cancellation_token) {
33 THROW_CHECK_DIR_EXISTS(image_path);
34 extraction_options.use_gpu = IsGPU(device);
35 THROW_CHECK(extraction_options.Check());
36
37 UpdateImageReaderOptionsFromCameraMode(reader_options, camera_mode);
38 reader_options.image_path = image_path;
39 reader_options.as_rgb = extraction_options.RequiresRGB();
40
41 if (!image_names.empty()) {
42 reader_options.image_names = image_names;
43 }
44
45 THROW_CHECK(ExistsCameraModelWithName(reader_options.camera_model));
46 THROW_CHECK(VerifyCameraParams(reader_options.camera_model,
47 reader_options.camera_params))
48 << "Invalid camera parameters.";
49
50 py::gil_scoped_release release;
51 std::unique_ptr<Thread> extractor = CreateFeatureExtractorController(
52 database_path, reader_options, extraction_options);
53 extractor->Start();
54 PyWait(extractor.get(), cancellation_token);
55 }
56
57 void BindExtractFeatures(py::module& m) {
58 m.def(
59 "extract_features",
60 &ExtractFeatures,
61 "database_path"_a,
62 "image_path"_a,
63 "image_names"_a = std::vector<std::string>(),
64 "camera_mode"_a = CameraMode::AUTO,
65 py::arg_v("reader_options", ImageReaderOptions(), "ImageReaderOptions()"),
66 py::arg_v("extraction_options",
67 FeatureExtractionOptions(),
68 "FeatureExtractionOptions()"),
69 "device"_a = Device::AUTO,
70 "cancellation_token"_a = py::none(),
71 "Extract SIFT Features and write them to database");
72 }