Skip to content

Commit f441a8f

Browse files
author
Gin
committed
save annotation automatically
1 parent 41b0e24 commit f441a8f

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

SerialPrograms/Source/ML/DataLabeling/SegmentAnythingModel.cpp

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
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
253253
void 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

SerialPrograms/Source/ML/Programs/ML_LabelImages.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ JsonValue LabelImages::to_json() const{
227227
JsonObject obj = std::move(*m_options.to_json().to_object());
228228
obj["ImageSetup"] = m_display_option.to_json();
229229

230+
save_annotation_to_file();
231+
return obj;
232+
}
233+
234+
void LabelImages::save_annotation_to_file() const{
230235
// m_annotation_file_path
231236
if (m_annotation_file_path.size() > 0 && !m_fail_to_load_annotation_file){
232237
JsonArray anno_json_arr;
@@ -236,7 +241,6 @@ JsonValue LabelImages::to_json() const{
236241
cout << "Saving annotation to " << m_annotation_file_path << endl;
237242
anno_json_arr.dump(m_annotation_file_path);
238243
}
239-
return obj;
240244
}
241245

242246
void LabelImages::clear_for_new_image(){
@@ -265,6 +269,7 @@ void LabelImages::load_image_related_data(const std::string& image_path, size_t
265269
if (!embedding_loaded){
266270
return; // no embedding, then no way for us to annotate
267271
}
272+
268273
// see if we can load the previously created labels
269274
const std::string anno_filename = std::filesystem::path(image_path).filename().replace_extension(".json").string();
270275

@@ -518,6 +523,7 @@ void LabelImages_Widget::on_config_value_changed(void* object){
518523
void LabelImages_Widget::post_startup(VideoSource* source){
519524
const std::string& image_path = m_display_session.option().m_image_path;
520525

526+
m_program.save_annotation_to_file(); // save the current annotation file
521527
clear_for_new_image();
522528
if (image_path.size() == 0){
523529
m_embedding_info_label->setText("");

SerialPrograms/Source/ML/Programs/ML_LabelImages.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ class LabelImages : public PanelInstance{
7373
virtual void from_json(const JsonValue& json) override;
7474
virtual JsonValue to_json() const override;
7575

76+
void save_annotation_to_file() const;
77+
7678
// called after loading a new image, clean up all internal data
7779
void clear_for_new_image();
7880

0 commit comments

Comments
 (0)