This is my first electron/node application, I m trying to use a json file as a datastore. so I created a simple one index.json under the app folder next to index.js|css|html
I installed a npm package jsonfile that is loading just fine
When I try to load my json file the EOF is rised claiming that there is no json file, and I can see that using the DevTools source tab that my json file is not there ( not loaded )
I tried force reload from electron app menu.
Here is my files code that is reading my json
const jsonfile = require('jsonfile')
const file = '/index.json';
var json;
jsonfile.readFile(file)
.then(obj => json = obj)
.catch(error => console.error(error))
------------ Edit
correcting the path name to index.json or ./index.json rises the same issue
You can use the native fs (filesystem) module.
let path = "index.json"
const fs = require('fs');
const json = JSON.parse(fs.readFileSync(path));
Thanks for your support
For me the issue was more about file system handling than electron.
All I did is to chmod my project folder to assure that I will be able to read and write into the index.json datastore
sudo chmod -R 777 /opt/workspaces/electron/myElectronPrpjectFolder
Then for a better path resolution I used the basic idea used in electron archtype, It more error safe
const path = require('path')
const file = path.join(__dirname,'index.json');
var json;
var html = "";// The returned object.
$(document).ready(function () {
jsonfile.readFile(file)
.then(obj => {
json = JSON.parse(JSON.stringify(obj));
console.log(JSON.stringify(json))
parseIssues(json.children);
document.getElementById('a').innerHTML = html;
})
.catch(error => console.error(error))
});
You can see that I m using JQuery in this snippet but it also works without JQuery.
in resume, better path resolve policy with granted priveleges on folder.
Thanks
Related
I build a Node.js tool in order to change GCP config quickly. But I'm a bit stuck on the parsing of the config_nameOfConfig file.
It looks like this :
[core]
account = email#example.fr
project = project-example
disable_usage_reporting = False
[compute]
region = europe-west1-b
(This file is available in '/Users/nameOfUser/.config/gcloud')
And I want to convert it in an object like this :
const config = {
account:"email#example.fr",
project:"project-example",
disable_usage_reporting:false,
region:"europe-west1-b",
};
I get the content of this file with the fs.readFileSync function who converts it into a string.
An idea ?
This seems to be an ini file format. Unless you want to do the parsing yourself, you'd better just download a library to your project, for example:
npm install ini
Then in the code:
var ini = require('ini')
var fs = require('fs')
var config = ini.parse(fs.readFileSync('./config_nameOfConfig', 'utf-8'))
In config you should now have object containing the data in the file.
You can console.log(config) to see how it exactly looks like.
I am trying to get json values using nodejs but not working.I have searched some question in stackoverflow related this but always I am getting [Object Object] like this.I do not know Why I am getting like this.Anyone can resolve this issue?
file.json:
{
"scripts": {
"mr": "place",
"kg": "time",
"bh": "sec"
}
}
extension.js:
var fs = require("fs");
var file = JSON.parse(fs.readFileSync("c:\\xampp\\htdocs\\projects\\file.json", "utf8"));
console.log(file);
This is not duplicate. I have tried many ways but not working.
Note:I am using this code inside my visual studio code extension.
In node, you can import JSON like a JavaScript file
const file = require('./file.json')
console.log(file)
See is there a require for json in node.js for more info
const data = require("./file.json")
console.log(data.scripts)
Try this one out it is simple.
const fs = require('fs');
const paht = require('path');
console.log(paht.join(__dirname,'../file.json'));
let file = JSON.parse(fs.readFileSync(paht.join(__dirname,'../file.json'), "utf8"));
__dirname gives you the directory of your current file, I used path.join to make sure I can go further.
I put the json file in upper directory in my case
I have converted a keras model to tensorflow json format and saved it locally in my computer. I am trying to load that json model in a javascript code using the below command
model = await tf.loadModel('web_model')
But the model is not getting loaded.
Is there a way to load tensorflow json model from local file system?
I know you're trying to load your model in a browser but if anybody lands here that's trying to do it in Node, here's how:
const tf = require("#tensorflow/tfjs");
const tfn = require("#tensorflow/tfjs-node");
const handler = tfn.io.fileSystem("./path/to/your/model.json");
const model = await tf.loadLayersModel(handler);
LoadModel uses fetch under the hood. And fetch cannot access the local files directly. It is meant to be used to get files served by a server. More on this here.
To load a local file with the browser, there is two approaches, asking the user to upload the file with
<input type="file"/>
Or serving the file by a server.
In these two scenarios, tf.js provides way to load the model.
Load the model by asking the user to upload the file
html
<input type="file" id="upload-json"/>
<input type="file" id="upload-weights"/>
js
const uploadJSONInput = document.getElementById('upload-json');
const uploadWeightsInput = document.getElementById('upload-weights');
const model = await tfl.loadModel(tf.io.browserFiles(
[uploadJSONInput.files[0], uploadWeightsInput.files[0]]));
Serving the local files using a server
To do so, one can use the following npm module http-server to serve the directory containing both the weight and the model. It can be installed with the following command:
npm install http-server -g
Inside the directory, one can run the following command to launch the server:
http-server -c1 --cors .
Now the model can be loaded:
// load model in js script
(async () => {
...
const model = await tf.loadFrozenModel('http://localhost:8080/model.pb', 'http://localhost:8080/weights.json')
})()
const tf = require('#tensorflow/tfjs');
const tfnode = require('#tensorflow/tfjs-node');
async function loadModel(){
const handler = tfnode.io.fileSystem('tfjs_model/model.json');
const model = await tf.loadLayersModel(handler);
console.log("Model loaded")
}
loadModel();
This worked for me in node. Thanks to jafaircl.
If you're using React with create-react-app, you can keep your saved model files in your public folder.
For example, say you want to use the blazeface model. You would
Download the .tar.gz model from that web page.
Unpack the model into your app's public directory. So now you have the files from the .tar.gz file in a public subdir:
%YOUR_APP%/public/blazeface_1_default_1/model.json
%YOUR_APP%/public/blazeface_1_default_1/group1-shard1of1.bin
Load the model in your React app using
tf.loadGraphModel(process.env.PUBLIC_URL + 'blazeface_1_default_1/model.json'
You could try:
const model = await tf.models.modelFromJSON(myModelJSON)
Here it is in the tensorflow.org docs
Check out our documentation for loading models: https://js.tensorflow.org/api/latest/#Models-Loading
You can use tf.loadModel takes a string which is a URL to your model definition which needs to get served over HTTP. This means you need to start an http-server to serve those files (it will not allow you to make a request to your filesystem because of CORS).
This package can do that for you: npmjs.com/package/http-server
You could use insecure chrome instance:
C:\Program Files (x86)\Google\Chrome\Application>chrome.exe --disable-web-security --disable-gpu --user-data-dir=C:/Temp
Than you could add this script to redefine fetch function
async function fetch(url) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest
xhr.onload = function() {
resolve(new Response(xhr.responseText, {status: 200}))
}
xhr.onerror = function() {
reject(new TypeError('Local request failed'))
}
xhr.open('GET', url)
xhr.send(null)
})
}
After that be shure that you use the right model loader
my comment about loader issue
BUT your weights will be incorrect - as I understand there are some encoding problems.
If you are trying to load it in server side, use #tensorflow/tfjs-node instead of #tensorflow/tfjs and update to 0.2.1 or higher version to resolve this issue.
I am using React js for loading model (for image classification and more machine learning stuff)
Tensorflow.js do not support an Api to read a previously model trained
const file= new Blob()
file.src=modelJSON
const files= new Blob()
files.src=modelWeights
console.log(files)
const model= await tf.loadLayersModel(tf.io.browserFiles([file, files]));
[![enter image description here][1]][1]
You be able to create an APi in Express.js for servering your model (model.json and weigths.bin) if you use a web app (for a tensorflow.lite you could use a opencv.readTensorflowmodel(model.pb, weight.pbtxt)
References: How to load tensorflow-js weights from express using tf.loadLayersModel()?
const classifierModel = await tf.loadLayersModel(
"https://rp5u7.sse.codesandbox.io/api/pokeml/classify"
);
const im = new Image()
im.src =imagenSample//'../../../../../Models/ShapesClassification/Samples/images (2).png';
const abc= this.preprocessImage(im);
const preds = await classifierModel.predict(abc)//.argMax(-1);
console.log('<Response>',preds,'Principal',preds.shape[0],'DATA',preds.dataSync())
const responde=[...preds.dataSync()]
console.log('Maxmimo Valor',Math.max.apply(Math, responde.map(function(o) { return o; })))
let indiceMax = this.indexOfMax(responde)
console.log(indiceMax)
console.log('<<<LABEL>>>',this.labelsReturn(indiceMax))
If you are using Django, you should:
create a directory static in your app and put your model there.
load that static directory to the template where you want to use your model:
var modelPath = "{% static 'sampleModel.json' %}">
Don't forget to also load tensorflow.js library:
<script src="https://cdn.jsdelivr.net/npm/#tensorflow/tfjs"></script>
Now you can load your model:
<script>model = await tf.loadGraphModel(modelPath)</script>
i found a solution that it works. You can replace the url with a localhost url on xampp, for example (directory = model) http://localhost/model/model.json and after that you have to disable your browser CORS policy. For me i found a chrome extention and removed cors for my specific tab and it worked.
Thank me later!!
I have created a zip file of a .txt file using node.js zlib and while trying to unzip the file i used the same node_module zlib but its throwing error.Please check my code snippet and please feel free to edit code.I also checked the file location its same as the
Error :
Error: unexpected end of file
at Unzip.zlibOnError (zlib.js:153:15)
Code :
const fs=require('fs');
const zlib = require('zlib');
const iii = fs.createReadStream('test.txt.gz');
const oo = fs.createWriteStream('test1.txt');
const unzip = zlib.createUnzip()
iii.pipe(unzip).pipe(oo)
I am unable to understand my mistake.Can anyone help me with the same?
Thanks in advance!!
Used below code to zip the file.
code to zip:
const fs=require('fs');
const zlib = require('zlib');
const gzip = zlib.createGzip();
const inp = fs.createReadStream('test.txt');
const out = fs.createWriteStream('test.txt.gz');
inp.pipe(gzip).pipe(out);
I was getting an error because the file got corrupted while trying the zip-unzip.
There was no error in the code.
So after deleting and recreating zip file, i was able to unzip it.
Note:
Whenever you try something like this,if you get such error, then always check file size it should not be zero. If it is zero,then recreate the zip and try again.
In the following path:
app/test/vehicle/
there are several .js file. Is there any way in Javascript where I can get the number of .js file contains in the above-mentioned path?
I am using webStorm environment
Yes, using node:
const fs = require('fs');
const dir = './directory';
fs.readdir(dir, (err, files) => {
console.log(files.length);
});
https://stackoverflow.com/a/43747896/3650835
Since javascript doesn't have local filesystem access when run in the browser, there is no way to access local files from javascript. If you are asking about accessing all files hosted on a cdn or something similar in that directory, there is still no way to tell which requests will return a 20x.
If you are using nodejs, use the fs.readdir function:
const fs = require('fs');
fs.readdir('app/test/vehicle/', (err, files) => {
files.forEach(file => { console.log(file);} );
})
There is no way to access the file system from a web page.
If you are using nodejs, then you can use fs.readdir or fs.readdirSync:
const fs = require('fs');
const dir = './directory';
fs.readdir(dir, (err, files) => {
console.log(files.filter(file => file.endsWith(".js")).length);
});
Where are you trying to get list of the files? On the client side it is impossible. JavaScript does not have access to the client file system. If you are trying to do it in the server side using for example node.js then you can do something like:
var filesys = require('fs');
var files = filesys.readdirSync('/app/test/vehicle/');