使用c ++实现EAST文本检测时出现未定义的cv :: dnn :: experimental_dnn错误

问题描述 投票:0回答:1

我正在使用C ++中的EAST Text Detection在图像上实现文本检测。我正在开发一个Android应用程序,它将使用c ++中的EAST文本检测来检测图像中的文本。但是在开发这个应用程序的过程中。我收到下面提到的错误。

Build command failed.
Error while executing process /home/karshsoni/Android/Sdk/cmake/3.6.4111459/bin/cmake with arguments {--build /home/karshsoni/Documents/CVTextDetection/app/.externalNativeBuild/cmake/debug/arm64-v8a --target native-lib}
[1/2] Building CXX object CMakeFiles/native-lib.dir/native-lib.cpp.o
[2/2] Linking CXX shared library /home/karshsoni/Documents/CVTextDetection/app/build/intermediates/cmake/debug/obj/arm64-v8a/libnative-lib.so
FAILED: : && /home/karshsoni/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=aarch64-none-linux-android21 --gcc-toolchain=/home/karshsoni/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/karshsoni/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security -stdlib=libc++ -std=gnu++11 -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o /home/karshsoni/Documents/CVTextDetection/app/build/intermediates/cmake/debug/obj/arm64-v8a/libnative-lib.so CMakeFiles/native-lib.dir/native-lib.cpp.o /home/karshsoni/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/21/liblog.so /home/karshsoni/Documents/CVTextDetection/app/src/main/jniLibs/arm64-v8a/libopencv_java3.so -latomic -lm && :
CMakeFiles/native-lib.dir/native-lib.cpp.o: In function `Java_com_example_cvtextdetection_MainActivity_getMat':
/home/karshsoni/Documents/CVTextDetection/app/src/main/cpp/native-lib.cpp:89: undefined reference to `cv::dnn::experimental_dnn_34_v10::Net::forward(cv::_OutputArray const&, std::__ndk1::vector<cv::String, std::__ndk1::allocator<cv::String> > const&)'
/home/karshsoni/Documents/CVTextDetection/app/src/main/cpp/native-lib.cpp:101: undefined reference to `cv::dnn::experimental_dnn_34_v10::NMSBoxes(std::__ndk1::vector<cv::RotatedRect, std::__ndk1::allocator<cv::RotatedRect> > const&, std::__ndk1::vector<float, std::__ndk1::allocator<float> > const&, float, float, std::__ndk1::vector<int, std::__ndk1::allocator<int> >&, float, int)'
/home/karshsoni/Documents/CVTextDetection/app/src/main/cpp/native-lib.cpp:123: undefined reference to `cv::dnn::experimental_dnn_34_v10::Net::getPerfProfile(std::__ndk1::vector<double, std::__ndk1::allocator<double> >&)'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

当我正在开发一个Android应用程序时,我正在通过JNI与来自java的cpp代码进行通信。

#include <jni.h>
#include <string>

#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/dnn.hpp>

using namespace cv;
using namespace cv::dnn;

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_cvtextdetection_MainActivity_stringFromJNI(
JNIEnv *env,
jobject / this /) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}

std::string jstring2string(JNIEnv *env, jstring jStr);

void decode(const Mat& scores, const Mat& geometry, float scoreThresh,
std::vector<RotatedRect>& detections, std::vector<float>& confidences)
{
detections.clear();
CV_Assert(scores.dims == 4); CV_Assert(geometry.dims == 4); CV_Assert(scores.size[0] == 1);
CV_Assert(geometry.size[0] == 1); CV_Assert(scores.size[1] == 1); CV_Assert(geometry.size[1] == 5);
CV_Assert(scores.size[2] == geometry.size[2]); CV_Assert(scores.size[3] == geometry.size[3]);

const int height = scores.size[2];
const int width = scores.size[3];
for (int y = 0; y < height; ++y)
{
const float* scoresData = scores.ptr<float>(0, 0, y);
const float* x0_data = geometry.ptr<float>(0, 0, y);
const float* x1_data = geometry.ptr<float>(0, 1, y);
const float* x2_data = geometry.ptr<float>(0, 2, y);
const float* x3_data = geometry.ptr<float>(0, 3, y);
const float* anglesData = geometry.ptr<float>(0, 4, y);
for (int x = 0; x < width; ++x)
{
float score = scoresData[x];
if (score < scoreThresh)
continue;

// Decode a prediction.
// Multiple by 4 because feature maps are 4 time less than input image.
float offsetX = x 4.0f, offsetY = y 4.0f;
float angle = anglesData[x];
float cosA = std::cos(angle);
float sinA = std::sin(angle);
float h = x0_data[x] + x2_data[x];
float w = x1_data[x] + x3_data[x];

Point2f offset(offsetX + cosA x1_data[x] + sinA x2_data[x],
offsetY - sinA x1_data[x] + cosA x2_data[x]);
Point2f p1 = Point2f(-sinA h, -cosA h) + offset;
Point2f p3 = Point2f(-cosA w, sinA w) + offset;
RotatedRect r(0.5f (p1 + p3), Size2f(w, h), -angle 180.0f / (float)CV_PI);
detections.push_back(r);
confidences.push_back(score);
}
}
}

extern "C"
JNIEXPORT void JNICALL
Java_com_example_cvtextdetection_MainActivity_getMat(JNIEnv *env, jobject instance, jlong mat,
jstring modelPath_) {
const char *modelPath = env->GetStringUTFChars(modelPath_, 0);

int inpWidth = 320;
int inpHeight = 320;
float confThreshold = 0.5;
float nmsThreshold = 0.5;

Mat* frame = (Mat*)mat;
String path = jstring2string(env, modelPath_);
Net net = readNet(path);

std::vector<Mat> output;
std::vector<String> outputLayers(2);
outputLayers[0] = "feature_fusion/Conv_7/Sigmoid";
outputLayers[1] = "feature_fusion/concat_3";

Mat blob;

blobFromImage(*frame, blob, 1.0, Size(inpWidth, inpHeight), Scalar(123.68, 116.78, 103.94), true, false);
net.setInput(blob);
net.forward(output, outputLayers);

Mat scores = output[0];
Mat geometry = output[1];

// Decode predicted bounding boxes.
std::vector<RotatedRect> boxes;
std::vector<float> confidences;
decode(scores, geometry, confThreshold, boxes, confidences);

// Apply non-maximum suppression procedure.
std::vector<int> indices;
NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices);

// Render detections.
Point2f ratio((float)(frame->cols)/ inpWidth, (float)frame->rows / inpHeight);
for (size_t i = 0; i < indices.size(); ++i)
{
RotatedRect& box = boxes[indices[i]];

Point2f vertices[4];
box.points(vertices);
for (int j = 0; j < 4; ++j)
{
vertices[j].x *= ratio.x;
vertices[j].y *= ratio.y;
}
for (int j = 0; j < 4; ++j)
line(*frame, vertices[j], vertices[(j + 1) % 4], Scalar(0, 255, 0), 2, LINE_AA);
}


}

std::string jstring2string(JNIEnv *env, jstring jStr) {
if (!jStr)
return "";

const jclass stringClass = env->GetObjectClass(jStr);
const jmethodID getBytes = env->GetMethodID(stringClass, "getBytes", "(Ljava/lang/String;)[B");
const jbyteArray stringJbytes = (jbyteArray) env->CallObjectMethod(jStr, getBytes, env->NewStringUTF("UTF-8"));

size_t length = (size_t) env->GetArrayLength(stringJbytes);
jbyte* pBytes = env->GetByteArrayElements(stringJbytes, NULL);

std::string ret = std::string((char *)pBytes, length);
env->ReleaseByteArrayElements(stringJbytes, pBytes, JNI_ABORT);

env->DeleteLocalRef(stringJbytes);
env->DeleteLocalRef(stringClass);
return ret;
}

我已经从Learnopencv github中引用了这段代码,其中我将链接放在这里。

https://github.com/spmallick/learnopencv/blob/master/TextDetectionEAST/textDetection.cpp

如果您对此问题有任何疑问,请与我们联系。谢谢。

c++ opencv android-ndk deep-learning java-native-interface
1个回答
0
投票

它似乎是你的CMakeLists.txt没有链接OpenCV库。

target_link_libraries( # Specifies the target library.
        native-lib
        -ljnigraphics

        # Links the target library to the log library
        # included in the NDK.
        ${OpenCV_LIBS})

还要检查所需的libopencv_java3.so文件是否存在于目标处理器的jniLibs路径中,即“arm64-v8a”。

/home/卡人身上哦你/documents/CV text detection/app/双人床/卖弄/教你libs/arm64-V8啊/lib OpenCV_Java3.so

© www.soinside.com 2019 - 2024. All rights reserved.