欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

python如何调用百度识图api

时间:2021-06-10 08:23:46|栏目:Python代码|点击:

一.先去百度识别官网注册开通服务且获得ak和sk

链接:https://cloud.baidu.com/doc/Reference/s/9jwvz2egb

二.代码模板

import cv2
import base64
import requests
import numpy as np
import traceback
from retrying import retry

token_list=[
  {
    "ak":"xxxxxx",
    "sk":"xxxxxxxxxx"
  },
]

def get_token(ak,sk):
  url = "https://aip.baidubce.com/oauth/2.0/token"
  params = {
    "grant_type": "client_credentials",
    "client_id": ak, # AK
    "client_secret": sk # SK
  }
  eaders={
    "Content-Type":"application/json; charset=UTF-8",
  }
  response = requests.get(url,params=params,headers=headers,timeout=8)
  res = response.json()
  access_token = res["access_token"]
  return access_token



def baidu_api(image,token):
  """
  百度通用文字识别
  :return:
  """
  # 通用文本识别接口
  url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
  # 网络图片识别接口
  # url = "https://aip.baidubce.com/rest/2.0/ocr/v1/webimage"
  params = {
    "access_token": token,
  }
  data = {
    "image": base64.b64encode(image) #图标的bs64编码
  }
  response = requests.post(url, params=params, data=data)
  data_res = response.json()
  print(data_res)
  words = [i["words"] for i in data_res["words_result"]]
  return words

def baidu_image_recognition(img_content):
  img2=img_content
  for i in range(len(token_list)):
    token = get_token(token_list[i]["ak"], token_list[i]["sk"])
    words = baidu_api(img2,token)
  	return words

上一篇:python字符串,数值计算

栏    目:Python代码

下一篇:python with提前退出遇到的坑与解决方案

本文标题:python如何调用百度识图api

本文地址:http://www.codeinn.net/misctech/139086.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有