一鍵識別圖片中的表格數據,並轉爲Excel

背景

有時候我們需要將圖片中的表格數據提取出來進行再次編輯,但一個字一個字的敲出來是件非常麻煩的事。

有沒有更好的辦法,一鍵提取圖片中的表格數據,並轉成Excel。

這時候,就需要用到OCR 技術了。

OCR

傳統OCR (Optical Character Recognition,光學字符識別)是指電子設備(例如掃描儀或數碼相機)檢查紙上打印的字符,通過檢測暗、亮的模式確定其形狀,然後用字符識別方法將形狀翻譯成計算機文字的過程;即,針對印刷體字符,採用光學的方式將紙質文檔中的文字轉換成爲黑白點陣的圖像文件,並通過識別軟件將圖像中的文字轉換成文本格式,供文字處理軟件進一步編輯加工的技術。

隨着深度學習在OCR領域的成功應用,檢測圖像中的文字區域以及識別文字內容已經變得越來越成熟。

圖片表格轉Excel,先上效果圖

待轉換圖片:

轉換後的Excel:

TableOCR

本文使用騰訊的“文字識別OCR”,每月免費1千次,可以滿足大部分普通用戶的需求。

使用準備:

  1. 申請騰訊雲賬戶secretId,secretKey
  2. 下載SDK,本文使用的是Python,可通過pip進行安裝:pip install tencentcloud-sdk-python

代碼分享:

import base64
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.ocr.v20181119 import ocr_client, models


class OCR(object):

    def img_to_excel(self, 
            image_path, 
            secret_id, 
            secret_key):

        # 實例化一個認證對象,入參需要傳入騰訊雲賬戶secretId,secretKey
        cred = credential.Credential(
            secret_id, 
            secret_key
            )

        # 實例化client對象
        httpProfile = HttpProfile()
        httpProfile.endpoint = "ocr.tencentcloudapi.com"
        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        clientProfile.signMethod = "TC3-HMAC-SHA256"
        client = ocr_client.OcrClient(cred, "ap-guangzhou", clientProfile)

        # 實例化一個請求對象
        req = models.GeneralFastOCRRequest()
        
        # 讀取圖片數據,使用Base64編碼
        with open(image_path, 'rb') as f:
            image = f.read()
            image_base64 = str(base64.b64encode(image), encoding='utf-8')
        req.ImageBase64 = image_base64

        # 通過client對象調用訪問接口,傳入請求對象
        resp = client.TableOCR(req)

        # 獲取返回數據(Data爲Base64編碼後的Excel數據)
        data = resp.Data

        # 轉換爲Excel
        path_excel = image_path + ".xlsx"
        with open(path_excel, 'wb') as f:
            f.write(base64.b64decode(data))
        return path_excel

tkGo封裝

import os
from menu.menu import EMenu
from utils.img.ocr import OCR
from utils.clipboard.clipboard import Clipboard


class MenuImg(EMenu):

    LABEL_NAME = "Img"
    LABEL_IMG_TO_EXCEL = "IMG to Excel"
    
    def __init__(self, master=None, cnf={}, **kw):
        super().__init__(master=master, cnf=cnf, **kw)
        # 添加主菜單
        master.add_cascade(label=self.LABEL_NAME, menu=self)
        # 添加子菜單-圖片表格數據轉Excel
        self.add_command(  
            label=self.LABEL_IMG_TO_EXCEL, 
            command=self.img_to_excel
            )
    
    @EMenu.thread_run(LABEL_IMG_TO_EXCEL)
    def img_to_excel(self):
        # 獲取圖片文件路徑
        data_type, data_content = Clipboard.get_data()
        if data_type != Clipboard.DATA_TYPE_FILE:
            self.msg_box_err("請先複製圖片文件", title="錯誤")
            return

        # 使用ocr進行轉換
        ocr = OCR()
        for file in data_content:
            path_excel = ocr.img_to_excel(
                image_path=file,
                secret_id=self.conf.api.TC_OCR_SECRET_ID,
                secret_key=self.conf.api.TC_OCR_SECRET_KEY
                )
            self.msg_box_info("轉換成功:\n" + path_excel)

使用說明

步驟1:複製圖片文件

步驟2:選擇Img菜單下的IMG to Excel子菜單

步驟3:轉換成功

完整代碼

GitHub上搜索TheUncleWhoGrowsBeans

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章