Skip to content

Commit

Permalink
Add opencv support (intel-analytics#33)
Browse files Browse the repository at this point in the history
* Add opencv support

* Add opencv support
  • Loading branch information
SeaOfOcean authored and i8run committed Nov 29, 2017
1 parent cdd2483 commit d6a47d6
Show file tree
Hide file tree
Showing 9 changed files with 319 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ target/
*.o
*.dylib
dependency-reduced-pom.xml
.idea/
5 changes: 5 additions & 0 deletions dist/dist-linux/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,10 @@
<artifactId>bigquant-java-x86_64-linux</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.intel.analytics.bigdl.core.native.opencv</groupId>
<artifactId>opencv-java-x86_64-linux</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
5 changes: 5 additions & 0 deletions dist/dist-mac/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,10 @@
<artifactId>bigquant-java-mac</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.intel.analytics.bigdl.core.native.opencv</groupId>
<artifactId>opencv-java-mac</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
70 changes: 70 additions & 0 deletions opencv/opencv-java-mac/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>opencv-parent</artifactId>
<groupId>com.intel.analytics.bigdl.core.native</groupId>
<version>0.4.0-SNAPSHOT</version>
</parent>

<groupId>com.intel.analytics.bigdl.core.native.opencv</groupId>
<artifactId>opencv-java-mac</artifactId>
<packaging>jar</packaging>

<properties>
<opencvlib>libopencv_java320.dylib</opencvlib>
<opencvjar>opencv-320.jar</opencvjar>
<opencvDir>/opt/opencv/</opencvDir>
</properties>

<build>
<resources>
<resource>
<directory>${opencvDir}</directory>
<includes>
<include>${opencvlib}</include>
<include>${opencvjar}</include>
</includes>
<targetPath>${project.build.directory}/classes</targetPath>
</resource>
</resources>
<sourceDirectory>../src/main/java</sourceDirectory>
<testSourceDirectory>../src/test/java</testSourceDirectory>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<phase>generate-sources</phase>
<configuration>
<tasks>
<echo>Extracting Java classes.</echo>
<unjar src="${opencvDir}/opencv-320.jar"
dest="${project.build.directory}/classes">
<patternset>
<include name="**/*.class"/>
</patternset>
</unjar>

<echo>Extracting Java sources.</echo>
<unjar src="${opencvDir}/opencv-320.jar"
dest="${project.build.directory}/upstream-sources">
<patternset>
<include name="**/*.java"/>
</patternset>
</unjar>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
70 changes: 70 additions & 0 deletions opencv/opencv-java-x86_64-linux/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>opencv-parent</artifactId>
<groupId>com.intel.analytics.bigdl.core.native</groupId>
<version>0.4.0-SNAPSHOT</version>
</parent>

<groupId>com.intel.analytics.bigdl.core.native.opencv</groupId>
<artifactId>opencv-java-x86_64-linux</artifactId>
<packaging>jar</packaging>

<properties>
<opencvlib>libopencv_java320.so</opencvlib>
<opencvjar>opencv-320.jar</opencvjar>
<opencvDir>/opt/opencv/</opencvDir>
</properties>

<build>
<resources>
<resource>
<directory>${opencvDir}</directory>
<includes>
<include>${opencvlib}</include>
<include>${opencvjar}</include>
</includes>
<targetPath>${project.build.directory}/classes</targetPath>
</resource>
</resources>
<sourceDirectory>../src/main/java</sourceDirectory>
<testSourceDirectory>../src/test/java</testSourceDirectory>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<phase>generate-sources</phase>
<configuration>
<tasks>
<echo>Extracting Java classes.</echo>
<unjar src="${opencvDir}/opencv-320.jar"
dest="${project.build.directory}/classes">
<patternset>
<include name="**/*.class"/>
</patternset>
</unjar>

<echo>Extracting Java sources.</echo>
<unjar src="${opencvDir}/opencv-320.jar"
dest="${project.build.directory}/upstream-sources">
<patternset>
<include name="**/*.java"/>
</patternset>
</unjar>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
51 changes: 51 additions & 0 deletions opencv/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>parent</artifactId>
<groupId>com.intel.analytics.bigdl.core</groupId>
<version>0.4.0-SNAPSHOT</version>
</parent>

<artifactId>opencv-parent</artifactId>
<groupId>com.intel.analytics.bigdl.core.native</groupId>
<packaging>pom</packaging>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>

<profiles>
<profile>
<id>linux</id>
<modules>
<module>opencv-java-x86_64-linux</module>
</modules>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>

<profile>
<id>mac</id>
<modules>
<module>opencv-java-mac</module>
</modules>
</profile>

<profile>
<id>deploy</id>
<modules>
<module>opencv-java-x86_64-linux</module>
<module>opencv-java-mac</module>
</modules>
</profile>
</profiles>
</project>
88 changes: 88 additions & 0 deletions opencv/src/main/java/com/intel/analytics/bigdl/opencv/OpenCV.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2016 The BigDL Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.intel.analytics.bigdl.opencv;

import java.io.*;
import java.net.URL;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;

import static java.io.File.createTempFile;
import static java.nio.channels.Channels.newChannel;

/**
* OpenCV Library Wrapper for JVM
*/
public class OpenCV {
private static boolean isLoaded = false;
private static File tmpFile = null;

static {
try {
String jopencvFileName = "libopencv_java320.so";
if (System.getProperty("os.name").toLowerCase().contains("mac")) {
jopencvFileName = "libopencv_java320.dylib";
}
tmpFile = extract(jopencvFileName);
System.load(tmpFile.getAbsolutePath());
tmpFile.delete(); // delete so temp file after loaded
isLoaded = true;
} catch (Exception e) {
isLoaded = false;
e.printStackTrace();
throw new RuntimeException("Failed to load OpenCV");
}
}

/**
* Check if opencv is loaded
* @return
*/
public static boolean isOpenCVLoaded() {
return isLoaded;
}

/**
* Get the temp path of the .so file
* @return
*/
public static String getTmpSoFilePath() {
if (tmpFile == null)
return "";
else
return tmpFile.getAbsolutePath();
}

// Extract so file from jar to a temp path
private static File extract(String path) {
URL url = OpenCV.class.getResource("/" + path);
if (url == null) {
throw new Error("Can't find so file in jar, path = " + path);
}
try {
InputStream in = OpenCV.class.getResourceAsStream("/" + path);
File file = createTempFile("dlNativeLoader", path.substring(path.lastIndexOf("/") + 1));

ReadableByteChannel src = newChannel(in);
FileChannel dest = new FileOutputStream(file).getChannel();
dest.transferFrom(src, 0, Long.MAX_VALUE);
return file;
} catch (Throwable e) {
throw new Error("Can't extract so file to /tmp dir");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2016 The BigDL Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.intel.analytics.bigdl.opencv;

import org.junit.Test;

import static org.junit.Assert.assertTrue;

public class OpenCVTest {
@Test
public void isOpenCVLoaded() throws Exception {
assertTrue(OpenCV.isOpenCVLoaded());
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<modules>
<module>mkl2017-xeon-blas</module>
<module>bigquant</module>
<module>opencv</module>
<module>dist</module>
</modules>

Expand Down

0 comments on commit d6a47d6

Please sign in to comment.