百度AI攻略:二维码识别

1.功能描述:python

对图片中的二维码、条形码进行检测和识别,返回存储的文字信息,获取相应信息,可应用于商品、药品出入库管理及货物运输管理等场景,轻松一扫便可快速完成对物品信息的读取、登记和存储,大幅度简化物品管理流程。json

2.平台接入app

具体接入方式比较简单,能够参考个人另外一个帖子,这里就不重复了:测试

http://ai.baidu.com/forum/topic/show/943327优化

3.调用攻略(Python3)及评测编码

3.1首先认证受权:url

在开始调用任何API以前须要先进行认证受权,具体的说明请参考:rest

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

具体Python3代码以下:orm

# -*- 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.2二维码识别分析接口调用:

详细说明请参考: https://ai.baidu.com/docs#/OCR-API/535c3463

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

你们须要注意的是:

API访问URL:https://aip.baidubce.com/rest/2.0/ocr/v1/qrcode

图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/jpeg/png/bmp格式

返回示例:

{

"log_id": 863402790,

"codes_result": [

{

"type": "QR_CODE",

"text": [

"中国",

"北京"

]

}

],

"codes_result_num": 1

}

Python3调用代码以下:

#二维码

#filename:图片名(本地存储包括路径),

def qrcode(filename):

    request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/qrcode"

    print(filename)

    # 二进制方式打开图片文件

    f = open(filename, 'rb')

    img = base64.b64encode(f.read())

 

    params = dict()

    params['image'] = img

    params = urllib.parse.urlencode(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)

        data = json.loads(content)

        #print(data)

        words_result=data['codes_result']

        print ("识别结果")

        i=1

        for item in words_result:

            print ('no.',i)

            print ('type:',item['type']) 

            print ('text:',item['text']) 

            i=i+1

qrcode('../img/qr3.jpg')

4.功能评测:

选用不一样的数据对效果进行测试,具体效果以下(如下例子均来自网上):

type: QR_CODE

text: ['http://app.core-apps.com/iscbconf']

type: QR_CODE

text: ['http://goo.gl/b620kT']

type: QR_CODE

text: ['http://u.soufun.cn/d_newhouse/zzsqb6/']

 

5.测试结论和建议

测试下来,总体识别效果不错。对于二维码识别的很准确,速度也很快,用起来很是的方便。

建议:

在一张图有多个不一样格式的条码的时候,识别效果稍差,但愿能在这方面进行优化。

相关文章
相关标签/搜索