百度智能api接口汇总

一:自然语言处理

# -*- coding: utf-8 -*-
# @Author  : FELIX
# @Date    : 2018/5/18 9:47

from aip import AipNlp

""" 你的 APPID AK SK 从百度开发者平台申请 """ 
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''

client = AipNlp(APP_ID, API_KEY, SECRET_KEY)

text='百度是一家高科技公司'
print(client.lexer(text))


text = "今天天气怎么样?"

""" 调用依存句法分析 """
print(client.depParser(text))

""" 如果有可选参数 """
options = {}
options["mode"] = 1

""" 带参数调用依存句法分析 """
print(client.depParser(text, options))



word = "张飞"

""" 调用词向量表示 """
print(client.wordEmbedding(word))



text = "床前明月光"

""" 调用DNN语言模型 """
print(client.dnnlm(text))



word1 = "北京"

word2 = "上海"

""" 调用词义相似度 """
print(client.wordSimEmbedding(word1, word2))

""" 如果有可选参数 """
options = {}

""" 带参数调用词义相似度 """
print(client.wordSimEmbedding(word1, word2, options))





text1 = "浙富股份"

text2 = "万事通自考网"

""" 调用短文本相似度 """
print(client.simnet(text1, text2))

""" 如果有可选参数 """
options = {}
options["model"] = "CNN"

""" 带参数调用短文本相似度 """
print(client.simnet(text1, text2, options))



text = "三星电脑电池不给力"

""" 调用评论观点抽取 """
print(client.commentTag(text))

""" 如果有可选参数 """
options = {}
options["type"] = 13

""" 带参数调用评论观点抽取 """
print(client.commentTag(text, options))

text = "苹果是一家伟大的公司"

""" 调用情感倾向分析 """
print(client.sentimentClassify(text))


title = "iphone手机出现“白苹果”原因及解决办法,用苹果手机的可以看下"

content = "如果下面的方法还是没有解决你的问题建议来我们门店看下成都市锦江区红星路三段99号银石广场24层01室。"

""" 调用文章标签 """
print(client.keyword(title, content))



title = "欧洲冠军杯足球赛"

content = "欧洲冠军联赛是欧洲足球协会联盟主办的年度足球比赛,代表欧洲俱乐部足球最高荣誉和水平,被认为是全世界最高素质、最具影响力以及最高水平的俱乐部赛事,亦是世界上奖金最高的足球赛事和体育赛事之一。"

""" 调用文章分类 """
print(client.topic(title, content))

二:图像识别

# -*- coding: utf-8 -*-
# @Author  : FELIX
# @Date    : 2018/5/18 11:32

from aip import AipImageClassify

""" 你的 APPID AK SK """
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''

client = AipImageClassify(APP_ID, API_KEY, SECRET_KEY)

""" 读取图片 """
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

image = get_file_content('ma.jpg')

""" 调用通用物体识别 """

print(client.advancedGeneral(image))

""" 调用菜品识别 """
print(client.dishDetect(image))

""" 调用车辆识别 """

print(client.carDetect(image))

""" 调用logo商标识别 """
print(client.logoSearch(image))

""" 调用动物识别 """
print(client.animalDetect(image))

""" 调用植物识别 """
print(client.plantDetect(image))

三:文字识别

# -*- coding: utf-8 -*-
# @Author  : FELIX
# @Date    : 2018/5/18 10:36

from aip import AipOcr


""" 你的 APPID AK SK """
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)


# """ 读取图片 """
# def get_file_content(filePath):
#     with open(filePath, 'rb') as fp:
#         return fp.read()
#
# image = get_file_content('code.jpg')
#
# """ 调用通用文字识别, 图片参数为本地图片 """
# print(client.basicGeneral(image))
#
# """ 如果有可选参数 """
# options = {}
# options["language_type"] = "CHN_ENG"
# options["detect_direction"] = "true"
# options["detect_language"] = "true"
# options["probability"] = "true"
#
# """ 带参数调用通用文字识别, 图片参数为本地图片 """
# print(client.basicGeneral(image, options))

# url = "https//www.x.com/sample.jpg"
#
# """ 调用通用文字识别, 图片参数为远程url图片 """
# client.basicGeneralUrl(url)
#
# """ 如果有可选参数 """
# options = {}
# options["language_type"] = "CHN_ENG"
# options["detect_direction"] = "true"
# options["detect_language"] = "true"
# options["probability"] = "true"
#
# """ 带参数调用通用文字识别, 图片参数为远程url图片 """
# client.basicGeneralUrl(url, options)



""" 读取图片 """
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

image = get_file_content('code.jpg')

""" 调用通用文字识别(含位置高精度版) """
print(client.accurate(image))
""" 调用网络图片文字识别, 图片参数为本地图片 """
print(client.webImage(image))
print(client.basicGeneral(image))


# """ 如果有可选参数 """
# options = {}
# options["recognize_granularity"] = "big"
# options["detect_direction"] = "true"
# options["vertexes_location"] = "true"
# options["probability"] = "true"
#
# """ 带参数调用通用文字识别(含位置高精度版) """
# print(client.accurate(image, options))

四:语音文字识别

# -*- coding: utf-8 -*-
# @Author  : FELIX
# @Date    : 2018/5/18 10:08

from aip import AipSpeech

""" 你的 APPID AK SK """
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

# result  = client.synthesis('你好百度', 'zh', 1, {
#     'vol': 5,
#     'per':3,
# })
#
# # 识别正确返回语音二进制 错误则返回dict 参照下面错误码
# if not isinstance(result, dict):
#     with open('auido.mp3', 'wb') as f:
#         f.write(result)
#




# 读取文件
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

# 识别本地文件
text=client.asr(get_file_content('auido.wav'), 'pcm', 16000, {
    'dev_pid': 1536,
})

print(text)

猜你喜欢

转载自www.cnblogs.com/felixwang2/p/9056123.html