1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        使用Tesseract做文字識別(OCR)

        共 2342字,需瀏覽 5分鐘

         ·

        2022-02-09 17:41

        前言

        OCR(optical character recognition,光學字符識別)是指直接將包含文本的圖像識別為計算機文字(計算機黑白點陣)的技術(shù)。圖像中的文本一般為印刷體文本。

        Tesseract是github上的OCR開源庫,今天我將使用Tesseract來進行文字識別。

        安裝Tesseract

        安裝Tesseract挺簡單的,直接按照官網(wǎng)上安裝方法安裝即可。安裝完記得配一下環(huán)境變量。

        Tesseract官網(wǎng)

        我是在Win10下安裝的,打開命令行,輸入tesseract -v查看Tesseract版本號,輸出以下信息表示安裝成功:

        Tesseract做文字識別

        現(xiàn)在我使用Tesseract來識別下面圖片中的字符:

        命令行運行:(指定簡體中文)

         tesseract 5.png stdout -l chi_sim

        輸出如下:

        可以看到,Tesseract很好的識別了圖片中的文字。

        上面的測試用例背景十分干凈,對比明顯,Tesseract識別得很好,但是現(xiàn)實中的圖片可能沒有這么好的條件,直接識別可能會出錯,往往要先進行圖像處理,然后將處理后的圖片送入Tesseract文字識別。

        Python中使用Tesseract

        Python安裝Tesseract接口:

        pip install pillow
        pip install pytesseract

        注意:Python只是提夠了調(diào)用Tesseract的接口,方便我們在Python程序中使用Tesseract,實際運行的還是前面安裝的Tesseract庫。

        使用以下代碼測試:

        # USAGE
        # python ocr.py --image images/example_01.png 
        # python ocr.py --image images/example_02.png  --preprocess blur
        
        # 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)
        
        cv2.imshow("Image", gray)
        
        # 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腳本對輸入圖像先進行了簡單的圖像處理,比如模糊和二值化。然后將處理后的圖片使用tesseract進行文字識別。

        測試圖片1為:

        測試圖片1

        命令行運行:

        python ocr.py --image images/example_01.png

        經(jīng)過閾值分割后的圖像如下,可以看到把背景陰影很好的去掉了:

        命令行輸出如下,正確的識別了結(jié)果。

        總結(jié)

        直接使用tesseract識別圖片文字容易出錯,一般先對圖片做圖像處理后再使用tesseract識別文字。

        瀏覽 24
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            国产青草网 | 动漫美女扒开腿让男人桶爽 | 美女被猛操 | 一级淫荡片 | 黄色在线免费电影 | 三级片无码麻豆视频 | 免费三级网站 | 黄色成人在线 | 男人捅女人免费网站 | 亚洲h片|