python识别图片中文字

ocr  光学字符识别  Optical Character Recognition

注意需要先安装 tesseract
    
    sudo apt-get install -y tesseract-ocr
    
验证Tesseract是否安装成功
 
    tesseract -v
安装Tesseract支持中文    

    sudo apt-get install -y tesseract-ocr-chi-sim
    
查看Tesseract支持的语言文字
    
    tesseract --list-langs
    
    
使用    Tesseract 识别图片中的文字

    tesseract ocr.png stdout -l chi_sim
    
使用    pytesseract   识别图片中的文字

#!/usr/bin/python3
"""
    作者:qzc
    功能:ocr 中文叫光学字符识别,英文全称是Optical Character Recognition
    版本:1.0
    日期:2020/6/29 下午4:25
"""
# import the necessary packages
from PIL import Image
import pytesseract
import argparse
import cv2
import os

def main():
    # construct the argument parse and parse the arguments
    ap = argparse.ArgumentParser()
    ap.add_argument("-i", "--image", required=True,help="path to input image to be OCR'd")
    ap.add_argument("-p", "--preprocess", type=str, default="thresh",help="type of preprocessing to be done")
    ap.add_argument("-l", "--lang", type=str, default="chi_sim",help="type of language to be done")
    args = vars(ap.parse_args())
    # load the example image and convert it to grayscale
    image = cv2.imread(args["image"])
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # check to see if we should apply thresholding to preprocess the image
    if args["preprocess"] == "thresh":
        gray = cv2.threshold(gray, 0, 255,   cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

    # make a check to see if median blurring should be done to remove noise
    elif args["preprocess"] == "blur":
        gray = cv2.medianBlur(gray, 3)

    # write the grayscale image to disk as a temporary file so we can apply OCR to it
    filename = "{}.png".format(os.getpid())
    cv2.imwrite(filename, gray)

    # load the image as a PIL/Pillow image, apply OCR, and then delete the temporary file
    if args["preprocess"] == "rgb":
        text = pytesseract.image_to_string(image, args["lang"])
    else:
        text = pytesseract.image_to_string(Image.open(filename),args["lang"])

    os.remove(filename)
    print(text)

    # show the output images
    cv2.imshow("Image", image)
    cv2.imshow("Output", gray)
    cv2.waitKey(0)

if __name__ == '__main__':
    print('示例:python ocr.py --image ocr.png --preprocess rgb --lang eng\npython ocr.py --image ocr.png --preprocess blur --lang eng\npython ocr.py --image ocr.png --preprocess thresh --lang chi_sim')
    main()
最后发现识别准确率不高,使用工具 utools吧

    https://res.u-tools.cn/currentversion/utools_1.1.3_amd64.deb
    然后安装 图片里的ocr,然后就能识别图片中的文字,似乎需要联网

python调用百度api应该准确率高也是需要联网

你可能感兴趣的:(python,python,ocr)