IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 人工智能 -> 百度接口(1)小票识别+票体主题内容处理 -> 正文阅读

[人工智能]百度接口(1)小票识别+票体主题内容处理

一、申请百度接口

  1. 注册百度账号
    https://login.bce.baidu.com/

  2. 百度票据识别
    在这里插入图片描述

  3. 领取免费试用
    在这里插入图片描述

  4. 选择通用文字识别,可以看到通过身份证号实名认证后可1000次/月试用,对开发者初调学习用还是很友好的,赞百度。
    在这里插入图片描述

  5. 回到主页可以看到我们已经申请成功了
    在这里插入图片描述

  6. 创建应用
    在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
前者是通过APIkey引入授权,安全方式需要单独配置许可证,这里仅测试,选择不需要。
在这里插入图片描述
在这里插入图片描述

二、根据文档编辑API访问

查看文档

文字识别接口

1.在应用列表、应用详情、可以查看到APIkey和SecretKey
在这里插入图片描述

  1. (这里用python测试)获取access_token
# encoding:utf-8
import requests 


# AccessKey = ''
# SecretKey = ''

# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+AccessKey+'&client_secret='+SecretKey
headers = {
    'Content-Type': 'application/json;charset=UTF-8'
}
access_token = ''
response = requests.get(url=host, headers=headers)
if response:
    res = response.json()
    access_token = res['access_token']
    print(access_token)

测试图片:(来源于网络)
请添加图片描述

图像识别:


# encoding:utf-8

import requests
import base64

'''
通用文字识别
'''

request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
# 二进制方式打开图片文件
f = open('/Users/wangyu/Desktop/xiaopiao.jpg', 'rb')
img = base64.b64encode(f.read())

params = {"image":img}

request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
    print (response.json())

识别结果:这里是文字识别效果也非常好,和小票拍摄清晰度也有一定关系,比较满意。
在这里插入图片描述

三、小票识别

1.识别

继续领取通用票据的识别接口测试权限
在这里插入图片描述


# encoding:utf-8

import requests
import base64
import json
'''
通用票据识别
'''

request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/receipt"
# 二进制方式打开图片文件
f = open('/Users/wangyu/Desktop/xiaopiao.jpg', 'rb')
img = base64.b64encode(f.read())

params = {"image":img}

request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
    print (json.dumps(response.json(),indent=1,ensure_ascii=False))

打印内容:文字、文字所处的位置
在这里插入图片描述打印出words可以发现识别内容非常鸡肋:
在这里插入图片描述

2.票体内容处理

明显发现识别的信息还是不够具体,图源网络,我们需要对小票内容分析,仅需要 票体:主题内容 即可。
在这里插入图片描述
思路:既然有location位置,那么优先以位置信息来定位头和尾部信息,并剔除;找到头部关键字距离顶部的高度top1(如图蓝色),找到底部的关键字距顶部的高度top2(如图黄色),票体的内容部分高度为:top1~top2(如图绿色部分)
在这里插入图片描述
关键字的确定:参考多种购物小票的排版,可以发现,头部有通用字样标识符:“单价、数量”,尾部有“总计、应付、合计、应收”等字样,但是像金额、优惠的字样不可取,上下均有出现。
在这里插入图片描述
多个关键字去匹配,匹配到的值:顶部取最大值,底部取最小值来精确中间body的高度。我这里还剔除了长数字和英文,因为我不需要单价等细节,直接正则匹配过滤了。

def _isbody(response):
    # 设计思路是找到票体主体内容的头部和主体内容的位部关键字,获取关键字距离顶部的坐标
    begin,end = 0,999999
    beginWords = ["数量","单价","售价","单位"]
    endWords = ["总计","总金额","支付","应收","应付","合计"]
    for idx in response['words_result']:
        for i in beginWords:
            if i in idx['words']:
                temp = idx['location']['top']
                if temp > begin:
                    begin = temp
        for j in endWords:
            if j in idx['words']:
                temp2 = idx['location']['top']
                if temp2 < end:
                    end = temp2
#     print(begin,end)
    # 取居于主体内容部分的中间的购买信息
    list = []
    for idx in response['words_result']:
        top = idx['location']['top']    
        if top > begin and top < end :
            # 剔除数字和英文字符,保留中文
            foods = re.sub('[^\u4e00-\u9fa5]', '', idx['words'])
            if foods != '':
                list.append(foods)
#     print(list)
    return list

打印出高度区间,取中间中文部分(已经非常nice了!)
在这里插入图片描述

3. 物品分类

我的需求是需要找到购买的物品是属于什么分类,那么在我已有的分类下去归类小票的购买物资就可:
在这里插入图片描述

def _isWhat(words,array):
    for key,value in array.items():
#         print('\n'+key,end=':')
        if key in words:
            return key
        for batching in value:
#             print(batching, end=' ')
            if batching in words:
                return batching
            
    return '其他'

array = {'糖果':{'棒棒糖','糖'},'蔬菜':{'青菜','瓜'},'饮料':{'果汁','可乐','橙汁','牛奶','奶茶'},'薯片':{'乐事'},'蛋糕':{'糕点',"绿豆糕"}}

resultList = []
  for food in wordList:
      type = _isWhat(food,array)
      resultList.append({food,type})
  print(resultList)

文章到这里就结束了,代码的逻辑很简单,需求不复杂;同理:百度的图像识别接口除了小票文字还有物体识别,好比如识别到某一类的物体,根据你的字典去归类区分,可以应用于垃圾分类,物品存储过期提醒,烹饪菜谱推荐等等。

四、源代码

# encoding:utf-8

import requests
import base64
import json
import re
'''
通用票据识别
'''

def getToken(AccessKey,SecretKey):
    # client_id 为官网获取的AK, client_secret 为官网获取的SK
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+AccessKey+'&client_secret='+SecretKey
    headers = {
        'Content-Type': 'application/json;charset=UTF-8'
    }
    access_token = ''
    response = requests.get(url=host, headers=headers)
    if response:
        res = response.json()
        access_token = res['access_token']
    return access_token

def getResult(url,access_token):
    request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/receipt"
    # 二进制方式打开图片文件
    f = open(url, 'rb')
    img = base64.b64encode(f.read())

    params = {"image":img}

    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    if response:
#         print (json.dumps(response.json(),indent=1,ensure_ascii=False))
        return response.json()

def _isbody(response):
    # 设计思路是找到票体主体内容的头部和主体内容的位部关键字,获取关键字距离顶部的坐标
    begin,end = 0,999999
    beginWords = ["数量","单价","售价","单位"]
    endWords = ["总计","总金额","支付","应收","应付","合计"]
    for idx in response['words_result']:
        for i in beginWords:
            if i in idx['words']:
                temp = idx['location']['top']
                if temp > begin:
                    begin = temp
        for j in endWords:
            if j in idx['words']:
                temp2 = idx['location']['top']
                if temp2 < end:
                    end = temp2
#     print(begin,end)
    # 取居于主体内容部分的中间的购买信息
    list = []
    for idx in response['words_result']:
        top = idx['location']['top']    
        if top > begin and top < end :
            # 剔除数字和英文字符,保留中文
            foods = re.sub('[^\u4e00-\u9fa5]', '', idx['words'])
            if foods != '':
                list.append(foods)
#     print(list)
    return list

def _isWhat(words,array):
    for key,value in array.items():
#         print('\n'+key,end=':')
        if key in words:
            return key
        for batching in value:
#             print(batching, end=' ')
            if batching in words:
                return batching
            
    return '其他'

def main():
    # 识别的图片
    url = '/Users/wangyu/Desktop/xiaopiao2.jpg'
    # 百度账号信息
    AccessKey = ''
    SecretKey = ''
    # 获取小票识别结果
    access_token = getToken(AccessKey,SecretKey)
    response = getResult(url,access_token)
    # 取小票识别主体,(不一定能够涵盖全面)
    wordList = _isbody(response)
    # 区分识别购买物资的类别
    array = {'糖果':{'棒棒糖','糖'},'蔬菜':{'青菜','瓜'},'饮料':{'果汁','可乐','橙汁','牛奶','奶茶'},'薯片':{'乐事'},'蛋糕':{'糕点',"绿豆糕"}}
    resultList = []
    for food in wordList:
        type = _isWhat(food,array)
        resultList.append({food,type})
    print(resultList)
    
main()

识别效果:
在这里插入图片描述

  人工智能 最新文章
2022吴恩达机器学习课程——第二课(神经网
第十五章 规则学习
FixMatch: Simplifying Semi-Supervised Le
数据挖掘Java——Kmeans算法的实现
大脑皮层的分割方法
【翻译】GPT-3是如何工作的
论文笔记:TEACHTEXT: CrossModal Generaliz
python从零学(六)
详解Python 3.x 导入(import)
【答读者问27】backtrader不支持最新版本的
上一篇文章      下一篇文章      查看所有文章
加:2021-08-07 12:05:08  更:2021-08-07 12:07:29 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/2 1:08:55-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码