如何识别图片中的表格数据(opencv 和pyteressact)

 

  在很多时候,我们的数据来源形式是多种多样的,有时候数据(或表格)也会呈现在图片中。那么,我们如何来获取图片中的有用数据呢?当一张图片中含有表格数据的时候,我们可以用OpenCV识别表格中的直线,然后再用OCR技术识别其中的文字。
  本文仅作为如何识别图片中的表格的一个例子,希望能给读者一些启示。笔者用到的工具如下:

  • opencv
  • pyteressact
  • numpy

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

示例图片 AI.png

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

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

识别表格中的横线

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

# -*- 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)

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

    # 横向直线检测
    def HorizontalLineDetect(self):

        # 图像二值化
        ret, thresh1 = cv2.threshold(self.gray, 240, 255, cv2.THRESH_BINARY)
        # 进行两次中值滤波
        blur = cv2.medianBlur(thresh1, 3)  # 模板大小3*3
        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

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

识别横线后的图片

识别表格中的竖线

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

    #  纵向直线检测
    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

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

[[13, 937, 13, 102]]
[[75, 937, 75, 102]]
[[77, 937, 77, 102]]
[[270, 937, 270, 104]]
[[272, 937, 272, 102]]
[[756, 937, 756, 102]]

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

识别竖线后的图片

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

识别图片中的单元格

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

    # 顶点检测
    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

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

第一个单元格的图片

识别单元格的文字

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

# 识别单元格中的文字
    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)

识别后的结果如下:

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

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

总结

  本文仅作为如何识别图片中的表格的一个例子,希望能给读者一些启示。对于不同的图片表格,需要具体问题具体分析。
  虽然笔者尽可能把整个过程写得简单明了,但其中的探索过程却是很复杂的。而且值得注意的是,在本文中的检测横线的方法仅适用于相邻两条记录有颜色差别的表格图片。
  完整的Python代码如下,希望能给大家一些思考。

# -*- 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)  # 模板大小3*3
        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()

注意:本人现已开通微信公众号: Python爬虫与算法(微信号为:easy_web_scrape), 欢迎大家关注哦~~

  • 8
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
使用Java进行OpenCV图像处理需要借助JavaCV库,这是一个Java与OpenCV的交互库。本文将介绍如何使用JavaCVOpenCV来实现表格文本识别。 首先需要安装JavaCVOpenCV: 1. 下载JavaCV库:https://github.com/bytedeco/javacv/releases 2. 下载OpenCV:https://opencv.org/releases/ 接下来,我们可以按照以下步骤来实现表格文本识别: 1. 加载图像 ```java Mat image = imread("path/to/image.jpg"); ``` 2. 图像预处理 图像预处理包括二值化、去噪等操作,可以提高后续的文字检测效果。 ```java Mat gray = new Mat(); cvtColor(image, gray, COLOR_BGR2GRAY); // 转换成灰度图像 Mat binaryImage = new Mat(); threshold(gray, binaryImage, 0, 255, THRESH_BINARY_INV | THRESH_OTSU); // 二值化 ``` 3. 文本检测 使用OpenCV提供的文本检测算法(如MSER、EAST等)来检测图像的文本区域。 ```java MatOfRect textRegions = new MatOfRect(); MatOfFloat confidenceScores = new MatOfFloat(); textDetector.detect(binaryImage, textRegions, confidenceScores); ``` 其,`textDetector`是文本检测器,可以根据需求选择不同的检测算法。 4. 文本识别 使用OCR技术来识别文本区域内的文字。 ```java Tesseract tesseract = new Tesseract(); // 初始化OCR引擎 tesseract.setLanguage("chi_sim"); // 设置语言 tesseract.setDatapath("path/to/tessdata"); // 设置tessdata路径 for(Rect rect : textRegions.toArray()) { Mat roi = new Mat(binaryImage, rect); String text = tesseract.doOCR(roi); System.out.println(text); } ``` 其,`Tesseract`是OCR引擎,需要下载安装,并设置tessdata路径。 完整代码示例: ```java import org.bytedeco.javacpp.Loader; import org.bytedeco.opencv.global.opencv_imgcodecs; import org.bytedeco.opencv.global.opencv_imgproc; import org.bytedeco.opencv.opencv_core.*; import static org.bytedeco.opencv.global.opencv_core.*; import static org.bytedeco.opencv.global.opencv_imgproc.*; public class TableTextRecognition { public static void main(String[] args) { Loader.load(opencv_imgcodecs.class); Loader.load(opencv_imgproc.class); Mat image = imread("path/to/image.jpg"); Mat gray = new Mat(); cvtColor(image, gray, COLOR_BGR2GRAY); Mat binaryImage = new Mat(); threshold(gray, binaryImage, 0, 255, THRESH_BINARY_INV | THRESH_OTSU); TextDetector textDetector = TextDetectorCNN.create("path/to/text_detection.pb"); MatOfRect textRegions = new MatOfRect(); MatOfFloat confidenceScores = new MatOfFloat(); textDetector.detect(binaryImage, textRegions, confidenceScores); Tesseract tesseract = new Tesseract(); tesseract.setLanguage("chi_sim"); tesseract.setDatapath("path/to/tessdata"); for(Rect rect : textRegions.toArray()) { Mat roi = new Mat(binaryImage, rect); String text = tesseract.doOCR(roi); System.out.println(text); } } } ``` 需要注意的是,使用OCR识别文本区域内的文字可能会存在识别准确率不高的问题,可以考虑使用深度学习模型来提高识别准确率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值