Giter Club home page Giter Club logo

Comments (20)

chriswiggins avatar chriswiggins commented on September 28, 2024 1

I'll close this, we have the package up and running :-)

Thanks for the help @hawkeye64 !

from onvif.

bbruno5 avatar bbruno5 commented on September 28, 2024 1

Long time passed, I know, but I think this is useful. In some cases, when trying to get URIs from a remote camera, it returns an URI based on its local IP. In my case, after broke my mind a big bit, I discovered this. The solution is based on replace local IP with standard remote IP with a few lines of code.

// Function to search for index of nth occurrences
function getPosition(string, subString, index) {
   return string.split(subString, index).join(subString).length;
}

this.getSnapshotUri({stream:'RTP-Multicast', protocol:'RTSP'}, function(err, stream) {
	// these first three lines do the fix
	let urii = stream.uri;
	let ibar = getPosition(urii, '/', 3);
	let fixedURI = 'http://' + HOST + ':' + PORT + urii.substring(ibar);
	request({
		method: 'GET',
		uri: fixedURI,
		...

from onvif.

chriswiggins avatar chriswiggins commented on September 28, 2024

Hi @cristhiangr - likely because you're using request without any authentication set. Try setting the username and password in the request library as well:

request.get(rutaOnvifImagen, {
  'auth': {
    'user': getUser(),
    'pass': 'getPassword()
  }
});

Let me know how you get on

from onvif.

cristhiangr avatar cristhiangr commented on September 28, 2024

Hi @chriswiggins Thanks for answering. I implemented what you indicate, but the result is the same.
I'm new in Node js, Until 2 days ago I didn't know what it was, but I'm looking for a solution to take images from Onvif and this looks good. I'm probably implementing something wrong

This is my code:

var http = require('http'),
Cam = require('onvif').Cam,
fs = require("fs"),
request = require('request');

init();

function init(){
if(checkIP() && checkUser() && checkPassword() && checkPort()){
new Cam({
hostname: getIP(),
username: getUser(),
password: getPassword(),
port: getPort()
}, function(err) {
if (err) {
console.log('Connection Failed for ' + getIP() + ' Port: ' + getPort() + ' Username: ' + getUser() + ' Password: ' + getPassword());
return;
}
console.log('CONNECTED');
this.absoluteMove({
x: 1
, y: 1
, zoom: 1
});

				this.getSnapshotUri({protocol:'RTSP'}, function(err, stream) {
				
					saveImage(stream.uri);

				});
			});
}
else
{
	console.log("datos de inicio incompletos o no validos");
}

}

function saveImage(pathOnvifImage){

request.get(pathOnvifImage, {
  'auth': {
	'user': getUser(),
	'pass': getPassword()
  }
});

var file = fs.createWriteStream("miImagen.jpg");

request(pathOnvifImage).pipe(file);

}

Thanks,

from onvif.

agsh avatar agsh commented on September 28, 2024

Thanks, Chris, that you still reply for every message, I really appreciate you for that. @chriswiggins

from onvif.

agsh avatar agsh commented on September 28, 2024

@cristhiangr, JavaScript is an asynchronous language, so you should put your getSnapshotUrl under the callback, cause communication with camera is async.
You can try to cover it to promise with bluebird library and use async-await with the es7 features
I'll try to add this ASAP
This is all that I can see in your code, that you send

from onvif.

chriswiggins avatar chriswiggins commented on September 28, 2024

from onvif.

cristhiangr avatar cristhiangr commented on September 28, 2024

Thanks, I created a gist and put what the code there. This is the link https://gist.github.com/cristhiangr Thank you very much @agsh and @chriswiggins . I'm reading about bluebird, but I still don't know what I should do.

from onvif.

chriswiggins avatar chriswiggins commented on September 28, 2024

Hi @cristhiangr,

I think I've found the problem. Try this in your save image function:

function saveImage(pathOnvifImage){
	var file = fs.createWriteStream("miImagen.jpg");

	request.get(pathOnvifImage, {
	  'auth': {
		'user': getUser(),
		'pass': getPassword()
	  }
	}).pipe(file);
	
}

What you were doing was making two requests to the same place, one with authentication and one without, and saving the one without authentication. Let me know if this works

from onvif.

cristhiangr avatar cristhiangr commented on September 28, 2024

@chriswiggins thank you very much for continuing to respond, I implemented the changes you suggest but I still have the same result

from onvif.

agsh avatar agsh commented on September 28, 2024

@cristhiangr, can you post here your pathOnvifImage string?
Also please check the authorization headers of GET response.

from onvif.

cristhiangr avatar cristhiangr commented on September 28, 2024

hi @agsh ofcourse, this is the path onvif (stream.uri), http://192.168.1.251/onvif-cgi/jpg/image.cgi?resolution=1280x1024&compression=30 that I get with the getSnapshotUri method. If I public this code like a webserver effectively I see the image in that web path (in the web browser)

from onvif.

agsh avatar agsh commented on September 28, 2024

So you can open an image via this url in the browser?
Try this code

request('http://192.168.1.251/onvif-cgi/jpg/image.cgi?resolution=1280x1024&compression=30')
.pipe(fs.createWriteStream('miImagen.jpg'));

from onvif.

cristhiangr avatar cristhiangr commented on September 28, 2024

Hello @agsh , excuse the delay in replying, I implemented that, but I still have the same result. Yes I can open an image via this url in the browser. I'll record a video and upload it so you can see in detail what happens.

Thanks

from onvif.

cristhiangr avatar cristhiangr commented on September 28, 2024

@agsh, @chriswiggins , today I tested with a vivotek camera and a hikvision camera, and I was able to save the image without any problem. However, with the camera axis, which I have had all this time, I still have the problem of authorization to save the image.

from onvif.

hawkeye64 avatar hawkeye64 commented on September 28, 2024

In case anyone finds this based on an issue they are having I have written a gist. The issues you might encounter are:

  1. Connecting to a camera (how to use authorization)
  2. Getting the snapshotUri
  3. Getting a snapshot
    a. How to get the raw data back using axios without any transformations (ie: line-feed conversions)
    b. How to get the snapshot from a camera that is set up for Basic Auth
    c. Base64 encode the raw data using Nodejs Buffer class
    d. Write the data to a file, for testing purposes

Remember, this is only a Gist and is not the fully completed code that I use.

https://gist.github.com/hawkeye64/a30e43c8301626f7687b985b5e7b04bd

Btw, I use Hikvision, Pelco, TrendNET and Axis cameras.

from onvif.

hawkeye64 avatar hawkeye64 commented on September 28, 2024

If it helps, I have an updated gist using request as axios would not work with Digest Realm of an Axis camera.

https://gist.github.com/hawkeye64/e67cb5aae8f46d9c94ea2c9a4bb9247b

Someone should close this issue. @chriswiggins ?

from onvif.

chriswiggins avatar chriswiggins commented on September 28, 2024

We should probably include a method to return the image as a Buffer into the code. How would you feel about that @hawkeye64?

from onvif.

hawkeye64 avatar hawkeye64 commented on September 28, 2024

Yeah, I can add it. Look for it sometime after the weekend.

from onvif.

hawkeye64 avatar hawkeye64 commented on September 28, 2024

There is now an onvif-snapshot package. Found here: https://github.com/chriswiggins/onvif-snapshot
As soon as @chriswiggins takes the latest PR, you should be able use it with node (currently snapshot.js library path is incorrect in package.json).

I have a sample app based on onvif and onvif-snapshot that I'll be releasing soon.

from onvif.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.