超简单使用Python识别图片中的中/英文字

摘要: 最近闲来无事, 使用Python写了个文字识别的程序, 在这里分享给大家

1. 准备工作

    申请百度开发者平台, 获取图片文字识别, 申请地址:

    https://console.bce.baidu.com/ai/?_=1553338619822&fromai=1#/ai/ocr/overview/index

    申请效果如下所述:

2. 配置依赖库 

pip install baidu-aip

3. 导入依赖库

from aip import AipOcr
import os,sys
import PIL.Image

4. 设置ID API Key Secret Key

    直接在百度云平台获取就可以, 以下为无效

API_ID = '1553XX'
API_KEY = 'LFIDSL3XXXXXXXXXXXFDSZXS'
SECRET_KEY = 'LFDSLFJIEJLXXXXXXXXXXJFKDL'

 5. 打开图片

    (1)判断打开文件是否为图片文件   

def is_picture(filename):
        cExt = ['.bmp','.png','jpg']
        num = 0
        for i in cExt:
                if i in filename:
                        break
                else:
                        num = num + 1
        if num == 3:
                return 0
        else:
                return 1

    (2)由于目前该接口只适用于.png文件, 因此需要将其他类型的文件转为.png文件再读取   

def get_image_from_file(filename):

        is_pic = is_picture(filename)

        if is_pic == 1:

                try:
                                       
                        png_filename = filename[:-4]+".png"
                
                        PIL.Image.open(filename).save(png_filename)

                        with open(png_filename,'rb') as fp:
                                content = fp.read()

                        #将转化后的Png删除
                        os.remove(png_filename)

                        return content
                except:
                        return 'error'
                        #tkinter.messagebox.showinfo('提示','图片已被其它工具打开,请关闭后重试!')
                
        else:
                return ''

6. 进行图片识别 

    (1)获取aip, 用来与百度云数据传输    

def get_apiOcr():

        aipOcr = AipOcr(API_ID,API_KEY,SECRET_KEY)

        return aipOcr

    (2)进行识别    

#获取图片内容
contents = get_image_from_file(filename)
    
#获取aip
api = get_apiOcr()

def get_words(AipOcr,content):

        #自动识别文字方向, 文字识别设置中\英文同时识别
        options = { 'detect_direction':'true', 'language_type':'CHN_ENG'}

        #获取识别内容
        result = AipOcr.webImage(content,options)

        #print (result)

        words = []

        #提取内容
        for i in result['words_result']:
                #print (i['words'])
                words.append(i['words'])

        print(words)

7.识别测试 

    (1)输入图片

    (2)识别结果

逆向工程在口腔医学上的应用具有高度的学科交叉性,是机械、计算机生物材料学、医学相融合的产物,
其应用领域和深度亦随着各学科的发展与交而飞速的发展起来。本文围绕逆向工程及其在口腔医学上的
扩展应用,针对网格模型处理技术进行了以下几个方面的研究与应用(1)建立了图像滤波和网格光顺之间
相应的桥梁,并对 Laplacian光顺Taub光顺、 debrun光顺、双边滤波光顺进行具体研究和对比分析,
得到了不同的光顺效果,从而对比显示出各种光顺算法的特性。

8.以下为借用python开发的GUI界面 

关注下面的公众号, 回复[文字识别]获取下载链接!