Tencent 图像识别 API - 优图
发布于 8 年前 作者 Samurais 19350 次浏览 来自 分享

注册帐号:http://open.youtu.qq.com/

得到AppSecret 和 Key

安装 SDK:

npm install tencentyoutuyun

写一个service module

/**
 * Tencent Youtu service
 * Get API docs from
 */

var logger = require('../common').logging.getLogger('service/youtu');
var config = require('../config');

var youtusdk = require('tencentyoutuyun'),
    youtuconf = youtusdk.conf,
    youtu = youtusdk.youtu;

youtuconf.setAppInfo(config.youtu.appId, config.youtu.secretId, config.youtu.secretKey, config.youtu.userId, 0);

exports = module.exports = youtu;

写一个 testcase - test/youtu.js

var request = require('supertest');
var assert = require('chai').assert;
var expect = require('chai').expect;
var path = require('path');
require('chai').should();

var logger = require('../common').logging.getLogger('test/youtu');
var config = require('../config');
var youtu = require('../services/youtu');

describe('YouTu API', function() {
    this.timeout(10000);
    it('get image tag', function(done) {
        youtu.imagetag(path.join(__dirname, '/a.jpeg'), function(data) {
            logger.info(JSON.stringify(data));
            if (data.code !== 200) {
                done(new Error(JSON.stringify(data)));
            } else {
                done();
            }
        });
    })
})

a.jpeg

0_1460898051142_upload-b2018d9c-e851-428a-82e0-ae85fca5b2c4

run testcase

mocha test/youtu.js

结果: 0_1460898127677_upload-e445b96f-334d-4596-8f84-867bca0c3a8e

{
  "httpcode": 200,
  "code": 200,
  "message": "HTTP OK",
  "data": {
    "errorcode": 0,
    "errormsg": "OK",
    "tags": [
      {
        "tag_name": "天空",
        "tag_confidence": 54
      },
      {
        "tag_name": "建筑",
        "tag_confidence": 14
      },
      {
        "tag_name": "高楼",
        "tag_confidence": 46
      }
    ],
    "faces": []
  }
}

从上可以看出,tag_confidence用来体现识别的自信度,哪个对象是主体。还可以看到,是可以识别人脸的,“faces”。

上个微信之父 …

    it('get image face', function(done) {
        youtu.imagetag(path.join(__dirname, '/zxl.png'), function(data) {
            logger.info(JSON.stringify(data));
            if (data.code !== 200) {
                done(new Error(JSON.stringify(data)));
            } else {
                done();
            }
        });
    })

zxl.png

0_1460898424640_upload-dc687b5c-7f93-4987-a67f-30cc8884c66f

结果:

{
  "httpcode": 200,
  "code": 200,
  "message": "HTTP OK",
  "data": {
    "errorcode": 0,
    "errormsg": "OK",
    "tags": [
      {
        "tag_name": "大头照",
        "tag_confidence": 13
      },
      {
        "tag_name": "男孩",
        "tag_confidence": 65
      }
    ],
    "faces": []
  }
}

其他

优图还有很多其他API,值得研究!

https://github.com/TencentYouTu/nodejs_sdk

5 回复

好像很不错的样子

没看见费用。。。

回到顶部