OpenCV (packaged by OpenPnP) library not loading - java

I am trying to use OpenVC and encounter an erorr that says that it is not in java.library.path. I have followed the github page for this version of OpenCV (https://github.com/openpnp/opencv) but still the probelm persisits, I tried using maven but it would still not locate the file.
I tried to use maven but the dependency would not work, I then used the inbuilt library system in IntelliJ but I am constantly getting this error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java460 in java.library.path: /Users/ranveerbehl/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2434)
at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:848)
at java.base/java.lang.System.loadLibrary(System.java:2015)
at Main.main(Main.java:13)
I am trying to compress video files as my previous library (IVCompressor) just plain stopped working one day and after days of troubleshooting, I was not able to fix it. Here is the code I wrote for reference:
public static void main(String[] args){
// Load the OpenCV library
nu.pattern.OpenCV.loadLocally();
System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME);
// Define the input and output file paths
String inputFile = "input.mov";
String outputFile = "output.mov";
// Create a VideoCapture object to read the input video
VideoCapture capture = new VideoCapture(inputFile);
// Get the video frames per second
double fps = capture.get(Videoio.CAP_PROP_FPS);
// Get the video frame size
int frameWidth = (int) capture.get(Videoio.CAP_PROP_FRAME_WIDTH);
int frameHeight = (int) capture.get(Videoio.CAP_PROP_FRAME_HEIGHT);
// Create a VideoWriter object to write the output video
VideoWriter writer = new VideoWriter(outputFile, VideoWriter.fourcc('M', 'P', '4', 'V'), fps, new Size(frameWidth, frameHeight));
// Read and write the video frames
Mat frame = new Mat();
while (capture.read(frame)) {
writer.write(frame);
}
// Release the resources
capture.release();
writer.release();
}
All imports are imported.
I feel the main probelm lies in these two lines of code if not with the library installation itself:
// Load the OpenCV library
nu.pattern.OpenCV.loadLocally();
System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME);
OpenCV version 4.6.0.
Thank you.

Related

com.sun.jdi.InvocationException occurred invoking method with ScreenRegion s = new DesktopScreenRegion() - Sikuli

I am using below sikuli code to make use of sikuli methods like click in Selenium,but when control come to ScreenRegion s = new DesktopScreenRegion() line, I observed com.sun.jdi.InvocationException occurred invoking method in 's' reference and at latst getting another exception that java.lang.NoSuchMethodError: com.google.common.base.Objects.toStringHelper(Ljava/lang/Object;)Lcom/google/common/base/Objects$ToStringHelper; No complier issues everything is fine, below are the jars/libraries used -
Java version: 8
Selenium version:selenium-server-standalone-3.4.0
Sikuli version:sikuli-api-1.0.2-standalone
Note: Working fine with selenium-server-standalone-2.53.0 version,below is the is tha code -
try
{
fw=new File(sImagePath);
// Specify an image as the target to find on the screen
Target imageTarget = new ImageTarget(fw);
imageTarget.setMinScore(0.4);
// ***** in below line i am getting InvocationException ********
ScreenRegion s = new DesktopScreenRegion();
s.wait(imageTarget, waitTime);
ScreenRegion r = s.find(imageTarget);
Canvas canvas = new DesktopCanvas();
canvas.addBox(r).display(1);
Mouse mouse = new DesktopMouse();
mouse.click(r.getCenter());
}
Can anyone, please help me to resolve the issue, Thanks

getting error : java.lang.UnsatisfiedLinkError: Native Library C:\opencv\build\java\x64\opencv_java300.dll

I am running a servlet program to read an image using opencv,
getting error :
java.lang.UnsatisfiedLinkError: Native Library C:\opencv\build\java\x64\opencv_java300.dll already loaded in another classloader . When restarting the IDE it works fine.
I loaded System.loadLibrary ( Core.NATIVE_LIBRARY_NAME ) ; in servlet only ones.
Can anybody suggest a solution for how to unload it. And also anybody know how to read an image from browser using opencv in java.?
It is because the library is not in the system path, it needs to first added to the system path, then load. First extract the OpenCV to C drive something like this c:\opencv\... then use this code below to during initializing, it will automatically load the OpenCV lib in windows environment.
public static void loadOpenCV_Lib() throws Exception {
String model = System.getProperty("sun.arch.data.model");
String libraryPath = "C:/opencv/build/java/x86/";
if(model.equals("64")) {
libraryPath = "C:/opencv/build/java/x64/";
}
System.setProperty("java.library.path", libraryPath);
Field sysPath = ClassLoader.class.getDeclaredField("sys_paths");
sysPath.setAccessible(true);
sysPath.set(null, null);
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
And also it will automatically detect the system model and load the lib according to the system model.

API to read text from Image file using OCR

I am looking out for an example code or API name from OCR (Optical character recognition) in Java using which I can extract all text present from an image file. Without comparing it with any image which I am doing using below code.
public class OCRTest {
static String STR = "";
public static void main(String[] args) {
OCR l = new OCR(0.70f);
l.loadFontsDirectory(OCRTest.class, new File("fonts"));
l.loadFont(OCRTest.class, new File("fonts", "font_1"));
ImageBinaryGrey i = new ImageBinaryGrey(Capture.load(OCRTest.class, "full.png"));
STR = l.recognize(i, 1285, 654, 1343, 677, "font_1");
System.out.println(STR);
}
}
You can try Tess4j or JavaCPP Presets for Tesseract. I perfer later as its easier than the former.
Add the dependency to your pom `
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>tesseract-platform</artifactId>
<version>3.04.01-1.3</version>
</dependency>
`
And its simple to code
import org.bytedeco.javacpp.*;
import static org.bytedeco.javacpp.lept.*;
import static org.bytedeco.javacpp.tesseract.*;
public class BasicExample {
public static void main(String[] args) {
BytePointer outText;
TessBaseAPI api = new TessBaseAPI();
// Initialize tesseract-ocr with English, without specifying tessdata path
if (api.Init(null, "eng") != 0) {
System.err.println("Could not initialize tesseract.");
System.exit(1);
}
// Open input image with leptonica library
PIX image = pixRead(args.length > 0 ? args[0] : "/usr/src/tesseract/testing/phototest.tif");
api.SetImage(image);
// Get OCR result
outText = api.GetUTF8Text();
System.out.println("OCR output:\n" + outText.getString());
// Destroy used object and release memory
api.End();
outText.deallocate();
pixDestroy(image);
}
}
Tess4j is little complex as its requires specific VC++ redistributable package to be installed.
You can try javaocr on sourceforge: http://javaocr.sourceforge.net/
There is also a great example with an applet which uses Encog: http://www.heatonresearch.com/articles/42/page1.html
That said, OCR requires a lot of power, so it means that if you are looking for a heavy use, you should look after OCR libraries written in C and integrate that with Java.
OCR is hard. So be sure to qualify your needs before adventuring yourself in it.
Tesseract and opencv (with javacv for integration for instance) are common choices. There are also commercial solutions such as ABBYY FineReader Engine and ABBYY Cloud OCR SDK.
Open Source OCR engine is available from Google for OCR.
It can be processed using CMD. You can process the CMD using java for web applications easily. Please visit https://www.youtube.com/watch?v=Mjg4yyuqr5E
. You will get the step by step details to process OCR using CMD.

How to open and process a video file like .mpeg or .avi using openCV's VideoCapture method in Java

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;
import org.opencv.highgui.VideoCapture;
import org.opencv.imgproc.Imgproc;
public class Video
{
public static void main(String[] args)
{
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
VideoCapture cap = new VideoCapture(0);
cap.open(1);
if(!cap.isOpened())
{
System.out.println("No camera");
}
else
{
System.out.println("Yes. Camera");
}
Mat frame = new Mat();
cap.retrieve(frame);
Highgui.imwrite("me1.jpg", frame);
Mat frameBlur = new Mat();
Imgproc.blur(frame, frameBlur, new Size(5,5));
Highgui.imwrite("me2-blurred.jpg", frameBlur);
Imgproc.GaussianBlur(frame, frameBlur, new Size(25, 25), 20);
Highgui.imwrite("me3-blurred.jpg", frameBlur);
cap.release();
}
}
I have used this code to open my Camera device and capture 3 different frames and made some operations on it. But, I couldn't open a file like .avi/.mpg/.mp4 etc., using {n_open} method of VideoCapture. There is a method in the VideoCapture implementation here. But because its a private and native method, that method can't be accesses using VideoCapture's object.
Could some one help how to do that using pure OpenCV 2.4.6 and Java
(Please dont suggest solution using Processing libraries)
Take a look on OpenCV 2.4.8. The VideoCapture API has been extended of public VideoCapture(String filename) method.
The question remains why this feature has been implemented so late.
If using of recent version of OpenCV isn't acceptable for you for some reason, you have several options:
Rebuild OpenCV by yourself with this method marked as public
HACKY ONE: Make your copy of VideoCapture class (or extend original one and play with reflection) with public VideoCapture(String) constructor. Then give support for native method private static native long n_VideoCapture(java.lang.String filename) by creating your own DLL using C++ OpenCV API. (Tested!)
I run into the same problem and this worked for me:
load the ffmpeg library
System.loadLibrary("opencv_ffmpeg300_64");
Open the file:
VideoCapture vC = new VideoCapture("res/video.mp4");
Copy opencv_ffmpeg300_64.dll from opencv\build\x64\vc11\bin to
opencv\build\java\x64
Please note that 64 and .dll may differ from an OS to another, those are for Windows x64

Unable to load library 'gsdll32'

I am running following code to create bmp image from pdf using Ghost4j
i have a commad which is executed by GhostScript generator to generate Bmp image of a page from pdf.
Code is:
package ghost;
import net.sf.ghost4j.Ghostscript;
import net.sf.ghost4j.GhostscriptException;
public class GhostDemo {
public static void main(String[] a){
Ghostscript gs = Ghostscript.getInstance(); //create gs instance
String[] gsArgs = new String[10];/*command string array*/
gsArgs[0] = "-dUseCropBox";/*use crop box*/
gsArgs[1] = "-dNOPAUSE";
gsArgs[2] = "-dBATCH";
gsArgs[3] = "-dSAFER";
gsArgs[3] = "-r300";
gsArgs[4] = "-sDEVICE=bmp16m";
gsArgs[6] = "-dTextAlphaBits=4";
gsArgs[5] = "-sOutputFile=C:/PagesWorkspace/1/masterData/1.bmp";/*bmp file location with name*/
gsArgs[6] = "C:/MasterWorkspace/pipeline.pdf";/*pdf location with name*/
try {
gs.initialize(gsArgs); /*initialise ghost interpreter*/
gs.exit();
} catch (GhostscriptException e) {
e.printStackTrace();
}
}
}
i am getting Exception
Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'gsdll32': The specified module could not be found.
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:145)
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:188)
at com.sun.jna.Library$Handler.<init>(Library.java:123)
at com.sun.jna.Native.loadLibrary(Native.java:255)
at com.sun.jna.Native.loadLibrary(Native.java:241)
at net.sf.ghost4j.GhostscriptLibraryLoader.loadLibrary(GhostscriptLibraryLoader.java:36)
at net.sf.ghost4j.GhostscriptLibrary.<clinit>(GhostscriptLibrary.java:32)
at net.sf.ghost4j.Ghostscript.initialize(Ghostscript.java:292)
at ghost.GhostDemo.main(GhostDemo.java:22)
Can any one tell me why i am getting this exception?
Do you have Ghostscript installed at all?
If yes, which version?
If yes, in which location?
Does it include a file gsdll32.dll?
If not, download the Ghostscript installer for Win32 and run it. After the installation, there should be a file gsdll32.dll in directory %your_install_dir%\gs\gs9.05\bin\
Pasting dll file in eclipse project made my program work!
For the SO community, another thing to check with this error is that you are using 32-bit Java. If your instance of Java is 64-bit, you will get the exact same message:
Unable to load library 'gsdll32': The specified module could not be found.
without any further explanation even if you are pointing to the correct dll.

Categories