bytedeco / javacpp-presets

The missing Java distribution of native C++ libraries
Other
2.65k stars 737 forks source link

how to build C++ project which depends on opencv #965

Open jobsLee-lcj opened 3 years ago

jobsLee-lcj commented 3 years ago

I'm on Mac, and I have follow files:

  1. libsportsai_basketball.dylib
  2. sportsai_AIbasketball_data_type.hpp
  3. sportsai_AIbasketball_api.h

sportsai_AIbasketball_data_type.hpp as follow:

#pragma once
#include <vector>
#include <opencv2/opencv.hpp>

/**********应用场景宏定义************/
#define   APPLICATION_SCENARIOS_FOR_MOBILE           true       //手机移动端使用
#define   APPLICATION_SCENARIOS_FOR_STILL_CAMERA      false      //场馆固定监控摄像头

typedef void* AIBASKETBALL_HANDLE;
 
/***********球员投球信息***********/
struct ShotPlayerInfo{
  cv::Rect position;        //球员位置
  cv::Mat  playerImg;       //球员截图
  float    shotResult = 0.f;    //投球得分结果
  float    positionConfidence = 0.f;    //球员检测可信度
  cv::Point courtPosition;  //标准球场映射位置 
};
/**********球场环境信息************/
struct BasketballSceneInfos{
    cv::Mat standardCourtImage;             // 标准球场俯视图
    std::vector<cv::Point2f> courtKeyPoints;    //场地线关键点
    std::vector<cv::Rect> backBoards;   //正常只有一个,此处使用vector可以检查是否检测到
    std::vector<cv::Rect> hoops;        //正常只有一个,此处使用vector可以检查是否检测到
    std::vector<cv::Rect> balls;        //检测到的球
    std::vector<cv::Rect> players;      //检测到的所有球员
    std::vector<float> playerConfidences;   //检测到的所有球员的置信度
};
/*********投篮分析结果***********/
struct AIbasketBallResult{
  int score = 0;                            //得分,-1:存在丢球, 0:无投球结果, 1:进球
  std::vector<cv::Point> trace;             //投球轨迹
  BasketballSceneInfos detectInfos;         //球场检测信息
  std::vector<ShotPlayerInfo> shotInfos;    //投球球员信息,有得分的情况下,大部分会有该项信息,但也存在因为未检测到球员而没有该项内容的情况
};

as the code shows, C++ header file need opencv. so, how could I write the java Parser config file. My code as follow:

import org.bytedeco.javacpp.*;
import org.bytedeco.javacpp.annotation.*;
import org.bytedeco.javacpp.tools.*;

@Properties(
    target = "org.bytedeco.ai_basketball",
    global = "org.bytedeco.ai_basketball.global",
    value = {
        @Platform(
            value = {
                "macosx-x86_64"
            },
            includepath = {"/Users/lcj/Desktop/work/javacpp-presets/ai_basketball/cppbuild/macosx-x86_64/sportsai_basketball/include"},
            linkpath = {"/Users/lcj/Desktop/work/javacpp-presets/ai_basketball/cppbuild/macosx-x86_64/sportsai_basketball/bin"},
            include = {"sportsai_AIbasketball_data_type.hpp","sportsai_AIbasketball_api.h"},
            link = {"libsportsai_basketball"}
        )
    }
)
public class ai_basketball implements InfoMapper {
    @Override
    public void map(InfoMap infoMap) {

    }
}

Then, run mvn clean install -Djavacpp.platform=macosx-x86_64 --projects=ai_basketball, it can generate the java interface class. but COMPILATION ERROR. error info as follow: [ERROR] /Users/lcj/Desktop/work/javacpp-presets/ai_basketball/src/gen/java/org/bytedeco/ai_basketball/global.java:[111,52] 找不到符号 符号: 类 Mat 位置: 类 org.bytedeco.ai_basketball.global [ERROR] /Users/lcj/Desktop/work/javacpp-presets/ai_basketball/src/gen/java/org/bytedeco/ai_basketball/global.java:[163,74] 找不到符号 符号: 类 Point 位置: 类 org.bytedeco.ai_basketball.global that means could not find class Mat or Point etc which from opencv.

My question is how to write the @Properties in java config file? Thank you for your help.

saudet commented 3 years ago

Please take a look at small existing projects depending on OpenCV this way. You can use those as templates for your own library: https://github.com/bytedeco/javacpp-presets/tree/master/chilitags https://github.com/bytedeco/javacpp-presets/tree/master/flandmark

jobsLee-lcj commented 3 years ago

Please take a look at small existing projects depending on OpenCV this way. You can use those as templates for your own library: https://github.com/bytedeco/javacpp-presets/tree/master/chilitags https://github.com/bytedeco/javacpp-presets/tree/master/flandmark

thank you for your reply. I have try the project of flandmark as follow steps:

  1. bash cppbuild.sh install flandmark
  2. mvn clean install -Djavacpp.platform=macosx-x86_64 -e -X --projects=flandmark

what a pity, it BUILD FAILURE. error info:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 52.049 s
[INFO] Finished at: 2020-10-23T10:09:38+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.bytedeco:javacpp:1.5.5-SNAPSHOT:build (javacpp-compiler) on project flandmark: Execution javacpp-compiler of goal org.bytedeco:javacpp:1.5.5-SNAPSHOT:build failed: Process exited with an error: 1 -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.bytedeco:javacpp:1.5.5-SNAPSHOT:build (javacpp-compiler) on project flandmark: Execution javacpp-compiler of goal org.bytedeco:javacpp:1.5.5-SNAPSHOT:build failed: Process exited with an error: 1
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:213)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:954)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
Caused by: org.apache.maven.plugin.PluginExecutionException: Execution javacpp-compiler of goal org.bytedeco:javacpp:1.5.5-SNAPSHOT:build failed: Process exited with an error: 1
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:148)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:954)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
Caused by: java.lang.RuntimeException: Process exited with an error: 1
    at org.bytedeco.javacpp.tools.Builder.generateAndCompile (Builder.java:628)
    at org.bytedeco.javacpp.tools.Builder.build (Builder.java:1174)
    at org.bytedeco.javacpp.tools.BuildMojo.execute (BuildMojo.java:417)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:954)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
[ERROR] 
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
jobsLee-lcj commented 3 years ago

Additional, some C++ build error info as follow when run mvn clean install -Djavacpp.platform=macosx-x86_64 -e -X --projects=flandmark.

In file included from /Users/lcj/Desktop/work/javacpp-presets/flandmark/target/native/org/bytedeco/flandmark/macosx-x86_64/jniflandmark.cpp:104:
In file included from /Users/lcj/Desktop/work/javacpp-presets/openblas/cppbuild/macosx-x86_64/include/lapacke_config.h:46:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/complex:245:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:317:9: error: no member named 'signbit' in the global namespace
using ::signbit;
      ~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:318:9: error: no member named 'fpclassify' in the global namespace
using ::fpclassify;
      ~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:319:9: error: no member named 'isfinite' in the global namespace; did you mean 'finite'?
using ::isfinite;
      ~~^
/usr/local/include/math.h:749:12: note: 'finite' declared here
extern int finite(double)
           ^
In file included from /Users/lcj/Desktop/work/javacpp-presets/flandmark/target/native/org/bytedeco/flandmark/macosx-x86_64/jniflandmark.cpp:104:
In file included from /Users/lcj/Desktop/work/javacpp-presets/openblas/cppbuild/macosx-x86_64/include/lapacke_config.h:46:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/complex:245:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:320:9: error: no member named 'isinf' in the global namespace
using ::isinf;
      ~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:321:9: error: no member named 'isnan' in the global namespace
using ::isnan;
      ~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:322:9: error: no member named 'isnormal' in the global namespace
using ::isnormal;
      ~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:323:9: error: no member named 'isgreater' in the global namespace
using ::isgreater;
      ~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:324:9: error: no member named 'isgreaterequal' in the global namespace
using ::isgreaterequal;
      ~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:325:9: error: no member named 'isless' in the global namespace
using ::isless;
      ~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:326:9: error: no member named 'islessequal' in the global namespace
using ::islessequal;
      ~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:327:9: error: no member named 'islessgreater' in the global namespace
using ::islessgreater;
      ~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:328:9: error: no member named 'isunordered' in the global namespace
using ::isunordered;
      ~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:329:9: error: no member named 'isunordered' in the global namespace
using ::isunordered;
      ~~^
In file included from /Users/lcj/Desktop/work/javacpp-presets/flandmark/target/native/org/bytedeco/flandmark/macosx-x86_64/jniflandmark.cpp:104:
saudet commented 3 years ago

It works fine on Travis CI. Looks like you have an old and/or broken version of Xcode installed. You'll need to fix that: https://github.com/bytedeco/javacpp-presets/wiki/Build-Environments#mac-os-x-x86_64

jobsLee-lcj commented 3 years ago

It works fine on Travis CI. Looks like you have an old and/or broken version of Xcode installed. You'll need to fix that: https://github.com/bytedeco/javacpp-presets/wiki/Build-Environments#mac-os-x-x86_64

I hava try that before。However,the build-environments for mac is outdated,my system is macOS Catalina 10.15.6 and Xcode is Xcode 11.5. /Library/Developer/CommandLineTools/Packages/ does not exist (so there's not yet a .pkg file to open).As the link shows: https://stackoverflow.com/questions/58278260/cant-compile-a-c-program-on-a-mac-after-upgrading-to-catalina-10-15/58278392#58278392

After export CPATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include, it still not solved my problem.

jobsLee-lcj commented 3 years ago

Ok, after half day work, I build the project of flandmark success. My solution depends on the link: https://stackoverflow.com/questions/58628377/catalina-c-using-cmath-headers-yield-error-no-member-named-signbit-in-th/59780819#59780819

because the #include <...> search order, after gcc -Wp,-v -E -

I just do sudo rm -rf /usr/local/include, then build success.maybe there is some danger unknown.

But when I run the SimpleExample class of flandmark. IDEA throw error:

Information:java: Errors occurred while compiling module 'opencv'
Information:javac 1.8.0_171 was used to compile java sources
Information:2020/10/23, 19:13 - Build completed with 1 error and 1 warning in 3 m 3 s
Error:java: java.lang.OutOfMemoryError: GC overhead limit exceeded
saudet commented 3 years ago

You'll need to make sure Java has enough memory, for example, by setting the -Xmx option this way:

export _JAVA_OPTIONS=-Xmx2048m

https://github.com/bytedeco/javacpp-presets/blob/master/ci/install-travis.sh#L232