python3下实现搜狗AI API的代码示例


Posted in Python onApril 10, 2018

1、背景

a、搜狗也发布了自己的人工智能 api,包括身份证ocr、名片ocr、文本翻译等API,初试感觉准确率一般般。

b、基于python3。

c、也有自己的签名生成这块,有了鹅厂的底子,相对写起来比较简单。

d、不过Sougou明显在接口标准化这块明显不如鹅厂,不同api应答包的主体结构竟然不一致,所以实施也只做了简单的结构化……

2、实现代码

直接放代码吧,github上也有: https://github.com/jdstkxx/PySougouAI

1、sogouai-example.py

# -*- coding: utf-8 -*-

'''
create by : joshua zou
create date : 2018.4.9
Purpose: check sougou ai api
'''

import glob,os
from SougouAPIMsg import *

#改成你自己搜狗AI的APPID、APPKEY、SecretKey
AppID = '0000'
ApiKey = '*********'
SecretKey= '0PLvS-AHShmq**************'

if __name__ == "__main__":
  sg = SougouAPIMsg(AppID,ApiKey,SecretKey)
  for file in glob.glob('D:\python\*.jpg'):
    filename=os.path.split(file)[1].split('.')[0]
    #调用ocr识别
    apiname = 'ocr'
    rest =sg.apiSougouOcr(apiname,file)
    #调用身份证识别
    #rest =sg.apiSougouOcr('idcard',file)
    

    js= rest.json()
    retext =""
    if apiname=='ocr':
      #文字识别,rest应答包,字符串
      #成功 {"result":[{"content":"01245177\n","frame":["0,0","207,0","207,59","0,59"]}],"success":1}
      #失败 {"success":0}      
      if js['success']==1 :
        retext = js['result'][0]['content'].strip()         
    elif apiname == 'idcard':
      #身份证识别应答包,逼死强迫症啊,请求结构,应答结构都不一样
      '''
      {
      "result": {
      "住址": "xxxxxx",
      "公民身份号码": "11001xxx30",
      "出生": "19900101",
      "姓名": "xxXX",
      "性别": "X",
      "民族": "xxx"
      },
      "status": 0,
      "statusText": "Success"
      }
      '''
      if js['status']==0 :
        retext = js['result']['公民身份号码'].strip()      
    print(filename,retext)

2、SougouAPI.py

# -*- coding: utf-8 -*-
# 搜狗API字典

SougouAPI={
  #基本文本分析API
  "ocr":  {
    'APINAME':'图像识别', #API中文简称
    'APIDESC': '识别图像中的文字', #API描述
    'APIURL': 'http://api.ai.sogou.com/pub/ocr' #API请求URL
  },
  "idcard":{
    'APINAME':'身份证识别', #API中文简称
    'APIDESC': '身份证识别', #API描述
    'APIURL': 'http://api.ai.sogou.com/pub/ocr/idcard' #API请求URL
  },
  
}

3、SougouAPIMsg.py

# -*- coding: utf-8 -*-

'''
create by : joshua zou
create date : 2018.4.9
Purpose: check sougou ai api
'''

import requests
import base64
import hashlib
import hmac
import time
from urllib import parse
import json
from SougouAPI import *

class SougouAPIMsg(object):
  def __init__(self,AppID=None,ApiKey=None,SecretKey=None):
    if not AppID: AppID = '88888'
    if not ApiKey: ApiKey = '5ADwS88888888Dtr6QG2'
    if not SecretKey: SecretKey= '0PLvS-AH8888888889n6NF6fVVTt7m'
    self.__app_id= AppID 
    self.__app_key= ApiKey 
    self.__app_secret= SecretKey 

  
  def get_time_stamp(self):
    return str(int(time.time()))
  
  '''
  1、应用相关前缀 {AuthPrefix}
  {AuthPrefix}=sac-auth-v1/{accessKey}/{secondsSinceEpoch}/{expirationPeriodInSeconds}
  2、请求相关数据 {Data}    
  {Data}={REQUEST_METHOD} + "\n" + {HOST} + "\n" + {URI} + "\n" + {SORTED_QUERY_STRING}
  其中,REQUEST_METHOD 为请求使用的 HTTP 方法, 如: GET|POST|PUT|DELETE
  HOST 为服务使用的域名, 如: api.ai.sogou.com
  URI 为请求的服务路径, 如: /speech/asr
  SORTED_QUERY_STRING 把 URL 中的 Query String(即 URL 中 “?” 后面的 “k1=v1&k2=v2” 字符串)进行编码后的结果。    
  编码方法为:
  将 Query String 根据 & 拆开成若干项,对每一项转换为 UriEncode(key) + "=" + UriEncode(value) 的形式, 其中 value 可以是空字符串
  将上面转换后的所有字符串按照字典顺序排序。
  将排序后的字符串按顺序用 & 符号链接起来。
  3、生成签名 {Signature}    
   {Signature}=HMAC-SHA256-BASE64({secretKey}, {AuthPrefix} + "\n" + {Data})
  4、生成认证信息, 通过 Authorization header 传递    
   Authorization: {AuthPrefix}/{Signature}
   Example:
   1\应用 accessKey/secretKey 分别为 bTkALtTB9x6GAxmFi9wetAGH / PMROwlieALT36qfdGClVz2iH4Sv8xZxe
    POST 方式访问 http://api.ai.sogou.com/speech/asr 接口
    GET 参数为 type=gbk&idx=1&starttime=1491810516
    当前系统时间为 1491810516
   2\计算过程     
    {AuthPrefix}="sac-auth-v1/bTkALtTB9x6GAxmFi9wetAGH/1491810516/3600"
    {Data}="POST\napi.ai.sogou.com\n/speech/asr\nidx=1&starttime=1491810516&type=gbk"
    {Signature}=HMAC-SHA256-BASE64("PMROwlieALT36qfdGClVz2iH4Sv8xZxe", {AuthPrefix} + "\n" + {Data})="vuVEkzcnUeFv8FxeWS50c7S0HaYH1QKgtIV5xrxDY/s="
   3\最终生成的 header 为
    Authorization: sac-auth-v1/bTkALtTB9x6GAxmFi9wetAGH/1491810516/3600/vuVEkzcnUeFv8FxeWS50c7S0HaYH1QKgtIV5xrxDY/s=
  '''
  def get_auth_sign_str(self,url,method):
    res= parse.urlparse(url)
    host= res.netloc
    uri = res.path
    query= res.query
        
    
    #1生成前置字符串
    authprefix= 'sac-auth-v1/%s/%s/%s' %(self.__app_key,self.get_time_stamp(),3600)
    #2生成data
    query=dict( (k, v if len(v)>1 else v[0] )
            for k, v in parse.parse_qs(res.query).items() )     
    sort_dict= sorted(query.items(), key=lambda item:item[0], reverse = False)
    sortquerystr= parse.urlencode(sort_dict)
    data= '%s\n%s\n%s\n%s' %(method,host,uri,sortquerystr)
    #3生成signstr
    signstr ='%s\n%s' %(authprefix,data)
    #调用hamc.sha256
    shastr =hmac.new(self.__app_secret.encode(), signstr.encode(), digestmod=hashlib.sha256).digest()
    #base64编码,还原成字符串
    signature = base64.b64encode(shastr).decode()
    
    #4组合成最终的授权码
    authstr= '%s/%s' %(authprefix,signature)
    return authstr

  '''
  $file = "OCR-test03.jpg";
  $url = "http://api.ai.sogou.com/pub/ocr";
  
  $hdr = array(
    "Content-Type: multipart/form-data",
    "Authorization: ".sign($ak, $sk, $url, "POST")
  ); // cURL headers for file uploading
  
  $postfields = array(
    "pic" => curl_file_create($file,'image/jpeg','a_b_c.jpg'),
  );
  
  $ch = curl_init();
  $options = array(
    CURLOPT_URL => $url,
    CURLOPT_HEADER => false,
    CURLOPT_POST => 1,
    CURLOPT_HTTPHEADER => $hdr,
    CURLOPT_POSTFIELDS => $postfields,
    CURLOPT_RETURNTRANSFER => true
  );
  '''
  def apiSougouOcr(self,apiname,picfilename):
    url = SougouAPI[apiname]['APIURL']
    name = SougouAPI[apiname]['APINAME']
    desc= SougouAPI[apiname]['APIDESC']
    
    authstr=self.get_auth_sign_str(url, method='POST')
    header={ "Authorization": authstr }
    
    picfile= {'pic':open(picfilename,'rb')}
    
    resp = requests.post(url,headers=header,files=picfile)      
    #print (resp.text)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python使用urllib2实现发送带cookie的请求
Apr 28 Python
python enumerate函数的使用方法总结
Nov 15 Python
在Python中Dataframe通过print输出多行时显示省略号的实例
Dec 22 Python
python sort、sort_index方法代码实例
Mar 28 Python
python使用pygame模块实现坦克大战游戏
Mar 25 Python
django组合搜索实现过程详解(附代码)
Aug 06 Python
python生成特定分布数的实例
Dec 05 Python
基于pytorch 预训练的词向量用法详解
Jan 06 Python
Python 实现向word(docx)中输出
Feb 13 Python
使用python 计算百分位数实现数据分箱代码
Mar 03 Python
Python IDLE或shell中切换路径的操作
Mar 09 Python
Python加载数据的5种不同方式(收藏)
Nov 13 Python
Python基于pycrypto实现的AES加密和解密算法示例
Apr 10 #Python
浅谈Pandas中map, applymap and apply的区别
Apr 10 #Python
对pandas中apply函数的用法详解
Apr 10 #Python
Python 25行代码实现的RSA算法详解
Apr 10 #Python
使用pandas中的DataFrame数据绘制柱状图的方法
Apr 10 #Python
Python基于socket模块实现UDP通信功能示例
Apr 10 #Python
pandas把dataframe转成Series,改变列中值的类型方法
Apr 10 #Python
You might like
深入理解:XML与对象的序列化与反序列化
2013/06/08 PHP
PHP修改session_id示例代码
2014/01/08 PHP
利用yahoo汇率接口实现实时汇率转换示例 汇率转换器
2014/01/14 PHP
ThinkPHP视图查询详解
2014/06/30 PHP
php实现的发送带附件邮件类实例
2014/09/22 PHP
PHP实现Soap通讯的方法
2014/11/03 PHP
php生成txt文件实例代码介绍
2016/04/28 PHP
Aster vs KG BO3 第二场2.19
2021/03/10 DOTA
JQuery Highcharts 动态生成图表的方法
2013/11/15 Javascript
jquery操作下拉列表、文本框、复选框、单选框集合(收藏)
2014/01/08 Javascript
JS合并数组的几种方法及优劣比较
2014/09/19 Javascript
jQuery检查事件是否触发的方法
2015/06/26 Javascript
高效的jquery数字滚动特效
2015/12/17 Javascript
jQuery制作input提示内容(兼容IE8以上)
2017/07/05 jQuery
nodeJs实现基于连接池连接mysql的方法示例
2018/02/10 NodeJs
jQuery动态添加li标签并添加属性和绑定事件方法
2018/02/24 jQuery
微信小程序实现图片滚动效果示例
2018/12/05 Javascript
JS代码检查工具ESLint介绍与使用方法
2020/02/04 Javascript
解决vue init webpack 下载依赖卡住不动的问题
2020/11/09 Javascript
[02:36]DOTA2英雄基础教程 斯拉克
2013/11/29 DOTA
[03:58]兄弟们,回来开黑了!DOTA2昔日战友招募宣传视频
2016/07/17 DOTA
十个Python程序员易犯的错误
2015/12/15 Python
Python+tkinter使用40行代码实现计算器功能
2018/01/30 Python
python3写的简单本地文件上传服务器实例
2018/06/04 Python
Django中信号signals的简单使用方法
2019/07/04 Python
python 默认参数相关知识详解
2019/09/18 Python
Python算法中的时间复杂度问题
2019/11/19 Python
Django模板之基本的 for 循环 和 List内容的显示方式
2020/03/31 Python
Python bisect模块原理及常见实例
2020/06/17 Python
python实现npy格式文件转换为txt文件操作
2020/07/01 Python
Python使用pickle进行序列化和反序列化的示例代码
2020/09/22 Python
HTML5+JS实现俄罗斯方块原理及具体步骤
2013/11/29 HTML / CSS
电子信息专业学生自荐信
2013/11/09 职场文书
nginx服务器的下载安装与使用详解
2021/08/02 Servers
Vue的生命周期一起来看看
2022/02/24 Vue.js
Ubuntu安装Mysql+启用远程连接的完整过程
2022/06/21 Servers