File Exchange

image thumbnail

HebiRobotics/HebiCa​m

version 1.1.0.0 (4.1 MB) by Florian Enner
Read images from live video streams (ip cameras, usb cameras, h264, mjpeg, etc.)

14 Downloads

Updated 26 Jan 2018

View License on GitHub

This submission provides a way to acquire real-time streaming video from a variety of sources. It is similar in functionality to MATLAB's IP Camera support package, but provides support for a wider range of formats. It is based on JavaCV and supports all formats that are supported by OpenCV and FFMpeg, including h264 and mjpeg streams. USB cameras are supported on Windows and OSX.
The image acquisition is done in a very efficient way that result in almost zero overhead to the main MATLAB thread. This also allows for reading multiple streams at the same time (e.g. stereo vision) and reading images from high resolution (1080p) streams.

Examples:
% (Axis) IP Camera
cam = HebiCam('http://<ip address>/mjpg/video.mjpg);
imshow(cam.getsnapshot());

% USB camera
clear cam; % make sure device is not in use
cam = HebiCam(1);
imshow(cam.getsnapshot());

% Displaying live images
figure();
img = imshow(getsnapshot(cam));
while true
set(img, 'CData', getsnapshot(cam));
drawnow;
end

% Forced gray-scale acquisition with timestamp
cam = HebiCam(address, 'ImageMode', 'Gray');
[image, frameNumber, timestamp] = getsnapshot(cam);

Readme: https://github.com/HebiRobotics/HebiCam
Download: https://github.com/HebiRobotics/HebiCam/releases

Comments and Ratings (27)

Florian Enner

@leon For USB cameras I'd recommend using MATLAB's usbcam. The used JavaCV/OpenCV combination seems to have some issues with USB cameras on some operating systems.

That being said, HebiCam is just a wrapper around OpenCV, so looking through the OpenCV documentation should provide some hints, e.g., http://answers.opencv.org/question/34461/how-to-set-camera-resolution-webcam-with-opencv/

You should be able to set the corresponding properties via frame grabber options (e.g. https://github.com/HebiRobotics/HebiCam/blob/master/src/main/resources/matlab/HebiCam.m#L130)

how do you set the resolution for an usb webcam?

SUCCESS CONNECT TO H264 IP CAMERA!!!
1: Download & unzip "2017-12-05-HebiCam-1.1.zip"
https://github.com/HebiRobotics/HebiCam/releases
2.1: In Matlab: Choose that folder is Matlab current folder
2.2: Type in command
cam = HebiCam('rtsp://user:password@ip-adress/');
example
cam = HebiCam('rtsp://admin:admin@192.168.100.50/');
:D B3: Get the image:
imshow(cam.getsnapshot());
------------------------------------------------------------------------------
OS: Windows 7 64bit, Matlab 2014b 64bit
Hope this help!!!

@matthew You can try to set the format manually (see HebiCam.m around line 140), e.g., "grabber.setFormat('<format>')". If this doesn't work, please check for connectivity issues between FFMpeg and your Foscam.

Alternatively, you could try to use mjpeg instead. http://foscam.us/forum/how-to-fetch-snapshots-and-mjpeg-stream-on-hd-cameras-t4328.html

matthew

(Note: in the text below, I have replaced the actual username with "user" and actual password with "password".)

I'm running into a problem. I have a Foscam FI9821P V3 ip camera. I can view the live stream in VLC at rtsp://user:password@192.168.1.1:88/videoMain, where user and password are the login credentials for the camera. When I use HebiCam('rtsp://user:password@192.168.1.1:88/videoMain') the following error is returned:

Error using HebiCam (line 177)
Java exception occurred:
org.bytedeco.javacv.FrameGrabber$Exception:
avformat_open_input() error -1094995529: Could not open input
"rtsp://user:password@192.168.1.1:88/videoMain".
(Has setFormat() been called?)
at
org.bytedeco.javacv.FFmpegFrameGrabber.startUnsafe(FFmpegFrameGrabber.java:437)
at
org.bytedeco.javacv.FFmpegFrameGrabber.start(FFmpegFrameGrabber.java:385)
at
us.hebi.matlab.streaming.BackgroundFrameGrabber.<init>(BackgroundFrameGrabber.java:51)

Any suggestions? Thanks, Matt

Florian Enner

@JamesHunt I think below someone had a similar problem and was able to fix it by disabling audio on the camera side. If this doesn't work, please check whether the URL works in other programs such as VLC.

JamesHunt

Hi!

I downloaded the archive from https://github.com/HebiRobotics/HebiCam/releases and added it to matlab path. I get an error while cam = HebiCam('rtsp://192.168.31.121:554/ch0_0.h264'):
Error using HebiCam (line 177)
Java exception occurred:
java.lang.IllegalArgumentException: Unsupported number of channels: 0

at us.hebi.matlab.streaming.BackgroundFrameGrabber.<init>(BackgroundFrameGrabber.java:63)

What am I doing wrong?

Florian Enner

@Michael I'm glad you found it useful. I've just uploaded a version with 32 bit binaries to the 1.1 release. I don't have a 32 bit test system, so please let me know in case you still encounter any problems. Going forward I'll see if I can find a better solution for managing binaries.

Michael

Thank you for this great submission. Before, I tried to get a stream using the Matlab-Addon ipcam but with no success. With HebiCam it works on R2015b 64bit on Windows 7 64bit. However, when running it in Matlab R2015b 32bit on both Windows 7 64bit and 32bit I get the following error:

Trial>> testh264
Warning: The following error was caught while executing 'HebiCam' class destructor:
Undefined function 'stop' for input arguments of type 'double'.
> In testh264 (line 5)
Undefined variable "org" or class "org.bytedeco.javacpp.avutil.AV_LOG_FATAL".

Error in HebiCam (line 154)
logLevel = org.bytedeco.javacpp.avutil.AV_LOG_FATAL;

Error in testh264 (line 5)
cam = HebiCam('rtsp://192.168.2.74');

Trial>>

I used 7zip to extract the .jar file and added all the folders that were created to the Matlab path.

Thank you in advance for your consideration.

Yo M

Yo M (view profile)

A fantastic addition in light of IPCAM not supporting h246.

I also had a problem with the channel numbers, disabling the audio (even though the camera doesn't have a mic) indeed proved to be the solution!

Yo M

Yo M (view profile)

I just found an issue when I tried to get video from IP camera Vantech VT-6300A, H.264, RTSP:

Unsupport number of channels (HebiCam.m line 177)

The issue is fixed when I config the camera to disable audio!

Florian Enner

@Levi Devries

Yes. It seems like you have checked out the raw source code, which would require you to install "maven 3" and run "mvn package" first. Both files would then be located in <path>/target/.

I'd recommend downloading the pre-built zip files from https://github.com/HebiRobotics/HebiCam/releases

Levi Devries

When I try to run the file for an IP camera I get the following error:

Warning: Invalid file or directory
'filepathomitted\hebicam-1.2-SNAPSHOT-all-x86_64.jar'.
> In javaclasspath>local_validate_dynamic_path (line 266)
In javaclasspath>local_javapath (line 182)
In javaclasspath (line 119)
In javaaddpath (line 71)
In HebiCam.loadLibs (line 81)
In HebiCam (line 102)
Warning: The following error was caught while
executing 'HebiCam' class destructor:
Undefined function 'stop' for input arguments of type
'double'.
Undefined variable "us" or class
"us.hebi.matlab.streaming.DeviceLocation".

Error in HebiCam (line 106)
loc =
us.hebi.matlab.streaming.DeviceLocation(args.URI);

When digging through HebiCam.m, it seems that my download of the package is missing the 'hebicam-1.2-SNAPSHOT-all-x86_64.jar' file. Is this the cause of the error?

Thank you in advance for your consideration.

Florian Enner

I've finally found some more time to work on this. v1.1 should address much of the binary incompatibility issues.

I've also moved all setup from the Java side to the MATLAB constructor to allow users to customize settings (e.g. FFMpeg flags).

Florian Enner

@C Fischer

It looks like some of the JavaCV binaries aren't being found. To debug and fix this I'd need some more information.

What operating system and version of MATLAB are you running? Is it a 32 or 64 bit system? Did you build from source or use the Github release?

C Fischer

When running the file I got the following error:

[code]
Warning: The following error was caught while executing 'HebiCam' class destructor:
Undefined function 'stop' for input arguments of type 'double'.
> In testcam (line 3)
Error using HebiCam (line 72)
Java exception occurred:
java.lang.NoClassDefFoundError: Could not initialize class org.bytedeco.javacpp.avutil

at us.hebi.matlab.streaming.BackingHebiCam.<init>(BackingHebiCam.java:276)

at us.hebi.matlab.streaming.BackingHebiCam.openRemoteUri(BackingHebiCam.java:99)

at us.hebi.matlab.streaming.BackingHebiCam.open(BackingHebiCam.java:66)

Error in testcam (line 3)
cam = HebiCam('rtsp://192.168.1.10/h264');
[/code]

What could I do?

Hi Florian,

I have deleted the camera object and then running again and it works correctly when I use a single function to receive the video. However, when I used various functions in my application, I get the error I said you. This is something strange and I don't know where is the problem.

Thank you.

Victoria.

Florian Enner

@ Jinang

I usually acquire RGB images and then convert them to grayscale using MATLAB's 'rgb2gray' function. Direct gray-scale acquisition is currently not supported, but it shouldn't be very difficult to implement. For new feature requests, please create an issue on Github.

@Mario Rossi

There seems to be a problem with FFMpeg reaching a timeout on this particular URL. When I switch from FFMpeg to the OpenCV frame grabber, it works as expected.

I may switch to always using the OpenCV frame grabber once I confirm that this change won't break other use cases.

Jinang

Jinang (view profile)

Hey Florian,

Thank you for the toolbox,it is of great help.

I would like to know if there a way to change the format/channel of acquired video from rgb to grey scale.I had a problem with gigecam wherein Matlab hangs when dealing with colored images and switching to grey scale solved the problem (something to do with buffer size and packet size of camera).

I'm facing the same problem with ip camera using Hebicam function.

Thanks!

Mario Rossi

@Florian Enner

Thank you for response.

The ip address of last message was wrong.

I will get a stream video from chine ip camera that work with rtsp protocol and that has h264 codec.

Since I did not have the ip camera with me, i test this url with HebiCam:
'rtsp://media.smart-streaming.com/mytest/mp4:sample'

When i call in command window:
cam = HebiCam('rtsp://media.smart-streaming.com/mytest/mp4:sample');

MATLAB return me an error:

"MATLAB has encountered an internal problem and needs to close".

When i click on "End now", MATLAB is closed.

I test this url in VLC and it works fine.
If can be to help i can send you the details of MATLAB System Error.

Thank you in advantage for response and sorry for my english.

Florian Enner

@Maria Victoria Garcia

Great to hear that you did get it running.

I unfortunately haven't seen this error before, so it'd be great to get some more information.

What does "stop it" and "return to run" mean? I'm assuming you are stopping via ctrl+c. When re-running, do you create a new camera object, or do you only execute the cam.getsnapshot() line?

Please check if it works if you delete the camera object (e.g. 'clear cam') before running again. Does that work?

Can you create a minimal example script that will cause this behavior?

Thanks
Florian

Hi Florian,

I have got your code runs correctly!! Thank you very much! Really you have helped me. I'm very pleasant.

But, I have a small problem. When I execute the code, stop it and return to run it I get this error:

Exception in thread "VideoInput-1" java.lang.RuntimeException: org.bytedeco.javacv.FrameGrabber$Exception: cvGrabFrame() Error: Could not grab frame. (Has start() been called?)
at us.hebi.matlab.streaming.BackingHebiCam$1.run(BackingHebiCam.java:263)
at java.lang.Thread.run(Unknown Source)
Caused by: org.bytedeco.javacv.FrameGrabber$Exception: cvGrabFrame() Error: Could not grab frame. (Has start() been called?)
at org.bytedeco.javacv.OpenCVFrameGrabber.grab(OpenCVFrameGrabber.java:242)
at org.bytedeco.javacv.FrameGrabber.grabFrame(FrameGrabber.java:361)
at us.hebi.matlab.streaming.BackingHebiCam.runAcquisitionLoop(BackingHebiCam.java:218)
at us.hebi.matlab.streaming.BackingHebiCam.access$000(BackingHebiCam.java:48)
at us.hebi.matlab.streaming.BackingHebiCam$1.run(BackingHebiCam.java:261)
... 1 more

Why can be the problem here?? I don't know nothing about Java, so I don't understand it.

I am looking forward to your response, please. Thanks.

Florian Enner

@Maria Victoria Garcia

That error can occur when the corresponding Java library (.jar file) can't be found or hasn't been added to the path correctly.

Please make sure that both files (HebiCam.m and the corresponding -all-*.jar file) are located in the same directory.

If it still doesn't work, please try to pre-load the Java library manually by calling

javaaddpath('<fill in absolute path to .jar file here>');

In case this doesn't work for you, please provide information regarding your operating system and MATLAB version.

Hi Florian,

I've tried your code and I get this error:

Undefined variable "us" or class "us.hebi.matlab.streaming.BackingHebiCam.open".
Error in HebiCam (line 72)
this.cam = us.hebi.matlab.streaming.BackingHebiCam.open(url);
Error in main (line 3)
cam = HebiCam(url);

Would you know where is the problem?

I am looking forward to your response, please. Thanks.

Regards.

Victoria.

Florian Enner

@Maro Rossi

I'm assuming that "http://192.168.1.65:8080/" is not the full address of the stream. You may be missing parts of the URL (for example, "/mjpg/video.mjpg" for Axis cameras).

Can you open the stream in other programs like VLC?

Mario Rossi

I take files from Github and put the content inside MATLAB path. I put a URL and it gives me this error:

Java exception occurred:
org.bytedeco.javacv.FrameGrabber$Exception: avformat_open_input() error -61: Could not open input
"http://192.168.1.65:8080/". (Has setFormat() been called?)
at org.bytedeco.javacv.FFmpegFrameGrabber.startUnsafe(FFmpegFrameGrabber.java:393)
at org.bytedeco.javacv.FFmpegFrameGrabber.start(FFmpegFrameGrabber.java:341)
at us.hebi.matlab.streaming.BackingHebiCam.<init>(BackingHebiCam.java:279)
at us.hebi.matlab.streaming.BackingHebiCam.openRemoteUri(BackingHebiCam.java:99)
at us.hebi.matlab.streaming.BackingHebiCam.open(BackingHebiCam.java:66)

What's wrong? Thank you for response.

Updates

1.1.0.0

added examples for 1.1 features

1.1.0.0

simplified description

1.0.0.0

added space to description for better readability

MATLAB Release Compatibility
Created with R2009b
Compatible with any release
Platform Compatibility
Windows macOS Linux

MATLAB Online Live Editor Challenge

View the winning live scripts from faculty and students who participated in the recent challenge.

Learn more

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video