0

I am trying to develop an android application using android studio by incorporating c++ code for facial landmark detection. I am using the native c++ activity provided in the android studio. The problem I am facing is in the folder app->src->main->cpp all the c++ related files including CMakeLists.txt, native-lib.cpp are saved I have saved all the related header files in the same folder for better access. But I am getting path errors for the header files and it is quite impossible to manually edit path in all the header files. Can anyone please give any suggestions on how to include the header files without path error?

I am unable to connect opencv dlib with native c++ in android studio. There is an include folder but it does not contain opencv dlib headers. Even they are not allowing to import android opencv sdk module. I was trying to manually include the header files in the above mentioned path, but it is giving path error.

#include <jni.h>
#include "opencv.h"
#include "highgui.hpp"
#include "frontal_face_detector.h"
#include "render_face_detections.h"
#include "image_processing.h"
#include "gui_widgets.h"

using namespace dlib;
using namespace std;
using namespace cv;

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_facedetectionc3_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {

        Mat img = imread("Sample1.jpg");
// Load face detection and pose estimation models.
        frontal_face_detector detector = get_frontal_face_detector();
        cvtColor(img, img, COLOR_BGR2GRAY);
        cv_image<bgr_pixel> dlib_img(img);
// Detect faces
        vector<rectangle> faces = detector(dlib_img);
        int totalFaces = facesRects.size();
//face detection rectangle
        vector<full_object_detection> shapes;
        for (int i = 0; i < faces.size(); i++){
                rectangle r((long)(faces[i].left()),
                            (long)(faces[i].top()),
                            (long)(faces[i].right()),
                            (long)(faces[i].bottom()));
        }
        Rect roi(faces[i].left(),faces[i].top(),faces[i].right(),faces[i].bottom());
        face = dlib_img(roi);
        cvtColor(dlib_img, dlib_img, COLOR_BGR2RGB);
//define landmark detector
        shape_predictor landmarkDetector;
        deserialize("shape_predictor_68_face_landmarks.dat")>>landmarkDetector;
//to get the points
        for(int i=0; i<totalFaces; i++){
                full_object_detection faceLandmark = landmarkDetector(dlib_img, faceRects[i]);
        }
        imshow("Image", img);
        waitKey(0);
        return 0;
}

This is my native-lib.cpp code. I am having error in the header files, but how can I edit the header files. And also I am having some problem in path of these header files. This is my CMakeLists.txt file.

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.10.2)

#opencv
set(OpenCV_STATIC_on)
set(OpenCV_DIR ${OPENCV_ANDROID}/sdk/native/jni)
find_package(OpenCV.REQUIRED)

# Declares and names the project.

project("facedetectionc3")

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             opencv_facedetection.cpp
             native-lib.cpp )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )
find_library(inigraphics-lib jnigraphics)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${openCV_LIBS}
                       ${jnigraphics-libs}
                       ${log-lib} )

Any suggestions?

Euphoria
  • 1
  • 2
  • 1
    Please show a [mre] – Alan Birtles Mar 25 '22 at 07:26
  • @AlanBirtles I am not able to attach any screen shots of the IDE or the error as this is my first post and they are not allowing me to. – Euphoria Mar 26 '22 at 06:49
  • That's not the point. Copy your whole project and then remove everything not necessary to demonstrate the problem. Make sure people can copy'n'paste the code from the question to reproduce the behaviour. Please read that link you were given! – Ulrich Eckhardt Mar 26 '22 at 07:00
  • I'm pretty sure `find_package(OpenCV.REQUIRED)` won't work, it should be `find_package(OpenCV REQUIRED)` – Alan Birtles Mar 26 '22 at 07:22
  • @AlanBirtles I tried this and it is giving me this error `CMake Error at CMakeLists.txt:11 (find_package): By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "OpenCV", but CMake did not find one.` – Euphoria Mar 26 '22 at 09:13
  • https://stackoverflow.com/questions/8711109/could-not-find-module-findopencv-cmake-error-in-configuration-process – Alan Birtles Mar 26 '22 at 10:13

0 Answers0