I am trying to detect faces in an image using the OpenCV library in Java. I am using version 4.7.0-dev of OpenCV. When I run the program, I get the following error:
[ INFO:0#0.885] global registry_parallel.impl.hpp:96 cv::parallel::ParallelBackendRegistry::ParallelBackendRegistry core(parallel): Enabled backends(3, sorted by priority): ONETBB(1000); TBB(990); OPENMP(980)
[ INFO:0#0.885] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load C:\Users\ricar\OneDrive\Documentos\libraries\opencv\build\java\x64\opencv_core_parallel_onetbb470_64d.dll => FAILED
[ INFO:0#0.886] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load opencv_core_parallel_onetbb470_64d.dll => FAILED
[ INFO:0#0.886] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load C:\Users\ricar\OneDrive\Documentos\libraries\opencv\build\java\x64\opencv_core_parallel_tbb470_64d.dll => FAILED
[ INFO:0#0.886] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load opencv_core_parallel_tbb470_64d.dll => FAILED
[ INFO:0#0.887] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load C:\Users\ricar\OneDrive\Documentos\libraries\opencv\build\java\x64\opencv_core_parallel_openmp470_64d.dll => FAILED
[ INFO:0#0.887] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load opencv_core_parallel_openmp470_64d.dll => FAILED
[ INFO:0#1.067] global ocl.cpp:1186 cv::ocl::haveOpenCL Initialize OpenCL runtime...
[ INFO:0#1.977] global ocl.cpp:1192 cv::ocl::haveOpenCL OpenCL: found 2 platforms
[ INFO:0#1.977] global ocl.cpp:984 cv::ocl::OpenCLExecutionContext::Impl::getInitializedExecutionContext OpenCL: initializing thread execution context
[ INFO:0#1.977] global ocl.cpp:994 cv::ocl::OpenCLExecutionContext::Impl::getInitializedExecutionContext OpenCL: creating new execution context...
[ INFO:0#2.571] global ocl.cpp:1012 cv::ocl::OpenCLExecutionContext::Impl::getInitializedExecutionContext OpenCL: device=NVIDIA GeForce MX230
[ INFO:0#2.571] global ocl.cpp:5370 cv::ocl::Context::Impl::__init_buffer_pools OpenCL: Initializing buffer pool for context#0 with max capacity: poolSize=0 poolSizeHostPtr=0
This is my code. I believe that my problem is an installation or configuration issue.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
class Main{
public static void main(String[] args){
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat image = Imgcodecs.imread("C:\\Users\\ricar\\Downloads\\DSC02281.JPG");
CascadeClassifier classifier = new CascadeClassifier("C:\\Users\\ricar\\OneDrive\\Documentos\\workspace\\LearningJava\\src\\haarcascade_frontalface_default.xml");
MatOfRect faceDetections = new MatOfRect();
classifier.detectMultiScale(image, faceDetections);
for(Rect rect : faceDetections.toArray()) {
Imgproc.rectangle(image, rect, new Scalar(0, 0, 255));
}
Imgcodecs.imwrite("C:\\Users\\ricar\\Downloads\\output.jpg", image);
}
}
The error message suggests that the problem lies with the OpenCV parallel plugins. This could be due to a missing or incompatible version of the TBB library, which OpenCV uses for parallel processing.
You should check if you have the correct version of TBB installed and that it is located in a directory that is included in your system's PATH environment variable.
Related
I'd like to check a text file in our monorepo codebase via JUnit tests. JUnit tests are executed via bazel test command. How can I configure Bazel to pass the source code directory path (not cached compiled JAR file path) to my unit tests? it could be CLI args, environment variables, etc.
One way to do this is to use the data attribute which makes files available to tests (and other binaries) at runtime, and for Java there's the Runfiles library provided with Bazel for finding data files. E.g.:
BUILD:
java_test(
name = "TextFileTest",
srcs = ["javatests/txtfile/TextFileTest.java"],
data = ["text.txt"],
deps = [
"#bazel_tools//tools/java/runfiles:runfiles",
"#maven//:junit_junit",
],
)
WORKSPACE:
load("#bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
RULES_JVM_EXTERNAL_TAG = "4.2"
RULES_JVM_EXTERNAL_SHA = "cd1a77b7b02e8e008439ca76fd34f5b07aecb8c752961f9640dea15e9e5ba1ca"
http_archive(
name = "rules_jvm_external",
strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
sha256 = RULES_JVM_EXTERNAL_SHA,
url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG,
)
load("#rules_jvm_external//:defs.bzl", "maven_install")
maven_install(
artifacts = [
"junit:junit:4.13.2",
],
repositories = [
"https://repo1.maven.org/maven2",
],
)
javatests/txtfile/TextFileTest.java:
package txtfile;
import com.google.devtools.build.runfiles.Runfiles;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
#RunWith(JUnit4.class)
public class TextFileTest {
#Test
public void testTextFile() throws Exception {
Runfiles runfiles = Runfiles.create();
Path path = Paths.get(runfiles.rlocation("__main__/text.txt"));
String line = Files.readAllLines(path).get(0);
Assert.assertEquals(line, "this is text");
}
}
text.txt:
this is text
$ bazel test TextFileTest
INFO: Build option --test_sharding_strategy has changed, discarding analysis cache.
INFO: Analyzed target //:TextFileTest (0 packages loaded, 547 targets configured).
INFO: Found 1 test target...
Target //:TextFileTest up-to-date:
bazel-bin/TextFileTest.jar
bazel-bin/TextFileTest
INFO: Elapsed time: 0.288s, Critical Path: 0.01s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
//:TextFileTest (cached) PASSED in 0.3s
Executed 0 out of 1 test: 1 test passes.
INFO: Build completed successfully, 1 total action
Note that in runfiles.rlocation("__main__/text.txt") the __main__ is the workspace name, which by default is __main__. If there's a workspace(name = "workspace name") in the WORKSPACE file, then the workspace name would be set from that.
I am trying to load a model and test it on windows in android studio. I don't want to test in the AVD, because it takes more time. So tried to run this code without AVD (I use a different method, to load the mappedBuffer, when launching instrumented tests, because I have the model file in Android resources, but this loadFileToMappedBuffer(...) should also work):
import android.os.Build
import androidx.annotation.RequiresApi
import org.tensorflow.lite.Interpreter
import java.nio.MappedByteBuffer
import java.nio.channels.FileChannel
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.StandardOpenOption
import java.util.*
#RequiresApi(Build.VERSION_CODES.O)
fun loadFileToMappedBuffer(fileName: String): MappedByteBuffer {
val path = Paths.get(fileName)
val fileChannel: FileChannel =
Files.newByteChannel(path, EnumSet.of(StandardOpenOption.READ)) as FileChannel
val mappedByteBuffer: MappedByteBuffer =
fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size())
return mappedByteBuffer
}
#RequiresApi(Build.VERSION_CODES.O)
fun main() {
val interpreter: Interpreter = Interpreter(
loadFileToMappedBuffer("C:\\Users\\PC\\Documents\\mnist.tflite")
)
}
Unfortunetely I get this error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Failed to load native TensorFlow Lite methods. Check that the correct native libraries are present, and, if using a custom native library, have been properly loaded via System.loadLibrary():
java.lang.UnsatisfiedLinkError: no tensorflowlite_jni in java.library.path
at org.tensorflow.lite.TensorFlowLite.init(TensorFlowLite.java:80)
at org.tensorflow.lite.NativeInterpreterWrapper.<init>(NativeInterpreterWrapper.java:52)
at org.tensorflow.lite.Interpreter.<init>(Interpreter.java:277)
at org.tensorflow.lite.Interpreter.<init>(Interpreter.java:262)
at com.ml.models.TestMnistModelKt.main(testMnistModel.kt:28)
at com.ml.models.TestMnistModelKt.main(testMnistModel.kt)
What should I do to load the Kotlin tensorflow library and run the model without launching the AVD?
I'm quite new to OpenCV - Java programming, and I'm trying to setup an application to read video frames from USB WebCam, to start with something.
This is the document I followed up to now: https://opencv-java-tutorials.readthedocs.io/en/latest/03-first-javafx-application-with-opencv.html#video-capturing
The setup is the following:
Java version: 10.0.1
OpenCV Version: 3.3.4 and 3.2.0, same error with both versions
OS: Windows 10 x64
The .dll is placed under C:\Windows, that is included in my java.library.path
I have some additional frameworks involved in the application, but I prepared an isolated test case to better check the issue:
import org.junit.Test;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.videoio.VideoCapture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CVCaptureTest {
private static final Logger LOG = LoggerFactory.getLogger(CVCaptureTest.class);
#Test
public void testFrameRead() {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
VideoCapture capture = new VideoCapture();
capture.open(0);
for (int i = 0; i < 100; i ++) {
if (capture.isOpened()) {
Mat frame = new Mat();
LOG.info("Capture open. Reading frame...");
capture.retrieve(frame);
LOG.info("Captured: {}", frame.dump());
}
}
}
}
Output:
[INFO] Running i.m.r.b.r.c.CVCaptureTest
20:15:57.757 [main] INFO i.m.r.b.r.c.CVCaptureTest - Capture open. Reading frame...
After this log line the program just hangs, without throwing any exception.
Any help on understanding the cause of the freeze is welcome.
Thanks & Regards,
Mattia!
Try to use the capture.read(frame);.
I have a simple Application which generates a png of a chart which is based on JavaFX. The app fails to run on displayless machine with the following exception, I don't need to render or display content on the console, just need to create the image.
"main" java.lang.UnsupportedOperationException: Unable to open DISPLAY
at com.sun.glass.ui.gtk.GtkApplication.<init>(GtkApplication.java:68)
at com.sun.glass.ui.gtk.GtkPlatformFactory.createApplication(GtkPlatformFactory.java:41)
at com.sun.glass.ui.Application.run(Application.java:146)
at com.sun.javafx.tk.quantum.QuantumToolkit.startup(QuantumToolkit.java:257)
at com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:211)
at javafx.embed.swing.JFXPanel.initFx(JFXPanel.java:215)
at javafx.embed.swing.JFXPanel.<init>(JFXPanel.java:230)
I'm trying to run this on a AWS instance. Is there a way to overcome this issue? Following is my sample code.
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.embed.swing.JFXPanel;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import javax.imageio.ImageIO;
public class Test {
public static void main(String[] args) {
String chartGenLocation = "/Users/tmp";
new JFXPanel();
ObservableList<PieChart.Data> pieChartData =
FXCollections.observableArrayList(
new PieChart.Data("Failed", 10),
new PieChart.Data("Skipped", 20));
final PieChart chart = new PieChart(pieChartData);
chart.setAnimated(false);
Platform.runLater(() -> {
Stage stage = new Stage();
Scene scene = new Scene(chart, 500, 500);
stage.setScene(scene);
WritableImage img = new WritableImage(500, 500);
scene.snapshot(img);
File file = new File(Paths.get(chartGenLocation, "a.png").toString());
try {
ImageIO.write(SwingFXUtils.fromFXImage(img, null), "png", file);
} catch (IOException e) {
//logger.error("Error occurred while writing the chart image
}
});
}
}
I have seen few SO answers which mostly talk about monocle and testfx, here I'm unable to add external dependencies. So adding testfx is not a option. I have also tried the following with xvbf, which hangs the system,
Xvfb :95 -screen 0 1024x768x16 &> xvfb.log &
export DISPLAY=:95.0
When I execute I see the following output and system hangs there.
(process:13112): Gtk-WARNING **: Locale not supported by C library.
Using the fallback 'C' locale.
Fontconfig warning: ignoring UTF-8: not a valid region tag
Update
Execution Sequence
Xvfb :92 -screen 0 1024x768x16 &> xvfb.log &
export DISPLAY=:92.0
No errors in xvbf.log, seems to start properly.
java Test
I see following in console out
(process:13356): Gtk-WARNING **: Locale not supported by C library.
Using the fallback 'C' locale.
Fontconfig warning: ignoring UTF-8: not a valid region tag
I do not see any log in xvbf.log, the execution doesn't proceed after the above log. My image is not getting generated.
Update 2
I would like to know if there is a way to bypass this validation since I really don't need a display rendering.
I hit the same JavaFX issue on Ubuntu server using xvfb as the display manager for my UI tests, the root cause for me was my forwarded DISPLAY wasn't injected into dbus's activation environment, so anything that dbus activates that tries to present a UI failed to connect to a display and resulted in the "Unable to open DISPLAY" exception.
Running dbus-update-activation-environment --systemd DISPLAY XAUTHORITY in that shell before launching the UI tests fixed this issue for me.
I’m just trying to add my ontology to the DB repository ……
My code is:
oConnection = H2Db.getM_oConnection();
m_oSDBConnection = new SDBConnection(oConnection);
StoreDesc oStoreDesc = new StoreDesc(LayoutType.LayoutTripleNodesHash, DatabaseType.H2);
m_oStore = SDBFactory.connectStore(m_oSDBConnection, oStoreDesc);
m_oModel = SDBFactory.connectDefaultModel(m_oStore);
InputStream oInputStream = this.getClass().getResourceAsStream("/META-INF/betaas_context.owl");
m_oModel.read(oInputStream, null);
At the beginning with the following dependencies:
ID State Blueprint Level Name
[ 994] [Active ] [ ] [ 80] H2 Database Engine (1.3.170)
[1114] [Active ] [ ] [ 80] wrap_mvn_org.apache.jena_jena-iri_1.0.0 (0)
[1223] [Active ] [ ] [ 80] wrap_mvn_org.apache.jena_jena-arq_2.11.0 (0) -> contains package org.apache.jena.riot.adapters
[1279] [Active ] [ ] [ 80] wrap_mvn_org.apache.jena_jena-core_2.11.0 (0)
[1311] [Active ] [ ] [ 80] wrap_mvn_xerces_xercesImpl_2.7.1 (0)
[1314] [Active ] [ ] [ 80] wrap_mvn_com.ibm.icu_icu4j_3.4.4 (0)
And I get this exception:
java.lang.Exception: com.hp.hpl.jena.shared.ConfigException: Reader not found on classpath
Caused by: com.hp.hpl.jena.shared.ConfigException: Reader not found on classpath
Caused by: java.lang.ClassNotFoundException:
org.apache.jena.riot.adapters.JenaReadersWriters$RDFReaderRIOT_RDFXML
And the problem was with the following line:oModel.read(oInputStream, null);
EDIT
Then, following the suggestion mentioned by AndyS(see bellow), I updated my Jena libraries and used the 2.11.1-SNAPSHOT:
[1511] [Active ] [ ] [ 80] wrap_mvn_org.apache.jena_jena-core_2.11.1-SNAPSHOT (0)
[1512] [Active ] [ ] [ 80] wrap_mvn_org.apache.jena_jena-arq_2.11.1-SNAPSHOT (0)
[1515] [Active ] [ ] [ 80] wrap_mvn_org.apache.jena_jena-sdb_1.4.1-SNAPSHOT (0)
[1516] [Resolved ] [ ] [ 80] wrap_mvn_xerces_xercesImpl_2.11.0 (0)
[1521] [Active ] [ ] [ 80] wrap_mvn_org.apache.jena_jena-iri_1.0.1-SNAPSHOT (0)
But the exception is the same:
ClassNotFoundException: org.apache.jena.riot.adapters.JenaReadersWriters$RDFReaderRIOT_RDFXML
There is a fixed bug to do with handling .owl files. This maybe the issue for some of the exceptions you are seeing. The current development snapshots have this fixed.
Unrelated:
You have the wrong version of xerces for jena. You do not need icu4j anymore.
The root error is a ClassNotFoundException for the following class: org.apache.jena.riot.adapters.JenaReadersWriters$RDFReaderRIOT_RDFXML. It looks like this library is using dynamic reflection to load the class, e.g. using Class.forName(), which is a really bad thing to do.
Anyway you should be able to fix it by adding the package org.apache.jena.riot.adapters to your Import-Package list.
At last, it was certainly a problem of Jena libraries versions.
I solved it with the following ones:
xerces/xercesImpl/2.9.1
com.ibm.icu/icu4j/3.4.4
org.slf4j/slf4j-api/1.6.1
com.hp.hpl.jena/arq/2.8.7
com.hp.hpl.jena/jena/2.6.4
com.hp.hpl.jena/iri/0.8
com.hp.hpl.jena/sdb/1.3.4
As Andy S. suggested me: "the problem is in the bundling.jena-core makes a reflection call to set up RIOT, and RIOT installs JenaReadersWriters$RDFReaderRIOT_RDFXML. This is instantiated with call of Class.newInstance(). It looks like the latter is failing.
This may be because of classloaders as setup by the OSGi bundling. You probably want one bundle with all of the Jena jars in it. As I understand your reported setup, you have a separate bundle, hence a different classloader, hence not found."
I have not checked it but probably the solution is to make a unique bundle jar with at least 2.11.0 version:
org.apache.jena/jena-core/2.11.0
org.apache.jena/jena-arq/2.11.0
org.apache.jena/jena-sdb/1.4.0
org.apache.jena/jena-iri/1.0.0
xerces/xercesImpl/2.11.0`
Thanks for the post!!
I had the same issue and fixed it by just adding org.apache.jena.riot.adapters package at the imports of jena-core.jar.
E.g. BND configuration file of jena-core-2.11.1.jar:
version=2.11.1
Bundle-Version: ${version}
Bundle-Name: Jena CORE
Export-Package: !etc, !jena, !jena-log4j.properties, !jena.cmdline, !ont-policy.rdf, *; version=${version}
Import-Package: org.apache.jena.riot.adapters, *