python识别图片中的文字

  1. 使用baidu提供的模块
    (1). 先安装baidu-aip模块
pip3 install baidu-aip

(2). 代码

    #import requests
    from aip import AipOcr
    
    #image = requests.get('https://***/d6d8d9dea5691705d04fef2306b52.png').content
    image = open(r'/home/allen/Desktop/poem.png', 'rb')
    image = image.read()
    APP_ID = '************'
    API_KEY = '**************************'
    SECRET_KEY = '******************************'
    
    client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
    res = client.basicGeneral(image)
    if 'words_result' in res.keys():
        for item in res['words_result']:
            print(item['words'])
    else:
        print(res)

or:

import requests
from aip import AipOcr

#image = requests.get('https://***/d6d8d9dea5691705d04fef2306b52.png').content
image = open(r'/home/allen/Desktop/poem.png', 'rb')
image = image.read()
APP_ID = '*23****23'
API_KEY = '**********************'
SECRET_KEY = '************************'

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
res = client.basicGeneral(image)
for i in res.get('words_result'):
    print(i.get('words'))

ID\key:
点击如下网站,百度开发者账号随便注册,放心,免费。

语音合成-百度AI​ai.baidu.com: http://console.bce.baidu.com/ai/?fromai=1#/ai/speech/app/list

然后,如下点击立即使用即可。
python识别图片中的文字

然后,如下点击创建应用即可,随便填。

python识别图片中的文字

然后返回应用详情,就看到了代码需要的三个ID

python识别图片中的文字

然后复制替换到代码中去。

https://blog.csdn.net/oh5W6HinUg43JvRhhB/article/details/78683112

  1. 使用pytesseract 识别图片中文字:(对中文支持不够好)
    (1)下载安装Tesseract
    For example to install Tesseract with German language traineddata:
    For RHEL 7 run the following as root:**
    yum-config-manager --add-repo https://download.opensuse.org/repositories/home:/Alexander_Pozdnyakov/RHEL_7/
    yum update
    yum install tesseract
    yum install tesseract-langpack-deu

(2) 下载需要的语言包:
git clone https://github.com/tesseract-ocr/tessdata
或者单独下载语言包
yum install tesseract-langpack-deu
yum install --disablerepo=“epel” tesseract-langpack-chi*

(3)
在系统环境变量中新建一个配置信息,命名为:TESSDATA_PREFIX,
变量值为安装的tessdata路径, 默认为: /usr/share/tesseract/4/tessdata/

(4)
修改tesseract_cmd,打开/usr/local/python3/lib/python3.6/site-packages/pytesseract/pytesseract.py,
修改tesseract_cmd为tesseract, 默认为tesseract, 不需要改

(5) 代码:

import pytesseract
from PIL import Image

#img = Image.open(r"/home/allen/Desktop/poem1.jpeg")
img = Image.open(r"/home/allen/Desktop/poem.png")
pytesseract.pytesseract.tesseract_cmd = '/bin/tesseract'
#text = pytesseract.image_to_string(img, lang='deu')
text = pytesseract.image_to_string(img, lang='chi_sim')
print(text)

解决识别率低的问题
可以增强图片的显示效果,或者将其转换为黑白的,这样可以使其识别率提升不少:
enhancer = ImageEnhance.Contrast(image1)
image2 = enhancer.enhance(4)
可以再对image2调用 image_to_string识别

额外附加一句:有可能中文识别出来了,但是乱码,需要相应地将text转换为你所用的中文编码方式,如:
text.decode(“utf8”)就可以了

以下转自: https://blog.csdn.net/XnCSD/article/details/80786793

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 12 09:37:38 2018
利用百度api实现图片文本识别
@author: XnCSD
"""

import glob
from os import path
import os
from aip import AipOcr
from PIL import Image

def convertimg(picfile, outdir):
    '''调整图片大小,对于过大的图片进行压缩
    picfile:    图片路径
    outdir:    图片输出路径
    '''
    img = Image.open(picfile)
    width, height = img.size
    while(width*height > 4000000):  # 该数值压缩后的图片大约 两百多k
        width = width // 2
        height = height // 2
    new_img=img.resize((width, height),Image.BILINEAR)
    new_img.save(path.join(outdir,os.path.basename(picfile)))
    
def baiduOCR(picfile, outfile):
    """利用百度api识别文本,并保存提取的文字
    picfile:    图片文件名
    outfile:    输出文件
    """
    filename = path.basename(picfile)
    
    APP_ID = '******' # 刚才获取的 ID,下同
    API_KEY = '******'
    SECRECT_KEY = '******'
    client = AipOcr(APP_ID, API_KEY, SECRECT_KEY)
    
    i = open(picfile, 'rb')
    img = i.read()
    print("正在识别图片:\t" + filename)
    message = client.basicGeneral(img)   # 通用文字识别,每天 50 000 次免费
    #message = client.basicAccurate(img)   # 通用文字高精度识别,每天 800 次免费
    print("识别成功!")
    i.close();
    
    with open(outfile, 'a+') as fo:
        fo.writelines("+" * 60 + '\n')
        fo.writelines("识别图片:\t" + filename + "\n" * 2)
        fo.writelines("文本内容:\n")
        # 输出文本内容
        for text in message.get('words_result'):
            fo.writelines(text.get('words') + '\n')
        fo.writelines('\n'*2)
    print("文本导出成功!")
    print()

if __name__ == "__main__":
    
    outfile = 'export.txt'
    outdir = 'tmp'
    if path.exists(outfile):
        os.remove(outfile)
    if not path.exists(outdir):
        os.mkdir(outdir)
    print("压缩过大的图片...")
    // 首先对过大的图片进行压缩,以提高识别速度,将压缩的图片保存与临时文件夹中
    for picfile in glob.glob("picture/*"):
        convertimg(picfile, outdir)
    print("图片识别...")
    for picfile in glob.glob("tmp/*"):
        baiduOCR(picfile, outfile)
        os.remove(picfile)
    print('图片文本提取结束!文本输出结果位于 %s 文件中。' % outfile)
    os.removedirs(outdir)