Skip to content

Commit 3fa1764

Browse files
author
Gin
committed
add button to compute embedding
1 parent f0341fd commit 3fa1764

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

SerialPrograms/Source/ML/DataLabeling/SegmentAnythingModel.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <iostream>
1212
#include <onnxruntime_cxx_api.h>
1313
#include <opencv2/imgcodecs.hpp>
14+
#include <opencv2/imgproc.hpp>
1415
#include "3rdParty/ONNX/OnnxToolsPA.h"
1516
#include "SegmentAnythingModel.h"
1617

@@ -248,7 +249,7 @@ bool load_image_embedding(const std::string& image_filepath, std::vector<float>&
248249
}
249250

250251

251-
void compute_embeddings_for_folder(const std::string& image_folder_path){
252+
void compute_embeddings_for_folder(const std::string& embedding_model_path, const std::string& image_folder_path){
252253
QDir image_dir(image_folder_path.c_str());
253254
if (!image_dir.exists()){
254255
std::cerr << "Error: input image folder path " << image_folder_path << " does not exist." << std::endl;
@@ -261,6 +262,22 @@ void compute_embeddings_for_folder(const std::string& image_folder_path){
261262
all_image_paths.emplace_back(image_file_iter.next().toStdString());
262263
}
263264
std::cout << "Found " << all_image_paths.size() << " images recursively in folder " << image_folder_path << std::endl;
265+
266+
SAMEmbedderSession embedding_session(embedding_model_path);
267+
std::vector<float> output_image_embedding;
268+
for (size_t i = 0; i < all_image_paths.size(); i++){
269+
const auto& image_path = all_image_paths[i];
270+
std::cout << (i+1) << "/" << all_image_paths.size();
271+
std::cout << ": computing embedding for " << image_path << "..." << std::endl;
272+
cv::Mat image_bgr = cv::imread(image_path);
273+
cv::Mat image;
274+
cv::cvtColor(image_bgr, image, cv::COLOR_BGRA2RGB);
275+
output_image_embedding.clear();
276+
embedding_session.run(image, output_image_embedding);
277+
save_image_embedding_to_disk(image_path, output_image_embedding);
278+
}
279+
280+
264281
}
265282

266283
}

SerialPrograms/Source/ML/DataLabeling/SegmentAnythingModel.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ bool load_image_embedding(const std::string& image_filepath, std::vector<float>&
2929
// Save the image embedding as a file with path <image_filepath>.embedding.
3030
void save_image_embedding_to_disk(const std::string& image_filepath, const std::vector<float>& embedding);
3131

32-
// Compute embeddings for all images in a folder.
32+
// Compute embeddings for all images in a folder. Only support .png, .jpg and .jpeg filename extensions so far.
3333
// This can be very slow!
34-
void compute_embeddings_for_folder(const std::string& image_folder_path);
34+
void compute_embeddings_for_folder(const std::string& embedding_model_path, const std::string& image_folder_path);
3535

3636

3737
class SAMEmbedderSession{

SerialPrograms/Source/ML/Programs/ML_LabelImages.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,9 @@ void LabelImages::compute_mask(VideoOverlaySet& overlay_set){
410410
}
411411

412412
void LabelImages::compute_embeddings_for_folder(const std::string& image_folder_path){
413-
ML::compute_embeddings_for_folder(image_folder_path);
413+
std::string embedding_model_path = RESOURCE_PATH() + "ML/sam_embedder_cpu.onnx";
414+
std::cout << "Use SAM Embedding model " << embedding_model_path << std::endl;
415+
ML::compute_embeddings_for_folder(embedding_model_path, image_folder_path);
414416
}
415417

416418

0 commit comments

Comments
 (0)