美文网首页大数据 爬虫Python AI SqlPython小哥哥
用OpenCV和OCR识别图片中的表格数据 !OpenCV简直太

用OpenCV和OCR识别图片中的表格数据 !OpenCV简直太

作者: 14e61d025165 | 来源:发表于2019-04-08 13:56 被阅读0次

    在很多时候,我们的数据来源形式是多种多样的,有时候数据(或表格)也会呈现在图片中。那么,我们如何来获取图片中的有用数据呢?当一张图片中含有表格数据的时候,我们可以用OpenCV识别表格中的直线,然后再用OCR技术识别其中的文字。

    本文仅作为如何识别图片中的表格的一个例子,希望能给读者一些启示。笔者用到的工具如下:

    • opencv
    • pyteressact
    • numpy

    我们用opencv来识别表格中的直线,用pyteressact来识别单元格文字,用numpy做数值处理。我们要识别的示例图片(AI.png)如下:

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1554639430478" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    示例图片 AI.png

    我们分以下几步进行识别:

    1. 识别表格中的横线,即分割记录(每一行)的横线;
    2. 识别表格中的竖线,即每个列的分割线;
    3. 找到数据所在的单元格;
    4. 利用pyteressact识别单元格的文字。

    识别表格中的横线

    识别横线之前,我们先创建一个图片表格识别类(ImageTableOCR),如下:

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># -- coding: utf-8 --
    import cv2
    import pytesseract
    import numpy as np
    class ImageTableOCR(object):
    # 初始化
    def init(self, ImagePath):
    # 读取图片
    self.image = cv2.imread(ImagePath, 1)
    # 把图片转换为灰度模式
    self.gray = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)
    </pre>

    其中self.image为RGB模块的图片,self.gray为灰度模式的图片。

    接下来,我们识别图片中的分割两条记录的横线。注意到,相邻两条记录之间的颜色是不一致的,因此,我们利用图片灰度化后,每一行像素的平均值的差的绝对值来作为相邻两条记录的分割线,这样就能检测出分割两条记录的横线了。具体的识别横线的函数的Python代码如下:(接以上代码)

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> # 横向直线检测
    def HorizontalLineDetect(self):
    # 图像二值化
    ret, thresh1 = cv2.threshold(self.gray, 240, 255, cv2.THRESH_BINARY)
    # 进行两次中值滤波
    blur = cv2.medianBlur(thresh1, 3) # 模板大小33
    blur = cv2.medianBlur(blur, 3) # 模板大小3
    3
    h, w = self.gray.shape
    # 横向直线列表
    horizontal_lines = []
    for i in range(h - 1):
    # 找到两条记录的分隔线段,以相邻两行的平均像素差大于120为标准
    if abs(np.mean(blur[i, :]) - np.mean(blur[i + 1, :])) > 120:
    # 在图像上绘制线段
    horizontal_lines.append([0, i, w, i])
    cv2.line(self.image, (0, i), (w, i), (0, 255, 0), 2)
    horizontal_lines = horizontal_lines[1:]
    # print(horizontal_lines)
    return horizontal_lines
    </pre>

    首先对图片进行二值化处理,再进行两次中值滤波,这样是为了使相邻两条记录之间的像素区别尽可能大。然后对该图片中的每一行的像素进行检测, 以相邻两行的平均像素差大于120为标准, 识别出分割两条记录的横线。识别后的横线如下:(图片中的绿色线段)

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1554639430499" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    识别横线后的图片

    识别表格中的竖线

    在这一步中,我们利用opencv中的Hough直线检测方法来检测图片中的竖线。完整的Python代码如下:(接以上代码)

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> # 纵向直线检测
    def VerticalLineDetect(self):
    # Canny边缘检测
    edges = cv2.Canny(self.gray, 30, 240)
    # Hough直线检测
    minLineLength = 500
    maxLineGap = 30
    lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100, minLineLength, maxLineGap).tolist()
    lines.append([[13, 937, 13, 102]])
    lines.append([[756, 937, 756, 102]])
    sorted_lines = sorted(lines, key=lambda x: x[0])
    # 纵向直线列表
    vertical_lines = []
    for line in sorted_lines:
    for x1, y1, x2, y2 in line:
    # 在图片上绘制纵向直线
    if x1 == x2:
    print(line)
    vertical_lines.append((x1, y1, x2, y2))
    cv2.line(self.image, (x1, y1), (x2, y2), (0, 0, 255), 2)
    return vertical_lines
    </pre>

    首先我们对灰度图片进行Canny边缘检测,在此基础上再利用Hough直线检测方法识别图片中的直线,要求识别的最大间距为30,线段长度最小为500,并且为竖直直线(x1 == x2),当然,也有一些人为的因素,那就是笔者自己添加了两条竖直直线([[13, 937, 13, 102]],[[756, 937, 756, 102]])。运行上述方法,输出的结果如下:

    <bi style="box-sizing: border-box; display: block;">[[13, 937, 13, 102]]</bi><bi style="box-sizing: border-box; display: block;">[[75, 937, 75, 102]]</bi><bi style="box-sizing: border-box; display: block;">[[77, 937, 77, 102]]</bi><bi style="box-sizing: border-box; display: block;">[[270, 937, 270, 104]]</bi><bi style="box-sizing: border-box; display: block;">[[272, 937, 272, 102]]</bi><bi style="box-sizing: border-box; display: block;">[[756, 937, 756, 102]]</bi>

    识别竖直直线后的图片如下:(图片中的红色线段)

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1554639430526" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    识别竖线后的图片

    可以看到,图片六条竖直的线段都已经完整标记出来了。

    识别图片中的单元格

    在识别图片中的单元格之前,我们先来识别每个单元格所在的顶点,也就是上述识别后的横线与竖线的交点。完整的Python代码如下:(接以上代码)

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> # 顶点检测
    def VertexDetect(self):
    vertical_lines = self.VerticalLineDetect()
    horizontal_lines = self.HorizontalLineDetect()
    # 顶点列表
    vertex = []
    for v_line in vertical_lines:
    for h_line in horizontal_lines:
    vertex.append((v_line[0], h_line[1]))
    #print(vertex)
    # 绘制顶点
    for point in vertex:
    cv2.circle(self.image, point, 1, (255, 0, 0), 2)
    return vertex
    </pre>

    顶点检测后的图片如下:(图片中的蓝色点即为每个单元格的顶点)

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1554639430549" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    顶点检测后的图片

    由此可见,我们识别出来的单元格的顶点是正确的。接着,我们把这些单元格取出来,代码如下:(接以上代码)

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 寻找单元格区域
    def CellDetect(self):
    vertical_lines = self.VerticalLineDetect()
    horizontal_lines = self.HorizontalLineDetect()
    # 顶点列表
    rects = []
    for i in range(0, len(vertical_lines) - 1, 2):
    for j in range(len(horizontal_lines) - 1):
    rects.append((vertical_lines[i][0], horizontal_lines[j][1],
    vertical_lines[i + 1][0], horizontal_lines[j + 1][1]))
    # print(rects)
    return rects
    </pre>

    以第一个单元格为例,其图像如下:

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1554639430574" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    第一个单元格的图片

    识别单元格的文字

    在识别出图片中表格的单元格后,我们可以对该单元格图片进行文字识别,我们使用的OCR工具为Teressact, 其Python的接口为pyteressact 。具体的Python代码如下:(接以上代码)

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 识别单元格中的文字
    def OCR(self):
    rects = self.CellDetect()
    thresh = self.gray
    # 特殊字符列表
    special_char_list = ' `~!@#$%^&*()-_=+[]{}|;:‘’,。《》/?ˇ'
    for i in range(20):
    rect1 = rects[i]
    DetectImage1 = thresh[rect1[1]:rect1[3], rect1[0]:rect1[2]]
    # Tesseract所在的路径
    pytesseract.pytesseract.tesseract_cmd = 'C://Program Files (x86)/Tesseract-OCR/tesseract.exe'
    # 识别数字(每行第一列)
    text1 = pytesseract.image_to_string(DetectImage1, config="--psm 10")
    print(text1, end='-->')
    # 识别汉字(每行第二列)
    rect2 = rects[i+20]
    DetectImage2 = thresh[rect2[1]:rect2[3], rect2[0]:rect2[2]]
    text2 = pytesseract.image_to_string(DetectImage2, config='--psm 7', lang='chi_sim')
    text2 = ''.join([char for char in text2 if char not in special_char_list])
    print(text2, end='-->')
    # 识别汉字(每行第三列)
    rect3 = rects[i+40]
    DetectImage3 = thresh[rect3[1]:rect3[3], rect3[0]:rect3[2]]
    text3 = pytesseract.image_to_string(DetectImage3, config='--psm 7', lang='chi_sim')
    text3 = ''.join([char for char in text3 if char not in special_char_list])
    print(text3)
    </pre>

    识别后的结果如下:

    <bi style="box-sizing: border-box; display: block;">I-->度一-->开放的人一智能服务平台</bi><bi style="box-sizing: border-box; display: block;">2-->肌讯-->互联网综合服务</bi><bi style="box-sizing: border-box; display: block;">3-->标为-->人一智能自动化业务、智能屹片</bi><bi style="box-sizing: border-box; display: block;">4-->阿里巴巴-->互联网综合服务</bi><bi style="box-sizing: border-box; display: block;">5-->平安集口-->人T智能金融研发平仄</bi><bi style="box-sizing: border-box; display: block;">6-->华大基因-->精准检测、医疗数据运营服务</bi><bi style="box-sizing: border-box; display: block;">d-->搜狗-->综合人T智能解决方案平台</bi><bi style="box-sizing: border-box; display: block;">8-->一科大讯飞-->智能语音技术</bi><bi style="box-sizing: border-box; display: block;">9-->一中利创汤-->智能终端平台技术</bi><bi style="box-sizing: border-box; display: block;">10-->珍山集团-->SaaS级智能营销云平台</bi><bi style="box-sizing: border-box; display: block;">i-->商汤科技-->人工智能视觉深度学习平台</bi><bi style="box-sizing: border-box; display: block;">12-->神州泰岳-->综合类软件产品及服务</bi><bi style="box-sizing: border-box; display: block;">13-->寒武红科技-->深度学对专用的智能盂片</bi><bi style="box-sizing: border-box; display: block;">14-->汉王科技-->文字识别技术与智能交工</bi><bi style="box-sizing: border-box; display: block;">15-->全志刑技-->智能芯片设计</bi><bi style="box-sizing: border-box; display: block;">16-->face旷视科技-->人T智能产品和行业解夷方案</bi><bi style="box-sizing: border-box; display: block;">17-->创略科技-->智能客户数据平台</bi><bi style="box-sizing: border-box; display: block;">18-->海云数据-->企业级大数据整体运营与分析服务</bi><bi style="box-sizing: border-box; display: block;">19-->影渭科技-->视觉技术、智能影像生产企业</bi><bi style="box-sizing: border-box; display: block;">20-->智蹈智能-->智能机器人技术提供和平台运萧</bi>

    下面,我们来统计一下识别的准确率。在原来的表格中,一共是20个数字加上280个汉字(包括标点)加上10个英语字母(包括标点),对于识别的结果,其中数字类识别正确17个,汉字正确256个,英语字母8个,因此,总的识别的准确率为90.6% ,识别的结果还是可以的。

    总结

    本文仅作为如何识别图片中的表格的一个例子,希望能给读者一些启示。对于不同的图片表格,需要具体问题具体分析。

    虽然笔者尽可能把整个过程写得简单明了,但其中的探索过程却是很复杂的。而且值得注意的是,在本文中的检测横线的方法仅适用于相邻两条记录有颜色差别的表格图片。

    完整的Python代码如下,希望能给大家一些思考。

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># -- coding: utf-8 --
    import cv2
    import pytesseract
    import numpy as np
    class ImageTableOCR(object):
    # 初始化
    def init(self, ImagePath):
    # 读取图片
    self.image = cv2.imread(ImagePath, 1)
    # 把图片转换为灰度模式
    self.gray = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)
    # 横向直线检测
    def HorizontalLineDetect(self):
    # 图像二值化
    ret, thresh1 = cv2.threshold(self.gray, 240, 255, cv2.THRESH_BINARY)
    # 进行两次中值滤波
    blur = cv2.medianBlur(thresh1, 3) # 模板大小33
    blur = cv2.medianBlur(blur, 3) # 模板大小3
    3
    h, w = self.gray.shape
    # 横向直线列表
    horizontal_lines = []
    for i in range(h - 1):
    # 找到两条记录的分隔线段,以相邻两行的平均像素差大于120为标准
    if abs(np.mean(blur[i, :]) - np.mean(blur[i + 1, :])) > 120:
    # 在图像上绘制线段
    horizontal_lines.append([0, i, w, i])
    # cv2.line(self.image, (0, i), (w, i), (0, 255, 0), 2)
    horizontal_lines = horizontal_lines[1:]
    # print(horizontal_lines)
    return horizontal_lines
    # 纵向直线检测
    def VerticalLineDetect(self):
    # Canny边缘检测
    edges = cv2.Canny(self.gray, 30, 240)
    # Hough直线检测
    minLineLength = 500
    maxLineGap = 30
    lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100, minLineLength, maxLineGap).tolist()
    lines.append([[13, 937, 13, 102]])
    lines.append([[756, 937, 756, 102]])
    sorted_lines = sorted(lines, key=lambda x: x[0])
    # 纵向直线列表
    vertical_lines = []
    for line in sorted_lines:
    for x1, y1, x2, y2 in line:
    # 在图片上绘制纵向直线
    if x1 == x2:
    # print(line)
    vertical_lines.append((x1, y1, x2, y2))
    # cv2.line(self.image, (x1, y1), (x2, y2), (0, 0, 255), 2)
    return vertical_lines
    # 顶点检测
    def VertexDetect(self):
    vertical_lines = self.VerticalLineDetect()
    horizontal_lines = self.HorizontalLineDetect()
    # 顶点列表
    vertex = []
    for v_line in vertical_lines:
    for h_line in horizontal_lines:
    vertex.append((v_line[0], h_line[1]))
    #print(vertex)
    # 绘制顶点
    for point in vertex:
    cv2.circle(self.image, point, 1, (255, 0, 0), 2)
    return vertex
    # 寻找单元格区域
    def CellDetect(self):
    vertical_lines = self.VerticalLineDetect()
    horizontal_lines = self.HorizontalLineDetect()
    # 顶点列表
    rects = []
    for i in range(0, len(vertical_lines) - 1, 2):
    for j in range(len(horizontal_lines) - 1):
    rects.append((vertical_lines[i][0], horizontal_lines[j][1],
    vertical_lines[i + 1][0], horizontal_lines[j + 1][1]))
    # print(rects)
    return rects
    # 识别单元格中的文字
    def OCR(self):
    rects = self.CellDetect()
    thresh = self.gray
    # 特殊字符列表
    special_char_list = ' `~!@#$%^&*()-_=+[]{}|;:‘’,。《》/?ˇ'
    for i in range(20):
    rect1 = rects[i]
    DetectImage1 = thresh[rect1[1]:rect1[3], rect1[0]:rect1[2]]
    # Tesseract所在的路径
    pytesseract.pytesseract.tesseract_cmd = 'C://Program Files (x86)/Tesseract-OCR/tesseract.exe'
    # 识别数字(每行第一列)
    text1 = pytesseract.image_to_string(DetectImage1, config="--psm 10")
    print(text1, end='-->')
    # 识别汉字(每行第二列)
    rect2 = rects[i+20]
    DetectImage2 = thresh[rect2[1]:rect2[3], rect2[0]:rect2[2]]
    text2 = pytesseract.image_to_string(DetectImage2, config='--psm 7', lang='chi_sim')
    text2 = ''.join([char for char in text2 if char not in special_char_list])
    print(text2, end='-->')
    # 识别汉字(每行第三列)
    rect3 = rects[i+40]
    DetectImage3 = thresh[rect3[1]:rect3[3], rect3[0]:rect3[2]]
    text3 = pytesseract.image_to_string(DetectImage3, config='--psm 7', lang='chi_sim')
    text3 = ''.join([char for char in text3 if char not in special_char_list])
    print(text3)
    # 显示图像
    def ShowImage(self):
    cv2.imshow('AI', self.image)
    cv2.waitKey(0)
    # cv2.imwrite('E://Horizontal.png', self.image)
    ImagePath = 'E://AI.png'
    imageOCR = ImageTableOCR(ImagePath)
    imageOCR.OCR()
    </pre>

    相关文章

      网友评论

        本文标题:用OpenCV和OCR识别图片中的表格数据 !OpenCV简直太

        本文链接:https://www.haomeiwen.com/subject/mskeiqtx.html