├── LICENSE ├── README.md ├── TencentYoutuyun ├── __init__.py ├── auth.py ├── conf.py └── youtu.py ├── sample.py └── setup.py /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 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-python 2 | 3 | python sdk for [腾讯优图开放平台](https://open.youtu.qq.com) 4 | 5 | ## 安装 6 | 7 | #### 依赖项 8 | ``` 9 | - Requests,获取更新版本 10 | http://docs.python-requests.org/en/latest/ 11 | ``` 12 | #### 构建工程 13 | ``` 14 | 1. 下载sdk到您的目录${python_sdk} 15 | git clone https://github.com/Tencent-YouTu/Python_sdk.git 16 | 2. 在您需要使用sdk的文件中import TencentYoutuyun包 17 | ``` 18 | 19 | ## 名词 20 | 21 | - `AppId` 平台添加应用后分配的AppId 22 | - `SecretId` 平台添加应用后分配的SecretId 23 | - `SecretKey` 平台添加应用后分配的SecretKey 24 | - `签名` 接口鉴权凭证,由AppId、SecretId、SecretKey等生成,详见 25 | 26 | 27 | ## 使用示例 28 | ``` 29 | # -*- coding: utf-8 -*- 30 | 31 | import time 32 | import TencentYoutuyun 33 | 34 | appid = 'xxx' 35 | secret_id = 'xxxxxxx' 36 | secret_key = 'xxxxxxxx' 37 | userid= 'xxx' 38 | 39 | #end_point = TencentYoutuyun.conf.API_TENCENTYUN_END_POINT // 腾讯云 40 | #end_point = TencentYoutuyun.conf.API_YOUTU_VIP_END_POINT // 人脸核身服务(需联系腾讯优图商务开通权限,否则无法使用) 41 | end_point = TencentYoutuyun.conf.API_YOUTU_END_POINT // 优图开放平台 42 | 43 | 44 | youtu = TencentYoutuyun.YouTu(appid, secret_id, secret_key, userid, end_point) 45 | 46 | ret = youtu.FaceCompare('you_path_one.jpg','you_path_two.jpg') 47 | print ret 48 | ``` 49 | 50 | ### SDK内部错误码说明 51 | ``` 52 | IMAGE_FILE_NOT_EXISTS -1 //文件不存在 53 | IMAGE_NETWORK_ERROR -2 //网络错误 54 | IMAGE_PARAMS_ERROR -3 //图片参数错误 55 | PERSON_ID_EMPTY -4 //参数person_id 为空 56 | GROUP_ID_EMPTY -5 //参数group_id 为空 57 | GROUP_IDS_EMPTY -6 //参数group_ids 为空 58 | IMAGES_EMPTY -7 //参数images 集合为空 59 | FACE_IDS_EMPTY -8 //参数face_ids 集合为空 60 | FACE_ID_EMPTY -9 //参数face_id为空 61 | LIST_TYPE_INVALID -10 //不是list类型 62 | IMAGE_PATH_EMPTY -11 //传入的image_path为空 63 | 64 | OCR_NOT_ENOUGH_TEXTLINES -5201 //名片无足够的文本 65 | OCR_TEXTLINES_SKEWED -5202 //名片文本行倾斜角度太大 66 | OCR_TEXTLINES_FUZZY -5203 //名片模糊 67 | OCR_UNRECOG_NAME -5204 //名片姓名识别失败 68 | OCR_UNRECOG_TEL -5205 //名片电话识别失败 69 | ``` 70 | 71 | ## 初始化 72 | ``` 73 | - 示例 74 | - `youtu = TencentYoutuyun.YouTu(appid, secret_id, secret_key, userid, end_point)` 75 | 76 | - 参数`ytopen_sdk::AppSign` 77 | - `appid` 业务中的应用标识AppId 78 | - `secret_id` 秘钥SecretId 79 | - `secret_key` 秘钥SecretKey 80 | - `userid` 用户id 81 | - `end_point` 服务后台路径,默认是优图开放平台,支持腾讯云,人脸核身(核身服务需联系腾讯优图商务开通权限) 82 | ``` 83 | 84 | ## 注意事项 85 | ``` 86 | - API分为开放平台API和人脸核身API,人脸核身API访问权限需要联系商务开通; 87 | - 开放平台API访问end_point为TencentYoutuyun.conf.API_YOUTU_END_POINT 88 | - 人脸核身API访问end_point为TencentYoutuyun.conf.API_YOUTU_VIP_END_POINT 89 | - 所有接口,对于编码问题,涉及到中文的请使用encode('iso8859-1').decode('utf-8')进行转码 90 | ``` 91 | 92 | ## 接口说明 93 | ``` 94 | 接口调用统一返回值说明 95 | - 返回值 96 | `Json`格式的返回结果,具体字段参考API文档 97 | 98 | - 人脸核身服务接口列表(需联系腾讯优图商务开通权限,否则无法使用) 99 | - 接口end_point选择: TencentYoutuyun.conf.API_YOUTU_VIP_END_POINT 100 | - livegetfour(self, seq = '') 101 | - livedetectfour(self, validate_data, video_path, seq = '', card_path = '', compare_flag = False) 102 | - idcardlivedetectfour(self, idcard_number, idcard_name, validate_data, video_path, seq = '') 103 | - idcardfacecompare(self, idcard_number, idcard_name, image_path, data_type = 0 , session_id = '') 104 | - FaceCompare(self, image_pathA, image_pathB, data_type = 0) 105 | - idcardocr(self, image_path, data_type = 0, card_type = 1 ,seq = '') 106 | ValidateIdcard(self, idcard_number, idcard_name, seq = '') 107 | 108 | - 腾讯优图开放平台接口列表 109 | - 接口end_point选择: TencentYoutuyun.conf.API_YOUTU_END_POINT 110 | - DetectFace(self, image_path, mode = 0, data_type = 0) 111 | - FaceShape(self, image_path, mode = 0, data_type = 0) 112 | - FaceCompare(self, image_pathA, image_pathB, data_type = 0) 113 | - FaceVerify(self, person_id, image_path, data_type = 0) 114 | - FaceIdentify(self, group_id, image_path, data_type = 0) 115 | - MultiFaceIdentify(self, group_id, group_ids, image_path, data_type = 0, topn = 5, min_size = 40) 116 | - NewPerson(self, person_id, image_path, group_ids, person_name= '', tag='', data_type = 0) 117 | - DelPerson(self, person_id) 118 | - AddFace(self, person_id, images, tag='', data_type = 0) 119 | - DelFace(self, person_id, face_ids) 120 | - GetInfo(self, person_id) 121 | - SetInfo(self, person_id, person_name='', tag='') 122 | - GetGroupIds(self) 123 | - GetPersonIds(self, group_id) 124 | - GetFaceIds(self, person_id) 125 | - GetFaceInfo(self, face_id) 126 | - fuzzydetect(self, image_path, data_type = 0, seq = '') 127 | - fooddetect(self, image_path, data_type = 0, seq = '') 128 | - imagetag(self, image_path, data_type = 0, seq = '') 129 | - imageporn(self, image_path, data_type = 0, seq = '') 130 | - imageterrorism(self, image_path, data_type = 0, seq = '') 131 | - carclassify(self, image_path, data_type = 0, seq = '') 132 | - idcardocr(self, image_path, data_type = 0, card_type = 1 ,seq = '') 133 | - driverlicenseocr(self, image_path, data_type = 0, proc_type = 0, seq = '') 134 | - bcocr(self, image_path, data_type = 0, seq = '') 135 | - generalocr(self, image_path, data_type = 0, seq = '') 136 | - creditcardocr(self, image_path, data_type = 0, seq = '') 137 | - bizlicenseocr(self, image_path, data_type = 0, seq = '') 138 | - plateocr(self, image_path, data_type = 0, seq = '') 139 | ``` 140 | 141 | ## 腾讯优图人脸核身接口(需联系腾讯优图商务开通权限,否则无法使用) 142 | ``` 143 | - 接口end_point选择: TencentYoutuyun.conf.API_YOUTU_VIP_END_POINT 144 | ``` 145 | 146 | ### 四字唇语获取 147 | ``` 148 | `livegetfour(self, seq = '')` 149 | -参数 150 | - `seq` 请求唯一标识 151 | ``` 152 | 153 | ### 四字活体检测 154 | ``` 155 | `livedetectfour(self, validate_data, video_path, seq = '', card_path = '', compare_flag = False)` 156 | -参数 157 | - `validate_date` livegetfour接口获取的四字唇语返回值 158 | - `video_path` 用户录制视频本地路径 159 | - `seq` 请求唯一标识 160 | - `card_path` 当compare_flag字段为true时,用于与视频提取图像作人脸对比的图像本地路径 161 | - `compare_flag` 决定是否需要作人脸对比,默认为false 162 | 163 | ``` 164 | 165 | ### 带网纹活体检测 166 | ``` 167 | `idcardlivedetectfour(self, idcard_number, idcard_name, validate_data, video_path, seq = '')` 168 | -参数 169 | - `idcard_number` 带网纹图像对应的身份证号码 170 | - `idcard_name` 带网纹图像对应的身份证姓名 171 | - `validate_data` livegetfour接口获取的四字唇语返回值 172 | - `video_path` 用户录制视频本地路径 173 | - `seq` 请求唯一标识 174 | 175 | ``` 176 | 177 | ### 带网纹人脸对比 178 | ``` 179 | `idcardfacecompare(self, idcard_number, idcard_name, image_path, data_type = 0 , session_id = '')` 180 | -参数 181 | - `idcard_number` 带网纹图像对应的身份证号码 182 | - `idcard_name` 带网纹图像对应的身份证姓名 183 | - `data_type` 0表示本地图像,1表示url下载图像 184 | - `image_path` 当data_type字段为0时,则image_path存放图像本地路径;data_type字段为1时,则image_path存放url路径 185 | - `session_id` 请求唯一标识 186 | 187 | ``` 188 | 189 | ### 不带网纹人脸比对 190 | ``` 191 | - 接口 192 | `FaceCompare(self, image_pathA, image_pathB, data_type = 0):` 193 | - 参数 194 | - `image_pathA` 待比对的A图片路径 195 | - `image_pathB` 待比对的B图片路径 196 | - `data_type` 用于表示image_pathA, image_pathB是图片还是url, 0代表图片,1代表url 197 | 198 | ``` 199 | 200 | ### 身份证OCR识别 201 | ``` 202 | `idcardocr(self, image_path, data_type = 0, card_type = 1 ,seq = '')` 203 | - 参数 204 | - `image_path` 标识图片信息 205 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 206 | - `card_type` 0 代表输入图像是身份证正面, 1代表输入是身份证反面 207 | - `seq` 请求唯一标识 208 | 209 | ``` 210 | 211 | ### 身份证认证 212 | ``` 213 | `ValidateIdcard(self, idcard_number, idcard_name, seq = '')` 214 | - 参数 215 | - `idcard_number`身份证号 216 | - `idcard_name` 身份证姓名 217 | - `seq` 请求唯一标识 218 | 219 | ``` 220 | 221 | ## 腾讯优图开放平台接口 222 | ``` 223 | - 接口end_point选择: TencentYoutuyun.conf.API_YOUTU_END_POINT 224 | ``` 225 | 226 | ### 人脸检测 227 | ``` 228 | - 接口 229 | `DetectFace(self, image_path, mode = 0, data_type = 0)` 230 | - 参数 231 | - `image_path` 待检测的图片路径 232 | - `mode` 是否大脸模式,默认非大脸模式 233 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 234 | ``` 235 | 236 | ### 人脸配准 237 | ``` 238 | - 接口 239 | `FaceShape(self, image_path, mode = 0, data_type = 0)` 240 | - 参数 241 | - `image_path` 待检测的图片路径 242 | - `mode` 是否大脸模式,默认非大脸模式 243 | - `data_type` 用于表示image是图片还是url, 0代表图片,1代表url 244 | ``` 245 | 246 | ### 人脸比对 247 | ``` 248 | - 接口 249 | `FaceCompare(self, image_pathA, image_pathB, data_type = 0):` 250 | - 参数 251 | - `image_pathA` 待比对的A图片路径 252 | - `image_pathB` 待比对的B图片路径 253 | - `data_type` 用于表示image_pathA, image_pathB是图片还是url, 0代表图片,1代表url 254 | ``` 255 | 256 | ### 人脸验证 257 | ``` 258 | - 接口 259 | `FaceVerify(self, person_id, image_path, data_type = 0)` 260 | - 参数 261 | - `person_id` 待验证的个体id 262 | - `image_path` 待验证的图片路径 263 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 264 | ``` 265 | 266 | ### 人脸识别 267 | ``` 268 | - 接口 269 | `FaceIdentify(self, group_id, image_path, data_type = 0)` 270 | - 参数 271 | - `group_id` 识别的组id 272 | - `image_path` 待识别的图片路径 273 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 274 | ``` 275 | 276 | ### 多人脸检索 277 | ``` 278 | 279 | - 接口 280 | `MultiFaceIdentify(self, group_id, group_ids, image_path, data_type = 0, topn = 5, min_size = 40)` 281 | - 参数 282 | - `group_id` 识别的组id 283 | - `group_ids` 识别的个体存放的组id,可以指定多个组id,用户指定 284 | - `topn` 候选人脸数量,一般使用默认值5 285 | - `min_size` 人脸检测最小尺寸,一般使用默认值40 286 | ``` 287 | 288 | ### 新建个体 289 | ``` 290 | - 接口 291 | `NewPerson(self, person_id, image_path, group_ids, person_name= '', tag='', data_type = 0)` 292 | - 参数 293 | - `person_id` 新建的个体id,用户指定,需要保证app_id下的唯一性 294 | - `person_name` 个体对应的姓名 295 | - `group_ids` 数组类型,用户指定(组默认创建)的个体存放的组id,可以指定多个组id 296 | - `image_path` 包含个体人脸的图片路径 297 | - `tag` 备注信息,用户自解释字段 298 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 299 | ``` 300 | 301 | ### 删除个体 302 | ``` 303 | - 接口 304 | `DelPerson(self, person_id)` 305 | - 参数 306 | - `person_id` 待删除的个体id 307 | ``` 308 | 309 | ### 增加人脸 310 | ``` 311 | - 接口 312 | `AddFace(self, person_id, images, tag='', data_type = 0)` 313 | - 参数 314 | - `person_id` 新增人脸的个体身份id 315 | - `images` 数组类型,待增加的包含人脸的图片路径,可加入多张(包体大小<2m) 316 | - `tag` 人脸备注信息,用户自解释字段 317 | - `data_type` 用于表示images是图片还是url, 0代表图片,1代表url 318 | ``` 319 | 320 | ### 删除人脸 321 | ``` 322 | - 接口 323 | `DelFace(self, person_id, face_ids)` 324 | - 参数 325 | - `person_id` 待删除人脸的个体身份id 326 | - `face_ids` 数组类型,待删除的人脸id 327 | ``` 328 | 329 | ### 获取信息 330 | ``` 331 | - 接口 332 | `GetInfo(self, person_id)` 333 | - 参数 334 | - `person_id` 待查询的个体身份id 335 | ``` 336 | 337 | ### 设置信息 338 | ``` 339 | - 接口 340 | `SetInfo(self, person_id, person_name='', tag='')` 341 | - 参数 342 | - `person_id` 待设置的个体身份id 343 | - `person_name` 新设置的个体名字,为空无效 344 | - `tag` 新设置的人脸备注信息,为空无效 345 | ``` 346 | 347 | ### 获取组列表 348 | ``` 349 | - 接口 350 | `GetGroupIds(self)` 351 | - 参数 352 | - 无 353 | ``` 354 | 355 | ### 获取个体列表 356 | ``` 357 | - 接口 358 | `GetPersonIds(self, group_id)` 359 | - 参数 360 | - `group_id` 待查询的组id 361 | ``` 362 | 363 | ### 获取人脸列表 364 | ``` 365 | - 接口 366 | `GetFaceIds(self, person_id)` 367 | - 参数 368 | - `person_id` 待查询的个体id 369 | ``` 370 | 371 | ### 获取人脸信息 372 | ``` 373 | - 接口 374 | `GetFaceInfo(self, face_id)` 375 | - 参数 376 | - `face_id` 待查询的人脸id 377 | ``` 378 | 379 | ### 模糊检测 380 | ``` 381 | `fuzzydetect(self, image_path, data_type = 0, seq = '')` 382 | - 参数 383 | - `image_path` 标识图片信息 384 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 385 | ``` 386 | 387 | ### 美食检测 388 | ``` 389 | `fooddetect(self, image_path, data_type = 0, seq = '')` 390 | - 参数 391 | - `image_path` 标识图片信息 392 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 393 | ``` 394 | 395 | ### 图片分类 396 | ``` 397 | `imagetag(self, image_path, data_type = 0, seq = '')` 398 | - 参数 399 | - `image_path` 标识图片信息 400 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 401 | ``` 402 | 403 | ### 色情图像检测 404 | ``` 405 | `imageporn(self, image_path, data_type = 0, seq = '')` 406 | - 参数 407 | - `image_path` 标识图片信息 408 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 409 | ``` 410 | 411 | ### 暴恐图片识别 412 | ``` 413 | `imageterrorism(self, image_path, data_type = 0, seq = '')` 414 | - 参数 415 | - `image_path` 标识图片信息 416 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 417 | ``` 418 | 419 | ### 车辆属性识别 420 | ``` 421 | `carclassify(self, image_path, data_type = 0, seq = '')` 422 | - 参数 423 | - `image_path` 标识图片信息 424 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 425 | ``` 426 | 427 | ### 身份证OCR识别 428 | ``` 429 | `idcardocr(self, image_path, data_type = 0, card_type = 1 ,seq = '')` 430 | - 参数 431 | - `image_path` 标识图片信息 432 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 433 | - `card_type` 0 代表输入图像是身份证正面, 1代表输入是身份证反面 434 | ``` 435 | 436 | ### 行驶证&驾驶证ocr识别 437 | ``` 438 | `driverlicenseocr(self, image_path, data_type = 0, proc_type = 0, seq = '')` 439 | - 参数 440 | - `image_path` 标识图片信息 441 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 442 | - `proc_type` 0代表行驶证, 1代表驾驶证 443 | 444 | ``` 445 | 446 | ### 新版名片ocr识别 447 | ``` 448 | `bcocr(self, image_path, data_type = 0, seq = '')` 449 | - 参数 450 | - `image_path` 标识图片信息 451 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 452 | 453 | ``` 454 | 455 | ### 通用ocr识别 456 | ``` 457 | `generalocr(self, image_path, data_type = 0, seq = '')` 458 | - 参数 459 | - `image_path` 标识图片信息 460 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 461 | 462 | ``` 463 | 464 | ### 银行卡OCR识别 465 | ``` 466 | `creditcardocr(self, image_path, data_type = 0, seq = '')` 467 | - 参数 468 | - `image_path` 标识图片信息 469 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 470 | 471 | ``` 472 | 473 | ### 营业执照OCR识别 474 | ``` 475 | `bizlicenseocr(self, image_path, data_type = 0, seq = '')` 476 | - 参数 477 | - `image_path` 标识图片信息 478 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 479 | 480 | ``` 481 | 482 | ### 车牌OCR识别 483 | ``` 484 | `plateocr(self, image_path, data_type = 0, seq = '')` 485 | - 参数 486 | - `image_path` 标识图片信息 487 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 488 | 489 | ``` 490 | 491 | ### 结构化OCR识别 492 | ``` 493 | `structureocr(self, image_path, data_type = 0, ocr_template = '', seq = '')` 494 | - 参数 495 | - `image_path` 标识图片信息 496 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 497 | - `ocr_template` 用于模板 498 | 499 | ``` 500 | 501 | ### 护照OCR识别 502 | ``` 503 | `passportocr(self, image_path, data_type = 0, ocr_template = '', seq = '')` 504 | - 参数 505 | - `image_path` 标识图片信息 506 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 507 | - `ocr_template` 用于模板 508 | 509 | ``` 510 | 511 | ### 增值税发票OCR识别 512 | ``` 513 | `invoiceocr(self, image_path, data_type = 0, ocr_template='', seq = '')` 514 | - 参数 515 | - `image_path` 标识图片信息 516 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 517 | - `ocr_template` 用于模板 518 | 519 | ``` 520 | 521 | ### 电子运单OCR识别 522 | ``` 523 | `waybillocr(self, image_path, data_type = 0, seq = '')` 524 | - 参数 525 | - `image_path` 标识图片信息 526 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 527 | 528 | ``` 529 | 530 | ### 高精度OCR识别 531 | ``` 532 | `hpgeneralocr(self, image_path, data_type = 0, seq = '')` 533 | - 参数 534 | - `image_path` 标识图片信息 535 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 536 | 537 | ``` 538 | 539 | ### 表格OCR识别 540 | ``` 541 | `tableocr(self, image_path, data_type = 0, seq = '')` 542 | - 参数 543 | - `image_path` 标识图片信息 544 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 545 | 546 | ``` 547 | 548 | ### 速算OCR识别 549 | ``` 550 | `arithmeticocr(self, image_path, data_type = 0, seq = '')` 551 | - 参数 552 | - `image_path` 标识图片信息 553 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 554 | 555 | ``` 556 | 557 | ### 金融票据OCR识别 558 | ``` 559 | `finanocr(self, image_path, data_type = 0, seq = '')` 560 | - 参数 561 | - `image_path` 标识图片信息 562 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 563 | 564 | ``` 565 | 566 | ### 车辆VIN码OCR识别 567 | ``` 568 | `vinocr(self, image_path, data_type = 0, seq = '')` 569 | - 参数 570 | - `image_path` 标识图片信息 571 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 572 | 573 | ``` 574 | 575 | 576 | ### 英文OCR识别 577 | ``` 578 | `ehocr(self, image_path, data_type = 0, seq = '')` 579 | - 参数 580 | - `image_path` 标识图片信息 581 | - `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url 582 | 583 | ``` 584 | 585 | 586 | 更多详情和文档说明参见 587 | [腾讯优图开放平台](https://open.youtu.qq.com) 588 | -------------------------------------------------------------------------------- /TencentYoutuyun/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from .youtu import YouTu 4 | from .auth import Auth 5 | 6 | 7 | -------------------------------------------------------------------------------- /TencentYoutuyun/auth.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import time 4 | import random 5 | import hmac, hashlib 6 | import binascii 7 | import base64 8 | from TencentYoutuyun import conf 9 | 10 | class Auth(object): 11 | 12 | def __init__(self, secret_id, secret_key, appid, userid): 13 | self.AUTH_URL_FORMAT_ERROR = -1 14 | self.AUTH_SECRET_ID_KEY_ERROR = -2 15 | 16 | self._secret_id = secret_id 17 | self._secret_key = secret_key 18 | self._appid = appid 19 | self._userid = userid 20 | 21 | def app_sign(self, expired=0): 22 | if not self._secret_id or not self._secret_key: 23 | return self.AUTH_SECRET_ID_KEY_ERROR 24 | 25 | puserid = '' 26 | if self._userid != '': 27 | if len(self._userid) > 64: 28 | return self.AUTH_URL_FORMAT_ERROR 29 | puserid = self._userid 30 | 31 | now = int(time.time()) 32 | rdm = random.randint(0, 999999999) 33 | plain_text = 'a=' + self._appid + '&k=' + self._secret_id + '&e=' + str(expired) + '&t=' + str(now) + '&r=' + str(rdm) + '&u=' + puserid + '&f=' 34 | bin = hmac.new(self._secret_key.encode(), plain_text.encode(), hashlib.sha1) 35 | s = bin.hexdigest() 36 | s = binascii.unhexlify(s) 37 | s = s + plain_text.encode('ascii') 38 | signature = base64.b64encode(s).rstrip() #生成签名 39 | return signature 40 | 41 | -------------------------------------------------------------------------------- /TencentYoutuyun/conf.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import pkg_resources 3 | import platform 4 | 5 | API_YOUTU_END_POINT = 'http://api.youtu.qq.com/' 6 | API_YOUTU_VIP_END_POINT = 'https://vip-api.youtu.qq.com/' 7 | 8 | APPID = 'xxx' 9 | SECRET_ID = 'xxx' 10 | SECRET_KEY = 'xx' 11 | USER_ID = 'xx' 12 | 13 | _config = { 14 | 'end_point':API_YOUTU_END_POINT, 15 | 'appid':APPID, 16 | 'secret_id':SECRET_ID, 17 | 'secret_key':SECRET_KEY, 18 | 'userid':USER_ID, 19 | } 20 | 21 | def get_app_info(): 22 | return _config 23 | 24 | def set_app_info(appid=None, secret_id=None, secret_key=None, userid=None, end_point=None): 25 | if appid: 26 | _config['appid'] = appid 27 | if secret_id: 28 | _config['secret_id'] = secret_id 29 | if secret_key: 30 | _config['secret_key'] = secret_key 31 | if userid: 32 | _config['userid'] = userid 33 | if end_point: 34 | _config['end_point'] = end_point 35 | 36 | 37 | -------------------------------------------------------------------------------- /TencentYoutuyun/youtu.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import os.path 4 | import time 5 | import requests 6 | import base64 7 | import json 8 | from TencentYoutuyun import conf 9 | from .auth import Auth 10 | 11 | class YouTu(object): 12 | 13 | def __init__(self, appid, secret_id, secret_key, userid='0', end_point=conf.API_YOUTU_END_POINT): 14 | self.IMAGE_FILE_NOT_EXISTS = -1 15 | self.IMAGE_NETWORK_ERROR = -2 16 | self.IMAGE_PARAMS_ERROR = -3 17 | self.PERSON_ID_EMPTY = -4 18 | self.GROUP_ID_EMPTY = -5 19 | self.GROUP_IDS_EMPTY = -6 20 | self.IMAGES_EMPTY = -7 21 | self.FACE_IDS_EMPTY = -8 22 | self.FACE_ID_EMPTY = -9 23 | self.LIST_TYPE_INVALID = -10 24 | self.IMAGE_PATH_EMPTY = -11 25 | 26 | self.VALIDATE_DATA_EMPTY = -12 27 | self.VIDEO_PATH_EMPTY = -13 28 | self.CARD_PATH_EMPTY = -14 29 | self.IDCARD_NAME_OR_ID_EMPTY = -15 30 | 31 | self.VIDEO_FILE_NOT_EXISTS = -16 32 | self.CARD_FILE_NOT_EXISTS = -17 33 | 34 | self.UNKNOW_CARD_TYPE = -18 35 | 36 | self.EXPIRED_SECONDS = 2592000 37 | self._secret_id = secret_id 38 | self._secret_key = secret_key 39 | self._appid = appid 40 | self._userid = userid 41 | self._end_point = end_point 42 | conf.set_app_info(appid, secret_id, secret_key, userid, end_point) 43 | 44 | def get_headers(self, req_type): 45 | 46 | expired = int(time.time()) + self.EXPIRED_SECONDS 47 | auth = Auth(self._secret_id, self._secret_key, self._appid, self._userid) 48 | 49 | sign = auth.app_sign(expired) 50 | headers = { 51 | 'Authorization':sign, 52 | 'Content-Type':'text/json' 53 | } 54 | 55 | return headers 56 | 57 | def generate_res_url(self, req_type, url_type = 0): 58 | 59 | app_info = conf.get_app_info() 60 | url_api_str = '' 61 | if url_type == 4: 62 | url_api_str = 'youtu/carapi' 63 | elif url_type == 3: 64 | url_api_str = 'youtu/openliveapi' 65 | elif url_type == 2: 66 | url_api_str = 'youtu/ocrapi' 67 | elif url_type == 1: 68 | url_api_str = 'youtu/imageapi' 69 | else : 70 | url_api_str = 'youtu/api' 71 | 72 | return app_info['end_point'] + url_api_str + '/' + str(req_type) 73 | 74 | def FaceCompare(self, image_pathA, image_pathB, data_type = 0): 75 | 76 | req_type = 'facecompare' 77 | headers = self.get_headers(req_type) 78 | url = self.generate_res_url(req_type) 79 | 80 | data = { 81 | "app_id": self._appid 82 | } 83 | 84 | if len(image_pathA) == 0 or len(image_pathB) == 0: 85 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY', 'session_id':'', 'similarity':0} 86 | 87 | if data_type == 0: 88 | filepathA = os.path.abspath(image_pathA) 89 | filepathB = os.path.abspath(image_pathB) 90 | 91 | if not os.path.exists(filepathA): 92 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS', 'session_id':'', 'similarity':0} 93 | if not os.path.exists(filepathB): 94 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS', 'session_id':'', 'similarity':0} 95 | 96 | data["imageA"] = base64.b64encode(open(filepathA, 'rb').read()).rstrip().decode('utf-8') 97 | data["imageB"] = base64.b64encode(open(filepathB, 'rb').read()).rstrip().decode('utf-8') 98 | else : 99 | data["urlA"] = image_pathA 100 | data["urlB"] = image_pathB 101 | 102 | r = {} 103 | try: 104 | r = requests.post(url, headers=headers, data = json.dumps(data)) 105 | if r.status_code != 200: 106 | return {'httpcode':r.status_code, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':'', 'session_id':'', 'similarity':0} 107 | ret = r.json() 108 | 109 | except Exception as e: 110 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e), 'session_id':'', 'similarity':0} 111 | 112 | return ret 113 | 114 | def FaceVerify(self, person_id, image_path, data_type = 0): 115 | 116 | req_type='faceverify' 117 | headers = self.get_headers(req_type) 118 | url = self.generate_res_url(req_type) 119 | 120 | data = { 121 | "app_id": self._appid, 122 | "person_id": person_id 123 | } 124 | 125 | if len(image_path) == 0: 126 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY', "confidence":0, "ismatch":0, "session_id":''} 127 | 128 | if data_type == 0: 129 | filepath = os.path.abspath(image_path) 130 | if not os.path.exists(filepath): 131 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS', "confidence":0, "ismatch":0, "session_id":''} 132 | if len(person_id) == 0: 133 | return {'httpcode':0, 'errorcode':self.PERSON_ID_EMPTY, 'errormsg':'PERSON_ID_EMPTY', "confidence":0, "ismatch":0, "session_id":''} 134 | 135 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 136 | else : 137 | data["url"] = image_path 138 | 139 | r = {} 140 | try: 141 | r = requests.post(url, headers=headers, data = json.dumps(data)) 142 | if r.status_code != 200: 143 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':'', "confidence":0, "ismatch":0, "session_id":''} 144 | ret = r.json() 145 | 146 | except Exception as e: 147 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e), "confidence":0, "ismatch":0, "session_id":''} 148 | 149 | return ret 150 | 151 | def FaceIdentify(self, group_id, image_path, data_type = 0): 152 | 153 | req_type='faceidentify' 154 | headers = self.get_headers(req_type) 155 | url = self.generate_res_url(req_type) 156 | 157 | data = { 158 | "app_id": self._appid 159 | } 160 | 161 | if len(image_path) == 0: 162 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY', "session_id":'', "candidates":[{}]} 163 | 164 | if data_type == 0: 165 | filepath = os.path.abspath(image_path) 166 | if not os.path.exists(filepath): 167 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS', "session_id":'', "candidates":[{}]} 168 | 169 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 170 | else : 171 | data["url"] = image_path 172 | 173 | if len(group_id) == 0: 174 | return {'httpcode':0, 'errorcode':self.GROUP_ID_EMPTY, 'errormsg':'GROUP_ID_EMPTY', "session_id":'', "candidates":[{}]} 175 | else : 176 | data["group_id"] = group_id 177 | 178 | r = {} 179 | try: 180 | r = requests.post(url, headers=headers, data = json.dumps(data)) 181 | if r.status_code != 200: 182 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':'', "session_id":'', "candidates":[{}]} 183 | ret = r.json() 184 | 185 | except Exception as e: 186 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e), "session_id":'', "candidates":[{}]} 187 | 188 | return ret 189 | 190 | def MultiFaceIdentify(self, group_id, group_ids, image_path, data_type = 0, topn = 5, min_size = 40): 191 | 192 | req_type='multifaceidentify' 193 | headers = self.get_headers(req_type) 194 | url = self.generate_res_url(req_type) 195 | 196 | data = { 197 | "app_id": self._appid, 198 | "topn": topn, 199 | "min_size": min_size 200 | } 201 | 202 | if len(image_path) == 0: 203 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY', "session_id":'', "candidates":[{}]} 204 | 205 | if data_type == 0: 206 | filepath = os.path.abspath(image_path) 207 | if not os.path.exists(filepath): 208 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS', "session_id":'', "candidates":[{}]} 209 | 210 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 211 | else : 212 | data["url"] = image_path 213 | 214 | if len(group_id) == 0 and len(group_ids) == 0: 215 | return {'httpcode':0, 'errorcode':self.ERROR_PARAMETER_EMPTY, 'errormsg':'ERROR_PARAMETER_EMPTY', "session_id":'', "candidates":[{}]} 216 | elif len(group_id) != 0: 217 | data["group_id"] = group_id 218 | else : 219 | data["group_ids"] = group_ids 220 | 221 | r = {} 222 | try: 223 | r = requests.post(url, headers=headers, data = json.dumps(data)) 224 | if r.status_code != 200: 225 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':'', "session_id":'', "candidates":[{}]} 226 | ret = r.json() 227 | 228 | except Exception as e: 229 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e), "session_id":'', "candidates":[{}]} 230 | 231 | return ret 232 | 233 | def DetectFace(self, image_path, mode = 0, data_type = 0): 234 | 235 | req_type='detectface' 236 | headers = self.get_headers(req_type) 237 | url = self.generate_res_url(req_type) 238 | 239 | data = { 240 | "app_id": self._appid, 241 | "mode": mode 242 | } 243 | 244 | if len(image_path) == 0: 245 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY', "session_id":'', "image_id":'', "image_height":0, "image_width":0, "face":[{}]} 246 | 247 | if data_type == 0: 248 | filepath = os.path.abspath(image_path) 249 | if not os.path.exists(filepath): 250 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS', "session_id":'', "image_id":'', "image_height":0, "image_width":0, "face":[{}]} 251 | 252 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 253 | else : 254 | data["url"] = image_path 255 | 256 | r = {} 257 | try: 258 | r = requests.post(url, headers=headers, data = json.dumps(data)) 259 | if r.status_code != 200: 260 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':'', "session_id":'', "image_id":'', "image_height":0, "image_width":0, "face":[{}]} 261 | ret = r.json() 262 | 263 | except Exception as e: 264 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e), "session_id":'', "image_id":'', "image_height":0, "image_width":0, "face":[{}]} 265 | 266 | return ret 267 | 268 | 269 | def NewPerson(self, person_id, image_path, group_ids, person_name= '', tag='', data_type = 0): 270 | 271 | req_type='newperson' 272 | headers = self.get_headers(req_type) 273 | url = self.generate_res_url(req_type) 274 | 275 | if len(person_id) == 0: 276 | return {'httpcode':0, 'errorcode':self.PERSON_ID_EMPTY, 'errormsg':'PERSON_ID_EMPTY', "person_id":'', "suc_group":'', "suc_face":0, "session_id":0, "face_id":'', "group_ids":''} 277 | 278 | if len(group_ids) == 0: 279 | return {'httpcode':0, 'errorcode':self.GROUP_IDS_EMPTY, 'errormsg':'GROUP_IDS_EMPTY', "person_id":'', "suc_group":'', "suc_face":0, "session_id":0, "face_id":'', "group_ids":''} 280 | 281 | if type(group_ids) != list: 282 | return {'httpcode':0, 'errorcode': self.LIST_TYPE_INVALID, 'errormsg':'LIST_TYPE_INVALID', "person_id":'', "suc_group":'', "suc_face":0, "session_id":0, "face_id":'', "group_ids":''} 283 | 284 | data = { 285 | "app_id": self._appid, 286 | "person_id" : person_id, 287 | "person_name": person_name, 288 | "group_ids": group_ids, 289 | "tag": tag 290 | } 291 | 292 | if len(image_path) == 0: 293 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY', "person_id":'', "suc_group":'', "suc_face":0, "session_id":0, "face_id":'', "group_ids":''} 294 | 295 | if data_type == 0: 296 | filepath = os.path.abspath(image_path) 297 | if not os.path.exists(filepath): 298 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS', "person_id":'', "suc_group":'', "suc_face":0, "session_id":0, "face_id":'', "group_ids":''} 299 | 300 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 301 | else: 302 | data["url"] = image_path 303 | 304 | r = {} 305 | try: 306 | r = requests.post(url, headers=headers, data = json.dumps(data)) 307 | if r.status_code != 200: 308 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':'', "person_id":'', "suc_group":'', "suc_face":0, "session_id":0, "face_id":'', "group_ids":''} 309 | 310 | ret = r.json() 311 | except Exception as e: 312 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e), "person_id":'', "suc_group":'', "suc_face":0, "session_id":0, "face_id":'', "group_ids":''} 313 | 314 | return ret 315 | 316 | def DelPerson(self, person_id) : 317 | 318 | req_type='delperson' 319 | headers = self.get_headers(req_type) 320 | url = self.generate_res_url(req_type) 321 | 322 | if len(person_id) == 0: 323 | return {'httpcode':0, 'errorcode':self.PERSON_ID_EMPTY, 'errormsg':'PERSON_ID_EMPTY', "deleted":0, "session_id":''} 324 | 325 | data = { 326 | "app_id": self._appid, 327 | "person_id" : person_id 328 | } 329 | 330 | r = {} 331 | try: 332 | r = requests.post(url, headers=headers, data = json.dumps(data)) 333 | if r.status_code != 200: 334 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':'', "deleted":0, "session_id":''} 335 | 336 | ret = r.json() 337 | except Exception as e: 338 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e), "deleted":0, "session_id":''} 339 | 340 | return ret 341 | 342 | def AddFace(self, person_id, images, tag='', data_type = 0): 343 | 344 | req_type='addface' 345 | headers = self.get_headers(req_type) 346 | url = self.generate_res_url(req_type) 347 | 348 | if len(person_id) == 0: 349 | return {'httpcode':0, 'errorcode':self.PERSON_ID_EMPTY, 'errormsg':'PERSON_ID_EMPTY', "face_ids":[], "session_id":'', "added": 0, "ret_codes":[]} 350 | 351 | data = { 352 | "app_id": self._appid, 353 | "person_id" : person_id, 354 | "tag" : tag 355 | } 356 | 357 | if len(images) == 0: 358 | return {'httpcode':0, 'errorcode':self.IMAGES_EMPTY, 'errormsg':'IMAGES_EMPTY', "face_ids":[], "session_id":'', "added": 0, "ret_codes":[]} 359 | 360 | if type(images) != list: 361 | return {'httpcode':0, 'errorcode':self.LIST_TYPE_INVALID, 'errormsg':'LIST_TYPE_INVALID', "face_ids":[], "session_id":'', "added": 0, "ret_codes":[]} 362 | 363 | if data_type == 0: 364 | images_content = [] 365 | for image in images: 366 | filepath = os.path.abspath(image) 367 | if not os.path.exists(filepath): 368 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS', "face_ids":[], "session_id":'', "added": 0, "ret_codes":[]} 369 | 370 | images_content.append(base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8')) 371 | data["images"] = images_content 372 | else : 373 | data["urls"] = images 374 | 375 | r = {} 376 | try: 377 | r = requests.post(url, headers=headers, data = json.dumps(data)) 378 | if r.status_code != 200: 379 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':'', "face_ids":[], "session_id":'', "added": 0, "ret_codes":[]} 380 | 381 | ret = r.json() 382 | except Exception as e: 383 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e), "face_ids":[], "session_id":'', "added": 0, "ret_codes":[]} 384 | 385 | return ret 386 | 387 | def DelFace(self, person_id, face_ids): 388 | 389 | req_type='delface' 390 | headers = self.get_headers(req_type) 391 | url = self.generate_res_url(req_type) 392 | 393 | if len(person_id) == 0: 394 | return {'httpcode':0, 'errorcode':self.PERSON_ID_EMPTY, 'errormsg':'PERSON_ID_EMPTY', "session_id":'', "deleted ": 0} 395 | 396 | if len(face_ids) == 0: 397 | return {'httpcode':0, 'errorcode':self.FACE_IDS_IMPTY, 'errormsg':'FACE_IDS_IMPTY', "session_id":'', "deleted ": 0} 398 | 399 | if type(face_ids) != list: 400 | return {'httpcode':0, 'errorcode':self.LIST_TYPE_INVALID, 'errormsg':'LIST_TYPE_INVALID', "session_id":'', "deleted ": 0} 401 | 402 | data = { 403 | "app_id": self._appid, 404 | "person_id":person_id, 405 | "face_ids":face_ids 406 | } 407 | 408 | r = {} 409 | try: 410 | r = requests.post(url, headers=headers, data = json.dumps(data)) 411 | if r.status_code != 200: 412 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':'', "session_id":'', "deleted ": 0} 413 | 414 | ret = r.json() 415 | except Exception as e: 416 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e), "session_id":'', "deleted ": 0} 417 | 418 | return ret 419 | 420 | 421 | def SetInfo(self, person_id, person_name='', tag=''): 422 | 423 | req_type='setinfo' 424 | headers = self.get_headers(req_type) 425 | url = self.generate_res_url(req_type) 426 | url_type 427 | if len(person_id) == 0: 428 | return {'httpcode':0, 'errorcode':self.PERSON_ID_EMPTY, 'errormsg':'PERSON_ID_EMPTY', "person_id":'', "session_id ": ''} 429 | 430 | data = { 431 | "app_id": self._appid, 432 | "person_id": person_id, 433 | "person_name": person_name, 434 | "tag":tag 435 | } 436 | 437 | r = {} 438 | try: 439 | r = requests.post(url, headers=headers, data = json.dumps(data)) 440 | if r.status_code != 200: 441 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':'', "person_id":'', "session_id ": ''} 442 | 443 | ret = r.json() 444 | except Exception as e: 445 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e), "person_id":'', "session_id ": ''} 446 | 447 | return ret 448 | 449 | def GetInfo(self, person_id): 450 | 451 | req_type='getinfo' 452 | headers = self.get_headers(req_type) 453 | url = self.generate_res_url(req_type) 454 | 455 | if len(person_id) == 0: 456 | return {'httpcode':0, 'errorcode':self.PERSON_ID_EMPTY, 'errormsg':'PERSON_ID_EMPTY', "person_id":'', "person_name ": '', "face_ids":[], "tag":'', "secret_id":''} 457 | 458 | data = { 459 | "app_id": self._appid, 460 | "person_id": person_id 461 | } 462 | 463 | r = {} 464 | try: 465 | r = requests.post(url, headers=headers, data = json.dumps(data)) 466 | if r.status_code != 200: 467 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':'', "person_id":'', "person_name ": '', "face_ids":[], "tag":'', "secret_id":''} 468 | ret = r.json() 469 | 470 | except Exception as e: 471 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e), "person_id":'', "person_name ": '', "face_ids":[], "tag":'', "secret_id":''} 472 | 473 | return ret 474 | 475 | def GetGroupIds(self): 476 | 477 | req_type='getgroupids' 478 | headers = self.get_headers(req_type) 479 | url = self.generate_res_url(req_type) 480 | 481 | data = { 482 | "app_id": self._appid 483 | } 484 | 485 | r = {} 486 | try: 487 | r = requests.post(url, headers=headers, data = json.dumps(data)) 488 | if r.status_code != 200: 489 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':'', "group_ids":[]} 490 | 491 | ret = r.json() 492 | except Exception as e: 493 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e), "group_ids":[]} 494 | 495 | return ret 496 | 497 | def GetPersonIds(self, group_id) : 498 | 499 | req_type='getpersonids' 500 | headers = self.get_headers(req_type) 501 | url = self.generate_res_url(req_type) 502 | 503 | if len(group_id) == 0: 504 | return {'httpcode':0, 'errorcode':self.GROUP_ID_EMPTY, 'errormsg':'GROUP_ID_EMPTY', "person_ids":[]} 505 | 506 | data = { 507 | "app_id": self._appid, 508 | "group_id": group_id 509 | } 510 | 511 | r = {} 512 | try: 513 | r = requests.post(url, headers=headers, data = json.dumps(data)) 514 | if r.status_code != 200: 515 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':'', "person_ids":[]} 516 | 517 | ret = r.json() 518 | except Exception as e: 519 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e), "person_ids":[]} 520 | 521 | return ret 522 | 523 | def GetFaceIds(self, person_id): 524 | 525 | req_type='getfaceids' 526 | headers = self.get_headers(req_type) 527 | url = self.generate_res_url(req_type) 528 | 529 | if len(person_id) == 0: 530 | return {'httpcode':0, 'errorcode':self.PERSON_ID_EMPTY, 'errormsg':'PERSON_ID_EMPTY', "face_ids":[]} 531 | 532 | data = { 533 | "app_id": self._appid, 534 | "person_id": person_id 535 | } 536 | 537 | r = {} 538 | try: 539 | r = requests.post(url, headers=headers, data = json.dumps(data)) 540 | if r.status_code != 200: 541 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':'', "face_ids":[]} 542 | 543 | ret = r.json() 544 | except Exception as e: 545 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e), "face_ids":[]} 546 | 547 | return ret 548 | 549 | def GetFaceInfo(self, face_id): 550 | 551 | req_type='getfaceinfo' 552 | headers = self.get_headers(req_type) 553 | url = self.generate_res_url(req_type) 554 | 555 | if len(face_id) == 0: 556 | return {'httpcode':0, 'errorcode':self.FACE_ID_EMPTY, 'errormsg':'FACE_ID_EMPTY', "face_info":[]} 557 | 558 | data = { 559 | "app_id": self._appid, 560 | "face_id": face_id 561 | } 562 | 563 | r = {} 564 | try: 565 | r = requests.post(url, headers=headers, data = json.dumps(data)) 566 | if r.status_code != 200: 567 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':'', "face_info":[]} 568 | 569 | ret = r.json() 570 | except Exception as e: 571 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e), "face_info":[]} 572 | 573 | return ret 574 | 575 | def FaceShape(self, image_path, mode = 0, data_type = 0): 576 | 577 | req_type='faceshape' 578 | headers = self.get_headers(req_type) 579 | url = self.generate_res_url(req_type) 580 | 581 | data = { 582 | "app_id": self._appid, 583 | "mode": mode 584 | } 585 | 586 | if len(image_path) == 0: 587 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY', "face_shape":[{}], "image_height":0, "image_width":0, "session_id":''} 588 | 589 | if data_type == 0: 590 | filepath = os.path.abspath(image_path) 591 | if not os.path.exists(filepath): 592 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS', "face_shape":[{}], "image_height":0, "image_width":0, "session_id":''} 593 | 594 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 595 | else: 596 | data["url"] = image_path 597 | 598 | r = {} 599 | try: 600 | r = requests.post(url, headers=headers, data = json.dumps(data)) 601 | if r.status_code != 200: 602 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':'', "face_shape":[{}], "image_height":0, "image_width":0, "session_id":''} 603 | 604 | ret = r.json() 605 | except Exception as e: 606 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e), "face_shape":[{}], "image_height":0, "image_width":0, "session_id":''} 607 | 608 | return ret 609 | 610 | def fuzzydetect(self, image_path, data_type = 0, seq = ''): 611 | 612 | req_type='fuzzydetect' 613 | headers = self.get_headers(req_type) 614 | url = self.generate_res_url(req_type, 1) 615 | 616 | data = { 617 | "app_id": self._appid, 618 | "seq": seq 619 | } 620 | 621 | if len(image_path) == 0: 622 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY'} 623 | 624 | if data_type == 0: 625 | filepath = os.path.abspath(image_path) 626 | if not os.path.exists(filepath): 627 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS'} 628 | 629 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 630 | else: 631 | data["url"] = image_path 632 | 633 | r = {} 634 | try: 635 | r = requests.post(url, headers=headers, data = json.dumps(data)) 636 | if r.status_code != 200: 637 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':''} 638 | 639 | ret = r.json() 640 | except Exception as e: 641 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e)} 642 | 643 | return ret 644 | 645 | def fooddetect(self, image_path, data_type = 0, seq = ''): 646 | 647 | req_type='fooddetect' 648 | headers = self.get_headers(req_type) 649 | url = self.generate_res_url(req_type, 1) 650 | 651 | data = { 652 | "app_id": self._appid, 653 | "seq": seq 654 | } 655 | 656 | if len(image_path) == 0: 657 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY'} 658 | 659 | if data_type == 0: 660 | filepath = os.path.abspath(image_path) 661 | if not os.path.exists(filepath): 662 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS'} 663 | 664 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 665 | else: 666 | data["url"] = image_path 667 | 668 | r = {} 669 | try: 670 | r = requests.post(url, headers=headers, data = json.dumps(data)) 671 | if r.status_code != 200: 672 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':''} 673 | 674 | ret = r.json() 675 | except Exception as e: 676 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e)} 677 | 678 | return ret 679 | 680 | 681 | 682 | def imagetag(self, image_path, data_type = 0, seq = ''): 683 | 684 | req_type='imagetag' 685 | headers = self.get_headers(req_type) 686 | url = self.generate_res_url(req_type, 1) 687 | 688 | data = { 689 | "app_id": self._appid, 690 | "seq": seq 691 | } 692 | 693 | if len(image_path) == 0: 694 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY'} 695 | 696 | if data_type == 0: 697 | filepath = os.path.abspath(image_path) 698 | if not os.path.exists(filepath): 699 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS'} 700 | 701 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 702 | else: 703 | data["url"] = image_path 704 | 705 | r = {} 706 | try: 707 | r = requests.post(url, headers=headers, data = json.dumps(data)) 708 | if r.status_code != 200: 709 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':''} 710 | 711 | ret = r.json() 712 | except Exception as e: 713 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e)} 714 | 715 | return ret 716 | 717 | def imageporn(self, image_path, data_type = 0, seq = ''): 718 | 719 | req_type='imageporn' 720 | headers = self.get_headers(req_type) 721 | url = self.generate_res_url(req_type, 1) 722 | 723 | data = { 724 | "app_id": self._appid, 725 | "seq": seq 726 | } 727 | 728 | if len(image_path) == 0: 729 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY'} 730 | 731 | if data_type == 0: 732 | filepath = os.path.abspath(image_path) 733 | if not os.path.exists(filepath): 734 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS'} 735 | 736 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 737 | else: 738 | data["url"] = image_path 739 | 740 | r = {} 741 | try: 742 | r = requests.post(url, headers=headers, data = json.dumps(data)) 743 | if r.status_code != 200: 744 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':''} 745 | 746 | ret = r.json() 747 | except Exception as e: 748 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e)} 749 | 750 | return ret 751 | 752 | def imageterrorism(self, image_path, data_type = 0, seq = ''): 753 | 754 | req_type='imageterrorism' 755 | headers = self.get_headers(req_type) 756 | url = self.generate_res_url(req_type, 1) 757 | 758 | data = { 759 | "app_id": self._appid, 760 | "seq": seq 761 | } 762 | 763 | if len(image_path) == 0: 764 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY'} 765 | 766 | if data_type == 0: 767 | filepath = os.path.abspath(image_path) 768 | if not os.path.exists(filepath): 769 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS'} 770 | 771 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 772 | else: 773 | data["url"] = image_path 774 | 775 | r = {} 776 | try: 777 | r = requests.post(url, headers=headers, data = json.dumps(data)) 778 | if r.status_code != 200: 779 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':''} 780 | 781 | ret = r.json() 782 | except Exception as e: 783 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e)} 784 | 785 | return ret 786 | 787 | def carclassify(self, image_path, data_type = 0, seq = ''): 788 | 789 | req_type='carclassify' 790 | headers = self.get_headers(req_type) 791 | url = self.generate_res_url(req_type, 4) 792 | 793 | data = { 794 | "app_id": self._appid, 795 | "seq": seq 796 | } 797 | 798 | if len(image_path) == 0: 799 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY'} 800 | 801 | if data_type == 0: 802 | filepath = os.path.abspath(image_path) 803 | if not os.path.exists(filepath): 804 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS'} 805 | 806 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 807 | else: 808 | data["url"] = image_path 809 | 810 | r = {} 811 | try: 812 | r = requests.post(url, headers=headers, data = json.dumps(data)) 813 | if r.status_code != 200: 814 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':''} 815 | 816 | ret = r.json() 817 | except Exception as e: 818 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e)} 819 | 820 | return ret 821 | 822 | def idcardocr(self, image_path, data_type = 0, card_type = 1 ,seq = ''): 823 | 824 | req_type='idcardocr' 825 | headers = self.get_headers(req_type) 826 | url = self.generate_res_url(req_type, 2) 827 | 828 | data = { 829 | "app_id": self._appid, 830 | "seq": seq, 831 | "card_type":card_type 832 | } 833 | 834 | if len(image_path) == 0: 835 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY'} 836 | 837 | if data_type == 0: 838 | filepath = os.path.abspath(image_path) 839 | if not os.path.exists(filepath): 840 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS'} 841 | 842 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 843 | else: 844 | data["url"] = image_path 845 | r = {} 846 | try: 847 | r = requests.post(url, headers=headers, data = json.dumps(data)) 848 | if r.status_code != 200: 849 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':''} 850 | 851 | ret = r.json() 852 | except Exception as e: 853 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e)} 854 | 855 | return ret 856 | 857 | def driverlicenseocr(self, image_path, data_type = 0, proc_type = 0, seq = ''): 858 | 859 | req_type='driverlicenseocr' 860 | headers = self.get_headers(req_type) 861 | url = self.generate_res_url(req_type, 2) 862 | data = { 863 | "app_id": self._appid, 864 | "session_id": seq, 865 | "type": proc_type 866 | } 867 | 868 | if len(image_path) == 0: 869 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY'} 870 | 871 | if data_type == 0: 872 | filepath = os.path.abspath(image_path) 873 | if not os.path.exists(filepath): 874 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS'} 875 | 876 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 877 | else: 878 | data["url"] = image_path 879 | 880 | r = {} 881 | try: 882 | r = requests.post(url, headers=headers, data = json.dumps(data)) 883 | if r.status_code != 200: 884 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':''} 885 | 886 | ret = r.json() 887 | except Exception as e: 888 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e)} 889 | 890 | return ret 891 | 892 | def bcocr(self, image_path, data_type = 0, seq = ''): 893 | 894 | req_type='bcocr' 895 | headers = self.get_headers(req_type) 896 | url = self.generate_res_url(req_type, 2) 897 | data = { 898 | "app_id": self._appid, 899 | "session_id": seq, 900 | } 901 | 902 | if len(image_path) == 0: 903 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY'} 904 | 905 | if data_type == 0: 906 | filepath = os.path.abspath(image_path) 907 | if not os.path.exists(filepath): 908 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS'} 909 | 910 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 911 | else: 912 | data["url"] = image_path 913 | 914 | r = {} 915 | try: 916 | r = requests.post(url, headers=headers, data = json.dumps(data)) 917 | if r.status_code != 200: 918 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':''} 919 | 920 | ret = r.json() 921 | except Exception as e: 922 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e)} 923 | 924 | return ret 925 | 926 | def generalocr(self, image_path, data_type = 0, seq = ''): 927 | 928 | req_type='generalocr' 929 | headers = self.get_headers(req_type) 930 | url = self.generate_res_url(req_type, 2) 931 | data = { 932 | "app_id": self._appid, 933 | "session_id": seq, 934 | } 935 | 936 | if len(image_path) == 0: 937 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY'} 938 | 939 | if data_type == 0: 940 | filepath = os.path.abspath(image_path) 941 | if not os.path.exists(filepath): 942 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS'} 943 | 944 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 945 | else: 946 | data["url"] = image_path 947 | 948 | r = {} 949 | try: 950 | r = requests.post(url, headers=headers, data = json.dumps(data)) 951 | if r.status_code != 200: 952 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':''} 953 | 954 | ret = r.json() 955 | except Exception as e: 956 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e)} 957 | 958 | return ret 959 | 960 | def creditcardocr(self, image_path, data_type = 0, seq = ''): 961 | 962 | req_type='creditcardocr' 963 | headers = self.get_headers(req_type) 964 | url = self.generate_res_url(req_type, 2) 965 | data = { 966 | "app_id": self._appid, 967 | "session_id": seq, 968 | } 969 | 970 | if len(image_path) == 0: 971 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY'} 972 | 973 | if data_type == 0: 974 | filepath = os.path.abspath(image_path) 975 | if not os.path.exists(filepath): 976 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS'} 977 | 978 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 979 | else: 980 | data["url"] = image_path 981 | 982 | r = {} 983 | try: 984 | r = requests.post(url, headers=headers, data = json.dumps(data)) 985 | if r.status_code != 200: 986 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':''} 987 | 988 | ret = r.json() 989 | except Exception as e: 990 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e)} 991 | 992 | return ret 993 | 994 | def bizlicenseocr(self, image_path, data_type = 0, seq = ''): 995 | 996 | req_type='bizlicenseocr' 997 | headers = self.get_headers(req_type) 998 | url = self.generate_res_url(req_type, 2) 999 | data = { 1000 | "app_id": self._appid, 1001 | "session_id": seq, 1002 | } 1003 | 1004 | if len(image_path) == 0: 1005 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY'} 1006 | 1007 | if data_type == 0: 1008 | filepath = os.path.abspath(image_path) 1009 | if not os.path.exists(filepath): 1010 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS'} 1011 | 1012 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 1013 | else: 1014 | data["url"] = image_path 1015 | 1016 | r = {} 1017 | try: 1018 | r = requests.post(url, headers=headers, data = json.dumps(data)) 1019 | if r.status_code != 200: 1020 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':''} 1021 | 1022 | ret = r.json() 1023 | except Exception as e: 1024 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e)} 1025 | 1026 | return ret 1027 | 1028 | def plateocr(self, image_path, data_type = 0, seq = ''): 1029 | 1030 | req_type='plateocr' 1031 | headers = self.get_headers(req_type) 1032 | url = self.generate_res_url(req_type, 2) 1033 | data = { 1034 | "app_id": self._appid, 1035 | "session_id": seq, 1036 | } 1037 | 1038 | if len(image_path) == 0: 1039 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY'} 1040 | 1041 | if data_type == 0: 1042 | filepath = os.path.abspath(image_path) 1043 | if not os.path.exists(filepath): 1044 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS'} 1045 | 1046 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 1047 | else: 1048 | data["url"] = image_path 1049 | 1050 | r = {} 1051 | try: 1052 | r = requests.post(url, headers=headers, data = json.dumps(data)) 1053 | if r.status_code != 200: 1054 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':''} 1055 | 1056 | ret = r.json() 1057 | except Exception as e: 1058 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e)} 1059 | 1060 | return ret 1061 | 1062 | def structureocr(self, image_path, data_type = 0, ocr_template = '', seq = ''): 1063 | 1064 | req_type='structureocr' 1065 | headers = self.get_headers(req_type) 1066 | url = self.generate_res_url(req_type, 2) 1067 | data = { 1068 | "app_id": self._appid, 1069 | "session_id": seq, 1070 | "ocr_template":ocr_template 1071 | } 1072 | 1073 | if len(image_path) == 0: 1074 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY'} 1075 | 1076 | if data_type == 0: 1077 | filepath = os.path.abspath(image_path) 1078 | if not os.path.exists(filepath): 1079 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS'} 1080 | 1081 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 1082 | else: 1083 | data["url"] = image_path 1084 | 1085 | r = {} 1086 | try: 1087 | r = requests.post(url, headers=headers, data = json.dumps(data)) 1088 | if r.status_code != 200: 1089 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':''} 1090 | 1091 | ret = r.json() 1092 | except Exception as e: 1093 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e)} 1094 | 1095 | return ret 1096 | 1097 | def passportocr(self, image_path, data_type = 0, ocr_template = '', seq = ''): 1098 | 1099 | req_type='passportocr' 1100 | headers = self.get_headers(req_type) 1101 | url = self.generate_res_url(req_type, 2) 1102 | data = { 1103 | "app_id": self._appid, 1104 | "session_id": seq, 1105 | "ocr_template": ocr_template 1106 | } 1107 | 1108 | if len(image_path) == 0: 1109 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY'} 1110 | 1111 | if data_type == 0: 1112 | filepath = os.path.abspath(image_path) 1113 | if not os.path.exists(filepath): 1114 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS'} 1115 | 1116 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 1117 | else: 1118 | data["url"] = image_path 1119 | 1120 | r = {} 1121 | try: 1122 | r = requests.post(url, headers=headers, data = json.dumps(data)) 1123 | if r.status_code != 200: 1124 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':''} 1125 | 1126 | ret = r.json() 1127 | except Exception as e: 1128 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e)} 1129 | 1130 | return ret 1131 | 1132 | def invoiceocr(self, image_path, data_type = 0, ocr_template='', seq = ''): 1133 | 1134 | req_type='invoiceocr' 1135 | headers = self.get_headers(req_type) 1136 | url = self.generate_res_url(req_type, 2) 1137 | data = { 1138 | "app_id": self._appid, 1139 | "session_id": seq, 1140 | "ocr_template": ocr_template 1141 | } 1142 | 1143 | if len(image_path) == 0: 1144 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY'} 1145 | 1146 | if data_type == 0: 1147 | filepath = os.path.abspath(image_path) 1148 | if not os.path.exists(filepath): 1149 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS'} 1150 | 1151 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 1152 | else: 1153 | data["url"] = image_path 1154 | 1155 | r = {} 1156 | try: 1157 | r = requests.post(url, headers=headers, data = json.dumps(data)) 1158 | if r.status_code != 200: 1159 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':''} 1160 | 1161 | ret = r.json() 1162 | except Exception as e: 1163 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e)} 1164 | 1165 | return ret 1166 | 1167 | def waybillocr(self, image_path, data_type = 0, seq = ''): 1168 | 1169 | req_type='waybillocr' 1170 | headers = self.get_headers(req_type) 1171 | url = self.generate_res_url(req_type, 2) 1172 | data = { 1173 | "app_id": self._appid, 1174 | "session_id": seq, 1175 | } 1176 | 1177 | if len(image_path) == 0: 1178 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY'} 1179 | 1180 | if data_type == 0: 1181 | filepath = os.path.abspath(image_path) 1182 | if not os.path.exists(filepath): 1183 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS'} 1184 | 1185 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 1186 | else: 1187 | data["url"] = image_path 1188 | 1189 | r = {} 1190 | try: 1191 | r = requests.post(url, headers=headers, data = json.dumps(data)) 1192 | if r.status_code != 200: 1193 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':''} 1194 | 1195 | ret = r.json() 1196 | except Exception as e: 1197 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e)} 1198 | 1199 | return ret 1200 | 1201 | def hpgeneralocr(self, image_path, data_type = 0, seq = ''): 1202 | 1203 | req_type='hpgeneralocr' 1204 | headers = self.get_headers(req_type) 1205 | url = self.generate_res_url(req_type, 2) 1206 | data = { 1207 | "app_id": self._appid, 1208 | "session_id": seq, 1209 | } 1210 | 1211 | if len(image_path) == 0: 1212 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY'} 1213 | 1214 | if data_type == 0: 1215 | filepath = os.path.abspath(image_path) 1216 | if not os.path.exists(filepath): 1217 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS'} 1218 | 1219 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 1220 | else: 1221 | data["url"] = image_path 1222 | 1223 | r = {} 1224 | try: 1225 | r = requests.post(url, headers=headers, data = json.dumps(data)) 1226 | if r.status_code != 200: 1227 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':''} 1228 | 1229 | ret = r.json() 1230 | except Exception as e: 1231 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e)} 1232 | 1233 | return ret 1234 | 1235 | def tableocr(self, image_path, data_type = 0, seq = ''): 1236 | 1237 | req_type='tableocr' 1238 | headers = self.get_headers(req_type) 1239 | url = self.generate_res_url(req_type, 2) 1240 | data = { 1241 | "app_id": self._appid, 1242 | "session_id": seq, 1243 | } 1244 | 1245 | if len(image_path) == 0: 1246 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY'} 1247 | 1248 | if data_type == 0: 1249 | filepath = os.path.abspath(image_path) 1250 | if not os.path.exists(filepath): 1251 | return {'httpcode':0, 'errorcode':self.IMAGE_FILE_NOT_EXISTS, 'errormsg':'IMAGE_FILE_NOT_EXISTS'} 1252 | 1253 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 1254 | else: 1255 | data["url"] = image_path 1256 | 1257 | r = {} 1258 | try: 1259 | r = requests.post(url, headers=headers, data = json.dumps(data)) 1260 | if r.status_code != 200: 1261 | return {'httpcode':r.status_code, 'errorcode':'', 'errormsg':''} 1262 | 1263 | ret = r.json() 1264 | except Exception as e: 1265 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e)} 1266 | 1267 | return ret 1268 | 1269 | def arithmeticocr(self, image_path, data_type=0, seq=''): 1270 | 1271 | req_type = 'arithmeticocr' 1272 | headers = self.get_headers(req_type) 1273 | url = self.generate_res_url(req_type, 2) 1274 | data = { 1275 | "app_id": self._appid, 1276 | "session_id": seq, 1277 | } 1278 | 1279 | if len(image_path) == 0: 1280 | return {'httpcode': 0, 'errorcode': self.IMAGE_PATH_EMPTY, 'errormsg': 'IMAGE_PATH_EMPTY'} 1281 | 1282 | if data_type == 0: 1283 | filepath = os.path.abspath(image_path) 1284 | if not os.path.exists(filepath): 1285 | return {'httpcode': 0, 'errorcode': self.IMAGE_FILE_NOT_EXISTS, 'errormsg': 'IMAGE_FILE_NOT_EXISTS'} 1286 | 1287 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 1288 | else: 1289 | data["url"] = image_path 1290 | 1291 | r = {} 1292 | try: 1293 | r = requests.post(url, headers=headers, data=json.dumps(data)) 1294 | if r.status_code != 200: 1295 | return {'httpcode': r.status_code, 'errorcode': '', 'errormsg': ''} 1296 | 1297 | ret = r.json() 1298 | except Exception as e: 1299 | return {'httpcode': 0, 'errorcode': self.IMAGE_NETWORK_ERROR, 'errormsg': str(e)} 1300 | 1301 | return ret 1302 | 1303 | def finanocr(self, image_path, ocr_template='', data_type=0, seq=''): 1304 | 1305 | req_type = 'finanocr' 1306 | headers = self.get_headers(req_type) 1307 | url = self.generate_res_url(req_type, 2) 1308 | data = { 1309 | "app_id": self._appid, 1310 | "session_id": seq, 1311 | "ocr_template": ocr_template 1312 | } 1313 | 1314 | if len(image_path) == 0: 1315 | return {'httpcode': 0, 'errorcode': self.IMAGE_PATH_EMPTY, 'errormsg': 'IMAGE_PATH_EMPTY'} 1316 | 1317 | if data_type == 0: 1318 | filepath = os.path.abspath(image_path) 1319 | if not os.path.exists(filepath): 1320 | return {'httpcode': 0, 'errorcode': self.IMAGE_FILE_NOT_EXISTS, 'errormsg': 'IMAGE_FILE_NOT_EXISTS'} 1321 | 1322 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 1323 | else: 1324 | data["url"] = image_path 1325 | 1326 | r = {} 1327 | try: 1328 | r = requests.post(url, headers=headers, data=json.dumps(data)) 1329 | if r.status_code != 200: 1330 | return {'httpcode': r.status_code, 'errorcode': '', 'errormsg': ''} 1331 | 1332 | ret = r.json() 1333 | except Exception as e: 1334 | return {'httpcode': 0, 'errorcode': self.IMAGE_NETWORK_ERROR, 'errormsg': str(e)} 1335 | 1336 | return ret 1337 | 1338 | def vinocr(self, image_path, data_type=0, seq=''): 1339 | 1340 | req_type = 'vinocr' 1341 | headers = self.get_headers(req_type) 1342 | url = self.generate_res_url(req_type, 2) 1343 | data = { 1344 | "app_id": self._appid, 1345 | "session_id": seq, 1346 | } 1347 | 1348 | if len(image_path) == 0: 1349 | return {'httpcode': 0, 'errorcode': self.IMAGE_PATH_EMPTY, 'errormsg': 'IMAGE_PATH_EMPTY'} 1350 | 1351 | if data_type == 0: 1352 | filepath = os.path.abspath(image_path) 1353 | if not os.path.exists(filepath): 1354 | return {'httpcode': 0, 'errorcode': self.IMAGE_FILE_NOT_EXISTS, 'errormsg': 'IMAGE_FILE_NOT_EXISTS'} 1355 | 1356 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 1357 | else: 1358 | data["url"] = image_path 1359 | 1360 | r = {} 1361 | try: 1362 | print "headers=",headers 1363 | print "url=",url 1364 | r = requests.post(url, headers=headers, data=json.dumps(data)) 1365 | if r.status_code != 200: 1366 | return {'httpcode': r.status_code, 'errorcode': '', 'errormsg': ''} 1367 | 1368 | ret = r.json() 1369 | except Exception as e: 1370 | return {'httpcode': 0, 'errorcode': self.IMAGE_NETWORK_ERROR, 'errormsg': str(e)} 1371 | 1372 | return ret 1373 | 1374 | def handwritingocr(self, image_path, data_type=0, seq=''): 1375 | 1376 | req_type = 'handwritingocr' 1377 | headers = self.get_headers(req_type) 1378 | url = self.generate_res_url(req_type, 2) 1379 | data = { 1380 | "app_id": self._appid, 1381 | "session_id": seq, 1382 | } 1383 | 1384 | if len(image_path) == 0: 1385 | return {'httpcode': 0, 'errorcode': self.IMAGE_PATH_EMPTY, 'errormsg': 'IMAGE_PATH_EMPTY'} 1386 | 1387 | if data_type == 0: 1388 | filepath = os.path.abspath(image_path) 1389 | if not os.path.exists(filepath): 1390 | return {'httpcode': 0, 'errorcode': self.IMAGE_FILE_NOT_EXISTS, 'errormsg': 'IMAGE_FILE_NOT_EXISTS'} 1391 | 1392 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 1393 | else: 1394 | data["url"] = image_path 1395 | 1396 | r = {} 1397 | try: 1398 | r = requests.post(url, headers=headers, data=json.dumps(data)) 1399 | if r.status_code != 200: 1400 | return {'httpcode': r.status_code, 'errorcode': '', 'errormsg': ''} 1401 | 1402 | ret = r.json() 1403 | except Exception as e: 1404 | return {'httpcode': 0, 'errorcode': self.IMAGE_NETWORK_ERROR, 'errormsg': str(e)} 1405 | 1406 | return ret 1407 | 1408 | def ehocr(self, image_path, data_type=0, seq=''): 1409 | 1410 | req_type = 'ehocr' 1411 | headers = self.get_headers(req_type) 1412 | url = self.generate_res_url(req_type, 2) 1413 | data = { 1414 | "app_id": self._appid, 1415 | "session_id": seq, 1416 | } 1417 | 1418 | if len(image_path) == 0: 1419 | return {'httpcode': 0, 'errorcode': self.IMAGE_PATH_EMPTY, 'errormsg': 'IMAGE_PATH_EMPTY'} 1420 | 1421 | if data_type == 0: 1422 | filepath = os.path.abspath(image_path) 1423 | if not os.path.exists(filepath): 1424 | return {'httpcode': 0, 'errorcode': self.IMAGE_FILE_NOT_EXISTS, 'errormsg': 'IMAGE_FILE_NOT_EXISTS'} 1425 | 1426 | data["image"] = base64.b64encode(open(filepath, 'rb').read()).rstrip().decode('utf-8') 1427 | else: 1428 | data["url"] = image_path 1429 | 1430 | r = {} 1431 | try: 1432 | r = requests.post(url, headers=headers, data=json.dumps(data)) 1433 | if r.status_code != 200: 1434 | return {'httpcode': r.status_code, 'errorcode': '', 'errormsg': ''} 1435 | 1436 | ret = r.json() 1437 | except Exception as e: 1438 | return {'httpcode': 0, 'errorcode': self.IMAGE_NETWORK_ERROR, 'errormsg': str(e)} 1439 | 1440 | return ret 1441 | 1442 | def livegetfour(self, seq = ''): 1443 | 1444 | req_type = 'livegetfour' 1445 | headers = self.get_headers(req_type) 1446 | url = self.generate_res_url(req_type, 3) 1447 | 1448 | data = { 1449 | 'app_id' : self._appid, 1450 | 'seq' : seq 1451 | } 1452 | 1453 | r = {} 1454 | try: 1455 | r = requests.post(url, headers = headers, data = json.dumps(data)) 1456 | if r.status_code != 200: 1457 | return {'httpcode' : r.status_code, 'errorcode' : '', 'errormsg' : '', 'validate_data' : ''} 1458 | ret = r.json() 1459 | except Exception as e: 1460 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e), 'validate_data' : ''} 1461 | 1462 | return ret 1463 | 1464 | def livedetectfour(self, validate_data, video_path, seq = '', card_path = '', compare_flag = False): 1465 | 1466 | req_type = 'livedetectfour' 1467 | headers = self.get_headers(req_type) 1468 | url = self.generate_res_url(req_type, 3) 1469 | 1470 | data = { 1471 | 'app_id' : self._appid, 1472 | 'seq' : seq 1473 | } 1474 | 1475 | if len(validate_data) == 0: 1476 | return {'httpcode' : 0, 'errorcode' : self.VALIDATE_DATA_EMPTY, 'errormsg' : 'VALIDATE_DATA_EMPTY', 'live_status' : '', 'live_msg' : '', 'compare_status' : '', 'compare_msg' : '', 'sim' : 0, 'photo' : ''} 1477 | 1478 | if len(video_path) == 0: 1479 | return {'httpcode' : 0, 'errorcode' : self.VIDEO_PATH_EMPTY, 'errormsg' : 'VIDEO_PATH_EMPTY,', 'live_status' : '', 'live_msg' : '', 'compare_status' : '', 'compare_msg' : '', 'sim' : 0, 'photo' : ''} 1480 | 1481 | if compare_flag == True and len(card_path) == 0: 1482 | return {'httpcode' : 0, 'errorcode' : self.CARD_PATH_EMPTY, 'errormsg' : 'CARD_PATH_EMPTY', 'live_status' : '', 'live_msg' : '', 'compare_status' : '', 'compare_msg' : '', 'sim' : 0, 'photo' : ''} 1483 | 1484 | videofile = os.path.abspath(video_path) 1485 | if not os.path.exists(videofile): 1486 | return {'httpcode' : 0, 'errorcode' : self.VIDEO_FILE_NOT_EXISTS, 'errormsg' : 'VIDEO_FILE_NOT_EXISTS', 'live_status' : '', 'live_msg' : '', 'compare_status' : '', 'compare_msg' : '', 'sim' : 0, 'photo' : ''} 1487 | else: 1488 | data["video"] = base64.b64encode(open(videofile, 'rb').read()).rstrip() 1489 | 1490 | 1491 | cardfile = os.path.abspath(card_path) 1492 | if compare_flag == True : 1493 | if not os.path.exists(cardfile): 1494 | return {'httpcode' : 0, 'errorcode' : self.CARD_FILE_NOT_EXISTS, 'errormsg' : 'CARD_FILE_NOT_EXISTS', 'live_status' : '', 'live_msg' : '', 'compare_status' : '', 'compare_msg' : '', 'sim' : 0, 'photo' : ''} 1495 | else: 1496 | data["card"] = base64.b64encode(open(cardfile, 'rb').read()).rstrip() 1497 | 1498 | data['validate_data'] = validate_data 1499 | data['compare_flag'] = compare_flag 1500 | 1501 | r = {} 1502 | try: 1503 | r = requests.post(url, headers = headers, data = json.dumps(data)) 1504 | if r.status_code != 200: 1505 | return {'httpcode' : r.status_code, 'errorcode' : '', 'errormsg' : '', 'live_status' : '', 'live_msg' : '', 'compare_status' : '', 'compare_msg' : '', 'sim' : 0, 'photo' : ''} 1506 | ret = r.json() 1507 | except Exception as e: 1508 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e), 'live_status' : '', 'live_msg' : '', 'compare_status' : '', 'compare_msg' : '', 'sim' : 0, 'photo' : ''} 1509 | 1510 | return ret 1511 | 1512 | def idcardlivedetectfour(self, idcard_number, idcard_name, validate_data, video_path, seq = ''): 1513 | 1514 | req_type = 'idcardlivedetectfour' 1515 | headers = self.get_headers(req_type) 1516 | url = self.generate_res_url(req_type, 3) 1517 | 1518 | data = { 1519 | 'app_id' : self._appid, 1520 | 'seq' : seq 1521 | } 1522 | 1523 | if len(idcard_name) == 0 or len(idcard_number) == 0: 1524 | return {'httpcode' : 0, 'errorcode' : self.IDCARD_NAME_OR_ID_EMPTY , 'errormsg' : 'IDCARD_NAME_OR_ID_EMPTY ', 'live_status' : '', 'live_msg' : '', 'compare_status' : '', 'compare_msg' : '', 'sim' : 0, 'video_photo' : ''} 1525 | 1526 | if len(validate_data) == 0: 1527 | return {'httpcode' : 0, 'errorcode' : self.VALIDATE_DATA_EMPTY, 'errormsg' : 'VALIDATE_DATA_EMPTY', 'live_status' : '', 'live_msg' : '', 'compare_status' : '', 'compare_msg' : '', 'sim' : 0, 'video_photo' : ''} 1528 | 1529 | if len(video_path) == 0: 1530 | return {'httpcode' : 0, 'errorcode' : self.VIDEO_PATH_EMPTY, 'errormsg' : 'VIDEO_PATH_EMPTY', 'live_status' : '', 'live_msg' : '', 'compare_status' : '', 'compare_msg' : '', 'sim' : 0, 'video_photo' : ''} 1531 | 1532 | videofile = os.path.abspath(video_path) 1533 | if not os.path.exists(videofile): 1534 | return {'httpcode' : 0, 'errorcode' : self.VIDEO_FILE_NOT_EXISTS, 'errormsg' : 'VIDEO_FILE_NOT_EXISTS', 'live_status' : '', 'live_msg' : '', 'compare_status' : '', 'compare_msg' : '', 'sim' : 0, 'video_photo' : ''} 1535 | else: 1536 | data["video"] = base64.b64encode(open(videofile, 'rb').read()).rstrip() 1537 | 1538 | data['idcard_number'] = idcard_number 1539 | data['idcard_name'] = idcard_name 1540 | data['validate_data'] = validate_data 1541 | 1542 | r = {} 1543 | try: 1544 | r = requests.post(url, headers = headers, data = json.dumps(data)) 1545 | if r.status_code != 200 : 1546 | return {'httpcode' : r.status_code, 'errorcode' : '', 'errormsg' : '', 'live_status' : '', 'live_msg' : '', 'compare_status' : '', 'compare_msg' : '', 'sim' : 0, 'video_photo' : ''} 1547 | ret = r.json() 1548 | except Exception as e: 1549 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e), 'live_status' : '', 'live_msg' : '', 'compare_status' : '', 'compare_msg' : '', 'sim' : 0, 'video_photo' : ''} 1550 | 1551 | return ret 1552 | 1553 | def idcardfacecompare(self, idcard_number, idcard_name, image_path, data_type = 0 , session_id = ''): 1554 | 1555 | req_type = 'idcardfacecompare' 1556 | headers = self.get_headers(req_type) 1557 | url = self.generate_res_url(req_type, 3) 1558 | 1559 | data = { 1560 | 'app_id' : self._appid, 1561 | 'session_id' : session_id 1562 | } 1563 | 1564 | if len(idcard_name) == 0 or len(idcard_number) == 0 : 1565 | return {'httpcode' : 0, 'errorcode' : self.IDCARD_NAME_OR_ID_EMPTY , 'errormsg' : 'IDCARD_NAME_OR_ID_EMPTY ', 'similarity' : '', 'session_id' : session_id} 1566 | 1567 | if len(image_path) == 0 : 1568 | return {'httpcode':0, 'errorcode':self.IMAGE_PATH_EMPTY, 'errormsg':'IMAGE_PATH_EMPTY', 'similarity' : '', 'session_id' : session_id} 1569 | 1570 | if data_type == 0: 1571 | imagefile = os.path.abspath(image_path) 1572 | if not os.path.exists(imagefile): 1573 | return {'httpcode' : 0, 'errorcode' : self.IMAGE_FILE_NOT_EXISTS, 'errormsg' : 'IMAGE_FILE_NOT_EXISTS', 'similarity' : '', 'session_id' : session_id} 1574 | else: 1575 | data['image'] = base64.b64encode(open(imagefile, 'rb').read()).rstrip() 1576 | else: 1577 | data['url'] = image_path 1578 | data['idcard_number'] = idcard_number 1579 | data['idcard_name'] = idcard_name 1580 | 1581 | r = {} 1582 | try: 1583 | r = requests.post(url, headers = headers, data = json.dumps(data)) 1584 | if r.status_code != 200: 1585 | return {'httpcode' : r.status_code, 'errorcode' : '', 'errormsg' : '', 'similarity' : '', 'session_id' : session_id} 1586 | ret = r.json() 1587 | except Exception as e: 1588 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e), 'similarity' : '', 'session_id' : session_id} 1589 | 1590 | return ret 1591 | 1592 | def ValidateIdcard(self, idcard_number, idcard_name, seq = "default"): 1593 | 1594 | req_type = 'validateidcard' 1595 | headers = self.get_headers(req_type) 1596 | url = self.generate_res_url(req_type, 3) 1597 | 1598 | data = { 1599 | "app_id": self._appid, 1600 | "idcard_number": idcard_number, 1601 | "idcard_name": idcard_name, 1602 | "seq": seq 1603 | } 1604 | 1605 | r = {} 1606 | try: 1607 | r = requests.post(url, headers=headers, data = json.dumps(data)) 1608 | if r.status_code != 200: 1609 | return {'httpcode':r.status_code, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':'', 'session_id':''} 1610 | ret = r.json() 1611 | 1612 | except Exception as e: 1613 | return {'httpcode':0, 'errorcode':self.IMAGE_NETWORK_ERROR, 'errormsg':str(e), 'session_id':''} 1614 | 1615 | return ret 1616 | 1617 | 1618 | -------------------------------------------------------------------------------- /sample.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import time 4 | import TencentYoutuyun 5 | 6 | # pip install requests 7 | # please get these values from http://open.youtu.qq.com 8 | appid = 'xxxxxx' 9 | secret_id = 'xxxxxx' 10 | secret_key = 'xxxxxx' 11 | userid = 'xxxxxx' 12 | 13 | #choose a end_point 14 | #end_point = TencentYoutuyun.conf.API_TENCENTYUN_END_POINT 15 | #end_point = TencentYoutuyun.conf.API_YOUTU_VIP_END_POINT 16 | end_point = TencentYoutuyun.conf.API_YOUTU_END_POINT 17 | 18 | youtu = TencentYoutuyun.YouTu(appid, secret_id, secret_key, userid, end_point) 19 | session_id = "" 20 | 21 | 22 | #for TencentYoutuyun.conf.API_YOUTU_VIP_END_POINT end_point 23 | #get four-character idioms 24 | #retlivegetfour = youtu.livegetfour(session_id) 25 | #print retlivegetfour 26 | 27 | #four-character live detect without face compare 28 | #retlivedetectfour = youtu.livedetectfour('1122', 'xxx.mp4', session_id) 29 | #print retlivedetectfour 30 | 31 | #four-character live detect with face compare 32 | #retlivedetectfour= youtu.livedetectfour('1122', 'xxx.mp4', session_id, 'xxx.jpg', True) 33 | #print retlivedetectfour 34 | 35 | #four-character idcard live detect 36 | #retidcardlivedetectfour = youtu.idcardlivedetectfour('123456789987654321', '张三', '1122', 'xxx.mp4', session_id ) 37 | #print retidcardlivedetectfour 38 | 39 | #idcard face compare: use local image compare with id card image 40 | #retidcardfacecompare = youtu.idcardfacecompare('123456789987654321', '张三', 'xxx.jpg', 0, session_id) 41 | #print retidcardfacecompare 42 | 43 | #idcard face compare :use url image compare with id card image 44 | #retidcardfacecompare = youtu.idcardfacecompare('123456789987654321', '张三', 'http://xxx.png', 1, session_id) 45 | #print retidcardfacecompare 46 | 47 | # face compare : use two local image to compare 48 | #retfacecompare = youtu.FaceCompare('xxx.jpg', 'xxx.jpg') 49 | #print retfacecompare 50 | 51 | # face compare : use two url image to compare 52 | #retfacecompare = youtu.FaceCompare('http://xxx.png', 'http://xxx.png', 1) 53 | #print retfacecompare 54 | 55 | #id card ocr: use local id card image 56 | retidcardocr = youtu.idcardocr('./idcard_1.jpg', data_type = 0, card_type = 2) 57 | print retidcardocr 58 | 59 | #id card ocr: use url id card image 60 | #retidcardocr = youtu.idcardocr('http://xxx.jpg', data_type = 1, card_type = 0) 61 | #print retidcardocr 62 | 63 | #driver license ocr: use local image 64 | #retdriverlicenseocr = youtu.driverlicenseocr('dlocrattach.jpg', data_type = 0, proc_type = 2) 65 | #print retdriverlicenseocr 66 | 67 | #business card ocr: use local image 68 | #retbcocr = youtu.bcocr('blocr.jpg', data_type = 0) 69 | #print retbcocr 70 | 71 | #general ocr: use local image 72 | #retgeneralocr = youtu.generalocr('ge.jpg', data_type = 0) 73 | #print retgeneralocr 74 | 75 | #creditcard ocr: use local image 76 | #retcreditcardocr = youtu.creditcardocr('ccard.jpg', data_type = 0) 77 | #print retcreditcardocr 78 | 79 | #bizlicense ocr: use local image 80 | #bizlicenseocr = youtu.bizlicenseocr('bzocr.jpg', data_type = 0) 81 | #print bizlicenseocr 82 | 83 | #passport ocr: use local image 84 | #passportocr = youtu.passportocr('passport.jpg', data_type = 0, ocr_template='PassPort') 85 | #print passportocr 86 | 87 | #structure ocr: use local image 88 | #structureocr = youtu.structureocr('vat.jpg', data_type = 0, ocr_template='VAT') 89 | #print structureocr 90 | 91 | #invoice ocr: use local image 92 | #invoiceocr = youtu.invoiceocr('vat.jpg', data_type = 0, ocr_template='VAT') 93 | #print invoiceocr 94 | 95 | #waybill ocr: use local image 96 | #waybillocr = youtu.waybillocr('waybill.jpg', data_type = 0) 97 | #print waybillocr 98 | 99 | #hpgeneral ocr: use local image 100 | #hpgeneralocr = youtu.hpgeneralocr('ge.jpg', data_type = 0) 101 | #print hpgeneralocr 102 | 103 | #table ocr: use local image 104 | #tableocr = youtu.tableocr('table.png', data_type = 0) 105 | #print tableocr 106 | 107 | #arithmetic ocr: use local image 108 | #arithmeticocr = youtu.arithmeticocr('arithmatic.jpg', data_type = 0) 109 | #print arithmeticocr 110 | 111 | #finan ocr: use local image 112 | #financocr = youtu.finanocr('jzd.jpg', data_type = 0, ocr_template='BankInSlip') 113 | #print financocr 114 | 115 | #vin ocr: use local image 116 | #vinocr = youtu.vinocr('vin.png', data_type = 0) 117 | #print vinocr 118 | 119 | #handwriting ocr: use local image 120 | #handwritingocr = youtu.handwritingocr('hw.jpg', data_type = 0) 121 | #print handwritingocr 122 | 123 | #eh ocr: use local image 124 | #ehocr = youtu.ehocr('eh.jpg', data_type = 0) 125 | #print ehocr 126 | 127 | #id card validate: validate the idcard is correct 128 | #retvalidateidcard = youtu.ValidateIdcard('123456789987654321', '张三', session_id) 129 | #print retvalidateidcard 130 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | from setuptools import setup, find_packages 5 | 6 | setup( 7 | name = 'TencentYoutuyun', 8 | version = '1.0.0', 9 | keywords = ('TencentYoutuyun', 'qcloud'), 10 | description = 'python sdk for open.youtu.qq.com', 11 | license = 'MIT License', 12 | install_requires=['requests'], 13 | packages = find_packages(), 14 | platforms = 'any', 15 | ) --------------------------------------------------------------------------------