Skip to content

Commit ab557e4

Browse files
committed
added jpg save to export annotation images
1 parent 82efc7d commit ab557e4

File tree

7 files changed

+69
-18
lines changed

7 files changed

+69
-18
lines changed

cmake/CompileOptions.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ set(DEFAULT_INCLUDE_DIRECTORIES)
4949
# Libraries
5050
#
5151

52-
set(DEFAULT_LIBRARIES ${Boost_LIBRARIES} ${OpenCV_LIBS} ${Poco_LIBRARIES})
52+
set(DEFAULT_LIBRARIES stdc++fs ${Boost_LIBRARIES} ${OpenCV_LIBS} ${Poco_LIBRARIES})
5353

5454
endif( OPTION_CONAN_PACKAGES )
5555
#

source/annotatorlib/include/AnnotatorLib/Export/AbstractExport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class ANNOTATORLIB_API AbstractExport {
2929

3030
virtual bool equals(std::shared_ptr<AbstractExport> other) = 0;
3131

32+
virtual void doExport() = 0;
33+
3234
virtual ~AbstractExport() {}
3335
};
3436
/************************************************************/

source/annotatorlib/include/AnnotatorLib/Export/ExportAnnotationImages.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <AnnotatorLib/AnnotatorLibDatastructs.h>
1010
#include <AnnotatorLib/Export/AbstractExport.h>
11+
#include <AnnotatorLib/Project.h>
1112
#include <AnnotatorLib/annotatorlib_api.h>
1213

1314
#include <string>
@@ -16,14 +17,18 @@
1617
#include <boost/filesystem.hpp>
1718

1819
namespace AnnotatorLib {
20+
21+
class Class;
22+
class Object;
23+
1924
namespace Export {
2025
/************************************************************/
2126
/**
2227
*
2328
*/
2429
class ANNOTATORLIB_API ExportAnnotationImages : public AbstractExport {
2530
public:
26-
ExportAnnotationImages(std::string path);
31+
ExportAnnotationImages(std::shared_ptr<Project> project, std::string path);
2732

2833
/**
2934
*
@@ -33,15 +38,16 @@ class ANNOTATORLIB_API ExportAnnotationImages : public AbstractExport {
3338

3439
virtual std::string getPath() override;
3540

41+
virtual void doExport() override;
42+
3643
virtual bool equals(std::shared_ptr<AbstractExport> other) override;
3744

3845
protected:
46+
void exportClass(std::shared_ptr<Class> theClass);
47+
void exportObject(std::shared_ptr<Object> object);
3948

49+
std::shared_ptr<Project> project;
4050
std::string path;
41-
42-
long position = 0;
43-
std::vector<boost::filesystem::path> images;
44-
std::vector<boost::filesystem::path>::const_iterator imgIter;
4551
};
4652
/************************************************************/
4753
/* External declarations (package visibility) */

source/annotatorlib/include/AnnotatorLib/Export/ExportFactory.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <AnnotatorLib/AnnotatorLibDatastructs.h>
1414
#include <AnnotatorLib/Export/AbstractExport.h>
1515
#include <AnnotatorLib/Export/ExportPlugin.h>
16+
#include <AnnotatorLib/Project.h>
1617
#include <AnnotatorLib/annotatorlib_api.h>
1718

1819
namespace AnnotatorLib {
@@ -30,9 +31,10 @@ class ANNOTATORLIB_API ExportFactory {
3031
* @return imageSet
3132
*/
3233
std::shared_ptr<AbstractExport> createExport(std::string type,
33-
std::string path);
34+
std::shared_ptr<Project> project,
35+
std::string path);
3436

35-
std::list<std::string> availableImageSets();
37+
std::list<std::string> availableExports();
3638

3739
/**
3840
* @brief loadPlugins

source/annotatorlib/include/AnnotatorLib/Export/ExportPlugin.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <string>
66

77
#include <AnnotatorLib/Export/AbstractExport.h>
8+
#include <AnnotatorLib/Project.h>
89
#include <AnnotatorLib/annotatorlib_api.h>
910

1011
namespace AnnotatorLib {
@@ -36,7 +37,7 @@ class ANNOTATORLIB_API ExportPlugin {
3637
* @return new instance of loader
3738
*/
3839
virtual std::shared_ptr<AnnotatorLib::Export::AbstractExport> create(
39-
std::string path) = 0;
40+
std::shared_ptr<Project> project, std::string path) = 0;
4041
};
4142
}
4243
}

source/annotatorlib/source/Export/ExportAnnotationImages.cpp

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
#define Annotator_AnnotatorLib_ExportAnnotationImages_BODY
44

55
/************************************************************
6-
ImageFolder class body
6+
ExportAnnotationImages class body
77
************************************************************/
88

99
// include associated header file
1010

11+
#include <experimental/filesystem>
12+
13+
#include "AnnotatorLib/Annotation.h"
1114
#include "AnnotatorLib/Export/ExportAnnotationImages.h"
1215

1316
#include <set>
@@ -17,10 +20,14 @@
1720

1821
// Derived includes directives
1922

23+
namespace fs = std::experimental::filesystem;
24+
2025
namespace AnnotatorLib {
2126
namespace Export {
2227

23-
ExportAnnotationImages::ExportAnnotationImages(std::string path) {
28+
ExportAnnotationImages::ExportAnnotationImages(std::shared_ptr<Project> project,
29+
std::string path) {
30+
this->project = project;
2431
this->path = path;
2532
}
2633

@@ -33,8 +40,42 @@ bool ExportAnnotationImages::equals(std::shared_ptr<AbstractExport> other) {
3340
return true;
3441
}
3542

43+
void ExportAnnotationImages::exportClass(std::shared_ptr<Class> theClass) {
44+
std::string classPath = path + "/" + theClass->getName();
45+
}
46+
47+
void ExportAnnotationImages::exportObject(std::shared_ptr<Object> object) {
48+
std::string classPath = path + "/" + object->getClass()->getName() + "/";
49+
if (!fs::exists(classPath)) {
50+
fs::create_directory(classPath);
51+
}
52+
std::string objectPath = classPath + object->getName() + "_";
53+
for (auto annotation : object->getAnnotations()) {
54+
unsigned long frame =
55+
annotation.second.lock()->getFrame()->getFrameNumber();
56+
cv::Mat tmp = project->getImageSet()->getImage(frame);
57+
float x = std::max(annotation.second.lock()->getX(), 0.f);
58+
float y = std::max(annotation.second.lock()->getY(), 0.f);
59+
float w = std::min(annotation.second.lock()->getWidth(), tmp.cols - x);
60+
float h = std::min(annotation.second.lock()->getHeight(), tmp.rows - y);
61+
62+
cv::Rect rect(x, y, w, h);
63+
64+
cv::Mat cropped;
65+
tmp(rect).copyTo(cropped);
66+
std::string imagePath = objectPath + std::to_string(frame) + ".jpg";
67+
cv::imwrite(imagePath, cropped);
68+
}
69+
}
70+
3671
std::string ExportAnnotationImages::getPath() { return path; }
3772

73+
void ExportAnnotationImages::doExport() {
74+
for (auto object : project->getSession()->getObjects()) {
75+
exportObject(object.second);
76+
}
77+
}
78+
3879
// static attributes (if any)
3980
} // of namespace Export
4081
} // of namespace AnnotatorLib

source/annotatorlib/source/Export/ExportFactory.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ static ExportFactory *_instance = nullptr;
3333
* @return imageSet
3434
*/
3535
std::shared_ptr<AbstractExport> ExportFactory::createExport(
36-
std::string type, std::string path) {
36+
std::string type, std::shared_ptr<Project> project, std::string path) {
3737
if ("annotationimages" == type)
38-
return std::make_shared<ExportAnnotationImages>(path);
38+
return std::make_shared<ExportAnnotationImages>(project, path);
3939

4040
std::shared_ptr<ExportPlugin> plugin = this->plugins[type];
41-
if (plugin) return plugin->create(path);
41+
if (plugin) return plugin->create(project, path);
4242
throw std::invalid_argument("ExportType unknown!");
4343
}
4444

45-
std::list<std::string> ExportFactory::availableImageSets() {
45+
std::list<std::string> ExportFactory::availableExports() {
4646
return _availableExports;
4747
}
4848

@@ -90,9 +90,8 @@ ExportFactory::~ExportFactory() { _instance = nullptr; }
9090

9191
void ExportFactory::addAvailableExport(ExportPlugin *plugin) {
9292
if (plugin) {
93-
bool found =
94-
(std::find(_availableExports.begin(), _availableExports.end(),
95-
plugin->name()) != _availableExports.end());
93+
bool found = (std::find(_availableExports.begin(), _availableExports.end(),
94+
plugin->name()) != _availableExports.end());
9695
if (!found)
9796
_availableExports.insert(_availableExports.end(), plugin->name());
9897
}

0 commit comments

Comments
 (0)