Asked — Edited
Resolved Resolved by ptp!

Outputting Snapshop From Videostream In C++

Has any one had success with setting up a camera stream and saving a snapshot to a file using c++?

This is what I have so far:


EZ_B::EZB ^ezb;
ezb = gcnew EZ_B::EZB("MyEZB");

EZ_B::Camera ^ezbCam;
ezbCam = gcnew EZ_B::Camera(ezb);

ezbCam->StartCamera(EZ_B::Camera::GetVideoCaptureDevices()[1], 320, 240);
ezbCam->SaveImageAsJPEG("c:\test.jpg");

I can confirm that the camera is on by running:


BOOL camOK = ezbCam->Camera::IsActive;
cout << "Camera on? " << camOK << "\n";

What am I missing here?

Best, Lars


ARC Pro

Upgrade to ARC Pro

Harnessing the power of ARC Pro, your robot can be more than just a simple automated machine.

PRO
Synthiam
#1  

Most likely the camera hasn't started immediately following the Start - because it's hardware related which means there is a slight delay for the hardware to kick in.

#2  

Ah okay, so you would suggest to insert a delay before calling saveImage?

PRO
Synthiam
#3  

Actually that might not be your problem at all...

"C:\test.jpg" would escape the \t to be a tab character. I suspect if you checked the ezb log, it would have an error reported that it can't save the file due to the file being c:[tab]est.jpg

you need to escape the \ slash


EZ_B::EZB ^ezb;
ezb = gcnew EZ_B::EZB(&quot;MyEZB&quot;);

EZ_B::Camera ^ezbCam;
ezbCam = gcnew EZ_B::Camera(ezb);

ezbCam-&gt;StartCamera(EZ_B::Camera::GetVideoCaptureDevices()[1], 320, 240);
ezbCam-&gt;SaveImageAsJPEG(&quot;c:\\test.jpg&quot;);

#4  

Hm well I don't get a warning and escaping it does not change anything. However, when I add


Sleep(1000);

after calling the camera device I can get the camera to return an image size, which it didn't before. It still does not save the file though.

Any other bright ideas?

By the way, what is that smiley doing in my code DJ?

#5  

What happens if you use c:test.jpg instead of c:\test.jpg? I'm not certain, but I think DJ is saying the \t part of the c:\test.jpg character sequence is being interpreted as an escape sequence.

The smiley is usually caused by the UBB Parser interpreting the combination of a quote and a right paren as a Winky type emoticon. I just tried to demonstrate that by putting a space between the quote and the right paren to show how you could eliminate it (in my original version of this post) but it didn't do it. Maybe it's been fixed?

#6  

I've tried "test.jpg", "c:\test.jpg", and "c://test.jpg". Same result. No errors or warnings, but also no jpeg.

#7  

So you actually tried c:test.jpg?

'test.jpg alone would try to save an image to the current default directory, whatever that may be at any time. Who knows, there may be one there. Unlike, c:\test.jpg which would try to place an image in the root directory of the C-drive, c:test.jpg will try to put one in whatever directory is currently active on the C-drive. The whole idea is to not have a slash of any kind in the path to eliminate the possibility of the slash causing an inadvertent escape to be generated. The image could even be in the directory where your ARC files are.

Basically, what I'm saying is that there may be a test.jpg image on your computer, just that it may not be in the root directory of the C-drive. Thereby leading you to conclude the image has not been stored.

#8  

Yes I realize that, but regardless of how I try to save, it's nowhere on my harddrive. This leads me to think that there is something missing in my code, I just can't figure out what.

PRO
USA
#9  

Lars,

Are you using VS to compile & run the code or using cmake to compile and run the program in the command prompt ?

#10  

Cmake to generate the vs solution, after that vs to build and run

PRO
USA
#11  

some guesses as usual ...

  1. EZB relies on windows libraries, one of them is the System.Drawing, if not in your dll references, please add.

  2. If is still not working, check the current bitmap and try to save:

c# code, convert to equivalent c++/cli:


Bitmap bitmap = cam.GetCurrentBitmap;
bitmap.Save(&quot;c:\\test1.jpg&quot;);

let me know if you got success with any of the steps.

PRO
Synthiam
#12  

Can you post the content from the ezb log before the program ends?

Get it from this property...


ezb-&gt;GetLastErrorMsg

#13  

DJ and ptp, I got caught up in something else and will be at a demo (with a few EZ-B's) all day tomorrow. I'll get back to you on sunday, monday at the latest. Thanks for you help so far!

PRO
Synthiam
#14  

Sounds good! Keep in touch!

#15  

Okay

@Sures: This is the output from GetLastErrorMsg:


Camera on? 1
image size: 10764
07-02-2016 19:59 - Object Recognition Initialized.
07-02-2016 19:59 - Camera Initialized: EZB://192.168.1.1 @ 320x240
07-02-2016 19:59 - Save Image (c:\test.jpg) Error: System.NullReferenceException: Object reference not set to an instance of an object.
   at EZ_B.Camera.SaveImageAsJPEG(String filename)
07-02-2016 19:59 - Camera Disabled

Using this source:


EZ_B::EZB ^ezb;
ezb = gcnew EZ_B::EZB(&quot;MyEZB&quot;);
EZ_B::Camera ^ezbCam;
ezbCam = gcnew EZ_B::Camera(ezb);

ezbCam-&gt;StartCamera(EZ_B::Camera::GetVideoCaptureDevices()[1], 320, 240);
Sleep(1000);
cout &lt;&lt; &quot;Camera on? &quot; &lt;&lt; ezbCam-&gt;Camera::IsActive &lt;&lt; &quot;\n&quot;;
cout &lt;&lt; &quot;image size: &quot; &lt;&lt; ezbCam-&gt;GetEZBv4CameraImageSize &lt;&lt; &quot;\n&quot;;
ezbCam-&gt;SaveImageAsJPEG(&quot;c:\\test.jpg&quot;);
ezbCam-&gt;StopCamera();

#16  

@ptp: yes the System.Drawing dll is in the references.

So I tried this:


Bitmap^ image = gcnew Bitmap(ezbCam-&gt;GetCurrentBitmap);

which compiles fine, but gives an unhandles exception and complains that object reference is not set to instance of an object at:


EZ_B.Camera.get_GetCurrentBitmap()

Thus, more or less the same error I get when running SaveAsJPEG.

PRO
Synthiam
#17  

What version of the SDK are you using? I think it's a really old one. I would try updating to a recent SDK package. I recall there being an issue with saving in the SDK quite a few months ago.

#18  

Really old, depends on you definition I guess. The SDK i was using was downloaded on January 4th this year. I just downloaded the latest and updated my references. The SaveImageAsJPEG error disappears, but is replaced with this one:


Camera on? 1
image size: 15360
'07-02-2016' is not recognized as an internal or external command, operable program or batch file.
07-02-2016 22:15 - Object Recognition Initialized.
07-02-2016 22:15 - Camera Initialized: EZB://192.168.1.1 @ 320x240
07-02-2016 22:15 - Camera Disabled

which I just find plain odd. And no, still no jpg file saved to disc.

PRO
Synthiam
#19  

Hmm - the jan 6th shouldn't have been that bad. That is a very very odd message! It's really strange and wouldn't think that a simple console app would cause so many challenges.

If you are u using the ez-robot camera, maybe the mono.dll would be a better fit for a console app because it doesn't have form dependencies

#20  

Okay, when running :


ezbCam-&gt;SaveImageAsJPEG(&quot;test.jpg&quot;);

rather than using an absolute path I get this error:


Camera on? 1
image size: 14336
08-02-2016 12:12 - Object Recognition Initialized.
08-02-2016 12:12 - Camera Initialized: EZB://192.168.1.1 @ 320x240
08-02-2016 12:12 - Save Image () Error: System.ArgumentException: Path cannot be the empty string or all whitespace.
   at System.IO.Directory.CreateDirectory(String path)
   at EZ_B.Camera.SaveImageAsJPEG(String filename)
08-02-2016 12:12 - Camera Disabled

which I find a bit odd since the string evidently is not empty.

PRO
USA
#21  

Lars,

Regarding the last post "System.ArgumentException: Path cannot be the empty string or all whitespace."

is correct, i'm familiar with that message...

guessing... the ezb code obtains the path from the file you specified, and then calls the CreateDirectory with an empty path generating the error/exception you got.

PRO
USA
#22  

Lars,

To help brainstorm the problem:

  1. Describe the Camera device in use.

  2. To isolate if the issue: sdk related, form vs console application or cmake/compiler options:

2.1 ) isolate EZ code in a console application, compile, run, debug with Visual Studio,

2.2) if 2.1 still does not work, create a Form application with the same code.

PRO
Synthiam
#23  

yeah - the filename must contain a path because it will create the folder if it doesn't exist, as ptp mentioned.

Try the mono.dll - i think you will have better luck because it has less overhead

#24  

Ptp and Sures, thank you for all your advice and efforts, I appreciate it. I have for the time being given up using the camera in the current context.

Unfortunately, C# and mono is incompatible with other components in the project. I am working with a group of roboticists on adapting a gaze spotter to be used with a JD-bot. The spotter is originally developed for the iCub robot, but has been adapted to other platforms as well. We should have a working system within a few days, I'll throw up a video once it does:)

#25  

Okay, I'm being reminded that I need to assign credit. The issue was never really resolved, but ptp offered many suggestions so I think credit is due. Of course Sures helped as well!