Reputation:
I am learning OpenCV.I am using OpenCV 3.2.0 jar. I am trying to open my webcam with it but getting an error. here is my Code
Java:
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.videoio.VideoCapture;
public class WebCam {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
VideoCapture videoCapture = new VideoCapture(0);
videoCapture.open("the video");
Mat frame = new Mat();
while (true){
videoCapture.read(frame);
}
}
}
The Error:
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00007fffbe7d605d, pid=922, tid=0x0000000000000307
#
# JRE version: Java(TM) SE Runtime Environment (8.0_101-b13) (build 1.8.0_101-b13)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.101-b13 mixed mode bsd-amd64 compressed oops)
# Problematic frame:
# C [libobjc.A.dylib+0x705d] objc_msgSend+0x1d
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /path /hs_err_pid922.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
#
Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
I have followed a tutorial for python and wrote this code
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while 1:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('THE CAMERA FOR FACE AND EYE',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
and it worked .
Please help me out. how can i solve this. on the other hand, i am not finding enough well-explained material online. Please suggest me some good resource.Thanks in advance
Upvotes: 0
Views: 2539
Reputation: 1388
The problem with your code is that you are trying to open VideoCapture again by using videocapture.open("the video")
without passing the type of the file. Your code to capture a stream from webcam should just be:
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.videoio.VideoCapture;
public class WebCam {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
VideoCapture videoCapture = new VideoCapture(0);
Mat frame = new Mat();
while (true){
videoCapture.read(frame);
showResult(frame);
}
}
public static void showResult(Mat img) {
Imgproc.resize(img, img, new Size(640, 480));
MatOfByte matOfByte = new MatOfByte();
Highgui.imencode(".jpg", img, matOfByte);
byte[] byteArray = matOfByte.toArray();
BufferedImage bufImage = null;
try {
InputStream in = new ByteArrayInputStream(byteArray);
bufImage = ImageIO.read(in);
JFrame frame = new JFrame();
frame.getContentPane().add(new JLabel(new ImageIcon(bufImage)));
frame.pack();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
In order to display the frame you need to use the solution mentioned here as there is no Java highgui wrapper yet. I hope this helps.
Upvotes: 1