import pytesseract
from PIL import Image
text = pytesseract.image_to_string(Image.open(r"d:\Desktop\39DEE621-40EA-4ad1-90CC-79EB51D39347.png"))print(text)
識別結果輸出:
Using Tesseract OCR with Python
# import the necessary packagesfrom PIL import Image
import pytesseract
import ergperse
import cv2
import os# construct the argument parse and parse the arguments
ap = argparse.ArgunentParser()
ap.add_argument("-i","--image", required-True,help="path to input image to be OCR'd")
ap.add_argument("-p","--preprocess", typesstr, default="thresh",
helpe"type of preprocessing to be done")
args =vars(ap.parse_args())
ocr = PaddleOCR(use_angle_cls=True, lang="ch")# 輸入待識別圖片路徑img_path =r"d:\Desktop\4A34A16F-6B12-4ffc-88C6-FC86E4DF6912.png"# 輸出結果保存路徑result = ocr.ocr(img_path, cls=True)for line in result:print(line)from PIL import Imageimage = Image.open(img_path).convert('RGB')boxes =[line[0]for line in result]txts =[line[1][0]for line in result]scores =[line[1][1]for line in result]im_show = draw_ocr(image, boxes, txts, scores)im_show = Image.fromarray(im_show)im_show.show()
import easyocr#設置識別中英文兩種語言
reader = easyocr.Reader(['ch_sim','en'], gpu =False)# need to run only once to load model into memory
result = reader.readtext(r"d:\Desktop\4A34A16F-6B12-4ffc-88C6-FC86E4DF6912.png", detail =0)print(result)
初次運行需要在線下載檢測模型和識別模型,建議在網速好點的環境運行:
Using CPU. Note: This module is much faster with a GPU.
Downloading detection model, please wait. This may take several minutes depending upon your network connection.
Downloading recognition model, please wait. This may take several minutes depending upon your network connection.