Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bounding Box Visualizer #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ci/macos-steps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ steps:
sudo xcode-select --switch /Applications/Xcode_10.1.app/Contents/Developer
unset BOOST_ROOT
pip install cython numpy pandas zipp configparser
brew install openblas armadillo boost
brew install openblas armadillo boost opencv
git clone --depth 1 https://github.com/mlpack/jenkins-conf.git conf
git clone --depth 1 https://github.com/mlpack/mlpack.git
displayName: 'Install Build Dependencies'
Expand Down
1 change: 1 addition & 0 deletions .ci/windows-steps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ steps:
nuget install boost_date_time-vc140 -o $(Agent.ToolsDirectory) -Version 1.60.0
nuget install boost_regex-vc140 -o $(Agent.ToolsDirectory) -Version 1.60.0
nuget install OpenBLAS -o $(Agent.ToolsDirectory)
nuget install OpenCV -o $(Agent.ToolsDirectory)

mkdir -p $(Agent.ToolsDirectory)/boost_libs
cp $(Agent.ToolsDirectory)/boost_program_options-vc140.1.60.0.0/lib/native/address-model-64/lib/*.* $(Agent.ToolsDirectory)/boost_libs
Expand Down
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ if (NOT ENSMALLEN_FOUND)
endif ()
endif ()

#find_package(OpenCV REQUIRED)

# Unfortunately this configuration variable is necessary and will need to be
# updated as time goes on and new versions are released.
set(Boost_ADDITIONAL_VERSIONS
Expand Down Expand Up @@ -196,10 +198,12 @@ set(MODELS_INCLUDE_DIRS ${MLPACK_INCLUDE_DIR}
${ENSMALLEN_INCLUDE_DIR}
${ARMADILLO_INCLUDE_DIR}
${Boost_INCLUDE_DIRS})
# ${OpenCV_INCLUDE_DIRS})

set(MODELS_LIBRARIES ${MLPACK_LIBRARY}
${ARMADILLO_LIBRARIES}
${Boost_LIBRARIES})
# ${OpenCV_LIBS})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this is commented out?


include_directories(${MODELS_INCLUDE_DIRS})

Expand All @@ -217,6 +221,7 @@ set(DIRS
utils/
dataloader/
tests/
# visualization_tools/
)

foreach(dir ${DIRS})
Expand Down
108 changes: 108 additions & 0 deletions visualization_tools/visualization_tools.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* @file visualization_tools.hpp
* @author Kartik Dutt
*
* Visualization tools to visualize detection and segmentation algorithms.
*
* mlpack is free software; you may redistribute it and/or modify it under the
* terms of the 3-clause BSD license. You should have received a copy of the
* 3-clause BSD license along with mlpack. If not, see
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
*/

#ifndef MODELS_VISUALIZATION_TOOLS_HPP
#define MODELS_VISUALIZATION_TOOLS_HPP

#include <mlpack/prereqs.hpp>
#include <opencv2/opencv.hpp>

/**
* Tools to visualize data and predictions.
*/
class VisualizationTools
{
public:
/**
* Save and plot bounding boxes on images.
*/
template<typename ImageType>
static void VisualizeBoundingBoxes(
ImageType& images,
arma::field<arma::vec>& boundingBoxes,
const size_t imageWidth = 224,
const size_t imageHeight = 224,
const size_t imageDepth = 3,
const bool cornerRepresentation = true,
const bool plot = false,
const bool saveImages = false,
const std::vector<std::string> imagePath = std::vector<std::string>())
{
Comment on lines +29 to +39
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the method name might be simplified into one like DrawBBox. I think it is not too ambiguous. Kindly let me know what you think.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll change it.

if (saveImages)
{
mlpack::Log::Assert(images.n_cols == imagePath.size(),
"Mismatch between number of images," + std::to_string(image.n_cols) +\
" and image file paths" + to_string(imagePath.size()) + ".");
}
for (size_t i = 0; i < image.n_cols; i++)
{
Comment on lines +46 to +47
Copy link
Member

@KimSangYeon-DGU KimSangYeon-DGU Aug 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it processes a set of images sequentially, why don't you set this method to get an image, not a set of images? Users can extend this feature by calling it multiple times in their codes. Of course, we can create two methods, one is for a single image and the other is for multiple images. I'm just worried about this API may constrain the flexibility.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure that makes sense. Will make the changes.

arma::vec bBoxes = boundingBoxes(0, i);
arma::cube image(imageWidth * imageHeight * imageDepth, 1, 1);
image.slice(0).col(0) = image.col(i);
image.reshape(imageWidth, imageHeight, imageDepth);
cv::mat img = CubeToOpenCV(image);
for (size_t boxIdx = 0; boxIdx < bBoxes.n_elem; boxIdx += 4)
{
cv::Point upperRightPoint, lowerLeftPoint;
if (cornerRepresentation)
{
upperRightPoint = cv::Point(bBoxes(boxIdx * 4),
bBoxes(boxIdx * 4 + 1));
lowerLeftPoint = cv::Point(bBoxes(boxIdx * 4 + 2),
bBoxes(boxIdx * 4 + 3));
}
else
{
upperRightPoint = cv::Point(bBoxes(boxIdx * 4),
bBoxes(boxIdx * 4 + 1));
lowerLeftPoint = cv::Point(bBoxes(boxIdx * 4) +
bBoxes(boxIdx * 4 + 2), bBoxes(boxIdx * 4 + 1) +
bBoxes(boxIdx * 4 + 3));
}

cv::rectangle(img, lowerRightPoint, upperRightPoint,
cv::Scalar(rand() % 255, rand() % 255, rand() % 255));
Comment on lines +72 to +73
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, it seems to be better for users to be able to choose a color of the bounding box.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how we would want to do this. There might be multiple objects in the image belonging to different class so either we could take input a map where each label corresponded to a tuple. Kindly let me know if this makes sense.

}
}

if (plot)
{
cv::imshow("Image", img);
}

if (saveImages)
{
cv::imwrite(imagePath[i], img);
}
}

private:
/**
* Convert armadillo cube to opencv matrix.
*/
template<typename eT>
cv::Mat<eT> CubeToOpenCVMat(const Mat<eT>& input)
{
vector<cv::Mat_<T>> channels;
for (size_t c = 0; c < input.n_slices; ++c)
{
auto* data = const_cast<T *>(input.slice(c).memptr());
channels.push_back({int(input.n_cols), int(input.n_rows), data});
}

cv::Mat dst;
cv::merge(channels, dst);
return dst;
}
};

#endif