forked from intel-analytics/ipex-llm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add opencv support (intel-analytics#33)
* Add opencv support * Add opencv support
- Loading branch information
1 parent
cdd2483
commit d6a47d6
Showing
9 changed files
with
319 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ target/ | |
*.o | ||
*.dylib | ||
dependency-reduced-pom.xml | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
88
opencv/src/main/java/com/intel/analytics/bigdl/opencv/OpenCV.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
opencv/src/test/java/com/intel/analytics/bigdl/opencv/OpenCVTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters