AI战疫【百度大脑新品体验】公式识别
才能我浪费99 发布于2020-05 浏览:8165 回复:8
1
收藏

1.功能描述:

百度的公式识别功能支持对试卷中的数学公式及题目内容进行识别,可提取公式部分进行单独识别,也可对题目和公式进行混合识别,并返回Latex格式公式内容及位置信息,便于进行后续处理

2.产品介绍
百度的公式识别支持公式提取识别和公式与题目的混合识别,并以Latex格式返回公式内容,方便进行后处理,为教育场景进阶功能提供高质量输入。依托百度优秀的文字图像处理技术和优质的教育数据,支持对数学公式及题目内容进行识别,能够满足教育场景对公式进行提取的需求。

3.调用攻略(Python3)及评测
3.1平台接入

具体接入方式比较简单,可以参考我的另一个帖子,这里就不重复了:
http://ai.baidu.com/forum/topic/show/943327

3.2首先认证授权:

在开始调用任何API之前需要先进行认证授权,具体的说明请参考:

http://ai.baidu.com/docs#/Auth/top

具体Python3代码如下:

# -*- coding: utf-8 -*-
#!/usr/bin/env python

import urllib
import base64
import json
#client_id 为官网获取的AK, client_secret 为官网获取的SK
client_id =【百度云应用的AK】
client_secret =【百度云应用的SK】

#获取token
def get_token():
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret
request = urllib.request.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib.request.urlopen(request)
token_content = response.read()
#print (token_content)
if token_content:
token_info = json.loads(token_content)
token_key = token_info['access_token']
return token_key

3.3公式识别分析接口调用:

详细说明请参考: https://ai.baidu.com/ai-doc/OCR/Ok3h7xxva

说明的比较清晰,这里就不重复了。

大家需要注意的是:
API访问URL:https://aip.baidubce.com/rest/2.0/ocr/v1/formula
输入图像:图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/jpeg/png/bmp格式,当image字段存在时url字段失效


返回示例:
{
"log_id": 2671713289176456793,
"direction": 0,
"formula_result_num": 3,
"formula_result": [
{
"location": {
"width": 258,
"top": 265,
"left": 450,
"height": 204
},
"words": "\\left\\{ \\begin{aligned} & x = - 1 1 \\\\ & y = 2 \\\\ \\end{aligned} \\right. "
},
{
"location": {
"width": 429,
"top": 546,
"left": 310,
"height": 203
},
"words": "\\left\\{ \\begin{aligned} & 3 x + 2 y = m \\\\ & n x - y = 2 \\\\ \\end{aligned} \\right. "
},
{
"location": {
"width": 142,
"top": 613,
"left": 1029,
"height": 71
},
"words": "m - \\left[ 1 0 0 , - \\infty \\right) "
}
],
"words_result_num": 5,
"words_result": [
{
"location": {
"width": 168,
"top": 313,
"left": 292,
"height": 110
},
"words": "已知"
},
{
"location": {
"width": 258,
"top": 265,
"left": 450,
"height": 204
},
"words": "\\left\\{ \\begin{aligned} & x = - 1 1 \\\\ & y = 2 \\\\ \\end{aligned} \\right. "
},
{
"location": {
"width": 582,
"top": 319,
"left": 728,
"height": 84
},
"words": "是二元一次方程组"
},
{
"location": {
"width": 429,
"top": 546,
"left": 310,
"height": 203
},
"words": "\\left\\{ \\begin{aligned} & 3 x + 2 y = m \\\\ & n x - y = 2 \\\\ \\end{aligned} \\right."
},
{
"location": {
"width": 780,
"top": 597,
"left": 745,
"height": 88
},
"words": "的解,则 m - \\left[ 1 0 0 , - \\infty \\right) 的值是()"
}
]
}


Python3调用代码如下:

def style_trans(filename,resultfilename,option):
    request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/style_trans"
    
    # 二进制方式打开图片文件
    f = open(filename, 'rb')
    img = base64.b64encode(f.read())
    
    params = dict()
    params['image'] = img
    params['option'] = option
    params = urllib.parse.urlencode(params).encode("utf-8")
    #params = json.dumps(params).encode('utf-8')
    
    access_token = get_token()
    
    begin = time.perf_counter()
    request_url = request_url + "?access_token=" + access_token
    request = urllib.request.Request(url=request_url, data=params)
    request.add_header('Content-Type', 'application/x-www-form-urlencoded')
    response = urllib.request.urlopen(request)
    content = response.read()
    end = time.perf_counter()

    print('处理时长:'+'%.2f'%(end-begin)+'秒')
    
    if content:
        #print(content)
        content=content.decode('utf-8')
        #print (content)
        #print(content)
        data = json.loads(content)
        img_str=data['image']
        save_base_image(img_str,resultfilename)

4.功能评测:
选用不同的数据对效果进行测试,具体效果如下(以下例子均来自网上):

处理时长:0.86秒
{'log_id': 937093195731821510, 'words_result_num': 1, 'words_result': [{'location': {'width': 263, 'top': 28, 'left': 27, 'height': 76}, 'words': 'x _ { 1 , 2 } = \\frac { - b \\pm \\sqrt { b ^ { 2 } - 4 a c } } { 2 a } '}]}

5.测试结论和建议
测试下来,整体效果非常惊艳,速度很快,转换结果准确。可用于各种教育、科研等场景,例如:
拍照搜题:使用公式识别技术,可对题目中的数学公式和题目内容进行识别,并以Latex格式返回公式内容,满足对含数字公式的题目进行识别后匹配题库内容的需求,有效提升可搜索题目类型的丰富性,提升用户使用体验
智能阅卷:使用公式识别技术,对学生日常作业及考试试卷中的公式及题目内容进行识别,完成学生作业、考卷的自动化录入,为智能阅卷平台提供数据录入基础,方便进行后续的线上批阅及教学数据沉淀分析,大幅度提升教师工作效率及质量,促进教学管理的数字化和智能化

收藏
点赞
1
个赞
共8条回复 最后由用户已被禁言回复于2022-04
#9才能我浪费99回复于2020-06

要是能直接解方程就更好了

0
#8才能我浪费99回复于2020-06

未来在教学方面会有很多应用

0
#7才能我浪费99回复于2020-05
#6 wangwei8638回复
再出个手写公式识别就更好了

估计未来会有的

0
#6wangwei8638回复于2020-05
#3 才能我浪费99回复
感觉在教育领域会有很好的应用

再出个手写公式识别就更好了

0
#5才能我浪费99回复于2020-05
#4 自尊心3回复
用来写寒假作业无敌了【滑稽】

哈哈,这倒是个好主意

0
#4自尊心3回复于2020-05

用来写寒假作业无敌了【滑稽】

0
#3才能我浪费99回复于2020-05

感觉在教育领域会有很好的应用

0
#2才能我浪费99回复于2020-05

识别速度很快

0
TOP
切换版块