iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 23
0
自我挑戰組

軟體開發隨筆雜記--試著解決問題系列 第 23

[Python][OpenCV]如何利用Tesserect OCR做辨識

  • 分享至 

  • twitterImage
  •  

https://zh.wikipedia.org/wiki/Tesseract
https://help.ubuntu.com/community/OCR
https://ithelp.ithome.com.tw/upload/images/20201008/201196081ECAPxxDrv.png
https://ithelp.ithome.com.tw/upload/images/20201008/20119608XpIlpijlPq.png
https://ithelp.ithome.com.tw/upload/images/20201009/20119608eZTJLVbGSG.jpg
https://ithelp.ithome.com.tw/upload/images/20201008/20119608C0mhTcaDD5.png

import pytesseract
    def init_tesserect_tab(self):
        self.tesserect_tab = tk.Frame(self.tesserectOCRPanel)
        self.tesserect_tab.pack(side = tk.TOP, expand=tk.YES, fill=tk.BOTH)
        self.tesseract_OCR_langType = ttk.Combobox(self.tesserect_tab,
                                                   font=('Courier', 10),
                                                           width = 25,
                                                   values = ["eng",
                                                             "chi_tra",
                                                             "chi_sim",
                                                             "jpn",
                                                             "ara"],
                                                   state = "readonly") 
        self.tesseract_OCR_langType.pack(side=tk.LEFT,
                                         expand=tk.NO,
                                         fill=tk.BOTH)
        self.tesseract_OCR_langType.current(0)
        self.tesseract_OCR_Button = tk.Button(self.tesserect_tab,
                                              text = "tesseract",
                                              font=('Courier', 10),
                                              command = self.tesseract_OCR)
        self.tesseract_OCR_Button.pack(side=tk.LEFT,
                                       expand=tk.NO,
                                       fill = tk.X)

    def tesseract_OCR(self, event = None):
        detectText = []
        self.OCR = pytesseract.image_to_string(
                            Image.open(self.imageFile),
                            lang = self.tesseract_OCR_langType.get() )
        self.DisplaySceneMarkInfo.insert(tk.END,self.OCR)
        detectText = pytesseract.image_to_boxes(
                            Image.open(self.imageFile),
                            lang = self.tesseract_OCR_langType.get()
                            ).splitlines()
        img = cv2.imread(self.imageFile)
        H, W, _ = img.shape
        for word in detectText:
            word_rectangle = word.split()
            x =int(word_rectangle[1])
            y =int(word_rectangle[2])
            w =int(word_rectangle[3])
            h =int(word_rectangle[4])
            imgOut = cv2.rectangle(img,
            (x,H-y),
            (w,H-h),
            self.color_1,
            int(self.linesizespinbox.get()))
        # 將 NumPy 陣列轉為 PIL 影像
        imgPil = Image.fromarray(imgOut)
        # 在圖片上加入文字
        draw = ImageDraw.Draw(imgPil)
        for word in detectText:
            word_rectangle = word.split()
            x =int(word_rectangle[1])
            y =int(word_rectangle[2])
            w =int(word_rectangle[3])
            h =int(word_rectangle[4])
            draw.text((x, H-y),
                      word_rectangle[0],
                      font=self.font,
                      fill = self.color_2)
        # 將 PIL 影像轉回 NumPy 陣列
        imgOut = np.array(imgPil)
        
        while True:
            # 顯示結果
            cv2.imshow("Output", imgOut)
            # 讀取使用者所按下的鍵
            k = cv2.waitKey(0) & 0xFF
            # 若按下 q 鍵,則離開
            if k == 113:
                break
            # 關閉圖形顯示視窗
        cv2.destroyAllWindows()
    def Select_font(self, event = None):
        .
        .
        .
    def List_font(self, event = None):
        .
        .
        .
    def init_Font_tab(self):
        self.Font_tab = tk.Frame(self.tesserectOCRPanel)
        self.Font_tab.pack(side = tk.TOP, expand=tk.YES, fill=tk.BOTH)
        ListFont = tk.Label(self.Font_tab)
        ListFont.pack(side=tk.TOP, expand=tk.NO)
        self.Table_of_font = ttk.Treeview(ListFont,height = 3)
        self.Table_of_font.heading("#0", text = "List of font")#icon column
        self.Table_of_font.column("#0", width = 500)
        self.Table_of_font.tag_configure('T', font = 'Courier,4')
        self.Table_of_font.bind("<Double-1>",self.Select_font)
        self.Table_of_font.pack(side=tk.TOP, expand=tk.NO, fill=tk.BOTH)
        self.List_font_Button = tk.Button(ListFont,
                                          text = "List font",
                                          font=('Courier', 10),
                                          command = self.List_font)
        self.List_font_Button.pack(side=tk.TOP, expand=tk.YES, fill = tk.BOTH)

設定文字,框線,字體...

    def init_setting_tab(self):
        self.setting_tab = tk.Frame(self.tesserectOCRPanel)
        self.setting_tab.pack(side = tk.TOP, expand=tk.YES, fill=tk.BOTH)
        self.MarkSettingPanel = tk.LabelFrame(self.setting_tab,
                           text="Color and font Setting Panel",
                                           font=('Courier', 10))
        self.MarkSettingPanel.pack(side=tk.TOP, expand=tk.YES, fill=tk.BOTH)

        '''Color Panel'''
        ColorPanel = tk.Frame(self.MarkSettingPanel)
        ColorPanel.grid(row = 0, column = 0 ,sticky = tk.E+tk.W)        
        self.Color1Button = tk.Button(ColorPanel,
                                      text = "Color 1",
                                      font=('Courier', 10),
                                      command = self.askcolor1)
        self.Color1Button.grid(row = 0, column = 0, sticky = tk.E+tk.W)
        self.Color2Button = tk.Button(ColorPanel,
                                      text = "Color 2",
                                      font=('Courier', 10),
                                      command = self.askcolor2)
        self.Color2Button.grid(row = 1, column = 0, sticky = tk.E+tk.W)
        '''font line type setting'''
        fontcv2Panel = tk.Frame(self.MarkSettingPanel)
        fontcv2Panel.grid(row = 0, column = 1 ,sticky = tk.E+tk.W)
        '''Line Size'''
        tk.Label(self.MarkSettingPanel,
        text = "Line size",font=('Courier', 10)).grid(row = 0,
                                                      column = 4,
                                                      sticky = tk.E+tk.W)
        self.linesizespinbox = tk.Spinbox(self.MarkSettingPanel,
                                          values = linewidth,
                                          width = 3)
        self.linesizespinbox.grid(row = 0, column = 5, sticky = tk.E+tk.W)
        '''font line type setting'''
        fontlinetypecv2Panel = tk.Frame(self.MarkSettingPanel)
        fontlinetypecv2Panel.grid(row = 0, column = 6 ,sticky = tk.E+tk.W)
        '''line type label'''
        tk.Label(fontlinetypecv2Panel,
                 text = "line type",
                 font=('Courier', 10)).pack(side = tk.TOP,
                                            expand=tk.YES,
                                            fill=tk.BOTH)
        self.fontlinetypecv2Var = tk.IntVar()
        self.fontlinetypecv2Var.set(8)
        for val, linetype, in fontlinetype_Item.items(): 
            tk.Radiobutton(fontlinetypecv2Panel,
                           text = linetype,
                           variable = self.fontlinetypecv2Var,
                           value = val,
                           font=('Courier', 10)).pack(side = tk.TOP,
                                                      expand=tk.YES,
                                                      fill=tk.BOTH)
   def askcolor1(self, event = None):
       .
       .
       .

   def askcolor2(self, event = None):
       .
       .
       .

上一篇
[Python]How to Extract Image Metadata in Python
下一篇
[Python][OpenCV]如何利用Tesserect OCR做翻譯器
系列文
軟體開發隨筆雜記--試著解決問題33
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言