沃森视觉识别运行错误 [英] Watson visual recognition run error

查看:105
本文介绍了沃森视觉识别运行错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Watson视觉识别api设置视觉识别应用程序.为此,我首先下载了watson-developer-cloud,然后将其放在我的index.html和api_request.js旁边的node_modules文件夹中.

I'm trying to set up a visual recognition app using the Watson visual recognition api. To do this I started by downloading watson-developer-cloud and I put it in my node_modules folder, which is next to my index.html and api_request.js.

这是我的api_request.js文件:

This is my api_request.js file:

 var watson = require('./node_modules/watson-developer-cloud');

 var visual_recognition = watson.visual_recognition({
   username: '*********',
   password: '*********',
   version: 'v2-beta',
   version_date: '2015-12-02'
 });
 visual_recognition.listClassifiers({},
    function(err, response) {
     if (err){
        console.log(err);
  }
     else {
        console.log(JSON.stringify(response, null, 2));
  }
    }
 );

它直接来自视觉识别API文档.我在终端中运行了该文件,它提供了所需的输出,该输出是视觉识别分类器的列表.但是,由于它具有node.js函数,因此我决定使用browserify使其在浏览器中运行.我安装了browserify并在与api_request.js和index.html文件相同的目录中从api_request.js构建了bundle.js.

It is taken directly from the visual recognition api documentation. I ran this file in the terminal and it provided the desired output which is a list of visual recognition classifiers. However as it has node.js functions I decided to use browserify to allow it to run in the browser. I installed browserify and built bundle.js out of api_request.js in the same directory as the api_request.js and index.html file.

将index.html链接到bundle.js之后,我在浏览器中将其打开,并且node.js函数没有任何问题.

Once index.html was linked to bundle.js I opened it in the browser and it didn't have any issues with node.js functions.

但是,如果watson-developer-cloud中的文件找不到另一个watson-developer-cloud中的文件,则会发生错误.具体来说index.js找不到v2-beta(我没有编辑watson-developer-cloud文件).我感到奇怪的是,当我在终端中运行api_request.js时,所有的watson-developer-cloud文件都没有任何问题,但是一旦我使用browserify,bundle.js就会记录下index.js找不到v2-的错误测试版.

However an error occurred when a file that was in watson-developer-cloud couldn't find another file that was inside watson-developer-cloud. To be specific index.js couldn't find v2-beta (I didn't edit the watson-developer-cloud files). What I find strange is that when I ran api_request.js in the terminal none of the watson-developer-cloud files had any problems, but once I used browserify, bundle.js logged the error that index.js couldn't find v2-beta.

"build": "browserify api_request.js -o bundle.js"

^这是我用来构建bundle.js的脚本.我能想到的唯一可能导致此错误的原因是browserify.还有其他可能导致这种情况的原因吗?

^that is the script I used to build bundle.js. The only thing I can think could be causing this error is browserify. Is there something else that could be causing this?

推荐答案

不幸的是,大多数IBM Watson API不支持CORS,因此无法在浏览器中使用.由于这个事实,watson-developer-cloud npm模块在编写时并没有考虑到browserify,而是以编程方式从磁盘加载了一些文件. Browserify无法处理-必须提前知道要从磁盘加载哪些文件.您可能可以通过在浏览器命令中添加--require node_modules/watson-developer-cloud/services/visual_recognition/v1.js来解决此问题,但这只会给您带来第一个问题:不支持CORS.

Unfortunately, most of the IBM Watson APIs do not support CORS and thus cannot be used from the browser. Due to this fact, the watson-developer-cloud npm module was not written with browserify in mind and loads some files from disk programmatically. Browserify cannot handle this - it has to know which files to load from disk ahead of time time. You could probably work around it by adding --require node_modules/watson-developer-cloud/services/visual_recognition/v1.js to your browserify command, but that would just leave you with the first issue: no CORS support.

因此,在不考虑所有这些情况的情况下,当前推荐的解决方案是在服务器中创建一个端点,该端点将请求从浏览器转发到watson服务.我知道这是一个额外的步骤,但这是目前可用的最佳选择. https://github.com/watson-developer-cloud/visual-recognition-nodejs 展示了一个很好的例子.

So, with all of that out of the way, the current recommended solution is to create an endpoint in your server that forwards requests from the browser to the watson services. I know it's an extra step, but it's the best option available right now. https://github.com/watson-developer-cloud/visual-recognition-nodejs shows a good example of this.

将来的Watson平台更新可能会全面启用CORS,如果发生这种情况,我们将确保更新npm模块以与browserify配合使用.

A future update the Watson platform may enable CORS across the board, and if that happens, we'll be sure to update the npm module to play nice with browserify.

这篇关于沃森视觉识别运行错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆