99#include < QDirIterator>
1010#include < fstream>
1111#include < iostream>
12+ #include < QMessageBox>
1213#include < onnxruntime_cxx_api.h>
1314#include < opencv2/imgcodecs.hpp>
1415#include < opencv2/imgproc.hpp>
@@ -249,7 +250,6 @@ bool load_image_embedding(const std::string& image_filepath, std::vector<float>&
249250}
250251
251252
252- // TODO: skip already computed embedding
253253void compute_embeddings_for_folder (const std::string& embedding_model_path, const std::string& image_folder_path){
254254 QDir image_dir (image_folder_path.c_str ());
255255 if (!image_dir.exists ()){
@@ -268,13 +268,39 @@ void compute_embeddings_for_folder(const std::string& embedding_model_path, cons
268268 std::vector<float > output_image_embedding;
269269 for (size_t i = 0 ; i < all_image_paths.size (); i++){
270270 const auto & image_path = all_image_paths[i];
271- std::cout << (i+1 ) << " /" << all_image_paths.size ();
272- std::cout << " : computing embedding for " << image_path << " ..." << std::endl;
271+ std::cout << (i+1 ) << " /" << all_image_paths.size () << " : " ;
272+ const std::string embedding_path = image_path + " .embedding" ;
273+ if (std::filesystem::exists (embedding_path)){
274+ std::cout << " skip already computed embedding " << embedding_path << " ." << std::endl;
275+ continue ;
276+ }
277+ std::cout << " computing embedding for " << image_path << " ..." << std::endl;
273278 cv::Mat image_bgr = cv::imread (image_path);
279+ if (image_bgr.empty ()){
280+ std::cerr << " Error: image empty. Probably the file is not an image?" << std::endl;
281+ QMessageBox box;
282+ box.warning (nullptr , " Unable To Open Image" ,
283+ QString::fromStdString (" Cannot open image file " + image_path + " . Probably not an actual image?" ));
284+ return ;
285+ }
274286 cv::Mat image;
275- cv::cvtColor (image_bgr, image, cv::COLOR_BGRA2RGB);
287+ if (image_bgr.channels () == 4 ){
288+ cv::cvtColor (image_bgr, image, cv::COLOR_BGRA2RGB);
289+ } else if (image_bgr.channels () == 3 ){
290+ cv::cvtColor (image_bgr, image, cv::COLOR_BGR2RGB);
291+ } else {
292+ std::cerr << " Error: wrong image channels. Only work with RGB or RGBA images." << std::endl;
293+ QMessageBox box;
294+ box.warning (nullptr , " Wrong Image Channels" ,
295+ QString::fromStdString (" Image has " + std::to_string (image_bgr.channels ()) + " channels. Only support 3 or 4 channels." ));
296+ return ;
297+ }
298+
299+ cv::Mat resized_mat; // resize to the shape for the ML model input
300+ cv::resize (image, resized_mat, cv::Size (SAM_EMBEDDER_INPUT_IMAGE_WIDTH, SAM_EMBEDDER_INPUT_IMAGE_HEIGHT));
301+
276302 output_image_embedding.clear ();
277- embedding_session.run (image , output_image_embedding);
303+ embedding_session.run (resized_mat , output_image_embedding);
278304 save_image_embedding_to_disk (image_path, output_image_embedding);
279305 }
280306
0 commit comments