Help with webcam capture

1,359 views
Skip to first unread message

AzazelPL

unread,
Feb 25, 2011, 10:48:17 AM2/25/11
to javacv
Hello
Not really sure this is the right place to post (if not would You be
so kind and point me to the right link ;)

I am trying to write a simple program that will capture images from a
webcam and work on them.

I have set the
classifierName = "C:\\OpenCV2.2\\data\\haarcascades\
\haarcascade_frontalface_alt.xml";

Every time i try to run the sample code i encounter a fatal error (1)
after

FrameRecorder recorder = new OpenCVFrameRecorder("output.avi",
width, height);
recorder.start();

I have installed the latest javacv and opencv . Do I need something
else?

My second question I have 2 webcams (1 internal in laptop 1 external
via usb [I'm trying to run the probgram only with the internal
activated]) chow can I make javacv capture images ONLY from the
external?

Thx for Your help :)


(1) The fatal error looks like this

#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00905a5f, pid=4908,
tid=3760
#
# JRE version: 6.0_23-b05
# Java VM: Java HotSpot(TM) Client VM (19.0-b09 mixed mode, sharing
windows-x86 )
# Problematic frame:
# C 0x00905a5f
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

--------------- T H R E A D ---------------

Current thread (0x02449400): JavaThread "main" [_thread_in_native,
id=3760, stack(0x021b0000,0x02200000)]

siginfo: ExceptionCode=0xc0000005, writing address 0x02e07796

Registers:
EAX=0x021ff53c, EBX=0x69803600, ECX=0x696f67c0, EDX=0x021ff720
ESP=0x021ff528, EBP=0x00000001, ESI=0x04af7cc8, EDI=0x021ff722
EIP=0x00905a5f, EFLAGS=0x00010296

Register to memory mapping:

EAX=0x021ff53c
0x021ff53c is pointing into the stack for thread: 0x02449400
"main" prio=6 tid=0x02449400 nid=0xeb0 runnable [0x021ff000]
java.lang.Thread.State: RUNNABLE

EBX=0x69803600
0x69803600 is pointing to unknown location

ECX=0x696f67c0
0x696f67c0 is pointing to unknown location

EDX=0x021ff720
0x021ff720 is pointing into the stack for thread: 0x02449400
"main" prio=6 tid=0x02449400 nid=0xeb0 runnable [0x021ff000]
java.lang.Thread.State: RUNNABLE

ESP=0x021ff528
0x021ff528 is pointing into the stack for thread: 0x02449400
"main" prio=6 tid=0x02449400 nid=0xeb0 runnable [0x021ff000]
java.lang.Thread.State: RUNNABLE

EBP=0x00000001
0x00000001 is pointing to unknown location

ESI=0x04af7cc8
0x04af7cc8 is pointing to unknown location

EDI=0x021ff722
0x021ff722 is pointing into the stack for thread: 0x02449400
"main" prio=6 tid=0x02449400 nid=0xeb0 runnable [0x021ff000]
java.lang.Thread.State: RUNNABLE


Top of Stack: (sp=0x021ff528)
0x021ff528: 692f75de 00000101 021ff53c 021ff61c
0x021ff538: 00000000 021ff6c9 00001000 00000004
0x021ff548: 05b0cfe8 05140000 05b0cfe0 00000088
0x021ff558: 00000000 0018359a 00000000 00000000
0x021ff568: 00000000 00000000 87a00000 00000000
0x021ff578: ffffffff ffffffff 021ff61c 00000000
0x021ff588: 00000000 00000000 021ff630 00000000
0x021ff598: 00001000 00000000 00000004 00000000

Instructions: (pc=0x00905a5f)
0x00905a4f: 00 00 00 00 00 00 00 00 00 1e 00 00 00 20 00 00
0x00905a5f: 00 b0 5a 82 c0 00 f9 ff ff 00 00 00 00 00 00 00


Stack: [0x021b0000,0x02200000], sp=0x021ff528, free space=317k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code,
C=native code)
C 0x00905a5f

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j com.googlecode.javacv.cpp.opencv_highgui.cvCreateVideoWriter(Ljava/
lang/String;IDLcom/googlecode/javacv/cpp/opencv_core$CvSize;I)Lcom/
googlecode/javacv/cpp/opencv_highgui$CvVideoWriter;+0
j com.googlecode.javacv.OpenCVFrameRecorder.start()V+28
j Demo.main([Ljava/lang/String;)V+369
v ~StubRoutines::call_stub

Samuel Audet

unread,
Feb 25, 2011, 10:10:06 PM2/25/11
to jav...@googlegroups.com
On 2011-02-26 00:48, AzazelPL wrote:
> Hello
> Not really sure this is the right place to post (if not would You be
> so kind and point me to the right link ;)

Yes of course

> I am trying to write a simple program that will capture images from a
> webcam and work on them.
>
> I have set the
> classifierName = "C:\\OpenCV2.2\\data\\haarcascades\
> \haarcascade_frontalface_alt.xml";
>
> Every time i try to run the sample code i encounter a fatal error (1)
> after
>
> FrameRecorder recorder = new OpenCVFrameRecorder("output.avi",
> width, height);
> recorder.start();
>
> I have installed the latest javacv and opencv . Do I need something
> else?

Ah, it seems the precompiled highgui of OpenCV 2.2 does not work with
the "CV_FOURCC_DEFAULT", but it works with "-1", so I guess I will
change the default in the next release of JavaCV.. for now, you can work
around that by putting this line before "recorder.start()":
recorder.setCodecID(-1);

> My second question I have 2 webcams (1 internal in laptop 1 external
> via usb [I'm trying to run the probgram only with the internal
> activated]) chow can I make javacv capture images ONLY from the
> external?

Just use another number than "0" when creating a new FrameGrabber. Try
1, 2, etc..

Samuel

AzazelPL

unread,
Feb 26, 2011, 4:59:49 PM2/26/11
to javacv
I did what You told me and here is what happens:

When asked to chose the webcam if I chose the external USB pccam i get
this error:

"
Exception in thread "main" java.lang.Exception:
cvCreateCameraCapture() Error: Could not create camera capture.
at
com.googlecode.javacv.OpenCVFrameGrabber.start(OpenCVFrameGrabber.java:
98)
at Demo.main(Demo.java:36)
"

on line " grabber.start(); "
(Is it the cammeras fault, non compatible ?)

The above is for
FrameGrabber grabber = new OpenCVFrameGrabber(-1); and for 0

When I change the number to any >0 i get the same error.

If I chose the internal cammera I get another list to select a
compressor

Anything other than Microsoft RLE gives me a black sample tile (a
frame with full black inside) and a black avi output file

the RLE crashes the application
"
Exception in thread "main" java.lang.Exception: cvCreateVideoWriter():
Could not create a writer
at
com.googlecode.javacv.OpenCVFrameRecorder.start(OpenCVFrameRecorder.java:
55)
at Demo.main(Demo.java:76)
"

Where line 76 is recorder.start();

Adam

Samuel Audet

unread,
Feb 26, 2011, 9:18:04 PM2/26/11
to jav...@googlegroups.com
On 2011-02-27 06:59, AzazelPL wrote:
> I did what You told me and here is what happens:
>
> When asked to chose the webcam if I chose the external USB pccam i get
> this error:
>
> "
> Exception in thread "main" java.lang.Exception:
> cvCreateCameraCapture() Error: Could not create camera capture.
> at
> com.googlecode.javacv.OpenCVFrameGrabber.start(OpenCVFrameGrabber.java:
> 98)
> at Demo.main(Demo.java:36)
> "
>
> on line " grabber.start(); "
> (Is it the cammeras fault, non compatible ?)
>
> The above is for
> FrameGrabber grabber = new OpenCVFrameGrabber(-1); and for 0

-1 is not a valid camera number

> When I change the number to any>0 i get the same error.

Then OpenCV does not see any other camera than the first one it seems,
so you should contact the OpenCV people to understand why your camera is
not supported

> If I chose the internal cammera I get another list to select a
> compressor
>
> Anything other than Microsoft RLE gives me a black sample tile (a
> frame with full black inside) and a black avi output file

I am not responsible for what OpenCV captures. If it captures all black
images, then JavaCV will get all black images. You should contact the
OpenCV people to understand why your camera is not supported

> the RLE crashes the application
> "
> Exception in thread "main" java.lang.Exception: cvCreateVideoWriter():
> Could not create a writer
> at
> com.googlecode.javacv.OpenCVFrameRecorder.start(OpenCVFrameRecorder.java:
> 55)
> at Demo.main(Demo.java:76)
> "

It seems OpenCV does not like "Microsoft RLE". You should, again,
contact the OpenCV people to understand why that is

Samuel

AzazelPL

unread,
Feb 28, 2011, 4:53:28 PM2/28/11
to javacv
This is how I worked my way AROUND the problem

After some research I found out some things that might be helpfull to
others.
IMPORTANT this is not a good solution THIS IS A BIG WAY AROUND the
problem.

At first I read that it is a common bug and if You try to catch a
image a second time after a few second break it will be ok.
*Did not work for me.

Second I found out that the problem could be 64bit OS
Downgraded to win 7 32 bit
*Did not help.

But After another downgrade to win xp 32bit sp3 everything started to
work like a charm.

Well almost like a charm.

As I wrote above I have 2 cameras , no matter what i wrote in
OpenCVFrameGrabber( number ); I either got an error or the image from
the internal cammera.
I solved this by manually deactivating the internal camera in the
hardware menager and everything works.

Hope this will help :)
Adam

Samuel Audet

unread,
Feb 28, 2011, 9:50:25 PM2/28/11
to jav...@googlegroups.com
On 2011-03-01 06:53, AzazelPL wrote:
> This is how I worked my way AROUND the problem
>
> After some research I found out some things that might be helpfull to
> others.
> IMPORTANT this is not a good solution THIS IS A BIG WAY AROUND the
> problem.
>
> At first I read that it is a common bug and if You try to catch a
> image a second time after a few second break it will be ok.
> *Did not work for me.
>
> Second I found out that the problem could be 64bit OS
> Downgraded to win 7 32 bit
> *Did not help.
>
> But After another downgrade to win xp 32bit sp3 everything started to
> work like a charm.

Ah, I remember rumors about Microsoft dropping support for Video for
Windows since Vista.. I guess they were true.

I suppose this means we need to use DirectShow to capture from Webcams
in Windows Vista and 7. OpenCV supports capture from DirectShow via
another library called videoInput, but it's not compiled in by default.

>
> Well almost like a charm.
>
> As I wrote above I have 2 cameras , no matter what i wrote in
> OpenCVFrameGrabber( number ); I either got an error or the image from
> the internal cammera.
> I solved this by manually deactivating the internal camera in the
> hardware menager and everything works.

Hum, you could try numbers starting from 200, which correspond to VFW.
i.e.: 201, 202..

Thanks for investigating and reporting!

Samuel

Gary Cunningham

unread,
Mar 1, 2011, 8:42:48 AM3/1/11
to jav...@googlegroups.com
 I am using Windows 7 and have been successful in capturing images from my webcam using OpenCVFrameGrabber(0), and displaying them in a CanvasFrame using displayFrame.showImage(capImage).  I can loop the FrameGrabber and show captured video just fine(although it's a bit choppy). The problem is this: I have a main application JFrame that contains:
A Controller panel(buttons for start camera, stop camera, snap photo, and cancel.
A panel to display the image that was captured when 'snap photo' is pressed.

When I start the app, and press the start camera button, my CanvasFrame comes up and displays the running video.  As soon as this happens, the video keeps playing, but the rest of the app stops responding to mouse input.  The button don't work(the stop button calls FrameGrabber.stop(), and I can't close either window.  I have to kill the process in netbeans.  For my loop I am using essentially what you used in the example on the JavaCV home page.

Samuel Audet

unread,
Mar 1, 2011, 8:12:18 PM3/1/11
to jav...@googlegroups.com

Would you have some code to show us? The shortest program that causes the problem...

Samuel

2011/03/01 22:42 "Gary Cunningham" <grcu...@gmail.com>:

Gary Cunningham

unread,
Mar 4, 2011, 12:48:00 PM3/4/11
to jav...@googlegroups.com
 
Here is a small program that demonstrates my problem.  It's just a frame that has two buttons to start and stop the video capture.  Once I press the start button the camera starts, the frames are displayed in the CanvasFrame, and the app no longer accepts any mouse input, even to close the windows.
All my programming classes have been in C or C++, so I'm very new to Java. This project has to be done in Java though, so I'm learning as I go. Please forgive me if I've made a stupid mistake..
 
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import com.googlecode.javacpp.Loader;
import com.googlecode.javacv.*;
import com.googlecode.javacv.cpp.*;
import static com.googlecode.javacv.cpp.opencv_core.*;
 
public class CFrameTest extends JFrame {
    JButton jbtStart;
    JButton jbtStop;
    CanvasFrame frame;
    FrameGrabber grabber;
    IplImage grabbedImage;
    public CFrameTest() {
        super("test");
        setSize(250, 100);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(1, 2));
        jbtStart = new JButton("Start camera");
        jbtStop = new JButton("Stop camera");
        this.add(jbtStart);
        this.add(jbtStop);
        setVisible(true);
        jbtStart.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("Startcap");
                        jbtStart.setEnabled(false);
                        jbtStop.setEnabled(true);
                        try {
                            startCamera();
                        } catch (Exception ex) {
                            Logger.getLogger(CFrameTest.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                });
        jbtStop.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("Stopcap");
                        jbtStart.setEnabled(true);
                        jbtStop.setEnabled(false);
                        frame.setVisible(false);
                    }
                });
        Loader.load(opencv_objdetect.class);
    }
    public static void main(String[] args) {
        CFrameTest testFrame = new CFrameTest();
    }
    public void startCamera() throws Exception {
        frame = new CanvasFrame("Test");
        grabber = new OpenCVFrameGrabber(0);
        CvMemStorage storage = CvMemStorage.create();
        grabber.start();
        grabbedImage = grabber.grab();
        while (frame.isVisible() && (grabbedImage = grabber.grab()) != null) {
            frame.showImage(grabbedImage);
            cvClearMemStorage(storage);
        }
        grabber.stop();
    }
}


Samuel Audet

unread,
Mar 4, 2011, 8:07:35 PM3/4/11
to jav...@googlegroups.com
On 2011-03-05 02:48, Gary Cunningham wrote:
> Here is a small program that demonstrates my problem. It's just a frame
> that has two buttons to start and stop the video capture. Once I press
> the start button the camera starts, the frames are displayed in the
> CanvasFrame, and the app no longer accepts any mouse input, even to
> close the windows.
> All my programming classes have been in C or C++, so I'm very new to
> Java. This project has to be done in Java though, so I'm learning as I
> go. Please forgive me if I've made a stupid mistake..

I see, your startCamera() method is running on the EDT (Event Dispatch
Thread), so it's a bit normal for the UI to freeze in that case. You
have to run long-lasting operations on the separate thread. An easy way
to do that is by using a SwingWorker:
http://download.oracle.com/javase/tutorial/uiswing/concurrency/worker.html

Samuel

Gary Cunningham

unread,
Mar 14, 2011, 10:18:49 PM3/14/11
to jav...@googlegroups.com
Swingworkers worked perfectly, thanks for your help and responsiveness!
Gary

Samuel Audet

unread,
May 11, 2011, 1:42:22 AM5/11/11
to jav...@googlegroups.com
Hello all,

Just to let you know that I fixed that in the latest release:
* New `videoInputLib` wrapper and corresponding
`VideoInputFrameGrabber` to capture using DirectShow, useful under
Windows 7 where OpenCV and FFmpeg can fail to capture using Video for
Windows (issue #58)

Samuel

On 2011-03-01 06:53, AzazelPL wrote:

Reply all
Reply to author
Forward
0 new messages