初识OCR,Tesseract的安装与使用
安装 初识 OCR Tesseract 使用
2023-09-14 09:14:36 时间
1、安装 Tesseract
Ubuntu
sudo apt-get install tesseract-ocr
mac
brew install tesseract
win10
下载地址:
tesseract下载地址:https://digi.bib.uni-mannheim.de/tesseract/
下载最新的,下载后双击安装。安装完成后配置环境变量。
右键“此电脑”,选择“属性“->高级系统设置->环境变量->Path,双击Path,然后将tesseract的安装路径添加到Path中。
验证:
$ tesseract -v
测试:
$ tesseract example_03.png stdout digits
python调用
安装python版的tesseract
pip install pytesseract
安装完成后,还需要给pytesseract配置tesseract的环境变量。打开pytesseract的安装位置,我的是D:\ProgramData\Anaconda3\Lib\site-packages\pytesseract\pytesseract.py,将tesseract.exe的完整路径赋值给tesseract_cmd,如下图
然后测试
python代码:
# import the necessary packages
from PIL import Image
import pytesseract
import argparse
import cv2
import os
# 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")
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
text = pytesseract.image_to_string(Image.open(filename))
os.remove(filename)
print(text)
# show the output images
cv2.imshow("Image", image)
cv2.imshow("Output", gray)
cv2.waitKey(0)
执行命令:
python ocr.py --image images/example_01.png
原图与二值化后的图:
输出结果:
代码:
https://download.csdn.net/download/hhhhhhhhhhwwwwwwwwww/36672457
相关文章
- adb安装TWRP
- PDF编辑器Acrobat DC(PDF) 功能简介+安装破解
- Linux||conda的安装及使用
- docker中安装es和kibana
- CATIA V5R21,初识CATIA产品设计软件,CATIA P3下载和安装教程
- OpenStack运维之安装Placement服务
- mysql 6.0 安装指南(mysql6.0安装)
- 系统安装使用Linux系统安装ecshop(ecshoplinux)
- 安装Linux U盘模拟安装简易步骤(linux模拟u盘)
- 初级:如何在 Linux Mint 上安装 Google Chrome
- 搜狗输入法在Linux系统上的安装指南(搜狗linux输入法安装)
- 让你65秒安装mysql(6.5安装mysql)