watson-developer-cloud / node-sdk

:comet: Node.js library to access IBM Watson services.
https://www.npmjs.com/package/ibm-watson
Apache License 2.0
1.48k stars 670 forks source link

Cannot read zip file in "createClassifier" method (400 code error) #1029

Closed bmhan9710 closed 4 years ago

bmhan9710 commented 4 years ago

[ Current environment ]

[ Content ] Hi I am using Visual Recognition library and found that the parameter for "createClassifier" method is not working properly.

As you may know, the "createClassifier" method gets "positiveExamples" as second parameter and the example (url in above) use "fs.createReadStream" method for input of image's zip file. However, when I call the function, it gives the error message like below. I think the problem in in the method itself since the test I did with "fs.createReadStream" method separately worked.

[ Error Message ] error: Bad Request: Bad Request at RequestWrapper.formatError (/home/byungmin/visual_recognition/node_modules/ibm-cloud-sdk-core/lib/request-wrapper.js:208:21) at /home/byungmin/visual_recognition/node_modules/ibm-cloud-sdk-core/lib/request-wrapper.js:196:25 at processTicksAndRejections (internal/process/task_queues.js:94:5) { statusText: 'Bad Request', name: 'Bad Request', status: 400, code: 400, message: 'Bad Request', body: '{"error":{"code":400,"error_id":"input_error","description":"Cannot execute learning task. Did not find any files to process in the POST request body. "}}', headers: { 'content-type': 'application/json', 'content-length': '198', 'cache-control': 'no-cache, no-store', 'content-security-policy': "default-src 'none'", expires: '0', pragma: 'no-cache', 'strict-transport-security': 'max-age=31536000; includeSubDomains;', 'x-content-type-options': 'nosniff', 'x-xss-protection': '1; mode=block', 'x-global-transaction-id': '062b935223fd91328ff1d3cadc9b1ebe', 'x-dp-watson-tran-id': '062b935223fd91328ff1d3cadc9b1ebe', 'x-edgeconnect-midmile-rtt': '61', 'x-edgeconnect-origin-mex-latency': '1272', date: 'Wed, 11 Mar 2020 07:14:10 GMT', connection: 'close' } }

I will also attach the code for reference. Please tell me if there are any additional information required.

[ Source Code ]

var formidable = require('formidable');
var fs = require('fs');
var express = require('express');
var app = express(); // Create server
var current_ip = _{mypublicIP hidden considering privacy}_;`

// Visual Recognition declare
const VisualRecognitionV3 = require('ibm-watson/visual-recognition/v3');
const { IamAuthenticator } = require('ibm-watson/auth');

const visualRecognition = new VisualRecognitionV3({
  version: '2018-03-19',
  authenticator: new IamAuthenticator({
    apikey: _{myAPIKey hidden considering privacy}_,
  }),
  url: _{myURL hidden considering privacy}_,
});

// this makes it possible to post files in server's directory
app.use(express.static(__dirname + '/fileupload'));

app.post('/CreateTrainClassifier', function(req, res) {
  var form = new formidable.IncomingForm();
  form.parse(req, function (err, fields, files) {
    // only input one file only so no need to think about multiple files
    for(var i=0; i< form.openedFiles.length; i++) {
      var oldpath = form.openedFiles[i].path;
      var newpath = __dirname + '/fileupload/uploadedzip'+ i +'.zip'; 

      fs.rename(oldpath, newpath, function (err) {
        if (err) throw err;
      });
    }
  });    

  const params = {
    name: 'game',
    positiveExamples: fs.createReadStream('http://' + current_ip + '/uploadedzip0.zip'),
  };

  visualRecognition.createClassifier(params, function(err, response) {
    if(err) {
      console.log('error:', err);
    } else {
      console.log(JSON.stringify(response, null, 2));
    }
  });
});

app.get('/', function(req, res){
  console.log(__dirname);

  res.writeHead(200, {'Content-Type': 'text/html'});
  res.write('<head><title>Visual_Recognition Function Test</title><body>');  
  res.write('<br><form action="CreateTrainClassifier" method="post" enctype="multipart/form-data">');
  res.write('<fieldset><legend>Place image file here</legend><br>');
  res.write('Will create and train Classifier<br><br>');
  res.write('<input type="file" name="filetoupload" multiple="multiple"><br><br>');
  res.write('<input type="submit">');
  res.write('<br></fieldset>');
  res.write('</form>');

  res.write('</body></head>');

  return res.end();
});

app.listen(3000, function() {
  console.log('Server running at port 3000');
});
stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has had no recent activity. It will be closed if no further activity occurs. Thank you for your contributions.