My Processing code is below.
import hypermedia.video.*;
import processing.video.*;
import java.awt.Rectangle;
OpenCV opencv;
int width = 320;
int height = 240;
void setup() {
size( 320, 240 ); //set window size
opencv = new OpenCV( this ); //setup openCV
opencv.capture( width, height ); // open video stream
opencv.cascade( OpenCV.CASCADE_FRONTALFACE_ALT );
}
void draw(){
opencv.read();
image(opencv.image(), 0, 0);
Rectangle[] faces = opencv.detect( 1.2, 2, OpenCV.HAAR_DO_CANNY_PRUNING, 40, 40 );
noFill();
stroke(255,0,0);
for( int i=0; i<faces.length; i++ ) {
rect( faces[i].x, faces[i].y, faces[i].width, faces[i].height );
}
}
This code works for few seconds then an exception occurs.
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d961b22, pid=232, tid=4008
#
# JRE version: 6.0_33-b03
# Java VM: Java HotSpot(TM) Client VM (20.8-b03 mixed mode windows-x86 )
# Problematic frame:
# V [jvm.dll+0xa1b22]
#
# An error report file with more information is saved as:
# C:\Documents and Settings\Administrator\Desktop\processing-2.0b7\hs_err_pid232.log
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
#
i think it is a memory allocation issue with opencv_core.CvMemStorage. this is a class that's not exposed in your library. i am having the same problem. I'm using javacv (directly, not javacvpro) because i want to run multiple haar cascades (you can load only one with javacvpro or the older hypermedia.video.*). if i run ALL of them on EACH frame, i'm good. if i run a different detector on each frame (and then cycle thru the detectors again), i get this error after a few cycles.
the snippet below FAILS, but it is what i want to do (process each subsequent frame with a different detector):
// Using JavaCV directly, not JavaCVPro!
import com.googlecode.javacpp.Loader;
import com.googlecode.javacv.*;
import com.googlecode.javacv.*;
import com.googlecode.javacv.cpp.*;
String cascades[] = {
"haarcascade_eye.xml", // 0
"haarcascade_eye_tree_eyeglasses.xml", // 1
"haarcascade_frontalface_alt.xml" // 2
}
int detectors[] = {1,3};
// haar detectors to use
String cascPath = "C:/opencv/data/haarcascades/";
// preload multiple classifiers. can do this with javacvpro, not with javacv or hypermedia.video.*
void haarSetup() {
for (int i = 0; i < detectors.length; i++) {
String classifierFile = cascPath+cascades[detectors[i]];
classifier[i] =
new opencv_objdetect.CvHaarClassifierCascade(opencv_core.cvLoad(classifierFile));
}
storage = opencv_core.CvMemStorage.create();
opencv_core.cvClearMemStorage(storage); // is this needed? couldn't hurt, right?
}
// contains list of preloaded haar cascades. code not included here...
opencv_core.CvSeq features[] = new opencv_core.CvSeq[detectors.length];
int whichHaar = 0;
// run one cascade per frame, then cycle through them.
void processHaars(PImage piz) {
// convert to IplImage...
BufferedImage imgBuf = (BufferedImage) piz.getNative();
opencv_core.IplImage iplImgOut=opencv_core.IplImage.createFrom(imgBuf);
// do one haar cascade per invocation.
int ii = whichHaar;
features[ii] = opencv_objdetect.cvHaarDetectObjects(iplImgOut, classifier[ii], storage, 1.1, 3, opencv_objdetect.CV_HAAR_DO_CANNY_PRUNING);
whichHaar++;
if (whichHaar >= detectors.length){
whichHaar = 0;
// is THIS causing the problem??
opencv_core.cvClearMemStorage(storage);
}
}
this snippet WORKS forever, but i don't want what it does (run all detectors on a single frame):
void processHaars(PImage piz) {
// convert to IplImage...
BufferedImage imgBuf = (BufferedImage) piz.getNative();
opencv_core.IplImage iplImgOut=opencv_core.IplImage.createFrom(imgBuf);
for (ii=0; ii<detectors.length; ii++)
faces[ii] = opencv_objdetect.cvHaarDetectObjects(iplImgOut, classifier[ii], storage, 1.1, 3, opencv_objdetect.CV_HAAR_DO_CANNY_PRUNING);
opencv_core.cvClearMemStorage(storage);
}
If i find a full solution, I'll post it.
Related
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x5f0c25fe, pid=14780, tid=11168
#
# JRE version: Java(TM) SE Runtime Environment (7.0_80-b15) (build 1.7.0_80-b15)
# Java VM: Java HotSpot(TM) Client VM (24.80-b11 mixed mode, sharing windows-x86 )
# Problematic frame:
# C [ZBRGraphics.dll+0x25fe]
I keep getting this error when using the Zebra printer DLL in Java program.
public class Tester {
public static void main(String[] args) {
ZBRGraphics zGraphics = ZBRGraphics.INSTANCE;
String text = "Print this";
byte[] textB = text.getBytes(StandardCharsets.UTF_8);
String font= "Arial";
byte[] fontB = text.getBytes(StandardCharsets.UTF_8);
System.out.println(zGraphics.ZBRGDIDrawText(0, 0, textB, fontB, 12, 1, 0x0FF0000, 0));
}
}
public interface ZBRGraphics extends Library {
ZBRGraphics INSTANCE = (ZBRGraphics) Native.loadLibrary("ZBRGraphics", ZBRGraphics.class);
int ZBRGDIDrawText(int x, int y, byte[] text, byte[] font, int fontSize, int fontStyle, int color, int err);
}
I have the DLL in C:\Windows\System32 and in my 32 bit Java .
I'm using a 64 bit machine as my laptop for development.
If my google-fu skills are any good, you appear to be interfacing with the Zebra printer's API. According to the "ZXP1 & ZXP3 Software Developers Reference Manual" (found here), the Java mapping of the function is incorrect.
This is the actual C function prototype:
int ZBRGDIDrawText(
int x,
int y,
char *text,
char *font,
int fontSize,
int fontStyle,
int color,
int *err
)
As you can see, err is not an int, but a pointer to one. Also, since text and font are strings, you can just use a String as the Java type. Additionally, the API docs say that the return value is an int with either 1 for success or 0 for failure, meaning that you can use a boolean for ease of use.
The following Java mapping should be correct:
boolean ZBRGDIDrawText(
int x,
int y,
String text,
String font,
int fontSize,
int fontStyle,
int color,
IntByReference err
);
and you might use it like so:
IntByReference returnCode = new IntByReference(0);
boolean success = zGraphics.ZBRGDIDrawText(
0,
0,
"Print this",
"Arial",
12,
1,
0x0FF0000,
returnCode
);
if (success) {
System.out.println("success");
} else {
System.out.println("ZBRGDIDrawText failed with code " + returnCode.getValue());
}
We are using Tess4J/Tesseract to perform OCR on a webapp. On Windows everything works fine but when deployed on a Linux machine(CentOS 6.8) the program crashes and automatically kill the Apache tomcat server.
We are read more than one file(different file) simultaneously.if we run the OCR it running approximately 1 minutes after it through fatal error. Can you please suggest how to resolve?
A fatal error has been detected by the Java Runtime Environment:
SIGSEGV (0xb) at pc=0x00007f7d5934ff90, pid=17649,
tid=140176377489152
JRE version: Java(TM) SE Runtime Environment (8.0_60-b27) (build 1.8.0_60-b27)
Java VM: Java HotSpot(TM) 64-Bit Server VM (25.60-b23 mixed mode linux-amd64 compressed oops)
Problematic frame:
C [libtesseract.so.3.0.2+0x22cf90] tesseract::HistogramRect(unsigned char const*, int, int, int, int, int, int, int*)+0x70
Failed to write core dump. Core dumps have been disabled. To enable core dumping, try ulimit -c unlimited before starting Java again
I fixed it by resizing the image to a fixed size (you could do a percentage resize I guess) in javacv before passing it to tess4j.
Example of my resize method.
public static IplImage resize(IplImage img_source){
IplImage resized = IplImage.create(600, 480, img_source.depth(), img_source.nChannels());
cvResize(img_source,resized);
return resized;
}
Then I do my tesseract extraction below:
public static String extract(BufferedImage bi, Rectangle r) throws CvHandler, IOException, TesseractException{
ITesseract tess = new Tesseract();
String tessPath = getTess();
tess.setPageSegMode(1);
tess.setLanguage("eng");
tess.setDatapath(tessPath);
tess.setOcrEngineMode(TessOcrEngineMode.OEM_DEFAULT);
tess.setTessVariable("load_system_dawg", "false");
tess.setTessVariable("load_freq_dawg", "false");
tess.setTessVariable("tessedit_create_hocr", "0");
tess.setTessVariable("tessedit_char_whitelist","ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
String result = "";
if (!r.getBounds().isEmpty()){
try{
result = tess.doOCR(bi, r);
}catch(TesseractException e){
throw new CvHandler(e.getMessage());
}
}else result = tess.doOCR(bi);
return result;
}
Helper method for converting IplImage to BufferedImage Source:
public static BufferedImage convertIplToBuffered(IplImage img){
OpenCVFrameConverter.ToIplImage grabberConverter = new OpenCVFrameConverter.ToIplImage();
Java2DFrameConverter paintConverter = new Java2DFrameConverter();
Frame frame = grabberConverter.convert(img);
BufferedImage img_result = paintConverter.getBufferedImage(frame,1);
return img_result;
}
I'm working on raycast, but I have a big problem. Here is the error message.
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00007f2a67b5345b, pid=19347, tid=139820316944128
#
# JRE version: OpenJDK Runtime Environment (7.0_79-b14) (build 1.7.0_79-b14)
# Java VM: OpenJDK 64-Bit Server VM (24.79-b02 mixed mode linux-amd64 compressed oops)
# Derivative: IcedTea 2.5.5
# Distribution: Custom build (Wed Apr 15 12:39:15 UTC 2015)
# Problematic frame:
# C [libgdx-box2d64.so+0x3a45b] void b2DynamicTree::RayCast<b2WorldRayCastWrapper>(b2WorldRayCastWrapper*, b2RayCastInput const&) const+0x3ab
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
You can look the log file here http://paste.ubuntu.com/11287130/
Here is my callback and splitObj.
private final RayCastCallback callback = new RayCastCallback() {
#Override
public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {
//We can just slice the field and field is ChainShape
if (fixture.getShape() instanceof ChainShape) {
Body body = fixture.getBody();
List<Vector2> pointVec = rayCastMap.get(body);
if (pointVec == null) {
pointVec = new ArrayList<Vector2>();
rayCastMap.put(body, pointVec);
}
if (!pointVec.isEmpty() && !pointVec.get(0).equals(point)) {
pointVec.add(point.cpy());
splitObj(body, pointVec);
} else {
pointVec.add(point.cpy());
}
}
return 1;
}
};
private void splitObj(Body sliceBody, List<Vector2> splitedPoints) {
Vector2 a = splitedPoints.get(0);
Vector2 b = splitedPoints.get(1);
Array<Vector2> shape1Vertices = new Array<Vector2>();
shape1Vertices.addAll(a, b);
Array<Vector2> shape2Vertices = new Array<Vector2>();
shape2Vertices.addAll(a, b);
for (Vector2 vec : field.getVertices()) {
float determinant = det(a, b, vec);
if (determinant > 0) {
if (!shape1Vertices.contains(vec, false))
shape1Vertices.add(vec);
} else if (determinant < 0) {
if (!shape2Vertices.contains(vec, false))
shape2Vertices.add(vec);
}
}
GeometryUtils.arrangeClockwise(shape1Vertices);
GeometryUtils.arrangeClockwise(shape2Vertices);
FloatArray shape1 = arrayToFloatArray(shape1Vertices);
FloatArray shape2 = arrayToFloatArray(shape2Vertices);
FloatArray newShape;
float shape1Area = calculateArea(shape1);
float shape2Area = calculateArea(shape2);
box2dWorld.destroyBody(sliceBody);
ball.box2dBall.setActive(false);
float splicedArea;
if (det(a, b, ball.getPosition()) > 0) {
splicedArea = shape2Area * 100 / (shape1Area + shape2Area);
newShape = shape1;
} else {
splicedArea = shape1Area * 100 / (shape1Area + shape2Area);
newShape = shape2;
}
field.setIsSliced(true);
// setting the properties of the two newly created shapes
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.StaticBody;
bodyDef.position.set(sliceBody.getPosition());
FixtureDef fixtureDef = new FixtureDef();
// creating the first shape
PolygonShape polygonShape = new PolygonShape();
polygonShape.set(newShape.toArray());
fixtureDef.shape = polygonShape;
sliceBody = box2dWorld.createBody(bodyDef);
sliceBody.createFixture(fixtureDef);
}
I want to slice an object(ChainShape) and there is a ball which can move around inside this chainshape.I listen touchDown , touchDragged and touchUp for input.
touchDown gets first point of the line for raycast , touchDragged gets second point of the line and touchUp is calling raycast like this one.
box2dWorld.rayCast(callback, p2, p1);
When I call that , sometimes it gives this fatal error.What is my problem? What can I do?
Usually if you see a SIGSEGV Java Runtime Environment error while working with box2d then it is related to the usage of an already destroyed body / joint etc.
Take a look at your code here you destroy a body:
box2dWorld.destroyBody(sliceBody);
Then some lines later you try to use that already destroyed body again (!):
bodyDef.position.set(sliceBody.getPosition());
You cannot use destroyed bodies anymore! Try to destroy bodies only after you are sure you are not going to use them anymore.
I make an introduction of the scenario:
A web service (SpringMVC) through an action with parameters dynamically generate images with text and returns the response to the client.
This service processes about 500 images per minute.
The images are generated with the library SWT [1]. This works fine locally.
To test or production environments, the application is installed on a server without X (CentOS / Ubuntu). And that SWT can draw the images requires the DISPLAY environment variable set correctly. So on the server install Xvfb package to emulate an X environment virtually.
Xvfb: 1-screen 0 1x1x24-dpi 96 &
DISPLAY = localhost: 1.0
export DISPLAY
$TOMCAT/bin/startup.sh
This works fine the first few minutes, but eventually the memory occupied by the Xvfb process grows without limit (from 1 mb to 1.3 Gb and growing ...).
I tried with different configurations and parameters Xvfb [3], but have not had success.
Xvfb: 1-screen 0 1x1x24 -dpi 96 -noreset &
Xvfb: 1-screen 0 1x1x24 -dpi 96 -reset &
Xvfb: 1-screen 0 1x1x24 -dpi 96 -ld 262144 -ls 262144 -lf 1024 &
Took several days with this problem without a solution?
I could guide you more try or where freshened?
[1]
public BufferedImage drawImage () {
// example code, real code is more complex
FontData [] FontData fontData = new [] {new FontData ("Arial", 8, SWT.NORMAL)};
Display display = this.getDisplay ();
Image image = new Image (display, IMAGE_WIDTH, IMAGE_HEIGHT);
GC gc = new GC (image);
gc.setAntialias (SWT.ON);
gc.setInterpolation (SWT.HIGH);
gc.setBackground (display.getSystemColor (SWT.COLOR_WHITE));
gc.fillRectangle (0, 0, this.image.getBounds (). width, this.image.getBounds (). height);
gc.setFont (new Font (display, fontData [0]));
gc.setForeground (display.getSystemColor (SWT.COLOR_RED));
gc.drawText ("Text to draw in image", 5, 6);
BufferedImage bi = null;
bi = this.convertToAWT (hi.getImage (). getImageData ());
return bi;
}
#RequestMapping
public void RetrieveImage (HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType ("image / png");
BufferedImage image = drawImage ();
ByteArrayOutputStream os = new ByteArrayOutputStream ();
ImageIO.write (bi, "png", os);
InputStream is = new ByteArrayInputStream (os.toByteArray ());
IOUtils.copy (is, response.getOutputStream ());
}
Update:
Add getDisplay method.
private Display getDisplay() throws DrawImageException {
if (display == null || display.isDisposed()) {
LOGGER.debug("Initializing display...");
try {
display = Display.getDefault();
} catch (Exception e) {
throw new DrawImageException("Can't get default display", e);
}
}
return display;
}
Solved
As Baz and Niranjan said, the problem was with the release of SWT resources. Now work fine.
I'm trying to run the unit tests in the tess4j distribution currently. And while running one of the unit tests, java crashed with the following error:
TessBaseAPIGetIterator
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6718f834, pid=5612, tid=3592
#
# JRE version: 7.0_17-b02
# Java VM: Java HotSpot(TM) Client VM (23.7-b01 mixed mode, sharing windows-x86 )
# Problematic frame:
# C [libtesseract302.dll+0xf834] tesseract::TessBaseAPI::Init+0x34
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# G:\Final year project\eclipse stuff\Testings\hs_err_pid5612.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.sun.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
Can i please get help? i'm fairly sure that the DLL file is not corrupt and neither is my harddisk. i'm using the Windows 8 OS and the latest version of eclipse to build the project.
The code is:
import java.io.File;
import net.sourceforge.tess4j.*;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.PointerByReference;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.*;
import java.util.Arrays;
import javax.imageio.ImageIO;
import net.sourceforge.tess4j.TessAPI1.*;
import net.sourceforge.vietocr.ImageIOHelper;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
public class TesseractExample{
String datapath = "G:\\Final year project\\eclipse stuff\\tessdemo\\tessdata";
String language = "eng";
String expOCRResult = "The (quick) [brown] {fox} jumps!\nOver the $43,456.78 <lazy> #90 dog";
TessAPI1.TessBaseAPI handle;
public void testResultIterator() throws Exception {
System.out.println("TessBaseAPIGetIterator");
String lang = "eng";
File tiff = new File("eurotext.tif");
BufferedImage image = ImageIO.read(new FileInputStream(tiff)); // require jai-imageio lib to read TIFF
ByteBuffer buf = ImageIOHelper.convertImageData(image);
int bpp = image.getColorModel().getPixelSize();
int bytespp = bpp / 8;
int bytespl = (int) Math.ceil(image.getWidth() * bpp / 8.0);
TessAPI1.TessBaseAPIInit3(handle, datapath, lang);
TessAPI1.TessBaseAPISetPageSegMode(handle, TessAPI1.TessPageSegMode.PSM_AUTO);
TessAPI1.TessBaseAPISetImage(handle, buf, image.getWidth(), image.getHeight(), bytespp, bytespl);
TessAPI1.TessBaseAPIRecognize(handle, null);
TessAPI1.TessResultIterator ri = TessAPI1.TessBaseAPIGetIterator(handle);
TessAPI1.TessPageIterator pi = TessAPI1.TessResultIteratorGetPageIterator(ri);
TessAPI1.TessPageIteratorBegin(pi);
System.out.println("Bounding boxes:\nchar(s) left top right bottom confidence font-attributes");
int height = image.getHeight();
do {
Pointer ptr = TessAPI1.TessResultIteratorGetUTF8Text(ri, TessAPI1.TessPageIteratorLevel.RIL_WORD);
String word = ptr.getString(0);
TessAPI1.TessDeleteText(ptr);
float confidence = TessAPI1.TessResultIteratorConfidence(ri, TessAPI1.TessPageIteratorLevel.RIL_WORD);
IntBuffer leftB = IntBuffer.allocate(1);
IntBuffer topB = IntBuffer.allocate(1);
IntBuffer rightB = IntBuffer.allocate(1);
IntBuffer bottomB = IntBuffer.allocate(1);
TessAPI1.TessPageIteratorBoundingBox(pi, TessAPI1.TessPageIteratorLevel.RIL_WORD, leftB, topB, rightB, bottomB);
int left = leftB.get();
int top = topB.get();
int right = rightB.get();
int bottom = bottomB.get();
System.out.print(String.format("%s %d %d %d %d %f", word, left, top, right, bottom, confidence));
System.out.println(String.format("%s %d %d %d %d", str, left, height - bottom, right, height - top)); // training box coordinates
IntBuffer boldB = IntBuffer.allocate(1);
IntBuffer italicB = IntBuffer.allocate(1);
IntBuffer underlinedB = IntBuffer.allocate(1);
IntBuffer monospaceB = IntBuffer.allocate(1);
IntBuffer serifB = IntBuffer.allocate(1);
IntBuffer smallcapsB = IntBuffer.allocate(1);
IntBuffer pointSizeB = IntBuffer.allocate(1);
IntBuffer fontIdB = IntBuffer.allocate(1);
String fontName = TessAPI1.TessResultIteratorWordFontAttributes(ri, boldB, italicB, underlinedB,
monospaceB, serifB, smallcapsB, pointSizeB, fontIdB);
boolean bold = boldB.get() == TessAPI1.TRUE;
boolean italic = italicB.get() == TessAPI1.TRUE;
boolean underlined = underlinedB.get() == TessAPI1.TRUE;
boolean monospace = monospaceB.get() == TessAPI1.TRUE;
boolean serif = serifB.get() == TessAPI1.TRUE;
boolean smallcaps = smallcapsB.get() == TessAPI1.TRUE;
int pointSize = pointSizeB.get();
int fontId = fontIdB.get();
System.out.println(String.format(" font: %s, size: %d, font id: %d, bold: %b," +
" italic: %b, underlined: %b, monospace: %b, serif: %b, smallcap: %b",
fontName, pointSize, fontId, bold, italic, underlined, monospace, serif, smallcaps));
} while (TessAPI1.TessPageIteratorNext(pi, TessAPI1.TessPageIteratorLevel.RIL_WORD) == TessAPI1.TRUE);
}
public static void main(String[] args) {
TesseractExample instance=new TesseractExample();
try {
instance.testResultIterator();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
Thank you!
Looks like handle has not been initialized.
handle = TessAPI1.TessBaseAPICreate();