This time I will bring you the method of calling Sogou AI API under python3. What are the precautions for calling Sogou AI API under python3. The following is a practical case, let's take a look.
1. Background
a. Sogou has also released its own artificial intelligence API, including ID card OCR, business card OCR, text translation and other APIs. The initial test feels that the accuracy is so so.
b, based on python3.
c. It also has its own signature generation. With the foundation of Goose Factory, it is relatively simple to write.
d. However, Sougou is obviously not as good as Goose Factory in the standardization of interface. The main structure of different api response packets is actually inconsistent, so the implementation only has a simple structure...
2. Implementation code
Just put the code directly, it is also available on 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)
I believe you have mastered the case after reading this article Method, for more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
What are the methods for Dataframe query in pandas
selenium cookie skips the verification code login implementation step Detailed explanation
The above is the detailed content of How to call Sogou AI API under python3. For more information, please follow other related articles on the PHP Chinese website!

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment