Merge pull request #28 from 8baby8/main

Modifying Ocr Scripts
This commit is contained in:
chaoke 2024-02-29 12:27:20 +08:00 committed by GitHub
commit 5193ad656b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,9 +1,9 @@
import os
import sys
import glob
try :
try:
import cv2
except:
except :
os.system('pip install opencv-python')
try :
from paddleocr import PaddleOCR , draw_ocr , download_with_progressbar
@ -16,9 +16,7 @@ if not os.path.exists(output_folder_path):
def get_pdf_files_in_directory(directory_path):
# 确保路径存在
if os.path.exists(directory_path) and os.path.isdir(directory_path):
# 使用glob模块搜索所有PDF文件
pdf_files = glob.glob(os.path.join(directory_path, '**', '*.pdf'), recursive=True)
return pdf_files
return glob.glob(os.path.join(directory_path, '**', '*.pdf'), recursive=True)
else:
return []
def ocr_pdf_folder(folder_path):
@ -30,23 +28,23 @@ def ocr_pdf_folder(folder_path):
for pdf_path in pdf_paths:
print(f'正在处理文件:{pdf_path}')
result = ocr . ocr ( pdf_path , cls = True )
for idx in range ( len ( result )):
res = result [ idx ]
for line in res :
print ( line )
result = ocr.ocr (pdf_path , cls = True )
for idx in range(len(result)):
res = result[idx]
for line in res :
print(line)
print(f'{pdf_path} 处理完毕')
ocr_result = ""
for idx in range(len(result)):
res = result[idx]
for line in res:
# print(line[1][0])
ocr_result = ocr_result + " " + str(line[1][0])
ocr_result = f"{ocr_result} {str(line[1][0])}"
filename = os.path.splitext(os.path.basename(pdf_path))[0]
# 构建TXT文件的完整路径
txt_path = os.path.join('res/', filename + '.txt')
txt_path = os.path.join('res/', f'{filename}.txt')
# 将提取的文本写入TXT文件
with open(txt_path, 'w', encoding='utf-8') as txt_file: