├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── index.js ├── package.json ├── sample └── sample.js └── tencentyoutuyun ├── auth.js ├── conf.js └── youtu.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/* 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # 1.0.0 / 2015-05-12 2 | 3 | * 第一个版本SDK 4 | * 支持腾讯云智能优图,优图开放平台的人脸识别功能 5 | 6 | 7 | # 1.0.1 / 2015-07-21 8 | * 修复部分代码语法Bug 9 | * 统一函数名为小写字母 10 | 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 tencent 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tencentyun-youtu-nodejs-sdk 2 | 3 | nodejs sdk for [腾讯优图开放平台](http://open.youtu.qq.com/) 4 | 5 | ## 安装 6 | 7 | ```sh 8 | git clone https://github.com/Tencent-YouTu/nodejs_sdk.git 9 | ``` 10 | 11 | ## 名词 12 | 13 | - `AppId` 平台添加应用后分配的AppId 14 | - `SecretId` 平台添加应用后分配的SecretId 15 | - `SecretKey` 平台添加应用后分配的SecretKey 16 | - `签名` 接口鉴权凭证,由`AppId`、`SecretId`、`SecretKey`等生成,详见 17 | 18 | ## API 19 | 20 | ### `conf` 21 | 22 | 配置项相关 23 | 24 | #### `conf.setAppInfo(appid, secretId, secretKey, userid, domain)` 25 | 26 | 初始化配置项 27 | 28 | - 参数 29 | - `appid` AppId 字符串类型 30 | - `secretId` SecretId 字符串类型 31 | - `secretKey` SecretKey 字符串类型 32 | - `userid` 业务中的用户标识 字符串类型 33 | - `domain` 主机标识,0(优图服务),1(腾讯云) 34 | - 返回值 无(`undefined`) 35 | 36 | #### 其它 37 | 38 | - `conf.USER_AGENT` 请求的UserAgent 39 | - `conf.API_YOUTU_SERVER` 请求的优图服务器地址 默认为 api.youtu.qq.com 40 | - `conf.API_YOUTU_PORT` 请求的优图服务器端口 默认为 80 41 | 42 | ### `auth` 43 | 44 | 接口调用时 计算签名鉴权相关逻辑。 45 | 46 | #### `auth.appSign(expired, userid)` 47 | 48 | 获取签名,依赖`conf`中的配置项。 49 | 50 | - 参数 51 | - `expired` 过期时间,UNIX时间戳, 计算的签名在过期时间之前有效. 52 | - `userid` 业务中的用户标识 53 | - 返回值 签名(base64) 54 | 55 | #### 其它 56 | 57 | - `auth.AUTH_PARAMS_ERROR` 参数错误常量(-1) 58 | - `auth.AUTH_SECRET_ID_KEY_ERROR` 密钥ID或者密钥KEY错误常量(-2) 59 | 60 | ### `youtu` 61 | 62 | 优图相关API封装,均为异步函数,使用回调函数方式获取结果。 63 | 64 | > 注意:此处callback函数并未遵从Node.js风格的错误回调`callback(error, data)`,因此需要对返回数据中的状态进行判断,只有200时认为是请求成功。 65 | 66 | #### `youtu.detectface(imagePath, isbigface, callback)` 67 | 68 | 人脸检测,检测给定图片(Image)中的所有人脸(Face)的位置和相应的面部属性。位置包括(x, y, w, h),面部属性包括性别(gender)、年龄(age) 69 | 表情(expression)、眼镜(glass)和姿态(pitch,roll,yaw)。 70 | 71 | - 参数 72 | - `imagePath` 图片路径 73 | - `isbigface` 是否大脸模式 0表示检测所有人脸, 1表示只检测照片最大人脸 适合单人照模式 74 | - `callback(data)` 回调函数 75 | 76 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 77 | 78 | - `httpcode` HTTP状态码 79 | - `code` 状态码,目前和`httpcode`相同 80 | - `message` 状态码对应的描述文字 81 | - `data` 数据对象,参考API文档 82 | 83 | 84 | #### `youtu.faceshape(imagePath, isbigface, callback)` 85 | 86 | 人脸定位,检测给定图片中人脸的五官。对请求图片进行人脸配准,计算构成人脸轮廓的88个点, 87 | 包括眉毛(左右各8点)、眼睛(左右各8点)、鼻子(13点)、嘴巴(22点)、脸型轮廓(21点) 88 | 89 | - 参数 90 | - `imagePath` 图片路径 91 | - `isbigface` 是否大脸模式 0表示检测所有人脸, 1表示只检测照片最大人脸 适合单人照模式 92 | - `callback(data)` 回调函数 93 | 94 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 95 | 96 | - `httpcode` HTTP状态码 97 | - `code` 状态码,目前和`httpcode`相同 98 | - `message` 状态码对应的描述文字 99 | - `data` 数据对象,参考API文档 100 | 101 | 102 | #### `youtu.facecompare(image_a, image_b, callback)` 103 | 104 | 人脸对比,计算两个Face的相似性以及五官相似度。 105 | 106 | - 参数 107 | - `image_a` 第一张图片路径 108 | - `image_b` 第二张图片路径 109 | - `callback(data)` 回调函数 110 | 111 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 112 | 113 | - `httpcode` HTTP状态码 114 | - `code` 状态码,目前和`httpcode`相同 115 | - `message` 状态码对应的描述文字 116 | - `data` 数据对象,参考API文档 117 | 118 | #### `youtu.facecompare_file_url(image_file, image_url, callback)` 119 | 120 | 人脸对比,计算两个Face的相似性以及五官相似度。 121 | 122 | - 参数 123 | - `image_file` 第一张图片路径 124 | - `image_url` 第二张图片url 125 | - `callback(data)` 回调函数 126 | 127 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 128 | 129 | - `httpcode` HTTP状态码 130 | - `code` 状态码,目前和`httpcode`相同 131 | - `message` 状态码对应的描述文字 132 | - `data` 数据对象,参考API文档 133 | 134 | #### `youtu.faceverify(image_a, person_id, callback)` 135 | 136 | 人脸验证,给定一个Face和一个Person,返回是否是同一个人的判断以及置信度。 137 | 138 | - 参数 139 | - `image_a` 图片路径 140 | - `person_id` 待验证的Person 141 | - `callback(data)` 回调函数 142 | 143 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 144 | 145 | - `httpcode` HTTP状态码 146 | - `code` 状态码,目前和`httpcode`相同 147 | - `message` 状态码对应的描述文字 148 | - `data` 数据对象,参考API文档 149 | 150 | #### `youtu.faceidentify(image_a, group_id, callback)` 151 | 152 | 人脸识别,对于一个待识别的人脸图片,在一个Group中识别出最相似的Top5 Person作为其身份返回,返回的Top5中按照相似度从大到小排列。 153 | 154 | - 参数 155 | - `image_a` 图片路径 156 | - `group_id` 需要识别的人 所在的组 157 | - `callback(data)` 回调函数 158 | 159 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 160 | 161 | - `httpcode` HTTP状态码 162 | - `code` 状态码,目前和`httpcode`相同 163 | - `message` 状态码对应的描述文字 164 | - `data` 数据对象,参考API文档 165 | 166 | #### `youtu.multifaceidentify(image_a, group_id, group_ids, topn, min_size, callback)` 167 | 168 | 上传人脸图片,进行多人脸检索。 169 | 170 | - 参数 171 | - `image_a` 图片路径 172 | - `group_id` 需要识别的人 所在的组 173 | - `group_ids` 需要识别的人所在的组的列表(数组) 174 | - `topn` 候选人脸数量,一般使用默认值5 175 | - `min_size` 人脸检测最小尺寸,一般使用默认值40 176 | - `callback(data)` 回调函数 177 | 178 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 179 | 180 | - `httpcode` HTTP状态码 181 | - `code` 状态码,目前和`httpcode`相同 182 | - `message` 状态码对应的描述文字 183 | - `data` 数据对象,参考API文档 184 | 185 | #### `youtu.newperson(image_a, person_id, person_name, group_ids, persontag, callback)` 186 | 187 | 个体创建,创建一个Person,并将Person放置到group_ids指定的组当中。 188 | 189 | - 参数 190 | - `image_a` 图片路径 191 | - `person_id` 个体Person 192 | - `person_name` 个体Person的名字 193 | - `group_ids` 要加入的组的列表(数组) 194 | - `persontag` 备注信息,用户自解释字段 195 | - `callback(data)` 回调函数 196 | 197 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 198 | 199 | - `httpcode` HTTP状态码 200 | - `code` 状态码,目前和`httpcode`相同 201 | - `message` 状态码对应的描述文字 202 | - `data` 数据对象,参考API文档 203 | 204 | #### `youtu.delperson(person_id, callback)` 205 | 206 | 删除一个Person 207 | 208 | - 参数 209 | - `person_id` 个体Person 210 | - `callback(data)` 回调函数 211 | 212 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 213 | 214 | - `httpcode` HTTP状态码 215 | - `code` 状态码,目前和`httpcode`相同 216 | - `message` 状态码对应的描述文字 217 | - `data` 数据对象,参考API文档 218 | 219 | 220 | #### `youtu.addface(person_id, images, facetag, callback)` 221 | 222 | 添加人脸,在创建一个Person后, 增加person下面的人脸, 可以用于后面的比对。 223 | 224 | - 参数 225 | - `person_id` 个体Person 226 | - `images` 图片路径(数组) 227 | - `facetag` 人脸自定义标签 228 | - `callback(data)` 回调函数 229 | 230 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 231 | 232 | - `httpcode` HTTP状态码 233 | - `code` 状态码,目前和`httpcode`相同 234 | - `message` 状态码对应的描述文字 235 | - `data` 数据对象,参考API文档 236 | 237 | 238 | 239 | #### `youtu.delface(person_id, face_ids, callback)` 240 | 241 | 删除人脸,删除一个person下的face,包括特征,属性和face_id。 242 | 243 | - 参数 244 | - `person_id` 个体Person 245 | - `face_ids` 要删除的faceId列表(数组) 246 | - `callback(data)` 回调函数 247 | 248 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 249 | 250 | - `httpcode` HTTP状态码 251 | - `code` 状态码,目前和`httpcode`相同 252 | - `message` 状态码对应的描述文字 253 | - `data` 数据对象,参考API文档 254 | 255 | #### `youtu.setinfo(person_name, person_id, tag, callback)` 256 | 257 | 设置Person的信息 258 | 259 | - 参数 260 | - `person_name` 个体Person的name 261 | - `person_id` 个体Person 262 | - `tag` 个体Person的tag, 用户自解释字段 263 | - `callback(data)` 回调函数 264 | 265 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 266 | 267 | - `httpcode` HTTP状态码 268 | - `code` 状态码,目前和`httpcode`相同 269 | - `message` 状态码对应的描述文字 270 | - `data` 数据对象,参考API文档 271 | 272 | #### `youtu.getinfo(person_id, callback)` 273 | 274 | 获取一个Person的信息,包括name、id、tag、相关的face以及groups等信息。 275 | 276 | - 参数 277 | - `person_id` 个体Person 278 | - `callback(data)` 回调函数 279 | 280 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 281 | 282 | - `httpcode` HTTP状态码 283 | - `code` 状态码,目前和`httpcode`相同 284 | - `message` 状态码对应的描述文字 285 | - `data` 数据对象,参考API文档 286 | 287 | #### `youtu.getgroupids(callback)` 288 | 289 | 获取一个AppId下所有group列表 290 | 291 | - 参数 292 | - `callback(data)` 回调函数 293 | 294 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 295 | 296 | - `httpcode` HTTP状态码 297 | - `code` 状态码,目前和`httpcode`相同 298 | - `message` 状态码对应的描述文字 299 | - `data` 数据对象,参考API文档 300 | 301 | #### `youtu.getpersonIds(group_id, callback)` 302 | 303 | 获取一个组Group中所有person列表 304 | 305 | - 参数 306 | - `group_id` 组 307 | - `callback(data)` 回调函数 308 | 309 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 310 | 311 | - `httpcode` HTTP状态码 312 | - `code` 状态码,目前和`httpcode`相同 313 | - `message` 状态码对应的描述文字 314 | - `data` 数据对象,参考API文档 315 | 316 | 317 | #### `youtu.getfaceIds(person_id, callback)` 318 | 319 | 获取一个组person中所有face列表 320 | 321 | - 参数 322 | - `person_id` Person 323 | - `callback(data)` 回调函数 324 | 325 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 326 | 327 | - `httpcode` HTTP状态码 328 | - `code` 状态码,目前和`httpcode`相同 329 | - `message` 状态码对应的描述文字 330 | - `data` 数据对象,参考API文档 331 | 332 | ### `youtu.getfaceinfo(face_id, callback)` 333 | 334 | 获取一个face的相关特征信息 335 | 336 | - 参数 337 | - `face_id` Face 338 | - `callback(data)` 回调函数 339 | 340 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 341 | 342 | - `httpcode` HTTP状态码 343 | - `code` 状态码,目前和`httpcode`相同 344 | - `message` 状态码对应的描述文字 345 | - `data` 数据对象,参考API文档 346 | 347 | ### `youtu.fuzzydetect(imagePath, callback)` 348 | 349 | 模糊检测 350 | 351 | - 参数 352 | - `imagePath` 图片路径(url或本地路径) 353 | - `callback(data)` 回调函数 354 | 355 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 356 | 357 | - `httpcode` HTTP状态码 358 | - `code` 状态码,目前和`httpcode`相同 359 | - `message` 状态码对应的描述文字 360 | - `data` 数据对象,参考API文档 361 | 362 | ### `youtu.fooddetect(imagePath, callback)` 363 | 364 | 检测食物 365 | 366 | - 参数 367 | - `imagePath` 图片路径(url或本地路径) 368 | - `callback(data)` 回调函数 369 | 370 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 371 | 372 | - `httpcode` HTTP状态码 373 | - `code` 状态码,目前和`httpcode`相同 374 | - `message` 状态码对应的描述文字 375 | - `data` 数据对象,参考API文档 376 | 377 | 378 | ### `youtu.imagetag(imagePath, callback)` 379 | 380 | 图片分类 381 | 382 | - 参数 383 | - `imagePath` 图片路径(url或本地路径) 384 | - `callback(data)` 回调函数 385 | 386 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 387 | 388 | - `httpcode` HTTP状态码 389 | - `code` 状态码,目前和`httpcode`相同 390 | - `message` 状态码对应的描述文字 391 | - `data` 数据对象,参考API文档 392 | 393 | ### `youtu.imageporn(imagePath, callback)` 394 | 395 | 色情图像检测 396 | 397 | - 参数 398 | - `imagePath` 图片路径(url或本地路径) 399 | - `callback(data)` 回调函数 400 | 401 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 402 | 403 | - `httpcode` HTTP状态码 404 | - `code` 状态码,目前和`httpcode`相同 405 | - `message` 状态码对应的描述文字 406 | - `data` 数据对象,参考API文档 407 | 408 | ### `youtu.imageterrorism(imagePath, callback)` 409 | 410 | 暴恐图片识别 411 | 412 | - 参数 413 | - `imagePath` 图片路径(url或本地路径) 414 | - `callback(data)` 回调函数 415 | 416 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 417 | 418 | - `httpcode` HTTP状态码 419 | - `code` 状态码,目前和`httpcode`相同 420 | - `message` 状态码对应的描述文字 421 | - `data` 数据对象,参考API文档 422 | 423 | ### `youtu.carclassify(imagePath, callback)` 424 | 425 | 车辆属性识别 426 | 427 | - 参数 428 | - `imagePath` 图片路径(url或本地路径) 429 | - `callback(data)` 回调函数 430 | 431 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 432 | 433 | - `httpcode` HTTP状态码 434 | - `code` 状态码,目前和`httpcode`相同 435 | - `message` 状态码对应的描述文字 436 | - `data` 数据对象,参考API文档 437 | 438 | 439 | ### `youtu.idcardocr(imagePath, cardType, callback)` 440 | 441 | 身份证OCR识别 442 | 443 | - 参数 444 | - `imagePath` 图片路径(url或本地路径) 445 | - `cardType` 0 代表输入图像是身份证正面, 1代表输入是身份证反面 446 | - `callback(data)` 回调函数 447 | 448 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 449 | 450 | - `httpcode` HTTP状态码 451 | - `code` 状态码,目前和`httpcode`相同 452 | - `message` 状态码对应的描述文字 453 | - `data` 数据对象,参考API文档 454 | 455 | 456 | ### `youtu.generalocr(imagePath, callback)` 457 | 458 | 通用OCR识别 459 | 460 | - 参数 461 | - `imagePath` 图片路径(url或本地路径) 462 | - `callback(data)` 回调函数 463 | 464 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 465 | 466 | - `httpcode` HTTP状态码 467 | - `code` 状态码,目前和`httpcode`相同 468 | - `message` 状态码对应的描述文字 469 | - `data` 数据对象,参考API文档 470 | 471 | 472 | ### `youtu.bcocr(imagePath, callback)` 473 | 474 | 名片OCR识别 475 | 476 | - 参数 477 | - `imagePath` 图片路径(url或本地路径) 478 | - `callback(data)` 回调函数 479 | 480 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 481 | 482 | - `httpcode` HTTP状态码 483 | - `code` 状态码,目前和`httpcode`相同 484 | - `message` 状态码对应的描述文字 485 | - `data` 数据对象,参考API文档 486 | 487 | 488 | ### `youtu.driverlicenseocr(imagePath, cardType, callback)` 489 | 490 | 行驶证&驾驶证OCR识别 491 | 492 | - 参数 493 | - `imagePath` 图片路径(url或本地路径) 494 | - `cardType` 0 代表输入图像是行驶证, 1代表输入是驾驶证 495 | - `callback(data)` 回调函数 496 | 497 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 498 | 499 | - `httpcode` HTTP状态码 500 | - `code` 状态码,目前和`httpcode`相同 501 | - `message` 状态码对应的描述文字 502 | - `data` 数据对象,参考API文档 503 | 504 | 505 | ### `youtu.creditcardocr(imagePath, callback)` 506 | 507 | 银行卡OCR识别 508 | 509 | - 参数 510 | - `imagePath` 图片路径(url或本地路径) 511 | - `callback(data)` 回调函数 512 | 513 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 514 | 515 | - `httpcode` HTTP状态码 516 | - `code` 状态码,目前和`httpcode`相同 517 | - `message` 状态码对应的描述文字 518 | - `data` 数据对象,参考API文档 519 | 520 | 521 | ### `youtu.bizlicenseocr(imagePath, callback)` 522 | 523 | 营业执照OCR识别 524 | 525 | - 参数 526 | - `imagePath` 图片路径(url或本地路径) 527 | - `callback(data)` 回调函数 528 | 529 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 530 | 531 | - `httpcode` HTTP状态码 532 | - `code` 状态码,目前和`httpcode`相同 533 | - `message` 状态码对应的描述文字 534 | - `data` 数据对象,参考API文档 535 | 536 | 537 | ### `youtu.plateocr(imagePath, callback)` 538 | 539 | 车牌OCR识别 540 | 541 | - 参数 542 | - `imagePath` 图片路径(url或本地路径) 543 | - `callback(data)` 回调函数 544 | 545 | 其中回调函数`callback`的参数`data`是一个对象,结构如下: 546 | 547 | - `httpcode` HTTP状态码 548 | - `code` 状态码,目前和`httpcode`相同 549 | - `message` 状态码对应的描述文字 550 | - `data` 数据对象,参考API文档 551 | 552 | ## 致谢 553 | 20150717 感谢[TooBug](https://github.com/TooBug) 提出的接口说明文档 和 bug 修复 554 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | var libpath = './tencentyoutuyun'; 3 | 4 | module.exports = { 5 | auth: require(libpath + '/auth.js'), 6 | conf: require(libpath + '/conf.js'), 7 | youtu: require(libpath + '/youtu.js'), 8 | }; 9 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tencentyoutuyun", 3 | "version": "1.0.0", 4 | "description": "node sdk for qcloud youtu", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/TencentYouTu/nodejs_sdk.git" 12 | }, 13 | "keywords": [ 14 | "qcloud", 15 | "youtu" 16 | ], 17 | "author": "tencentyoutuyun", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/TencentYouTu/nodejs_sdk/issues" 21 | }, 22 | "homepage": "https://github.com/TencentYouTu/nodejs_sdk#readme", 23 | "engines": [ 24 | "node >= 0.4.7" 25 | ], 26 | "dependencies": { 27 | "iconv-lite": "^0.4.13" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /sample/sample.js: -------------------------------------------------------------------------------- 1 | var tencentyoutuyun = require('..'); 2 | var conf = tencentyoutuyun.conf; 3 | var youtu = tencentyoutuyun.youtu; 4 | 5 | // 设置开发者和应用信息, 请填写你在开放平台 6 | var appid = ''; 7 | var secretId = ''; 8 | var secretKey = ''; 9 | var userid = ''; 10 | 11 | conf.setAppInfo(appid, secretId, secretKey, userid, 0) 12 | 13 | 14 | //youtu.imageporn('a1.jpg', function(data){ 15 | // console.log("imagetag:" + JSON.stringify(data)); 16 | //}); 17 | youtu.imageporn('http://open.youtu.qq.com/app/img/experience/porn/image_porn01.jpg', function(data){ 18 | console.log("imagetag:" + JSON.stringify(data)); 19 | }); 20 | 21 | //youtu.idcardocr('a.jpg', 0, function(data){ 22 | // console.log("idcardocr:" + JSON.stringify(data)); 23 | //}); 24 | 25 | //youtu.namecardocr('a.jpg', false, function(data){ 26 | // console.log("namecardocr:" + JSON.stringify(data)); 27 | //}); 28 | 29 | youtu.imageterrorism('http://open.youtu.qq.com/app/img/experience/terror/img_terror01.jpg', function(data){ 30 | console.log("imageterrorism:" + JSON.stringify(data)); 31 | }); 32 | 33 | youtu.carclassify('http://open.youtu.qq.com/app/img/experience/car/car_01.jpg', function(data){ 34 | console.log("carclassify:" + JSON.stringify(data)); 35 | }); 36 | 37 | youtu.creditcardocr('http://open.youtu.qq.com/app/img/experience/char_general/ocr_card_1.jpg', function(data){ 38 | console.log("creditcardocr:" + JSON.stringify(data)); 39 | }); 40 | 41 | youtu.bizlicenseocr('http://open.youtu.qq.com/app/img/experience/char_general/ocr_yyzz_01.jpg', function(data){ 42 | console.log("bizlicenseocr:" + JSON.stringify(data)); 43 | }); 44 | 45 | youtu.plateocr('http://open.youtu.qq.com/app/img/experience/char_general/ocr_license_1.jpg', function(data){ 46 | console.log("plateocr:" + JSON.stringify(data)); 47 | }); 48 | 49 | youtu.multifaceidentify('http://open.youtu.qq.com/app/img/experience/face_img/face_01.jpg', 'test', [], 5, 40, function(data){ 50 | console.log("multifaceidentify:" + JSON.stringify(data)); 51 | }); 52 | 53 | -------------------------------------------------------------------------------- /tencentyoutuyun/auth.js: -------------------------------------------------------------------------------- 1 | var crypto = require('crypto'); 2 | var urlM = require('url'); 3 | var conf = require('./conf'); 4 | 5 | exports.AUTH_PARAMS_ERROR = -1; 6 | exports.AUTH_SECRET_ID_KEY_ERROR = -2; 7 | 8 | exports.appSign = function(expired, userid) { 9 | 10 | var secretId = conf.SECRET_ID || ''; 11 | var secretKey = conf.SECRET_KEY || ''; 12 | var appid = conf.APPID || ''; 13 | 14 | var pexpired = expired || ''; 15 | var puserid = userid || conf.USERID; 16 | 17 | 18 | if (!pexpired || !puserid ) { 19 | return module.exports.AUTH_PARAMS_ERROR; 20 | } 21 | 22 | if ( !secretId || !secretKey ) { 23 | return module.exports.AUTH_SECRET_ID_KEY_ERROR; 24 | } 25 | 26 | var now = parseInt(Date.now() / 1000); 27 | var rdm = parseInt(Math.random() * Math.pow(2, 32)); 28 | 29 | // the order of every key is not matter verify 30 | var plainText = 'a=' + appid + '&k=' + secretId + '&e=' + pexpired + '&t=' + now + '&r=' + rdm + '&u=' + puserid; 31 | 32 | var data = new Buffer(plainText,'utf8'); 33 | 34 | var res = crypto.createHmac('sha1',secretKey).update(data).digest(); 35 | 36 | var bin = Buffer.concat([res,data]); 37 | 38 | var sign = bin.toString('base64'); 39 | 40 | return sign; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /tencentyoutuyun/conf.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | var path = require('path'); 3 | var os = require('os'); 4 | 5 | // 请到 open.youtu.qq.com查看您对应的appid相关信息并填充 6 | // 请统一 通过 setAppInfo 设置 7 | 8 | exports.APPID = ''; 9 | exports.SECRET_ID = ''; 10 | exports.SECRET_KEY = ''; 11 | exports.USERID = ''; 12 | 13 | var pkg = JSON.parse(fs.readFileSync(path.join(__dirname, '../', 'package.json'))); 14 | var ua = function() { 15 | return 'QcloudNodejs/' + pkg.version + ' (' + os.type() + '; ' + os.platform() + '; ' + os.arch() + '; ) '; 16 | } 17 | 18 | exports.USER_AGENT = ua; 19 | exports.API_YOUTU_SERVER= 'api.youtu.qq.com'; 20 | exports.API_YOUTU_PORT= 80; 21 | exports.API_DOMAIN = 0; 22 | 23 | // 初始化 应用信息 24 | exports.setAppInfo = function(appid, secretId, secretKey, userid, domain) { 25 | module.exports.APPID = appid; 26 | module.exports.SECRET_ID = secretId; 27 | module.exports.SECRET_KEY = secretKey; 28 | module.exports.USERID = userid; 29 | if(domain == 0) 30 | { 31 | exports.API_YOUTU_SERVER= 'api.youtu.qq.com'; 32 | } 33 | else 34 | { 35 | exports.API_YOUTU_SERVER= 'api.youtu.qq.com'; 36 | } 37 | exports.API_DOMAIN = domain; 38 | } 39 | 40 | 41 | -------------------------------------------------------------------------------- /tencentyoutuyun/youtu.js: -------------------------------------------------------------------------------- 1 | var http = require('http'); 2 | var https = require('https'); 3 | var fs = require('fs'); 4 | 5 | 6 | 7 | var auth = require('./auth'); 8 | var conf = require('./conf'); 9 | 10 | // 30 days 11 | var EXPIRED_SECONDS = 2592000; 12 | 13 | /** 14 | * return the status message 15 | */ 16 | function statusText(status) { 17 | 18 | var statusText = 'unkown'; 19 | 20 | switch (status) { 21 | case 200: 22 | statusText = 'HTTP OK'; 23 | break; 24 | case 400: 25 | statusText = 'Bad Request'; 26 | break; 27 | case 401: 28 | statusText = 'Unauthorized'; 29 | break; 30 | case 403: 31 | statusText = 'Forbidden'; 32 | break; 33 | case 500: 34 | statusText = 'Internal Server Error'; 35 | break; 36 | } 37 | return statusText; 38 | }; 39 | 40 | function getrequest(protocol, params, callback) { 41 | 42 | return protocol.request(params, function(response) { 43 | 44 | // console.log('STATUS: ' + response.statusCode); 45 | // console.log('HEADERS: ' + JSON.stringify(response.headers)); 46 | 47 | if( response.statusCode != 200 ){ 48 | callback({'httpcode':response.statusCode, 'code':response.statusCode , 'message':statusText(response.statusCode) , 'data':{}}); 49 | return; 50 | } 51 | 52 | var body = ''; 53 | response.setEncoding('utf8'); 54 | 55 | response.on('data', function (chunk) { 56 | body += chunk; 57 | }); 58 | response.on('end', function(){ 59 | callback({'httpcode':response.statusCode, 'code':response.statusCode , 'message':statusText(response.statusCode) , 'data':JSON.parse(body)}); 60 | }); 61 | 62 | response.on('error', function(e){ 63 | callback({'httpcode':response.statusCode, 'code':response.statusCode , 'message': '' + e , 'data':{} }); 64 | }); 65 | }); // 66 | }; 67 | 68 | /** 69 | * @brief detectface 70 | * @param imagePath 待检测的路径(本地路径或url) 71 | * @param isbigface 是否大脸模式 0表示检测所有人脸, 1表示只检测照片最大人脸 适合单人照模式 72 | * @param callback 回调函数, 参见Readme 文档 73 | */ 74 | exports.detectface = function(imagePath, isbigface, callback) { 75 | 76 | callback = callback || function(ret){console.log(ret)}; 77 | 78 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 79 | var sign = auth.appSign(expired); 80 | var tag = imagePath.substring(0,4); 81 | var request_body = ''; 82 | if (tag == 'http') 83 | { 84 | request_body = JSON.stringify({ 85 | app_id: conf.APPID, 86 | url : imagePath, 87 | mode : isbigface, 88 | }); 89 | } 90 | else 91 | { 92 | try { 93 | var data = fs.readFileSync(imagePath).toString('base64'); 94 | } catch (e) { 95 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); 96 | return; 97 | } 98 | 99 | if(data == null) { 100 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); 101 | return; 102 | }; 103 | 104 | request_body = JSON.stringify({ 105 | app_id: conf.APPID, 106 | image : data.toString('base64'), 107 | mode : isbigface, 108 | }); 109 | } 110 | var params = { 111 | hostname: conf.API_YOUTU_SERVER, 112 | path: '/youtu/api/detectface', 113 | method: 'POST', 114 | headers: { 115 | 'Authorization': sign, 116 | 'User-Agent' : conf.USER_AGENT(), 117 | 'Content-Length': request_body.length, 118 | 'Content-Type': 'text/json' 119 | } 120 | }; 121 | 122 | //console.log(request_body); 123 | //console.log(params); 124 | var request = null; 125 | if (conf.API_DOMAIN == 0) 126 | { 127 | request = getrequest(http, params, callback); 128 | } 129 | else { 130 | request = getrequest(https, params, callback); 131 | } 132 | 133 | request.on('error', function(e) { 134 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 135 | }); 136 | 137 | // send the request body 138 | request.end(request_body); 139 | //console.log(request_body); 140 | } 141 | 142 | 143 | /** 144 | * @brief faceshape 145 | * @param imagePath 待检测的路径(本地路径或url) 146 | * @param isbigface 是否大脸模式 147 | * @param callback 回调函数, 参见Readme 文档 148 | */ 149 | exports.faceshape = function(imagePath, isbigface, callback) { 150 | 151 | callback = callback || function(ret){console.log(ret)}; 152 | 153 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 154 | var sign = auth.appSign(expired); 155 | var tag = imagePath.substring(0,4); 156 | var request_body = ''; 157 | if (tag == 'http') 158 | { 159 | request_body = JSON.stringify({ 160 | app_id: conf.APPID, 161 | url : imagePath, 162 | mode : isbigface, 163 | }); 164 | } 165 | else 166 | { 167 | try { 168 | var data = fs.readFileSync(imagePath).toString('base64'); 169 | } catch (e) { 170 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); 171 | return; 172 | } 173 | if(data == null) { 174 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); 175 | return; 176 | }; 177 | 178 | request_body = JSON.stringify({ 179 | app_id: conf.APPID, 180 | image : data.toString('base64'), 181 | mode : isbigface, 182 | }); 183 | } 184 | var params = { 185 | hostname: conf.API_YOUTU_SERVER, 186 | path: '/youtu/api/faceshape', 187 | method: 'POST', 188 | headers: { 189 | 'Authorization': sign, 190 | 'User-Agent' : conf.USER_AGENT(), 191 | 'Content-Length': request_body.length, 192 | 'Content-Type': 'text/json' 193 | } 194 | }; 195 | 196 | var request = null; 197 | if (conf.API_DOMAIN == 0) 198 | { 199 | request = getrequest(http, params, callback); 200 | } 201 | else { 202 | request = getrequest(https, params, callback); 203 | } 204 | 205 | request.on('error', function(e) { 206 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 207 | }); 208 | 209 | // send the request body 210 | request.end(request_body); 211 | } 212 | 213 | 214 | /** 215 | * @brief facecompare 216 | * @param image_a 待比对的A图片路径(本地路径或url) 217 | * @param image_b 待比对的B图片路径(本地路径或url) 218 | * @param callback 回调函数, 参见Readme 文档 219 | */ 220 | exports.facecompare = function(image_a, image_b, callback) { 221 | 222 | callback = callback || function(ret){console.log(ret)}; 223 | 224 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 225 | var sign = auth.appSign(expired); 226 | var tag = image_a.substring(0,4); 227 | var request_body = ''; 228 | if (tag == 'http') 229 | { 230 | var request_body = JSON.stringify({ 231 | app_id: conf.APPID, 232 | urlA : image_a, 233 | urlB : image_b, 234 | }); 235 | } 236 | else 237 | { 238 | try { 239 | var data_a = fs.readFileSync(image_a).toString('base64'); 240 | var data_b = fs.readFileSync(image_b).toString('base64'); 241 | } catch (e) { 242 | callback({'httpcode':0, 'code':-1, 'message':'file ' + image_a + ' or ' + image_b + ' not exists', 'data':{}}); 243 | return; 244 | } 245 | 246 | 247 | 248 | if(data_a == null || data_b == null ) { 249 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); 250 | return; 251 | }; 252 | 253 | var request_body = JSON.stringify({ 254 | app_id: conf.APPID, 255 | imageA : data_a.toString('base64'), 256 | imageB : data_b.toString('base64'), 257 | }); 258 | } 259 | 260 | var params = { 261 | hostname: conf.API_YOUTU_SERVER, 262 | path: '/youtu/api/facecompare', 263 | method: 'POST', 264 | headers: { 265 | 'Authorization': sign, 266 | 'User-Agent' : conf.USER_AGENT(), 267 | 'Content-Length': request_body.length, 268 | 'Content-Type': 'text/json' 269 | } 270 | }; 271 | 272 | //console.log(request_body); 273 | var request = null; 274 | if (conf.API_DOMAIN == 0) 275 | { 276 | request = getrequest(http, params, callback); 277 | } 278 | else { 279 | request = getrequest(https, params, callback); 280 | } 281 | 282 | request.on('error', function(e) { 283 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 284 | }); 285 | 286 | // send the request body 287 | request.end(request_body); 288 | } 289 | 290 | 291 | /** 292 | * @brief facecompare 293 | * @param image_file 待比对的A图片路径(本地路径) 294 | * @param image_url 待比对的B图片路径(url) 295 | * @param callback 回调函数, 参见Readme 文档 296 | */ 297 | exports.facecompare_file_url = function(image_file, image_url, callback) { 298 | 299 | callback = callback || function(ret){console.log(ret)}; 300 | 301 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 302 | var sign = auth.appSign(expired); 303 | 304 | try { 305 | var data_a = fs.readFileSync(image_file).toString('base64'); 306 | } catch (e) { 307 | callback({'httpcode':0, 'code':-1, 'message':'file ' + image_file + ' not exists', 'data':{}}); 308 | return; 309 | } 310 | 311 | if(data_a == null ) { 312 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); 313 | return; 314 | }; 315 | 316 | var request_body = JSON.stringify({ 317 | app_id: conf.APPID, 318 | imageA : data_a.toString('base64'), 319 | urlB : image_url, 320 | }); 321 | 322 | 323 | var params = { 324 | hostname: conf.API_YOUTU_SERVER, 325 | path: '/youtu/api/facecompare', 326 | method: 'POST', 327 | headers: { 328 | 'Authorization': sign, 329 | 'User-Agent' : conf.USER_AGENT(), 330 | 'Content-Length': request_body.length, 331 | 'Content-Type': 'text/json' 332 | } 333 | }; 334 | 335 | //console.log(request_body); 336 | var request = null; 337 | if (conf.API_DOMAIN == 0) 338 | { 339 | request = getrequest(http, params, callback); 340 | } 341 | else { 342 | request = getrequest(https, params, callback); 343 | } 344 | 345 | request.on('error', function(e) { 346 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 347 | }); 348 | 349 | // send the request body 350 | request.end(request_body); 351 | } 352 | 353 | 354 | /** 355 | * @brief faceverify 356 | * @param person_id 待验证的人脸id 357 | * @param imagePath 待验证的图片路径(本地路径或url) 358 | * @param callback 回调函数, 参见Readme 文档 359 | */ 360 | exports.faceverify = function(imagePath, person_id, callback) { 361 | 362 | callback = callback || function(ret){console.log(ret)}; 363 | 364 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 365 | var sign = auth.appSign(expired); 366 | var tag = imagePath.substring(0,4); 367 | var request_body = ''; 368 | 369 | if (tag == 'http') 370 | { 371 | request_body = JSON.stringify({ 372 | app_id: conf.APPID, 373 | url : imagePath, 374 | person_id : person_id, 375 | }); 376 | } 377 | else 378 | { 379 | try { 380 | var data = fs.readFileSync(imagePath).toString('base64'); 381 | } catch (e) { 382 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); 383 | return; 384 | } 385 | 386 | if(data == null) { 387 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); 388 | return; 389 | }; 390 | 391 | request_body = JSON.stringify({ 392 | app_id: conf.APPID, 393 | image : data.toString('base64'), 394 | person_id : person_id, 395 | }); 396 | } 397 | 398 | var params = { 399 | hostname: conf.API_YOUTU_SERVER, 400 | path: '/youtu/api/faceverify', 401 | method: 'POST', 402 | headers: { 403 | 'Authorization': sign, 404 | 'User-Agent' : conf.USER_AGENT(), 405 | 'Content-Length': request_body.length, 406 | 'Content-Type': 'text/json' 407 | } 408 | }; 409 | 410 | //console.log(request_body); 411 | var request = null; 412 | if (conf.API_DOMAIN == 0) 413 | { 414 | request = getrequest(http, params, callback); 415 | } 416 | else { 417 | request = getrequest(https, params, callback); 418 | } 419 | 420 | 421 | request.on('error', function(e) { 422 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 423 | }); 424 | 425 | // send the request body 426 | request.end(request_body); 427 | 428 | } 429 | 430 | 431 | /** 432 | * @brief faceidentify 433 | * @param group_id 识别的组id 434 | * @param imagePath 待识别的图片路径(本地路径或url) 435 | * @param callback 回调函数, 参见Readme 文档 436 | */ 437 | exports.faceidentify= function(imagePath, group_id, callback) { 438 | 439 | callback = callback || function(ret){console.log(ret)}; 440 | 441 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 442 | var sign = auth.appSign(expired); 443 | var tag = imagePath.substring(0,4); 444 | var request_body = ''; 445 | 446 | if (tag == 'http') 447 | { 448 | request_body = JSON.stringify({ 449 | app_id: conf.APPID, 450 | url : imagePath, 451 | group_id : group_id, 452 | }); 453 | } 454 | else 455 | { 456 | try { 457 | var data = fs.readFileSync(imagePath).toString('base64'); 458 | } catch (e) { 459 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); 460 | return; 461 | } 462 | 463 | 464 | if(data == null) { 465 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); 466 | return; 467 | }; 468 | 469 | request_body = JSON.stringify({ 470 | app_id: conf.APPID, 471 | image : data.toString('base64'), 472 | group_id : group_id, 473 | }); 474 | } 475 | 476 | var params = { 477 | hostname: conf.API_YOUTU_SERVER, 478 | path: '/youtu/api/faceidentify', 479 | method: 'POST', 480 | headers: { 481 | 'Authorization': sign, 482 | 'User-Agent' : conf.USER_AGENT(), 483 | 'Content-Length': request_body.length, 484 | 'Content-Type': 'text/json' 485 | } 486 | }; 487 | 488 | var request = null; 489 | if (conf.API_DOMAIN == 0) 490 | { 491 | request = getrequest(http, params, callback); 492 | } 493 | else { 494 | request = getrequest(https, params, callback); 495 | } 496 | 497 | request.on('error', function(e) { 498 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 499 | }); 500 | 501 | // send the request body 502 | request.end(request_body); 503 | } 504 | 505 | /** 506 | * @brief multifaceidentify 507 | * @param group_id 识别的组id 508 | * @param group_ids s个体存放的组id,可以指定多个组id,用户指定(组默认创建) 509 | * @param imagePath 待识别的图片路径(本地路径或url) 510 | * @param callback 回调函数, 参见Readme 文档 511 | */ 512 | exports.multifaceidentify = function(imagePath, group_id, group_ids, topn, min_size, callback) { 513 | 514 | callback = callback || function(ret){console.log(ret)}; 515 | 516 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 517 | var sign = auth.appSign(expired); 518 | var tag = imagePath.substring(0,4); 519 | var request_body = ''; 520 | 521 | if (tag == 'http') 522 | { 523 | request_body = JSON.stringify({ 524 | app_id: conf.APPID, 525 | url : imagePath, 526 | group_id : group_id, 527 | group_ids : group_ids, 528 | topn : topn, 529 | min_size: min_size, 530 | }); 531 | } 532 | else 533 | { 534 | try { 535 | var data = fs.readFileSync(imagePath).toString('base64'); 536 | } catch (e) { 537 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); 538 | return; 539 | } 540 | 541 | 542 | if(data == null) { 543 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); 544 | return; 545 | }; 546 | 547 | request_body = JSON.stringify({ 548 | app_id: conf.APPID, 549 | image : data.toString('base64'), 550 | group_id : group_id, 551 | group_ids : group_ids, 552 | topn : topn, 553 | min_size: min_size, 554 | }); 555 | } 556 | 557 | var params = { 558 | hostname: conf.API_YOUTU_SERVER, 559 | path: '/youtu/api/multifaceidentify', 560 | method: 'POST', 561 | headers: { 562 | 'Authorization': sign, 563 | 'User-Agent' : conf.USER_AGENT(), 564 | 'Content-Length': request_body.length, 565 | 'Content-Type': 'text/json' 566 | } 567 | }; 568 | 569 | var request = null; 570 | if (conf.API_DOMAIN == 0) 571 | { 572 | request = getrequest(http, params, callback); 573 | } 574 | else { 575 | request = getrequest(https, params, callback); 576 | } 577 | 578 | request.on('error', function(e) { 579 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 580 | }); 581 | 582 | // send the request body 583 | request.end(request_body); 584 | } 585 | 586 | /** 587 | * @brief newperson 588 | * @param imagePath 图片路径(本地路径或url) 589 | * @param person_id 新建的个体id,用户指定,需要保证app_id下的唯一性 590 | * @param person_name 个体的名字 591 | * @param group_ids 新建的个体存放的组id,可以指定多个组id,用户指定(组默认创建) 592 | * @param persontag 人备注信息,用户自解释字段 593 | * @param callback 回调函数, 参见Readme 文档 594 | */ 595 | exports.newperson= function(imagePath, person_id, person_name, group_ids, persontag, callback) { 596 | 597 | callback = callback || function(ret){console.log(ret)}; 598 | 599 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 600 | var sign = auth.appSign(expired); 601 | 602 | var tag = imagePath.substring(0,4); 603 | var request_body = ''; 604 | 605 | if (tag == 'http') 606 | { 607 | request_body = JSON.stringify({ 608 | app_id: conf.APPID, 609 | url : imagePath, 610 | person_id : person_id, 611 | person_name: person_name, 612 | group_ids : group_ids, 613 | tag: persontag, 614 | }); 615 | } 616 | else 617 | { 618 | try { 619 | var data = fs.readFileSync(imagePath).toString('base64'); 620 | } catch (e) { 621 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); 622 | return; 623 | } 624 | 625 | 626 | if(data == null) { 627 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); 628 | return; 629 | }; 630 | 631 | request_body = JSON.stringify({ 632 | app_id: conf.APPID, 633 | image : data.toString('base64'), 634 | person_id : person_id, 635 | person_name: person_name, 636 | group_ids : group_ids, 637 | tag: persontag, 638 | }); 639 | } 640 | 641 | 642 | var buffer = new Buffer(request_body, "UTF-8"); 643 | 644 | var params = { 645 | hostname: conf.API_YOUTU_SERVER, 646 | path: '/youtu/api/newperson', 647 | method: 'POST', 648 | headers: { 649 | 'Authorization': sign, 650 | 'User-Agent' : conf.USER_AGENT(), 651 | 'Content-Length': buffer.length, 652 | 'Content-Type': 'text/json' 653 | } 654 | }; 655 | 656 | var request = null; 657 | if (conf.API_DOMAIN == 0) 658 | { 659 | request = getrequest(http, params, callback); 660 | } 661 | else { 662 | request = getrequest(https, params, callback); 663 | } 664 | 665 | request.on('error', function(e) { 666 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 667 | }); 668 | 669 | // send the request body 670 | request.end(request_body); 671 | } 672 | 673 | 674 | /** 675 | * @brief delperson 676 | * @param person_id 待删除的个体id 677 | * @param callback 回调函数, 参见Readme 文档 678 | */ 679 | exports.delperson= function(person_id, callback) { 680 | 681 | callback = callback || function(ret){console.log(ret)}; 682 | 683 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 684 | var sign = auth.appSign(expired); 685 | 686 | var request_body = JSON.stringify({ 687 | app_id: conf.APPID, 688 | person_id : person_id, 689 | }); 690 | 691 | var params = { 692 | hostname: conf.API_YOUTU_SERVER, 693 | port: conf.API_YOUTU_PORT, 694 | path: '/youtu/api/delperson', 695 | method: 'POST', 696 | headers: { 697 | 'Authorization': sign, 698 | 'User-Agent' : conf.USER_AGENT(), 699 | 'Content-Length': request_body.length, 700 | 'Content-Type': 'text/json' 701 | } 702 | }; 703 | 704 | var request = null; 705 | if (conf.API_DOMAIN == 0) 706 | { 707 | request = getrequest(http, params, callback); 708 | } 709 | else { 710 | request = getrequest(https, params, callback); 711 | } 712 | 713 | request.on('error', function(e) { 714 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 715 | }); 716 | 717 | // send the request body 718 | request.end(request_body); 719 | } 720 | 721 | 722 | 723 | /** 724 | * @brief addface 725 | * @param person_id 新增人脸的个体身份id 726 | * @param images 待增加的包含人脸的图片lu路径数组,可加入多张(包体大小<2m) 727 | * @param facetag 人脸备注信息,用户自解释字段 728 | * @param callback 回调函数, 参见Readme 文档 729 | */ 730 | exports.addface = function(person_id, images, facetag, callback) { 731 | 732 | callback = callback || function(ret){console.log(ret)}; 733 | 734 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 735 | var sign = auth.appSign(expired); 736 | 737 | var tag = images[0].substring(0,4); 738 | var request_body = ''; 739 | 740 | if (tag == 'http') 741 | { 742 | request_body = JSON.stringify({ 743 | app_id: conf.APPID, 744 | urls : images, 745 | person_id : person_id, 746 | tag : facetag 747 | }); 748 | } 749 | else 750 | { 751 | var image_bufs = new Array(); 752 | 753 | for( var idx in images) 754 | { 755 | try { 756 | var data =fs.readFileSync(images[idx]); 757 | } catch (e) { 758 | callback({'httpcode':0, 'code':-1, 'message':'file ' + images[idx] + ' not exists', 'data':{}}); 759 | return; 760 | } 761 | 762 | if(data == null) { 763 | callback({'httpcode':0, 'code':-1, 'message': images[idx] + ": read failed!", 'data':{}}); 764 | return; 765 | } 766 | 767 | image_bufs[idx] = data.toString('base64'); 768 | } 769 | 770 | request_body = JSON.stringify({ 771 | app_id: conf.APPID, 772 | images : image_bufs, 773 | person_id : person_id, 774 | tag : facetag 775 | }); 776 | } 777 | 778 | //console.log(request_body); 779 | var buffer = new Buffer(request_body, "UTF-8"); 780 | var params = { 781 | hostname: conf.API_YOUTU_SERVER, 782 | path: '/youtu/api/addface', 783 | method: 'POST', 784 | headers: { 785 | 'Authorization': sign, 786 | 'User-Agent' : conf.USER_AGENT(), 787 | 'Content-Length': buffer.length, 788 | 'Content-Type': 'text/json' 789 | } 790 | }; 791 | 792 | //console.log(params); 793 | var request = null; 794 | if (conf.API_DOMAIN == 0) 795 | { 796 | request = getrequest(http, params, callback); 797 | } 798 | else { 799 | request = getrequest(https, params, callback); 800 | } 801 | 802 | request.on('error', function(e) { 803 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 804 | }); 805 | 806 | // send the request body 807 | request.end(request_body); 808 | 809 | } 810 | 811 | 812 | /** 813 | * @brief delface 814 | * @param person_id 待删除人脸的个体身份id 815 | * @param face_ids 待删除的人脸id 数组 816 | * @param callback 回调函数, 参见Readme 文档 817 | */ 818 | exports.delface = function(person_id, face_ids, callback) { 819 | 820 | callback = callback || function(ret){console.log(ret)}; 821 | 822 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 823 | var sign = auth.appSign(expired); 824 | 825 | var request_body = JSON.stringify({ 826 | app_id: conf.APPID, 827 | face_ids: face_ids, 828 | person_id : person_id, 829 | }); 830 | 831 | var params = { 832 | hostname: conf.API_YOUTU_SERVER, 833 | path: '/youtu/api/delface', 834 | method: 'POST', 835 | headers: { 836 | 'Authorization': sign, 837 | 'User-Agent' : conf.USER_AGENT(), 838 | 'Content-Length': request_body.length, 839 | 'Content-Type': 'text/json' 840 | } 841 | }; 842 | 843 | var request = null; 844 | if (conf.API_DOMAIN == 0) 845 | { 846 | request = getrequest(http, params, callback); 847 | } 848 | else { 849 | request = getrequest(https, params, callback); 850 | } 851 | 852 | request.on('error', function(e) { 853 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 854 | }); 855 | 856 | // send the request body 857 | request.end(request_body); 858 | } 859 | 860 | 861 | /** 862 | * @brief setinfo 863 | * @param person_id 待设置的个体身份id 864 | * @param person_name 新设置的个体名字 865 | * @param tag 新设置的人脸备注信息 866 | * @param callback 回调函数, 参见Readme 文档 867 | */ 868 | exports.setinfo = function(person_name, person_id, tag, callback) { 869 | 870 | callback = callback || function(ret){console.log(ret)}; 871 | 872 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 873 | var sign = auth.appSign(expired); 874 | 875 | var request_body = JSON.stringify({ 876 | app_id: conf.APPID, 877 | person_name: person_name, 878 | person_id : person_id, 879 | tag: tag, 880 | }); 881 | 882 | var buffer = new Buffer(request_body, "UTF-8"); 883 | var params = { 884 | hostname: conf.API_YOUTU_SERVER, 885 | path: '/youtu/api/setinfo', 886 | method: 'POST', 887 | headers: { 888 | 'Authorization': sign, 889 | 'User-Agent' : conf.USER_AGENT(), 890 | 'Content-Length': buffer.length, 891 | 'Content-Type': 'text/json' 892 | } 893 | }; 894 | 895 | var request = null; 896 | if (conf.API_DOMAIN == 0) 897 | { 898 | request = getrequest(http, params, callback); 899 | } 900 | else { 901 | request = getrequest(https, params, callback); 902 | } 903 | 904 | request.on('error', function(e) { 905 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 906 | }); 907 | 908 | // send the request body 909 | request.end(request_body); 910 | } 911 | 912 | 913 | 914 | 915 | /** 916 | * @brief getinfo 917 | * @param person_id 待查询的个体身份id 918 | * @param callback 回调函数, 参见Readme 文档 919 | */ 920 | exports.getinfo = function(person_id, callback) { 921 | 922 | callback = callback || function(ret){console.log(ret)}; 923 | 924 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 925 | var sign = auth.appSign(expired); 926 | 927 | 928 | var request_body = JSON.stringify({ 929 | app_id: conf.APPID, 930 | person_id : person_id, 931 | }); 932 | 933 | var params = { 934 | hostname: conf.API_YOUTU_SERVER, 935 | path: '/youtu/api/getinfo', 936 | method: 'POST', 937 | headers: { 938 | 'Authorization': sign, 939 | 'User-Agent' : conf.USER_AGENT(), 940 | 'Content-Length': request_body.length, 941 | 'Content-Type': 'text/json' 942 | } 943 | }; 944 | 945 | var request = null; 946 | if (conf.API_DOMAIN == 0) 947 | { 948 | request = getrequest(http, params, callback); 949 | } 950 | else { 951 | request = getrequest(https, params, callback); 952 | } 953 | 954 | request.on('error', function(e) { 955 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 956 | }); 957 | 958 | // send the request body 959 | request.end(request_body); 960 | } 961 | 962 | 963 | /** 964 | * @brief getgroupids 965 | * @param callback 回调函数, 参见Readme 文档 966 | */ 967 | exports.getgroupids = function(callback) { 968 | 969 | callback = callback || function(ret){console.log(ret)}; 970 | 971 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 972 | var sign = auth.appSign(expired); 973 | 974 | var request_body = JSON.stringify({ 975 | app_id: conf.APPID, 976 | }); 977 | 978 | var params = { 979 | hostname: conf.API_YOUTU_SERVER, 980 | path: '/youtu/api/getgroupids', 981 | method: 'POST', 982 | headers: { 983 | 'Authorization': sign, 984 | 'User-Agent' : conf.USER_AGENT(), 985 | 'Content-Length': request_body.length, 986 | 'Content-Type': 'text/json' 987 | } 988 | }; 989 | 990 | 991 | var request = null; 992 | if (conf.API_DOMAIN == 0) 993 | { 994 | request = getrequest(http, params, callback); 995 | } 996 | else { 997 | request = getrequest(https, params, callback); 998 | } 999 | 1000 | request.on('error', function(e) { 1001 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 1002 | }); 1003 | 1004 | // send the request body 1005 | request.end(request_body); 1006 | } 1007 | 1008 | 1009 | /** 1010 | * @brief getpersonids 1011 | * @param group_id 待查询的组id 1012 | * @param callback 回调函数, 参见Readme 文档 1013 | */ 1014 | exports.getpersonids = function(group_id, callback) { 1015 | 1016 | callback = callback || function(ret){console.log(ret)}; 1017 | 1018 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 1019 | var sign = auth.appSign(expired); 1020 | 1021 | 1022 | var request_body = JSON.stringify({ 1023 | app_id: conf.APPID, 1024 | group_id:group_id 1025 | }); 1026 | 1027 | var params = { 1028 | hostname: conf.API_YOUTU_SERVER, 1029 | port: conf.API_YOUTU_PORT, 1030 | path: '/youtu/api/getpersonids', 1031 | method: 'POST', 1032 | headers: { 1033 | 'Authorization': sign, 1034 | 'User-Agent' : conf.USER_AGENT(), 1035 | 'Content-Length': request_body.length, 1036 | 'Content-Type': 'text/json' 1037 | } 1038 | }; 1039 | 1040 | var request = null; 1041 | if (conf.API_DOMAIN == 0) 1042 | { 1043 | request = getrequest(http, params, callback); 1044 | } 1045 | else { 1046 | request = getrequest(https, params, callback); 1047 | } 1048 | 1049 | request.on('error', function(e) { 1050 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 1051 | }); 1052 | 1053 | // send the request body 1054 | request.end(request_body); 1055 | } 1056 | 1057 | 1058 | 1059 | 1060 | /** 1061 | * @brief getfaceids 1062 | * @param person_id 待查询的个体id 1063 | * @param callback 回调函数, 参见Readme 文档 1064 | */ 1065 | exports.getfaceids = function(person_id, callback) { 1066 | 1067 | callback = callback || function(ret){console.log(ret)}; 1068 | 1069 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 1070 | var sign = auth.appSign(expired); 1071 | 1072 | 1073 | var request_body = JSON.stringify({ 1074 | app_id: conf.APPID, 1075 | person_id:person_id 1076 | }); 1077 | 1078 | var params = { 1079 | hostname: conf.API_YOUTU_SERVER, 1080 | path: '/youtu/api/getfaceids', 1081 | method: 'POST', 1082 | headers: { 1083 | 'Authorization': sign, 1084 | 'User-Agent' : conf.USER_AGENT(), 1085 | 'Content-Length': request_body.length, 1086 | 'Content-Type': 'text/json' 1087 | } 1088 | }; 1089 | 1090 | var request = null; 1091 | if (conf.API_DOMAIN == 0) 1092 | { 1093 | request = getrequest(http, params, callback); 1094 | } 1095 | else { 1096 | request = getrequest(https, params, callback); 1097 | } 1098 | 1099 | request.on('error', function(e) { 1100 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 1101 | }); 1102 | 1103 | // send the request body 1104 | request.end(request_body); 1105 | } 1106 | 1107 | 1108 | 1109 | /** 1110 | * @brief getfaceinfo 1111 | * @param face_id 待查询的人脸id 1112 | * @param callback 回调函数, 参见Readme 文档 1113 | */ 1114 | exports.getfaceinfo = function(face_id, callback) { 1115 | 1116 | callback = callback || function(ret){console.log(ret)}; 1117 | 1118 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 1119 | var sign = auth.appSign(expired); 1120 | 1121 | var request_body = JSON.stringify({ 1122 | app_id: conf.APPID, 1123 | face_id:face_id 1124 | }); 1125 | 1126 | var params = { 1127 | hostname: conf.API_YOUTU_SERVER, 1128 | path: '/youtu/api/getfaceinfo', 1129 | method: 'POST', 1130 | headers: { 1131 | 'Authorization': sign, 1132 | 'User-Agent' : conf.USER_AGENT(), 1133 | 'Content-Length': request_body.length, 1134 | 'Content-Type': 'text/json' 1135 | } 1136 | }; 1137 | 1138 | var request = null; 1139 | if (conf.API_DOMAIN == 0) 1140 | { 1141 | request = getrequest(http, params, callback); 1142 | } 1143 | else { 1144 | request = getrequest(https, params, callback); 1145 | } 1146 | 1147 | request.on('error', function(e) { 1148 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 1149 | }); 1150 | 1151 | // send the request body 1152 | request.end(request_body); 1153 | } 1154 | 1155 | /** 1156 | * @brief fuzzydetect 1157 | * @param imagePath 待检测的路径 1158 | * @param callback 回调函数, 参见Readme 文档 1159 | */ 1160 | exports.fuzzydetect = function(imagePath, callback) { 1161 | 1162 | callback = callback || function(ret){console.log(ret)}; 1163 | 1164 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 1165 | var sign = auth.appSign(expired); 1166 | var tag = imagePath.substring(0,4); 1167 | var request_body = ''; 1168 | if (tag == 'http') 1169 | { 1170 | request_body = JSON.stringify({ 1171 | app_id: conf.APPID, 1172 | url : imagePath, 1173 | }); 1174 | } 1175 | else 1176 | { 1177 | try { 1178 | var data = fs.readFileSync(imagePath).toString('base64'); 1179 | } catch (e) { 1180 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); 1181 | return; 1182 | } 1183 | 1184 | 1185 | if(data == null) { 1186 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); 1187 | return; 1188 | }; 1189 | 1190 | request_body = JSON.stringify({ 1191 | app_id: conf.APPID, 1192 | image : data.toString('base64'), 1193 | }); 1194 | } 1195 | var params = { 1196 | hostname: conf.API_YOUTU_SERVER, 1197 | path: '/youtu/imageapi/fuzzydetect', 1198 | method: 'POST', 1199 | headers: { 1200 | 'Authorization': sign, 1201 | 'User-Agent' : conf.USER_AGENT(), 1202 | 'Content-Length': request_body.length, 1203 | 'Content-Type': 'text/json' 1204 | } 1205 | }; 1206 | //console.log(request_body); 1207 | //console.log(params); 1208 | var request = null; 1209 | if (conf.API_DOMAIN == 0) 1210 | { 1211 | request = getrequest(http, params, callback); 1212 | } 1213 | else { 1214 | request = getrequest(https, params, callback); 1215 | } 1216 | 1217 | request.on('error', function(e) { 1218 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 1219 | }); 1220 | 1221 | // send the request body 1222 | request.end(request_body); 1223 | } 1224 | 1225 | /** 1226 | * @brief fooddetect 1227 | * @param imagePath 待检测的路径 1228 | * @param callback 回调函数, 参见Readme 文档 1229 | */ 1230 | exports.fooddetect = function(imagePath, callback) { 1231 | 1232 | callback = callback || function(ret){console.log(ret)}; 1233 | 1234 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 1235 | var sign = auth.appSign(expired); 1236 | var tag = imagePath.substring(0,4); 1237 | var request_body = ''; 1238 | if (tag == 'http') 1239 | { 1240 | request_body = JSON.stringify({ 1241 | app_id: conf.APPID, 1242 | url : imagePath, 1243 | }); 1244 | } 1245 | else 1246 | { 1247 | 1248 | try { 1249 | var data = fs.readFileSync(imagePath).toString('base64'); 1250 | } catch (e) { 1251 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); 1252 | return; 1253 | } 1254 | if(data == null) { 1255 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); 1256 | return; 1257 | }; 1258 | 1259 | request_body = JSON.stringify({ 1260 | app_id: conf.APPID, 1261 | image : data.toString('base64'), 1262 | }); 1263 | } 1264 | var params = { 1265 | hostname: conf.API_YOUTU_SERVER, 1266 | path: '/youtu/imageapi/fooddetect', 1267 | method: 'POST', 1268 | headers: { 1269 | 'Authorization': sign, 1270 | 'User-Agent' : conf.USER_AGENT(), 1271 | 'Content-Length': request_body.length, 1272 | 'Content-Type': 'text/json' 1273 | } 1274 | }; 1275 | //console.log(request_body); 1276 | //console.log(params); 1277 | var request = null; 1278 | if (conf.API_DOMAIN == 0) 1279 | { 1280 | request = getrequest(http, params, callback); 1281 | } 1282 | else { 1283 | request = getrequest(https, params, callback); 1284 | } 1285 | 1286 | request.on('error', function(e) { 1287 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 1288 | }); 1289 | 1290 | // send the request body 1291 | request.end(request_body); 1292 | } 1293 | 1294 | /** 1295 | * @brief imagetag 1296 | * @param imagePath 待检测的路径 1297 | * @param callback 回调函数, 参见Readme 文档 1298 | */ 1299 | exports.imagetag = function(imagePath, callback) { 1300 | 1301 | callback = callback || function(ret){console.log(ret)}; 1302 | 1303 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 1304 | var sign = auth.appSign(expired); 1305 | var tag = imagePath.substring(0,4); 1306 | var request_body = ''; 1307 | if (tag == 'http') 1308 | { 1309 | request_body = JSON.stringify({ 1310 | app_id: conf.APPID, 1311 | url : imagePath, 1312 | }); 1313 | } 1314 | else 1315 | { 1316 | try { 1317 | var data = fs.readFileSync(imagePath).toString('base64'); 1318 | } catch (e) { 1319 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); 1320 | return; 1321 | } 1322 | 1323 | if(data == null) { 1324 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); 1325 | return; 1326 | }; 1327 | 1328 | request_body = JSON.stringify({ 1329 | app_id: conf.APPID, 1330 | image : data.toString('base64'), 1331 | }); 1332 | } 1333 | var params = { 1334 | hostname: conf.API_YOUTU_SERVER, 1335 | path: '/youtu/imageapi/imagetag', 1336 | method: 'POST', 1337 | headers: { 1338 | 'Authorization': sign, 1339 | 'User-Agent' : conf.USER_AGENT(), 1340 | 'Content-Length': request_body.length, 1341 | 'Content-Type': 'text/json' 1342 | } 1343 | }; 1344 | //console.log(request_body); 1345 | //console.log(params); 1346 | 1347 | var request = null; 1348 | if (conf.API_DOMAIN == 0) 1349 | { 1350 | request = getrequest(http, params, callback); 1351 | } 1352 | else { 1353 | request = getrequest(https, params, callback); 1354 | } 1355 | 1356 | request.on('error', function(e) { 1357 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 1358 | }); 1359 | 1360 | // send the request body 1361 | request.end(request_body); 1362 | } 1363 | 1364 | /** 1365 | * @brief imageporn 1366 | * @param imagePath 待检测的路径 1367 | * @param callback 回调函数, 参见Readme 文档 1368 | */ 1369 | exports.imageporn = function(imagePath, callback) { 1370 | 1371 | callback = callback || function(ret){console.log(ret)}; 1372 | 1373 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 1374 | var sign = auth.appSign(expired); 1375 | var tag = imagePath.substring(0,4); 1376 | var request_body = ''; 1377 | if (tag == 'http') 1378 | { 1379 | request_body = JSON.stringify({ 1380 | app_id: conf.APPID, 1381 | url : imagePath, 1382 | }); 1383 | } 1384 | else 1385 | { 1386 | try { 1387 | var data = fs.readFileSync(imagePath).toString('base64'); 1388 | } catch (e) { 1389 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); 1390 | return; 1391 | } 1392 | if(data == null) { 1393 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); 1394 | return; 1395 | }; 1396 | 1397 | request_body = JSON.stringify({ 1398 | app_id: conf.APPID, 1399 | image : data.toString('base64'), 1400 | }); 1401 | } 1402 | var params = { 1403 | hostname: conf.API_YOUTU_SERVER, 1404 | path: '/youtu/imageapi/imageporn', 1405 | method: 'POST', 1406 | headers: { 1407 | 'Authorization': sign, 1408 | 'User-Agent' : conf.USER_AGENT(), 1409 | 'Content-Length': request_body.length, 1410 | 'Content-Type': 'text/json' 1411 | } 1412 | }; 1413 | //console.log(request_body); 1414 | //console.log(params); 1415 | 1416 | var request = null; 1417 | if (conf.API_DOMAIN == 0) 1418 | { 1419 | request = getrequest(http, params, callback); 1420 | } 1421 | else { 1422 | request = getrequest(https, params, callback); 1423 | } 1424 | 1425 | request.on('error', function(e) { 1426 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 1427 | }); 1428 | 1429 | // send the request body 1430 | request.end(request_body); 1431 | } 1432 | 1433 | /** 1434 | * @brief imageterrorism 1435 | * @param imagePath 待检测的路径 1436 | * @param callback 回调函数, 参见Readme 文档 1437 | */ 1438 | exports.imageterrorism = function(imagePath, callback) { 1439 | 1440 | callback = callback || function(ret){console.log(ret)}; 1441 | 1442 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 1443 | var sign = auth.appSign(expired); 1444 | var tag = imagePath.substring(0,4); 1445 | var request_body = ''; 1446 | if (tag == 'http') 1447 | { 1448 | request_body = JSON.stringify({ 1449 | app_id: conf.APPID, 1450 | url : imagePath, 1451 | }); 1452 | } 1453 | else 1454 | { 1455 | try { 1456 | var data = fs.readFileSync(imagePath).toString('base64'); 1457 | } catch (e) { 1458 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); 1459 | return; 1460 | } 1461 | if(data == null) { 1462 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); 1463 | return; 1464 | }; 1465 | 1466 | request_body = JSON.stringify({ 1467 | app_id: conf.APPID, 1468 | image : data.toString('base64'), 1469 | }); 1470 | } 1471 | var params = { 1472 | hostname: conf.API_YOUTU_SERVER, 1473 | path: '/youtu/imageapi/imageterrorism', 1474 | method: 'POST', 1475 | headers: { 1476 | 'Authorization': sign, 1477 | 'User-Agent' : conf.USER_AGENT(), 1478 | 'Content-Length': request_body.length, 1479 | 'Content-Type': 'text/json' 1480 | } 1481 | }; 1482 | //console.log(request_body); 1483 | //console.log(params); 1484 | 1485 | var request = null; 1486 | if (conf.API_DOMAIN == 0) 1487 | { 1488 | request = getrequest(http, params, callback); 1489 | } 1490 | else { 1491 | request = getrequest(https, params, callback); 1492 | } 1493 | 1494 | request.on('error', function(e) { 1495 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 1496 | }); 1497 | 1498 | // send the request body 1499 | request.end(request_body); 1500 | } 1501 | 1502 | /** 1503 | * @brief carclassify 1504 | * @param imagePath 待检测的路径 1505 | * @param callback 回调函数, 参见Readme 文档 1506 | */ 1507 | exports.carclassify = function(imagePath, callback) { 1508 | 1509 | callback = callback || function(ret){console.log(ret)}; 1510 | 1511 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 1512 | var sign = auth.appSign(expired); 1513 | var tag = imagePath.substring(0,4); 1514 | var request_body = ''; 1515 | if (tag == 'http') 1516 | { 1517 | request_body = JSON.stringify({ 1518 | app_id: conf.APPID, 1519 | url : imagePath, 1520 | }); 1521 | } 1522 | else 1523 | { 1524 | try { 1525 | var data = fs.readFileSync(imagePath).toString('base64'); 1526 | } catch (e) { 1527 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); 1528 | return; 1529 | } 1530 | if(data == null) { 1531 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); 1532 | return; 1533 | }; 1534 | 1535 | request_body = JSON.stringify({ 1536 | app_id: conf.APPID, 1537 | image : data.toString('base64'), 1538 | }); 1539 | } 1540 | var params = { 1541 | hostname: conf.API_YOUTU_SERVER, 1542 | path: '/youtu/carapi/carclassify', 1543 | method: 'POST', 1544 | headers: { 1545 | 'Authorization': sign, 1546 | 'User-Agent' : conf.USER_AGENT(), 1547 | 'Content-Length': request_body.length, 1548 | 'Content-Type': 'text/json' 1549 | } 1550 | }; 1551 | //console.log(request_body); 1552 | //console.log(params); 1553 | 1554 | var request = null; 1555 | if (conf.API_DOMAIN == 0) 1556 | { 1557 | request = getrequest(http, params, callback); 1558 | } 1559 | else { 1560 | request = getrequest(https, params, callback); 1561 | } 1562 | 1563 | request.on('error', function(e) { 1564 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 1565 | }); 1566 | 1567 | // send the request body 1568 | request.end(request_body); 1569 | } 1570 | 1571 | 1572 | /** 1573 | * @brief idcardocr 1574 | * @param imagePath 待检测的路径 1575 | * @param cardType 0 代表输入图像是身份证正面, 1代表输入是身份证反面 1576 | * @param callback 回调函数, 参见Readme 文档 1577 | */ 1578 | exports.idcardocr = function(imagePath, cardType, callback) { 1579 | 1580 | callback = callback || function(ret){console.log(ret)}; 1581 | 1582 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 1583 | var sign = auth.appSign(expired); 1584 | var tag = imagePath.substring(0,4); 1585 | var request_body = ''; 1586 | if (tag == 'http') 1587 | { 1588 | request_body = JSON.stringify({ 1589 | app_id: conf.APPID, 1590 | card_type: cardType, 1591 | url : imagePath, 1592 | }); 1593 | } 1594 | else 1595 | { 1596 | try { 1597 | var data = fs.readFileSync(imagePath).toString('base64'); 1598 | } catch (e) { 1599 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); 1600 | return; 1601 | } 1602 | if(data == null) { 1603 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); 1604 | return; 1605 | }; 1606 | 1607 | request_body = JSON.stringify({ 1608 | app_id: conf.APPID, 1609 | card_type: cardType, 1610 | image : data.toString('base64'), 1611 | }); 1612 | } 1613 | var params = { 1614 | hostname: conf.API_YOUTU_SERVER, 1615 | path: '/youtu/ocrapi/idcardocr', 1616 | method: 'POST', 1617 | headers: { 1618 | 'Authorization': sign, 1619 | 'User-Agent' : conf.USER_AGENT(), 1620 | 'Content-Length': request_body.length, 1621 | 'Content-Type': 'text/json' 1622 | } 1623 | }; 1624 | //console.log(request_body); 1625 | //console.log(params); 1626 | 1627 | var request = null; 1628 | if (conf.API_DOMAIN == 0) 1629 | { 1630 | request = getrequest(http, params, callback); 1631 | } 1632 | else { 1633 | request = getrequest(https, params, callback); 1634 | } 1635 | 1636 | request.on('error', function(e) { 1637 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 1638 | }); 1639 | 1640 | // send the request body 1641 | request.end(request_body); 1642 | } 1643 | 1644 | 1645 | /** 1646 | * @brief generalocr 1647 | * @param imagePath 待处理的图片路径(本地路径或url) 1648 | * @param callback 回调函数, 参见Readme 文档 1649 | */ 1650 | exports.generalocr = function(imagePath, callback) { 1651 | 1652 | callback = callback || function(ret){console.log(ret)}; 1653 | 1654 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 1655 | var sign = auth.appSign(expired); 1656 | var tag = imagePath.substring(0,4); 1657 | var request_body = ''; 1658 | if (tag == 'http') 1659 | { 1660 | var request_body = JSON.stringify({ 1661 | app_id: conf.APPID, 1662 | url : imagePath 1663 | }); 1664 | } 1665 | else 1666 | { 1667 | try { 1668 | var image_data = fs.readFileSync(imagePath).toString('base64'); 1669 | } catch (e) { 1670 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); 1671 | return; 1672 | } 1673 | 1674 | if(image_data == null) { 1675 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); 1676 | return; 1677 | }; 1678 | 1679 | var request_body = JSON.stringify({ 1680 | app_id: conf.APPID, 1681 | image : image_data.toString('base64') 1682 | }); 1683 | } 1684 | 1685 | var params = { 1686 | hostname: conf.API_YOUTU_SERVER, 1687 | path: '/youtu/ocrapi/generalocr', 1688 | method: 'POST', 1689 | headers: { 1690 | 'Authorization': sign, 1691 | 'User-Agent' : conf.USER_AGENT(), 1692 | 'Content-Length': request_body.length, 1693 | 'Content-Type': 'text/json' 1694 | } 1695 | }; 1696 | 1697 | //console.log(request_body); 1698 | var request = null; 1699 | if (conf.API_DOMAIN == 0) 1700 | { 1701 | request = getrequest(http, params, callback); 1702 | } 1703 | else { 1704 | request = getrequest(https, params, callback); 1705 | } 1706 | 1707 | request.on('error', function(e) { 1708 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 1709 | }); 1710 | 1711 | // send the request body 1712 | request.end(request_body); 1713 | } 1714 | 1715 | /** 1716 | * @brief driverlicenseocr 1717 | * @param imagePath 待处理的图片路径(本地路径或url) 1718 | * @param cardType 图片类型:行驶证/驾驶证 0/1 1719 | * @param callback 回调函数, 参见Readme 文档 1720 | */ 1721 | exports.driverlicenseocr = function(imagePath, cardType, callback) { 1722 | 1723 | callback = callback || function(ret){console.log(ret)}; 1724 | 1725 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 1726 | var sign = auth.appSign(expired); 1727 | var tag = imagePath.substring(0,4); 1728 | var request_body = ''; 1729 | if (tag == 'http') 1730 | { 1731 | var request_body = JSON.stringify({ 1732 | app_id: conf.APPID, 1733 | type: cardType, 1734 | url : imagePath 1735 | }); 1736 | } 1737 | else 1738 | { 1739 | try { 1740 | var image_data = fs.readFileSync(imagePath).toString('base64'); 1741 | } catch (e) { 1742 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); 1743 | return; 1744 | } 1745 | 1746 | if(image_data == null) { 1747 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); 1748 | return; 1749 | }; 1750 | 1751 | var request_body = JSON.stringify({ 1752 | app_id: conf.APPID, 1753 | type: cardType, 1754 | image : image_data.toString('base64') 1755 | }); 1756 | } 1757 | 1758 | var params = { 1759 | hostname: conf.API_YOUTU_SERVER, 1760 | path: '/youtu/ocrapi/driverlicenseocr', 1761 | method: 'POST', 1762 | headers: { 1763 | 'Authorization': sign, 1764 | 'User-Agent' : conf.USER_AGENT(), 1765 | 'Content-Length': request_body.length, 1766 | 'Content-Type': 'text/json' 1767 | } 1768 | }; 1769 | 1770 | //console.log(request_body); 1771 | var request = null; 1772 | if (conf.API_DOMAIN == 0) 1773 | { 1774 | request = getrequest(http, params, callback); 1775 | } 1776 | else { 1777 | request = getrequest(https, params, callback); 1778 | } 1779 | 1780 | request.on('error', function(e) { 1781 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 1782 | }); 1783 | 1784 | // send the request body 1785 | request.end(request_body); 1786 | } 1787 | 1788 | /** 1789 | * @brief bcocr 1790 | * @param imagePath 待处理的图片路径(本地路径或url) 1791 | * @param callback 回调函数, 参见Readme 文档 1792 | */ 1793 | exports.bcocr = function(imagePath, callback) { 1794 | 1795 | callback = callback || function(ret){console.log(ret)}; 1796 | 1797 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 1798 | var sign = auth.appSign(expired); 1799 | var tag = imagePath.substring(0,4); 1800 | var request_body = ''; 1801 | if (tag == 'http') 1802 | { 1803 | var request_body = JSON.stringify({ 1804 | app_id: conf.APPID, 1805 | url : imagePath 1806 | }); 1807 | } 1808 | else 1809 | { 1810 | try { 1811 | var image_data = fs.readFileSync(imagePath).toString('base64'); 1812 | } catch (e) { 1813 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); 1814 | return; 1815 | } 1816 | 1817 | if(image_data == null) { 1818 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); 1819 | return; 1820 | }; 1821 | 1822 | var request_body = JSON.stringify({ 1823 | app_id: conf.APPID, 1824 | image : image_data.toString('base64') 1825 | }); 1826 | } 1827 | 1828 | var params = { 1829 | hostname: conf.API_YOUTU_SERVER, 1830 | path: '/youtu/ocrapi/bcocr', 1831 | method: 'POST', 1832 | headers: { 1833 | 'Authorization': sign, 1834 | 'User-Agent' : conf.USER_AGENT(), 1835 | 'Content-Length': request_body.length, 1836 | 'Content-Type': 'text/json' 1837 | } 1838 | }; 1839 | 1840 | //console.log(request_body); 1841 | var request = null; 1842 | if (conf.API_DOMAIN == 0) 1843 | { 1844 | request = getrequest(http, params, callback); 1845 | } 1846 | else { 1847 | request = getrequest(https, params, callback); 1848 | } 1849 | 1850 | request.on('error', function(e) { 1851 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 1852 | }); 1853 | 1854 | // send the request body 1855 | request.end(request_body); 1856 | } 1857 | 1858 | /** 1859 | * @brief creditcardocr 1860 | * @param imagePath 待处理的图片路径(本地路径或url) 1861 | * @param callback 回调函数, 参见Readme 文档 1862 | */ 1863 | exports.creditcardocr = function(imagePath, callback) { 1864 | 1865 | callback = callback || function(ret){console.log(ret)}; 1866 | 1867 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 1868 | var sign = auth.appSign(expired); 1869 | var tag = imagePath.substring(0,4); 1870 | var request_body = ''; 1871 | if (tag == 'http') 1872 | { 1873 | var request_body = JSON.stringify({ 1874 | app_id: conf.APPID, 1875 | url : imagePath 1876 | }); 1877 | } 1878 | else 1879 | { 1880 | try { 1881 | var image_data = fs.readFileSync(imagePath).toString('base64'); 1882 | } catch (e) { 1883 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); 1884 | return; 1885 | } 1886 | 1887 | if(image_data == null) { 1888 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); 1889 | return; 1890 | }; 1891 | 1892 | var request_body = JSON.stringify({ 1893 | app_id: conf.APPID, 1894 | image : image_data.toString('base64') 1895 | }); 1896 | } 1897 | 1898 | var params = { 1899 | hostname: conf.API_YOUTU_SERVER, 1900 | path: '/youtu/ocrapi/creditcardocr', 1901 | method: 'POST', 1902 | headers: { 1903 | 'Authorization': sign, 1904 | 'User-Agent' : conf.USER_AGENT(), 1905 | 'Content-Length': request_body.length, 1906 | 'Content-Type': 'text/json' 1907 | } 1908 | }; 1909 | 1910 | //console.log(request_body); 1911 | var request = null; 1912 | if (conf.API_DOMAIN == 0) 1913 | { 1914 | request = getrequest(http, params, callback); 1915 | } 1916 | else { 1917 | request = getrequest(https, params, callback); 1918 | } 1919 | 1920 | request.on('error', function(e) { 1921 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 1922 | }); 1923 | 1924 | // send the request body 1925 | request.end(request_body); 1926 | } 1927 | 1928 | /** 1929 | * @brief bizlicenseocr 1930 | * @param imagePath 待处理的图片路径(本地路径或url) 1931 | * @param callback 回调函数, 参见Readme 文档 1932 | */ 1933 | exports.bizlicenseocr = function(imagePath, callback) { 1934 | 1935 | callback = callback || function(ret){console.log(ret)}; 1936 | 1937 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 1938 | var sign = auth.appSign(expired); 1939 | var tag = imagePath.substring(0,4); 1940 | var request_body = ''; 1941 | if (tag == 'http') 1942 | { 1943 | var request_body = JSON.stringify({ 1944 | app_id: conf.APPID, 1945 | url : imagePath 1946 | }); 1947 | } 1948 | else 1949 | { 1950 | try { 1951 | var image_data = fs.readFileSync(imagePath).toString('base64'); 1952 | } catch (e) { 1953 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); 1954 | return; 1955 | } 1956 | 1957 | if(image_data == null) { 1958 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); 1959 | return; 1960 | }; 1961 | 1962 | var request_body = JSON.stringify({ 1963 | app_id: conf.APPID, 1964 | image : image_data.toString('base64') 1965 | }); 1966 | } 1967 | 1968 | var params = { 1969 | hostname: conf.API_YOUTU_SERVER, 1970 | path: '/youtu/ocrapi/bizlicenseocr', 1971 | method: 'POST', 1972 | headers: { 1973 | 'Authorization': sign, 1974 | 'User-Agent' : conf.USER_AGENT(), 1975 | 'Content-Length': request_body.length, 1976 | 'Content-Type': 'text/json' 1977 | } 1978 | }; 1979 | 1980 | //console.log(request_body); 1981 | var request = null; 1982 | if (conf.API_DOMAIN == 0) 1983 | { 1984 | request = getrequest(http, params, callback); 1985 | } 1986 | else { 1987 | request = getrequest(https, params, callback); 1988 | } 1989 | 1990 | request.on('error', function(e) { 1991 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 1992 | }); 1993 | 1994 | // send the request body 1995 | request.end(request_body); 1996 | } 1997 | 1998 | /** 1999 | * @brief plateocr 2000 | * @param imagePath 待处理的图片路径(本地路径或url) 2001 | * @param callback 回调函数, 参见Readme 文档 2002 | */ 2003 | exports.plateocr = function(imagePath, callback) { 2004 | 2005 | callback = callback || function(ret){console.log(ret)}; 2006 | 2007 | var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; 2008 | var sign = auth.appSign(expired); 2009 | var tag = imagePath.substring(0,4); 2010 | var request_body = ''; 2011 | if (tag == 'http') 2012 | { 2013 | var request_body = JSON.stringify({ 2014 | app_id: conf.APPID, 2015 | url : imagePath 2016 | }); 2017 | } 2018 | else 2019 | { 2020 | try { 2021 | var image_data = fs.readFileSync(imagePath).toString('base64'); 2022 | } catch (e) { 2023 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); 2024 | return; 2025 | } 2026 | 2027 | if(image_data == null) { 2028 | callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); 2029 | return; 2030 | }; 2031 | 2032 | var request_body = JSON.stringify({ 2033 | app_id: conf.APPID, 2034 | image : image_data.toString('base64') 2035 | }); 2036 | } 2037 | 2038 | var params = { 2039 | hostname: conf.API_YOUTU_SERVER, 2040 | path: '/youtu/ocrapi/plateocr', 2041 | method: 'POST', 2042 | headers: { 2043 | 'Authorization': sign, 2044 | 'User-Agent' : conf.USER_AGENT(), 2045 | 'Content-Length': request_body.length, 2046 | 'Content-Type': 'text/json' 2047 | } 2048 | }; 2049 | 2050 | //console.log(request_body); 2051 | var request = null; 2052 | if (conf.API_DOMAIN == 0) 2053 | { 2054 | request = getrequest(http, params, callback); 2055 | } 2056 | else { 2057 | request = getrequest(https, params, callback); 2058 | } 2059 | 2060 | request.on('error', function(e) { 2061 | callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); 2062 | }); 2063 | 2064 | // send the request body 2065 | request.end(request_body); 2066 | } 2067 | 2068 | --------------------------------------------------------------------------------