自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

神經(jīng)網(wǎng)絡(luò)提取PDF表格工具來(lái)了,支持圖片,還能白嫖谷歌GPU資源

新聞 深度學(xué)習(xí)
百度一下,網(wǎng)上有大量提取PDF表格的工具,但是,它們都只支持文本格式PDF。一種使用深度神經(jīng)網(wǎng)絡(luò)識(shí)別提取表格的開(kāi)源工具可以幫助你。

 [[335375]]

本文經(jīng)AI新媒體量子位(公眾號(hào)ID:QbitAI)授權(quán)轉(zhuǎn)載,轉(zhuǎn)載請(qǐng)聯(lián)系出處。

“表哥表姐”們還在為大量PDF文件中的表格發(fā)愁嗎?

百度一下,網(wǎng)上有大量提取PDF表格的工具,但是,它們都只支持文本格式PDF。

但掃描生成的圖片表格怎么辦?

別著急,一種使用深度神經(jīng)網(wǎng)絡(luò)識(shí)別提取表格的開(kāi)源工具可以幫助你。

兼容圖片、高準(zhǔn)確率、還不占用本地運(yùn)算資源,如此實(shí)用的工具值得你擁有。

測(cè)試實(shí)例

如果在輸入的PDF文件中檢測(cè)的表格,模型會(huì)在邊界框(bounding box)標(biāo)出表格邊框:

神經(jīng)網(wǎng)絡(luò)提取PDF表格工具來(lái)了,支持圖片,還能白嫖谷歌GPU資源

然后,表格數(shù)據(jù)會(huì)被轉(zhuǎn)化為Panda數(shù)據(jù)框架,方便后續(xù)處理:

神經(jīng)網(wǎng)絡(luò)提取PDF表格工具來(lái)了,支持圖片,還能白嫖谷歌GPU資源

怎么樣,是不是很實(shí)用?那這個(gè)工具如何使用呢?

使用姿勢(shì)詳解

神經(jīng)網(wǎng)絡(luò)算法,還不占用本地運(yùn)算資源?

對(duì),你沒(méi)聽(tīng)錯(cuò),這個(gè)工具的所有代碼都可以在谷歌Colab上運(yùn)行。也就是說(shuō)你可以利用Colab云端資源完成訓(xùn)練和推理,無(wú)需本地安裝。

這個(gè)工具使用到的深度神經(jīng)網(wǎng)絡(luò)是Keras-RetinaNet,首先要在Colab上安裝Keras-RetinaNet,通過(guò)一下一行代碼就可以完成:

  1. git clone https://github.com/fizyr/keras-retinanet 

同時(shí)需要安裝必要的庫(kù):

  1. pip install .python setup.py build_ext — inplace 

訓(xùn)練Keras-RetinaNet識(shí)別表格

首先要構(gòu)建或一個(gè)訓(xùn)練使用的數(shù)據(jù)庫(kù)。

這里要用到Colab工具PDF2Img,將PDF文件轉(zhuǎn)為JPG格式供算法學(xué)習(xí)。

神經(jīng)網(wǎng)絡(luò)提取PDF表格工具來(lái)了,支持圖片,還能白嫖谷歌GPU資源

然后將轉(zhuǎn)好的圖片保存在Images文件夾中。接下來(lái)需要手動(dòng)給這些訓(xùn)練數(shù)據(jù)打標(biāo)簽,這里推薦使用在線標(biāo)記工具makesense.ai。

神經(jīng)網(wǎng)絡(luò)提取PDF表格工具來(lái)了,支持圖片,還能白嫖谷歌GPU資源

將XML注釋文件保存在注釋文件夾中,并創(chuàng)建用于培訓(xùn)和測(cè)試的PDF文件列表,將該列表導(dǎo)入train.txt和test.txt中。

神經(jīng)網(wǎng)絡(luò)提取PDF表格工具來(lái)了,支持圖片,還能白嫖谷歌GPU資源

接下來(lái),克隆Github項(xiàng)目https://github.com/ferrygun/PDFTableExtract,并安裝beautifulsoup。

運(yùn)行以下命令以將PASCALVOC格式轉(zhuǎn)換為Keras-RetinaNet所需的格式:

  1. python build_logos.py 
神經(jīng)網(wǎng)絡(luò)提取PDF表格工具來(lái)了,支持圖片,還能白嫖谷歌GPU資源

運(yùn)行上述命令后,會(huì)得到retinanet_classes.csv,retinanet_test.csv和retinanet_train.csv。

在retinanet_classses.csv中,由于只識(shí)別PDF文檔中的表,所以只有到一個(gè)class,即class 0。

如果你在打標(biāo)簽的過(guò)程中加入頁(yè)眉頁(yè)腳等標(biāo)簽,相應(yīng)能得到多個(gè)class。

完整的文件和文件夾結(jié)構(gòu)的列表:

神經(jīng)網(wǎng)絡(luò)提取PDF表格工具來(lái)了,支持圖片,還能白嫖谷歌GPU資源

然后,將retinanet_classes.csv,retinanet_test.csv,retinanet_train.csv,train.txt和test.txt 導(dǎo)入keras-retinanet的根文件夾中:

神經(jīng)網(wǎng)絡(luò)提取PDF表格工具來(lái)了,支持圖片,還能白嫖谷歌GPU資源

接下來(lái),運(yùn)行Colab TrainOCR,可以根據(jù)要訓(xùn)練的JPG文件數(shù)量來(lái)調(diào)整訓(xùn)練的epoch數(shù)量。

訓(xùn)練完成后,就會(huì)得到權(quán)重文件output.h5,下載此文件并將其保存到本地主機(jī)。后面將使用該文件來(lái)運(yùn)行測(cè)試。

神經(jīng)網(wǎng)絡(luò)提取PDF表格工具來(lái)了,支持圖片,還能白嫖谷歌GPU資源

這里需要注意的一點(diǎn)是,在Colab中,已將文件上傳到Git并進(jìn)行了Git克隆。

運(yùn)行測(cè)試

運(yùn)行測(cè)試前,還需要安裝處理PDF文件必要的庫(kù)。

PyPDF2是一個(gè)python工具庫(kù),能夠提取文檔信息,裁剪頁(yè)面等。

使用以下命令安裝此庫(kù):

  1. pip install PyPDF2 

Camelot是專門用于解析PDF頁(yè)面表格的python庫(kù)。

使用以下命令安裝此庫(kù):

  1. pip install camelot-py[cv] 

PDF2IMG是將Python轉(zhuǎn)換為PIL Image對(duì)象的python庫(kù)。

使用以下命令安裝此庫(kù):

  1. pip install pdf2imag 

在運(yùn)行預(yù)測(cè)之前,需要使用從訓(xùn)練中獲得的權(quán)重文件output.h5加載模型,并從retinanet_classes.csv定義標(biāo)簽class 0。

  1. model_path = ‘output.h5’model = models.load_model(model_path, backbone_name=’resnet50’)labels = ‘retinanet_classes.csv’LABELS = open(labels).read().strip().split(‘\n’)LABELS = {int(L.split(‘,’)[1]): L.split(‘,’)[0for L in LABELS}print(LABELS){0: ‘tabel’} 

接下來(lái)運(yùn)行測(cè)試

  1. import numpy as npimport cv2import matplotlib.pyplot as pltimage_path = imgfname#image = cv2.imread(image_path)image = read_image_bgr(image_path)image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)output = image.copy()output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)print(output.shape) # row (height) x column (width) x color (3)image = preprocess_image(image)(image, scale) = resize_image(image)image = np.expand_dims(image, axis=0)# detect objects in the input image and correct for the image scale(boxes, scores, labels) = model.predict_on_batch(image)boxes /= scaleconfidence =  0.2from google.colab.patches import cv2_imshowimport matplotlib.pyplot as pltlabel_out = []result = ""# loop over the detectionsfor (box, score, label) in zip(boxes[0], scores[0], labels[0]):  # filter out weak detections  if score < confidence:    continue  # convert the bounding box coordinates from floats to integers  box = box.astype("int")  # build the label and draw the label + bounding box on the output  # image  labeli = label  label = "{}: {:.2f}".format(LABELS[label], score)  print(label)  if LABELS[labeli] not in label_out:      label_out.append(LABELS[labeli])      cv2.rectangle(output, (box[0], box[1]), (box[2], box[3]), (25500), 12)      print(box[0])      print(box[1])      print(box[2])      print(box[3])      #result            cv2.putText(output, label, (box[0], box[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 4.5, (25511), 12)plt.figure(figsize=(2020))plt.imshow(output)plt.show() 

如果檢測(cè)的表格,模型會(huì)在邊界框(bounding box)標(biāo)出表格邊框:

神經(jīng)網(wǎng)絡(luò)提取PDF表格工具來(lái)了,支持圖片,還能白嫖谷歌GPU資源

邊界框坐標(biāo)(x1,y1,x2,y2),將table_area輸入到Camelot read_pdf函數(shù)中,table_area是已標(biāo)準(zhǔn)化的邊界框。

  1. interesting_areas=[]output = [[x1, y1, x2, y2]]for x in output:  [x1, y1, x2, y2] = bboxes_pdf(img, pdf_page, x)  bbox_camelot = [            ",".join([str(x1), str(y1), str(x2), str(y2)])        ][0]  # x1,y1,x2,y2 where (x1, y1) -> left-top and (x2, y2) -> right-bottom in PDF coordinate space        #print(bbox_camelot)  interesting_areas.append(bbox_camelot)print(interesting_areas)output_camelot = camelot.read_pdf(    filepath=pdf_file, pages=str(pg), flavor="stream", table_areas=interesting_areas)output_camelot[0].df 
  2. 這樣就可以將表格數(shù)據(jù)轉(zhuǎn)化為Panda數(shù)據(jù)框架,方便后續(xù)處理: 
神經(jīng)網(wǎng)絡(luò)提取PDF表格工具來(lái)了,支持圖片,還能白嫖谷歌GPU資源

圖片怎么辦

針對(duì)掃描圖片格式的PDF表格,可以使用Ocrmypdf包來(lái)將圖片格式PDF轉(zhuǎn)為文本格式。

Ocrmypdf是一個(gè)python軟件包,可將基于圖像的pdf轉(zhuǎn)換為基于文本的PDF。

安裝ocrmypdf,可以通過(guò)以下命令行將其用于macOS和Linux:

  1. brew install ocrmypdf 

通過(guò)以下命令調(diào)用:

  1. ocrmypdf input_file.pdf output_file.pdf 

之后就可以按照上面的方法進(jìn)行表格提取了。

怎么樣,這款兼容圖片,又能白嫖谷歌GPU的PDF表格工具對(duì)你有幫助嗎?

傳送門

項(xiàng)目地址:
https://github.com/ferrygun/PDFTableExtract

在線打標(biāo)簽工具:
https://www.makesense.ai/

 

責(zé)任編輯:張燕妮 來(lái)源: 量子位
相關(guān)推薦

2019-07-02 13:37:23

神經(jīng)網(wǎng)絡(luò)運(yùn)算Python

2023-10-29 18:08:33

GPU神經(jīng)網(wǎng)絡(luò)CPU

2017-06-20 14:41:12

google神經(jīng)網(wǎng)絡(luò)

2017-03-22 11:59:40

深度神經(jīng)網(wǎng)絡(luò)

2018-06-18 14:19:47

IBM神經(jīng)網(wǎng)絡(luò)GPU

2023-05-12 09:59:51

人工智能谷歌

2018-07-03 16:10:04

神經(jīng)網(wǎng)絡(luò)生物神經(jīng)網(wǎng)絡(luò)人工神經(jīng)網(wǎng)絡(luò)

2019-10-09 15:51:45

Python 開(kāi)發(fā)編程語(yǔ)言

2020-09-07 06:30:00

神經(jīng)網(wǎng)絡(luò)數(shù)據(jù)圖形

2023-06-13 23:13:40

ChatGPT人工智能語(yǔ)言模型

2018-03-21 10:14:38

JavaScript交叉GPU

2022-01-22 08:53:37

瀏覽器端Windows 端Java開(kāi)發(fā)

2019-07-18 08:54:42

谷歌Android開(kāi)發(fā)者

2017-09-10 07:07:32

神經(jīng)網(wǎng)絡(luò)數(shù)據(jù)集可視化

2025-02-25 14:13:31

2021-12-17 12:12:22

Python 開(kāi)發(fā)數(shù)據(jù)

2023-05-24 09:56:40

谷歌AI編程神器

2020-08-06 10:11:13

神經(jīng)網(wǎng)絡(luò)機(jī)器學(xué)習(xí)算法

2020-10-20 09:45:23

神經(jīng)網(wǎng)絡(luò)數(shù)據(jù)圖形

2017-05-04 18:30:34

大數(shù)據(jù)卷積神經(jīng)網(wǎng)絡(luò)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)