-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path003-main1a.py
More file actions
149 lines (123 loc) · 6.2 KB
/
Copy path003-main1a.py
File metadata and controls
149 lines (123 loc) · 6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import concurrent.futures
import os
import shutil
import numpy as np
from pdf2image import convert_from_path
from paddleocr import PaddleOCR
import random
from PIL import Image
def convert_pdf_to_images(pdf_path, min_snapshot_size=336, max_snapshot_size=768):
pages = convert_from_path(pdf_path)
snapshots = [extract_snapshots(page, random_snapshot_size(min_snapshot_size, max_snapshot_size)) for page in pages]
return snapshots
def random_snapshot_size(min_size, max_size):
snapshot_size = random.randint(min_size, max_size)
return (snapshot_size, snapshot_size)
def extract_snapshots(image, snapshot_size=(512, 512)):
width, height = image.size
snapshots = []
for y in range(0, height, snapshot_size[1]):
for x in range(0, width, snapshot_size[0]):
snapshot = image.crop((x, y, x + snapshot_size[0], y + snapshot_size[1]))
snapshots.append(snapshot)
return snapshots
def extract_text_and_boxes(snapshots):
ocr = PaddleOCR(lang='en', show_log=False)
results = []
for page_snapshots in snapshots:
page_result = []
for img in page_snapshots:
img_array = np.array(img)
result = ocr.ocr(img_array)
page_result.extend(result)
results.append(page_result)
return results
def save_snapshot_image(image, snapshot_idx, output_dir):
image_path = f"{output_dir}/snapshot_{snapshot_idx:03}.png"
image.save(image_path)
def save_results(snapshots, text_and_boxes, output_folder, resized_size=(336, 336)):
os.makedirs(output_folder, exist_ok=True)
snapshot_counter = 0
for page, (page_snapshots, page_result) in enumerate(zip(snapshots, text_and_boxes), start=1):
with open(f"{output_folder}/page_{page:03}.txt", "w") as output_file:
output_file.write(f"Page {page}:\n")
for snapshot_idx, (snapshot_image, result) in enumerate(zip(page_snapshots, page_result)):
output_file.write(f"snapshot_{snapshot_counter + 1}:\n")
image_path = f"{output_folder}/snapshot_{snapshot_counter:03}.png"
save_snapshot_image(snapshot_image, snapshot_counter, output_folder)
img_width, img_height = snapshot_image.size
bb_file_path = f"{output_folder}/snapshot_{snapshot_counter:03}_bb.txt"
with open(bb_file_path, "w") as bb_file:
for line in result:
if len(line) >= 2 and len(line[1]) > 0:
bbox = [[coord[0] / img_width, coord[1] / img_height] for coord in line[0]]
text = line[1][0]
bb_file.write(f"Bounding box: {bbox}, Text: {text}\n")
bb_file.flush()
os.fsync(bb_file.fileno())
if os.stat(bb_file_path).st_size == 0:
os.remove(bb_file_path)
if os.path.exists(image_path):
os.remove(image_path)
else:
with Image.open(image_path) as img:
resized_img = img.resize(resized_size)
resized_img.save(image_path)
snapshot_counter += 1
output_file.flush()
os.fsync(output_file.fileno())
def move_files(src_dir, dst_dir):
if not os.path.exists(dst_dir):
os.makedirs(dst_dir)
for filename in os.listdir(src_dir):
src_path = os.path.join(src_dir, filename)
dst_path = os.path.join(dst_dir, filename)
shutil.move(src_path, dst_path)
# Removed URL processing and download functions
def main(pdf_path, output_folder):
# Get the basename of the PDF file (without the '.pdf' extension)
pdf_basename = os.path.basename(pdf_path).rsplit('.', 1)[0]
# Check if the OCR result for this PDF file already exists
if os.path.exists(os.path.join('pdf-img-cluster', pdf_basename)):
print(f"Skipping {pdf_path} because it has already been OCR'd.")
# Remove the processed PDF file
os.remove(pdf_path)
return
try:
snapshots = convert_pdf_to_images(pdf_path)
text_and_boxes = extract_text_and_boxes(snapshots)
save_results(snapshots, text_and_boxes, output_folder)
move_files('output-images', output_folder)
except Exception as e:
print(f"Failed to process {pdf_path} due to error: {str(e)}")
# If the PDF file is corrupted or unreadable, delete it
os.remove(pdf_path)
return
# Remove the processed PDF file
os.remove(pdf_path)
if __name__ == "__main__":
# Directory containing the PDF files
directory = 'tmp1'
if os.path.exists(directory):
# Get the list of PDF files, including those in subdirectories
pdf_files = [os.path.join(dirpath, filename)
for dirpath, dirnames, filenames in os.walk(directory)
for filename in filenames if filename.endswith('.pdf')]
if pdf_files:
# Order the PDF files by size (smallest first)
pdf_files.sort(key=os.path.getsize)
# Use a process pool to process each PDF file
with concurrent.futures.ProcessPoolExecutor(max_workers=os.cpu_count()) as executor:
futures = {executor.submit(main, pdf_file, 'image-text-bbox-cluster2/' + os.path.basename(pdf_file).split('.')[0]): pdf_file for pdf_file in pdf_files}
for future in concurrent.futures.as_completed(futures):
pdf_file = futures[future]
try:
future.result() # If the function completed without error, this will be None
except Exception as exc:
print(f'{pdf_file} generated an exception: {exc}')
else:
print(f'{pdf_file} processed successfully')
else:
print("No PDF files found in the directory.")
else:
print(f"The directory {directory} does not exist.")