亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python如何調用百度識圖api

 更新時間:2020年09月29日 15:29:19   作者:小小咸魚YwY  
這篇文章主要介紹了python如何調用百度識圖api,幫助大家更好的理解和使用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如何調用百度識圖api的詳細內容,更多關于python調用api的資料請關注腳本之家其它相關文章!

相關文章

最新評論