Update preprocess.py
Browse files- preprocess.py +104 -125
preprocess.py
CHANGED
|
@@ -1,125 +1,104 @@
|
|
| 1 |
-
# %% [markdown]
|
| 2 |
-
# ### Step 1: Reading PDF Files
|
| 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 |
-
print("
|
| 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 |
-
# Duyệt qua tất cả ảnh
|
| 107 |
-
for filename in os.listdir(input_folder):
|
| 108 |
-
if filename.lower().endswith((".jpg", ".jpeg", ".png", ".bmp")):
|
| 109 |
-
input_path = os.path.join(input_folder, filename)
|
| 110 |
-
output_path = os.path.join(output_folder, filename)
|
| 111 |
-
|
| 112 |
-
try:
|
| 113 |
-
processed_img = preprocess_image(input_path)
|
| 114 |
-
|
| 115 |
-
# Chuyển ảnh về PIL để lưu với Unicode path
|
| 116 |
-
pil_result = Image.fromarray(processed_img)
|
| 117 |
-
pil_result.save(output_path)
|
| 118 |
-
|
| 119 |
-
except Exception as e:
|
| 120 |
-
print(f"❌ Lỗi xử lý {filename}: {e}")
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
| 1 |
+
# %% [markdown]
|
| 2 |
+
# ### Step 1: Reading PDF Files
|
| 3 |
+
# Setup directories
|
| 4 |
+
pdf_directory = r"F:\Preprocessing"
|
| 5 |
+
output_directory = r"F:\Images"
|
| 6 |
+
os.makedirs(output_directory, exist_ok=True)
|
| 7 |
+
|
| 8 |
+
pages = convert_from_path(pdf_path, dpi=dpi)
|
| 9 |
+
|
| 10 |
+
# %% [markdown]
|
| 11 |
+
# ### Step 2: Convert PDF files to Images
|
| 12 |
+
|
| 13 |
+
# %%
|
| 14 |
+
import os
|
| 15 |
+
import cv2
|
| 16 |
+
import numpy as np
|
| 17 |
+
from pdf2image import convert_from_path
|
| 18 |
+
import glob
|
| 19 |
+
|
| 20 |
+
# Hàm chuyển PDF sang ảnh
|
| 21 |
+
def pdf_to_images(pdf_path, output_dir, dpi=300):
|
| 22 |
+
try:
|
| 23 |
+
pages = convert_from_path(pdf_path, dpi=dpi)
|
| 24 |
+
for i, page in enumerate(pages):
|
| 25 |
+
image_name = f"{os.path.splitext(os.path.basename(pdf_path))[0]}_page_{i+1}.jpg"
|
| 26 |
+
image_path = os.path.join(output_dir, image_name)
|
| 27 |
+
page.save(image_path, "JPEG", quality=95)
|
| 28 |
+
return len(pages) # Trả về số lượng ảnh được tạo
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print(f"✗ Error processing {pdf_path}: {e}")
|
| 31 |
+
return 0
|
| 32 |
+
|
| 33 |
+
# Xử lý toàn bộ file PDF
|
| 34 |
+
def process_all_pdfs():
|
| 35 |
+
pdf_files = glob.glob(os.path.join(pdf_directory, "*.pdf"))
|
| 36 |
+
total_images = 0
|
| 37 |
+
|
| 38 |
+
if not pdf_files:
|
| 39 |
+
print(f"No PDF files found in {pdf_directory}")
|
| 40 |
+
return
|
| 41 |
+
|
| 42 |
+
for pdf_file in pdf_files:
|
| 43 |
+
num_pages = pdf_to_images(pdf_file, output_directory)
|
| 44 |
+
total_images += num_pages
|
| 45 |
+
|
| 46 |
+
print(f"\n✓ Tổng số file PDF: {len(pdf_files)}")
|
| 47 |
+
print(f"✓ Tổng số ảnh đã chuyển đổi: {total_images}")
|
| 48 |
+
|
| 49 |
+
# MAIN EXECUTION
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
print("PDF TO IMAGES CONVERTER")
|
| 52 |
+
print(f"Input directory: {pdf_directory}")
|
| 53 |
+
print(f"Output directory: {output_directory}")
|
| 54 |
+
print()
|
| 55 |
+
|
| 56 |
+
if not os.path.exists(pdf_directory):
|
| 57 |
+
print(f"✗ Input directory does not exist: {pdf_directory}")
|
| 58 |
+
exit(1)
|
| 59 |
+
|
| 60 |
+
process_all_pdfs()
|
| 61 |
+
print("\n✓ Processing completed!")
|
| 62 |
+
|
| 63 |
+
# %% [markdown]
|
| 64 |
+
# ### Step 3: Image Preprocessing
|
| 65 |
+
|
| 66 |
+
# %%
|
| 67 |
+
import os
|
| 68 |
+
import cv2
|
| 69 |
+
import numpy as np
|
| 70 |
+
from PIL import Image
|
| 71 |
+
|
| 72 |
+
def preprocess_image(image_path):
|
| 73 |
+
pil_img = Image.open(image_path)
|
| 74 |
+
img = np.array(pil_img)
|
| 75 |
+
|
| 76 |
+
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
| 77 |
+
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
|
| 78 |
+
contrast_img = clahe.apply(gray)
|
| 79 |
+
_, binary = cv2.threshold(contrast_img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 80 |
+
kernel = np.ones((1, 1), np.uint8)
|
| 81 |
+
bold_img = cv2.dilate(binary, kernel, iterations=1)
|
| 82 |
+
|
| 83 |
+
return bold_img
|
| 84 |
+
|
| 85 |
+
# Thư mục đầu vào và đầu ra
|
| 86 |
+
input_folder = r"F:\Images"
|
| 87 |
+
output_folder = r"F:\Images_Processed"
|
| 88 |
+
os.makedirs(output_folder, exist_ok=True)
|
| 89 |
+
|
| 90 |
+
# Duyệt qua tất cả ảnh
|
| 91 |
+
for filename in os.listdir(input_folder):
|
| 92 |
+
if filename.lower().endswith((".jpg", ".jpeg", ".png", ".bmp")):
|
| 93 |
+
input_path = os.path.join(input_folder, filename)
|
| 94 |
+
output_path = os.path.join(output_folder, filename)
|
| 95 |
+
|
| 96 |
+
try:
|
| 97 |
+
processed_img = preprocess_image(input_path)
|
| 98 |
+
|
| 99 |
+
# Chuyển ảnh về PIL để lưu với Unicode path
|
| 100 |
+
pil_result = Image.fromarray(processed_img)
|
| 101 |
+
pil_result.save(output_path)
|
| 102 |
+
|
| 103 |
+
except Exception as e:
|
| 104 |
+
print(f"❌ Lỗi xử lý {filename}: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|