当前位置:   article > 正文

调用百度图像识别api处理网络图片(文字识别)_c#图文识别之百度api调用

c#图文识别之百度api调用

要想使用百度图像识别api得先注册一个百度账号,然后创建一个图像识别应用,获取其中的APPID、AK、SK

账号注册什么的就不多说了,直接开始

创建应用

在这里插入图片描述

创建成功

在这里插入图片描述

代码

# -*- coding: utf-8 -*- 
# @File : img2num.py

from aip import AipOcr
import urllib.request
'''
使用百度api读取图片中的文字
'''

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

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

options = {}  # 配置字典
options["language_type"] = "CHN_ENG"  # 识别文字类型
options["detect_direction"] = "true"  # 是否检测图片的朝向
options["detect_language"] = "true"  # 是否检测语言
options["probability"] = "true"  # 是否返回置信度

def get_file_content(filePath): # 本地图片
    with open(filePath, 'rb') as fp:
        return fp.read()  # 获取图片信息
        
# 获取网络图片内容
def get_img_word(url):
    res = urllib.request.urlopen(url)
    result = client.basicGeneral(res.read(), options)
    if "words_result" not in result.keys(): # 当识别失败时返回图片地址
        print(result)
        print("无结果")
        return url
    word = ""
    for i in result["words_result"]:
        word = i['words']
    print(word)
    return word

def main():
    img_url = "网络图片地址"
    get_img_word(img_url)

if __name__ == '__main__':
    main()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

图片

在这里插入图片描述
转换结果

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/705368
推荐阅读
相关标签
  

闽ICP备14008679号