-
Notifications
You must be signed in to change notification settings - Fork 45
Improve image processing utils #451
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
base: main
Are you sure you want to change the base?
Improve image processing utils #451
Conversation
packages/react-native-executorch/common/rnexecutorch/data_processing/ImageProcessing.cpp
Outdated
Show resolved
Hide resolved
packages/react-native-executorch/common/rnexecutorch/data_processing/ImageProcessing.h
Outdated
Show resolved
Hide resolved
I'll wait for writing tests for this one till merging #438 |
packages/react-native-executorch/common/rnexecutorch/data_processing/ImageProcessing.cpp
Outdated
Show resolved
Hide resolved
packages/react-native-executorch/common/rnexecutorch/data_processing/ImageProcessing.cpp
Outdated
Show resolved
Hide resolved
408c1ae
to
b380ba8
Compare
packages/react-native-executorch/common/rnexecutorch/data_processing/ImageProcessing.cpp
Outdated
Show resolved
Hide resolved
packages/react-native-executorch/common/rnexecutorch/tests/TestRunner.cpp
Outdated
Show resolved
Hide resolved
packages/react-native-executorch/common/rnexecutorch/tests/LogTest.cpp
Outdated
Show resolved
Hide resolved
packages/react-native-executorch/common/rnexecutorch/tests/ImageProcessingTest.cpp
Outdated
Show resolved
Hide resolved
I had a problem with adding
I left one dummy test for this file and commented lines that adds the file to testing via cmake. If someone wants to play with it and potentially fix the issue just uncomment these commented lines in |
…essing/ImageProcessing.cpp
…essing/ImageProcessing.h
…essing/ImageProcessing.cpp
61c58dd
to
41b915d
Compare
packages/react-native-executorch/common/rnexecutorch/data_processing/Numerical.cpp
Outdated
Show resolved
Hide resolved
packages/react-native-executorch/common/rnexecutorch/tests/ImageProcessingTest.cpp
Outdated
Show resolved
Hide resolved
packages/react-native-executorch/common/rnexecutorch/tests/LogTest.cpp
Outdated
Show resolved
Hide resolved
packages/react-native-executorch/common/rnexecutorch/tests/TestRunner.cpp
Outdated
Show resolved
Hide resolved
for (size_t i = 0; i < numChannels; ++i) { | ||
backgroundScalar[i] = cvFloor(backgroundScalar[i]); | ||
} | ||
cv::Scalar backgroundScalar = computeBackgroundScalar(corners); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cv::Scalar backgroundScalar = computeBackgroundScalar(corners); | |
const cv::Scalar backgroundScalar = computeBackgroundScalar(corners); |
auto imageAsMatrix = image_processing::readImageToMatrix(imageSource); | ||
const auto tensorDims = getAllInputShapes()[0]; | ||
image_processing::adaptImageForTensor(tensorDims, imageAsMatrix); | ||
auto inputTensor = | ||
image_processing::readImageToTensor(imageSource, getAllInputShapes()[0]) | ||
.first; | ||
image_processing::getTensorFromMatrix(tensorDims, imageAsMatrix); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not quite sure why we can't have a single function for this like we did before? Maybe restore readImageToTensor
and place those four lines inside?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because original original scope of function readImageToTensor
is not getting tensor from matrix, but also resizing, converting to accurate format etc. My intention by splitting this was to get more self-explanatory code. The second thing is that original function returns ugly std::pair
and one value is often unused. Having code splitted internally inside image_processing::readImageToTensor
might be a way to go
for (int i = 0; i < pixelCount; ++i) { | ||
int row = i / mat.cols; | ||
int col = i % mat.cols; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for (int i = 0; i < pixelCount; ++i) { | |
int row = i / mat.cols; | |
int col = i % mat.cols; | |
for (size_t i = 0; i < pixelCount; ++i) { | |
size_t row = i / mat.cols; | |
size_t col = i % mat.cols; |
for (int i = 0; i < pixelCount; ++i) { | ||
const int row = i / matSize.width; | ||
const int col = i % matSize.width; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for (int i = 0; i < pixelCount; ++i) { | |
const int row = i / matSize.width; | |
const int col = i % matSize.width; | |
for (size_t i = 0; i < pixelCount; ++i) { | |
const size_t row = i / matSize.width; | |
const size_t col = i % matSize.width; | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
constexpr int kMinCornerPatchSize = 1; | ||
constexpr int kCornerPatchFractionSize = 30; | ||
int cornerPatchSize = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
constexpr int kMinCornerPatchSize = 1; | |
constexpr int kCornerPatchFractionSize = 30; | |
int cornerPatchSize = | |
constexpr uint32_t kMinCornerPatchSize = 1; | |
constexpr uint32_t kCornerPatchFractionSize = 30; | |
uint32_t cornerPatchSize = |
static cv::Mat handleBase64Data(const std::string &imageURI) { | ||
std::stringstream uriStream(imageURI); | ||
std::string stringData; | ||
std::size_t segmentIndex{0}; | ||
while (std::getline(uriStream, stringData, ',')) { | ||
++segmentIndex; | ||
} | ||
if (segmentIndex != 1) { | ||
throw std::runtime_error("Read image error: invalid base64 URI"); | ||
} | ||
auto data = base64_decode(stringData); | ||
cv::Mat encodedData(1, data.size(), CV_8UC1, | ||
static_cast<void *>(data.data())); | ||
return cv::imdecode(encodedData, cv::IMREAD_COLOR); | ||
} | ||
|
||
static cv::Mat handleLocalFile(const std::string &imageURI) { | ||
auto url = ada::parse(imageURI); | ||
return cv::imread(std::string{url->get_pathname()}, cv::IMREAD_COLOR); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: handle these via anonymous namespaces, not static functions
Description
This PR make ImageProcessing module cleaner and more readable.
Needs to be tested - implement tests and run them
Type of change
Tested on
Testing instructions
Screenshots
Related issues
Checklist
Additional notes