-
Notifications
You must be signed in to change notification settings - Fork 15
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
How to extend this project for selfie segmentation? #20
Comments
I started adding a helper class for selfie segmentation: #include "SelfieSegmentation.hpp"
my::SelfieSegmentation::SelfieSegmentation(std::string modelDir) :
my::ModelLoader(modelDir + std::string("/selfie_segmentation.tflite"))
{}
void my::SelfieSegmentation::loadImageToInput(const cv::Mat& in, int index) {
ModelLoader::loadImageToInput(in);
}
void my::SelfieSegmentation::runInference() {
ModelLoader::runInference();
}
However ModelLoader fails at
Have you ever encountered such an error? |
Hi @postacik, I'm sorry that I hadn't noticed your issue until your latest response. |
Hi @pntt3011, thank you for your reply. |
I succeeded to run void my::ModelLoader::buildInterpreter(int numThreads) {
tflite::ops::builtin::BuiltinOpResolver resolver;
resolver.AddCustom("Convolution2DTransposeBias", mediapipe::tflite_operations::RegisterConvolution2DTransposeBias());
if (tflite::InterpreterBuilder(*m_model, resolver)(&m_interpreter) != kTfLiteOk) {
std::cerr << "Failed to build interpreter." << std::endl;
std::exit(1);
}
m_interpreter->SetNumThreads(numThreads);
} I copied However when I run Your How can I convert this float array to a matrix the same size of the input image? |
@postacik I am very delightful to hear about your success. You can try resizing the output tensor to the same width and height of the input image. |
I think I should do the reverse of this function of your code: cv::Mat my::ModelLoader::preprocessImage(const cv::Mat& in, int idx) const {
auto out = convertToRGB(in);
std::vector<int> inputShape = getInputShape(idx);
int H = inputShape[1];
int W = inputShape[2];
cv::Size wantedSize = cv::Size(W, H);
cv::resize(out, out, wantedSize);
/*
Equivalent to (out - mean)/ std
*/
out.convertTo(out, CV_32FC3, 1 / INPUT_NORM_STD, -INPUT_NORM_MEAN / INPUT_NORM_STD);
return out;
} Am I on the right path? |
I wrote a helper function as below and it seems to work: std::vector<float> my::SelfieSegmentation::getSegmentationMask() const {
return ModelLoader::loadOutput(0);
}
cv::Mat my::SelfieSegmentation::loadOutputImage(int imageHeight, int imageWidth) const
{
auto vec = getSegmentationMask();
std::vector<int> outputShape = getOutputShape(0);
int H = outputShape[1];
int W = outputShape[2];
cv::Mat out = cv::Mat(H, W, CV_32FC1);
if (vec.size() == H * W * sizeof(float)) // check that the rows and cols match the size of your vector
{
// copy vector to mat
memcpy(out.data, vec.data(), vec.size());
}
cv::Size wantedSize = cv::Size(imageWidth, imageHeight);
cv::resize(out, out, wantedSize);
return out;
} I would appreciate your valuable comments. |
Hi @postacik, I think the way you did is correct. Congratulations! |
Thanks for your help, closing... |
Hi,
This repo is the only repo I could find to use mediapipe in a C++ application. Thanks for sharing it.
Can you please show the right way of adding a class for selfie segmentation and create a demo similar to the python example?
The text was updated successfully, but these errors were encountered: