ITK JS readImageDICOMFileSeries() reports 'filePath is undefined' on console - javascript

I'm new to Javascript and ITK. I started by cloning the ITK-JS repository, and am working thru the examples.
My objective -- from my browser, to read all of the images in a directory (an MR series) into a single ITK image (3D volume).
I started with the 'webpack' example in the ITK-JS example -- got that working in my browser.
I then:
1. Changed the index.html to enable multiple for the <input type='file'> element. I confirmed that worked properly by writing the contents of the FileList object to the html element, and got that working with the 'readFile()'; then
2. Replaced the 'readFile(null, files[0])' with 'readImageDICOMFileSeries(null, files)'.
When I run the code with the 'readImageDICOMFileSeries()' I get the console message: "TypeError: filePath is undefined".
I have tried several variants, but always seem to get the same error. Here is my current code:
//
// read the images
// return readFile(null, files[0]) // -- this line works
return readImageDICOMFileSeries(null, files) // the causes TypeError: filePath is undefined
.then((worker)=>{
const {image}=worker;
let mFile = JSON.stringify(image) ;
alert(mFile)
console.log(image);
})
.catch(error=>{
console.error(error);
alert(error.message);
})
What am I doing wrong?
Thanks in advance for your help.
Doug

This is an example using ITK and emscripten to compile an ITK library to read most medical image formats. It works in the browser and node.js.
https://www.npmjs.com/package/med-img-reader
I hope it helps.
There may be additional resources/examples here https://github.com/InsightSoftwareConsortium/itk-js/tree/master/examples/

Related

Problem passing file to an input-type-file webelement

How to set File objects and length property at FileList object where the files are also reflected at FormData object?
I was trying to use the hack described in the link above to insert through code the file I want to upload to an '<input type='file', but I am having trouble to implement it.
I am using the following code as a snippet in Chrome devtools Console Panel:
inp = document.querySelector("input[type=file]");
const dT = new DataTransfer();
dT.items.add(new File(['foo'], 'C:\\Users\\MyUser\\Desktop\\SomeFolder\\MyTestFile.txt'));
inp.files = dT.files;
The input text that should display only the file name shows the full path, and when I press the button to upload the file it return an error saying that the file was not founded.
I already check the path and it is 100% right. I don't know much about javascript, in fact I am using Selenium to do the rest of the automation, but this particular subject looks like can only be achieved with js.
Could anyone help me? Am I mising something or did I misunderstood the hack solution and what I am trying to do is not possible at all?

Using JavaScript to load haarcascasdes file always return false

I'm doing my first web application with opencv, this OpenCV Face Detection tutorial guide me to load haarcascade_frontalface_default.xml then detect face in a photo, the sample code below:
let faceCascade = new cv.CascadeClassifier();
faceCascade.load('haarcascade_frontalface_default.xml'); //<--return false
My haarcascades file structure
Because of the failure loading, I cannot use detectMultiScale function, the error log is :
Assertion failed (!empty()) in detectMultiScale, file /build/master-contrib_docs-lin64/opencv/modules/objdetect/src/cascadedetect.cpp, line 1698
Does my haarcascade file put in a wrong place? or parameter in load() is wrong?

How to read metadata from mp4 using mp4.js node module?

I am trying to read the metadata tags from an mp4 file into my javascript using the node module mp4js. I followed the model provided on that page but with some issue.
I tried the following:
var mp4 = require('mp4js');
mp4({ file: 'small.mp4', type: 'local' }, function (err, tags) {
// tags now contains your MP4 tags
console.log('Tags: ' + tags)
$scope.mp4Tags = tags;
});
and I was met with Uncaught TypeError: Cannot read property 'join' of undefined
Does anyone know what I'm missing here? Could it be that the mp4 doesn't have any tags? I just googled "sample mp4" and downloaded something. It's my first time trying this module and I am pretty new to node.
I'm also open to other suggestions for reading mp4 metadata.
Thank you very much for your time and let me know if you need any additional from me.
EDIT 8.26.2015
Following Brad's suggestion, I tried the following code to read metadata from an mp4:
var child_process = require('child_process');
ls = child_process.spawn('ffmpeg/bin/ffprobe.exe', ['-print_format', 'json', 'out.mp4']);
ls.stdout.on('data', function (data) {
console.log('stdout: ' + data);
$scope.mp4data = data;
});
I created mp4.out by following this example for adding a title meta tag using ffmpeg from the command line. Here is a screenshot of my output. Here is the command I entered:
ffmpeg -i in.avi -metadata title="my title" out.flv
After running my app with the code above, the value stored in $scope.mp4data is a Native Buffer that looks like this. All I see are some numbers, but no metadata tags. How can I view the metadata such as the title that I set using the command above? Am I missing something in the arguments? Or maybe I should be looking at something other than data?
Thanks for the help. I may just post this as a new question since the focus has shifted.
EDIT: Solution
Here's where I landed thanks to Brad's help:
$scope.mp4data = "";
ls.stdout.on('data', function (data) {
$scope.mp4data = $scope.mp4data + data.toString();
});
ls.stdout.on('end', function (data) {
$scope.mp4data = JSON.parse($scope.mp4data);
});
works great for reading metadata from my mp4! Thanks Brad!
I recommend using FFprobe (from the FFmpeg project) to do this instead. It supports many containers, including MP4.
You can use ffprobe -print_format json to have it output JSON data that you can parse inside your Node.js app.
Some examples here: https://stackoverflow.com/a/8191228/362536

Read local file in Phonegap

I'm trying to read a local file in Phonegap to load the language strings for the application and I can't make it work :(
The code is pretty straightforward:
var pathToLocalFile = "/home/user/android/assets/www/js/";
var langCache = new FileReader();
langCache.onload = function(data){
col = JSON.parse(data);
refreshAllStrings();
};
langCache.onerror = function(err){
debug.error(err);
};
langCache.readAsText(pathToLocalFile+currentLang+'.json');
This code doesn't work on the Ripple emulator and I replaced it with
var pathToLocalFile = "file:///android_asset/www/js/";
in case of android, with the same result:
Uncaught TypeError: Object #<Console> has no method 'warning' cordova-2.4.0.js:2616
initRead cordova-2.4.0.js:2616
FileReader.readAsText cordova-2.4.0.js:2660
loadLanguage
In the Ripple emulator, I started Chrome with google-chrome -–allow-file-access-from-files and the Android config and manifest has all the permissions for and the plugins set.
Of course I'm missing something, but any idea what this could be?
Regards.
If the file is under the www folder of your app you don't need to add '/home/..' or 'file:///'. You should just be able to load the contents using an "AJAX" fetch even though it is bundled in the app.
$.get('js/filename.ext');
The error you're seeing is actually a result of cordova-2.4.0.js calling a non-existent "warning" method. It should be console.warn. It is attempting to warn you that using a string for the readAsText method is now deprecated, and that you should use a File reference instead.
What should be happening is a console.warn of "Using a string argument with FileReader.readAs functions is deprecated."

NetUtil.asyncCopy from one file to append to another in Firefox extension

I'm trying to use NetUtil.asyncCopy to append data from one file to the end of another file from a Firefox extension. I have based this code upon a number of examples at https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_O, particularly the 'Copy a stream to a file' example. Given what it says on that page, my code below:
Creates nsIFile objects for the file to copy from and file to append to and initialises these objects with the correct paths.
Creates an output stream to the output file.
Runs the NetUtil.asyncCopy function to copy between the file (which, I believe, behaves as an nsIInputStream) and the output stream.
I run this code as append_text_from_file("~/CopyFrom.txt", "~/AppendTo.txt");, but nothing gets copied across. The Appending Text and After ostream dumps appear on the console, but not the Done or Error dumps.
Does anyone have any idea what I'm doing wrong here? I'm fairly new to both Firefox extensions and javascript (although I am a fairly experienced programmer) - so I may be doing something really silly. If my entire approach is wrong then please let me know - I would have thought that this approach would allow me to append a file easily, and asynchronously, but it may not be possible for some reason that I don't know about.
function append_text_from_file(from_filename, to_filename) {
var from_file = Components.classes["#mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
from_file.initWithPath(from_filename);
var to_file = Components.classes["#mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
to_file.initWithPath(to_filename);
dump("Appending text\n");
var ostream = FileUtils.openFileOutputStream(to_file, FileUtils.MODE_WRONLY | FileUtils.MODE_APPEND)
dump("After ostream\n");
NetUtil.asyncCopy(from_file, ostream, function(aResult) {
dump("Done\n");
if (!Components.isSuccessCode(aResult)) {
// an error occurred!
dump(aResult);
dump("Error!\n")
}
});
}
asyncCopy() requires an input stream not a file.
you can do this:
var fstream = Cc["#mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream);
fstream.init(from_file, 0x01, 4, null);
NetUtil.asyncCopy(fstream, ostream, function(aResult)....

Categories

Resources