位置: IT常识 - 正文

Table Transformer做表格检测和识别实践(clh锅)

编辑:rootadmin
Table Transformer做表格检测和识别实践

推荐整理分享Table Transformer做表格检测和识别实践(clh锅),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:clh锅,ambition锅,锅具 wmf,锅mini,wmm锅,sentruth锅,sentruth锅,sentruth锅,内容如对您有帮助,希望把文章链接给更多的朋友!

计算机视觉方面的三大顶级会议:ICCV,CVPR,ECCV.统称ICE CVPR 2022文档图像分析与识别相关论文26篇汇集简介

论文: PubTables-1M: Towards comprehensive table extraction from unstructured documents是发表于CVPR上的一篇论文 作者发布了两个模型,表格检测和表格结构识别。

论文讲解可以参考【论文阅读】PubTables- 1M: Towards comprehensive table extraction from unstructured documents

hugging face Table Transformer 使用文档 hugging face Table DETR 使用文档

检测表格from huggingface_hub import hf_hub_downloadfrom transformers import AutoImageProcessor, TableTransformerForObjectDetectionimport torchfrom PIL import Imagefile_path = hf_hub_download(repo_id="nielsr/example-pdf", repo_type="dataset", filename="example_pdf.png")image = Image.open(file_path).convert("RGB")image_processor = AutoImageProcessor.from_pretrained("microsoft/table-transformer-detection")model = TableTransformerForObjectDetection.from_pretrained("microsoft/table-transformer-detection")inputs = image_processor(images=image, return_tensors="pt")outputs = model(**inputs)# convert outputs (bounding boxes and class logits) to COCO APItarget_sizes = torch.tensor([image.size[::-1]])results = image_processor.post_process_object_detection(outputs, threshold=0.9, target_sizes=target_sizes)[ 0]for score, label, box in zip(results["scores"], results["labels"], results["boxes"]): box = [round(i, 2) for i in box.tolist()] print( f"Detected {model.config.id2label[label.item()]} with confidence " f"{round(score.item(), 3)} at location {box}" ) region = image.crop(box) #检测 region.save('xxx.jpg') #保存# Detected table with confidence 1.0 at location [202.1, 210.59, 1119.22, 385.09]

Table Transformer做表格检测和识别实践(clh锅)

结果 :效果不错

表格结构识别

参考:https://github.com/NielsRogge/Transformers-Tutorials/blob/master/Table%20Transformer/Using_Table_Transformer_for_table_detection_and_table_structure_recognition.ipynb

import torchfrom PIL import Imagefrom transformers import DetrFeatureExtractorfrom transformers import AutoImageProcessor, TableTransformerForObjectDetectionfrom huggingface_hub import hf_hub_downloadfeature_extractor = DetrFeatureExtractor()file_path = hf_hub_download(repo_id="nielsr/example-pdf", repo_type="dataset", filename="example_pdf.png")image = Image.open(file_path).convert("RGB")encoding = feature_extractor(image, return_tensors="pt")model = TableTransformerForObjectDetection.from_pretrained("microsoft/table-transformer-structure-recognition")with torch.no_grad(): outputs = model(**encoding)target_sizes = [image.size[::-1]]results = feature_extractor.post_process_object_detection(outputs, threshold=0.6, target_sizes=target_sizes)[0]# plot_results(image, results['scores'], results['labels'], results['boxes'])results

获取列图像:

columns_box_list = [results['boxes'][i].tolist() for i in range(len(results['boxes'])) if results['labels'][i].item()==1]columns_1 = image.crop(columns_box_list[0]) columns_1.save('columns_1.jpg') #保存

可视化:import matplotlib.pyplot as plt# colors for visualizationCOLORS = [[0.000, 0.447, 0.741], [0.850, 0.325, 0.098], [0.929, 0.694, 0.125], [0.494, 0.184, 0.556], [0.466, 0.674, 0.188], [0.301, 0.745, 0.933]]def plot_results(pil_img, scores, labels, boxes): plt.figure(figsize=(16, 10)) plt.imshow(pil_img) ax = plt.gca() colors = COLORS * 100 for score, label, (xmin, ymin, xmax, ymax), c in zip(scores.tolist(), labels.tolist(), boxes.tolist(), colors): ax.add_patch(plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin, fill=False, color=c, linewidth=3)) text = f'{model.config.id2label[label]}: {score:0.2f}' ax.text(xmin, ymin, text, fontsize=15, bbox=dict(facecolor='yellow', alpha=0.5)) plt.axis('off') plt.show()post_process_object_detection方法:

OpenCV PIL图像格式互转

参考:https://blog.csdn.net/dcrmg/article/details/78147219

PIL–》OpenCV

cv2.cvtColor(numpy.asarray(image),cv2.COLOR_RGB2BGR)import cv2from PIL import Imageimport numpyimage = Image.open("plane.jpg")image.show()img = cv2.cvtColor(numpy.asarray(image),cv2.COLOR_RGB2BGR)cv2.imshow("OpenCV",img)cv2.waitKey()

OpenCV --》 PIL

Image.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))import cv2from PIL import Imageimport numpyimg = cv2.imread("plane.jpg")cv2.imshow("OpenCV",img)image = Image.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))image.show()cv2.waitKey()

综上,模型检测列代码如下

# 检测模型import cv2from huggingface_hub import hf_hub_downloadfrom transformers import AutoImageProcessor, TableTransformerForObjectDetectionimport torchfrom PIL import Imageimport torchfrom PIL import Imagefrom transformers import DetrFeatureExtractorfrom transformers import AutoImageProcessor, TableTransformerForObjectDetectionfrom huggingface_hub import hf_hub_downloadimport numpy as npimport matplotlib.pyplot as pltimport cv2def dectect_table(file_path): # file_path = hf_hub_download(repo_id="nielsr/example-pdf", repo_type="dataset", filename="example_pdf.png") image = Image.open(file_path).convert("RGB") # transformers.AutoImageProcessor 是一个通用图像处理器 image_processor = AutoImageProcessor.from_pretrained("microsoft/table-transformer-detection") model = TableTransformerForObjectDetection.from_pretrained("microsoft/table-transformer-detection") inputs = image_processor(images=image, return_tensors="pt") outputs = model(**inputs) # convert outputs (bounding boxes and class logits) to COCO API target_sizes = torch.tensor([image.size[::-1]]) results = image_processor.post_process_object_detection(outputs, threshold=0.9, target_sizes=target_sizes)[ 0 ] box_list = [] for score, label, box in zip(results["scores"], results["labels"], results["boxes"]): box = [round(i, 2) for i in box.tolist()] print( f"Detected {model.config.id2label[label.item()]} with confidence " f"{round(score.item(), 3)} at location {box}" ) box_list.append(box) region = image.crop(box) #检测 # region.save('xxx.jpg') #保存 return region#def plot_results(pil_img, scores, labels, boxes): # colors for visualization COLORS = [[0.000, 0.447, 0.741], [0.850, 0.325, 0.098], [0.929, 0.694, 0.125], [0.494, 0.184, 0.556], [0.466, 0.674, 0.188], [0.301, 0.745, 0.933]] plt.figure(figsize=(16, 10)) plt.imshow(pil_img) ax = plt.gca() colors = COLORS * 100 for score, label, (xmin, ymin, xmax, ymax), c in zip(scores.tolist(), labels.tolist(), boxes.tolist(), colors): if label == 1: ax.add_patch(plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin, fill=False, color=c, linewidth=3)) # text = f'{model.config.id2label[label]}: {score:0.2f}' text = f'{score:0.2f}' ax.text(xmin, ymin, text, fontsize=15, bbox=dict(facecolor='yellow', alpha=0.5)) plt.axis('off') plt.show()def cv_show(img): ''' 展示图片 @param img: @param name: @return: ''' cv2.namedWindow('name', cv2.WINDOW_KEEPRATIO) # cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO cv2.imshow('name', img) cv2.waitKey(0) cv2.destroyAllWindows()def dect_col(file_path): ''' 识别列 :param file_path: :return: ''' # example_table= region # width, height = image.size # image.resize((int(width * 0.5), int(height * 0.5))) table = dectect_table(file_path) # 截取左半边 feature_extractor = DetrFeatureExtractor() # file_path = hf_hub_download(repo_id="nielsr/example-pdf", repo_type="dataset", filename="example_table.png") # image = Image.open(file_path).convert("RGB") # image = cv2.imread(file_path) left_table = table.crop((0, 0, table.size[0]//2,table.size[1])) encoding = feature_extractor(left_table, return_tensors="pt") model = TableTransformerForObjectDetection.from_pretrained("microsoft/table-transformer-structure-recognition") with torch.no_grad(): outputs = model(**encoding) target_sizes = [left_table.size[::-1]] results = feature_extractor.post_process_object_detection(outputs, threshold=0.6, target_sizes=target_sizes)[0] plot_results(left_table, results['scores'], results['labels'], results['boxes']) # columns_box_list = [results['boxes'][i].tolist() for i in range(len(results['boxes'])) if results['labels'][i].item()==1] # columns_box_list.sort() # columns_1 = left_table.crop(columns_box_list[0]) # left, upper, right, lower # columns_1.save('columns_1.jpg') #保存 return columns_box_listdect_col(r'xxxx.jpg')
本文链接地址:https://www.jiuchutong.com/zhishi/295358.html 转载请保留说明!

上一篇:07---vue前端实现增删改查(vue.js前端)

下一篇:HTML学生个人网站作业设计:个人主页博客web网页设计制作 (HTML+CSS) (1)(学生个人网页制作html5)

  • 电风扇不转是什么原因嗡嗡响(电风扇不转是什么原因)(电风扇不转是什么原因?怎么解决)

    电风扇不转是什么原因嗡嗡响(电风扇不转是什么原因)(电风扇不转是什么原因?怎么解决)

  • 小米日历广告如何关闭(小米日历广告关闭)

    小米日历广告如何关闭(小米日历广告关闭)

  • 显卡和cpu哪个更重要(显卡和cpu哪个更耗电)

    显卡和cpu哪个更重要(显卡和cpu哪个更耗电)

  • vivo手机如何连接蓝牙(vivo手机如何连接空调开关)

    vivo手机如何连接蓝牙(vivo手机如何连接空调开关)

  • i36100配什么样的显卡(i36100配什么系统)

    i36100配什么样的显卡(i36100配什么系统)

  • word文档怎样给文档加入页码(word文档怎样给图片添加边框)

    word文档怎样给文档加入页码(word文档怎样给图片添加边框)

  • sim卡是手机号卡吗(手机sim卡属于什么卡)

    sim卡是手机号卡吗(手机sim卡属于什么卡)

  • x_t文件用什么软件打开

    x_t文件用什么软件打开

  • 荣耀x10什么时候预售(荣耀70什么时候上市)

    荣耀x10什么时候预售(荣耀70什么时候上市)

  • 淘宝裂变优惠券怎么设置(淘宝裂变优惠券怎么设置到详情页)

    淘宝裂变优惠券怎么设置(淘宝裂变优惠券怎么设置到详情页)

  • 开发者选项要不要开启(开发者选项开启的坏处)

    开发者选项要不要开启(开发者选项开启的坏处)

  • qq空间注销申请要多久(qq空间注销申请网址官方网站)

    qq空间注销申请要多久(qq空间注销申请网址官方网站)

  • 二维码检票是什么意思(二维码检票是什么情况)

    二维码检票是什么意思(二维码检票是什么情况)

  • 手机ipad同时上微信记录不同步怎么办

    手机ipad同时上微信记录不同步怎么办

  • eva-al00是华为什么型号(华为eva-al00什么型号)

    eva-al00是华为什么型号(华为eva-al00什么型号)

  • 怎么让电脑下载一晚上(怎么让电脑下载完自动关机)

    怎么让电脑下载一晚上(怎么让电脑下载完自动关机)

  • 计算机性能主要取决于(计算机性能主要指标是指)

    计算机性能主要取决于(计算机性能主要指标是指)

  • 进程实体是由哪三部分组成(进程实体由哪四部分组成)

    进程实体是由哪三部分组成(进程实体由哪四部分组成)

  • 华为畅享10plus是闪充吗(华为畅享10plus是什么屏幕)

    华为畅享10plus是闪充吗(华为畅享10plus是什么屏幕)

  • id被锁了怎么才能解锁(id被锁了怎么才能解锁电子邮件验证不了)

    id被锁了怎么才能解锁(id被锁了怎么才能解锁电子邮件验证不了)

  • 两个抖音号怎么合并(两个抖音号怎么切换)

    两个抖音号怎么合并(两个抖音号怎么切换)

  • iphonexs参数(iphonexsmax参数)

    iphonexs参数(iphonexsmax参数)

  • iphonex不贴膜能划坏屏幕吗(iphonex不贴膜会刮花吗)

    iphonex不贴膜能划坏屏幕吗(iphonex不贴膜会刮花吗)

  • 微信主页面怎么更换(微信主页面怎么变成黑色了)

    微信主页面怎么更换(微信主页面怎么变成黑色了)

  • vivo如何查询有碎屏险(怎样查看vivo)

    vivo如何查询有碎屏险(怎样查看vivo)

  • ipone哪里设置学生模式(苹果手机如何设置学生模式)

    ipone哪里设置学生模式(苹果手机如何设置学生模式)

  • 乐视视频如何缓存视频(乐视视频缓存的视频怎么保存到手机)

    乐视视频如何缓存视频(乐视视频缓存的视频怎么保存到手机)

  • win10开机启动文件夹路径是什么(win10开机启动文件路径)

    win10开机启动文件夹路径是什么(win10开机启动文件路径)

  • 费用的进项税额可以抵扣吗
  • 企业所得税应纳税所得额怎么算
  • 计税基础怎么算
  • 清算组的性质
  • 税法规定的增值税
  • 个人社保部分公司承担可以入费用吗
  • 开发票需要填银行吗
  • 公司零星支出没有发票收据怎么开
  • 公司现金支票取钱需要带什么资料
  • 包工包料的税率2023
  • 个税身份验证不通过
  • 预算为负数实际为负数 怎么计算完成率
  • 通过认证的增值税怎么算
  • 哪些合同不需要缴纳印花税的通知
  • 结转本年度收入
  • 事业单位利息收入
  • 购买座机计入哪个科目?
  • 个人建筑安装如何交税
  • 关于建筑工程发包与承包下列说法正确的是
  • 减半征收城建税文件
  • 租用服务器会泄漏数据吗
  • 被替换的账面价值题目
  • 错账查找的方法
  • windows没有搜索
  • 印花税按次按月
  • bwkp.exe是什么程序
  • vue 大屏可视化设计 开源
  • 转入固定资产清理会计科目
  • 土地使用税计入管理费用还是税金及附加
  • 小微企业的季度所得税怎么计算
  • mysql触发器创建
  • 大前端技术架构
  • 终止cat命令
  • 机票的退票费计入什么会计科目
  • 帝国cms界面
  • system error
  • wordpress建立数据库错误
  • 固定资产清理是什么意思
  • 软件公司股权
  • SQLite中的WAL机制详细介绍
  • 购买债券的利息会计分录
  • 购买的烟酒怎么入账
  • 税控盘每年的服务费可以全额抵扣吗
  • 管理费用主要包括
  • 一般纳税人什么情况可以开3%的发票
  • 公司注销实收资本怎么处理
  • 营业成本指的什么
  • 一般纳税人不得领用专票的情形
  • 事业单位结余是什么意思
  • 成本结转怎么做分录
  • 小规模注销时盈余怎么算
  • 纳税评估补缴的增值税影响所得税吗
  • 小规模取得增值税专用发票怎么做账
  • 增值税普通发票怎么开
  • 货物发生退运了怎么办
  • 劳务费收入交印花税吗
  • 普票的销项负数怎么开
  • 地方教育附加申报
  • 票据粘贴处怎么粘
  • 发票金额大于付款金额可以报销吗
  • 营改增租赁服务有哪些
  • 存货盘亏进项税额转出会计分录
  • 启用账簿时应在账簿上签名或盖章的是
  • 个体工商户达到多少缴税
  • win10免费安装吗
  • vic32.dll是什么
  • mac怎么设置应用权限
  • win8系统怎么安装win10
  • win8.1如何关闭windows defender
  • 新版itunes怎么导入音乐
  • bootcamp不用u盘
  • macbookair怎么验证
  • win10如何删除windows账户
  • jquery源码分析笔记
  • dos的命令大全
  • androidstudio 教程
  • python类的理解
  • jquery map遍历
  • 房屋附属设备和配套设施计征房产税
  • 国家税务局总局政策咨询
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设