feat: Add app
This commit is contained in:
49
main.py
49
main.py
@@ -1,15 +1,13 @@
|
||||
from pathlib import Path
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
import supervision as sv
|
||||
import torch
|
||||
import yaml
|
||||
from pdf2image import convert_from_bytes, convert_from_path
|
||||
from PIL import Image
|
||||
from tqdm import tqdm
|
||||
|
||||
from groundingdino.util.inference import Model, preprocess_caption
|
||||
from pdf_converter import PdfConverter
|
||||
|
||||
GROUNDING_DINO_CONFIG = "groundingdino/config/GroundingDINO_SwinT_OGC.py"
|
||||
GROUNDING_DINO_CHECKPOINT = "gdino_checkpoints/groundingdino_swint_ogc.pth"
|
||||
@@ -17,49 +15,6 @@ BOX_THRESHOLD = 0.4
|
||||
TEXT_THRESHOLD = 0.25
|
||||
|
||||
|
||||
class PdfConverterService:
|
||||
"""
|
||||
A service to convert PDF files to images and resize images,
|
||||
using pypdfium2 for PDFs and Pillow for images.
|
||||
"""
|
||||
|
||||
def __init__(self, dpi: int):
|
||||
self._pdfium_initialized = False # Track if PDFium needs explicit init/deinit
|
||||
self._dpi = dpi
|
||||
|
||||
def convert_pdf_to_jpg(self, file_path: str) -> list[Image.Image]:
|
||||
"""
|
||||
Converts a PDF file to JPG images at different scales.
|
||||
"""
|
||||
pil_images = convert_from_path(file_path, dpi=self._dpi)
|
||||
return pil_images
|
||||
|
||||
def resize_image(self, img: Image.Image, size: tuple[int, int]) -> Image.Image:
|
||||
"""
|
||||
Resizes a PIL Image to the specified size.
|
||||
"""
|
||||
return img.resize(size, Image.LANCZOS)
|
||||
|
||||
@staticmethod
|
||||
def save_image_as_png(img: Image.Image, file_path: str):
|
||||
"""
|
||||
Saves a PIL Image as a PNG file.
|
||||
"""
|
||||
img.save(file_path, format="PNG")
|
||||
|
||||
@staticmethod
|
||||
def to_cv2_image(img: Image.Image):
|
||||
open_cv_image = np.array(img.convert("RGB"))
|
||||
return open_cv_image[:, :, ::-1].copy()
|
||||
|
||||
def convert_pdf_bytes_to_jpg(self, pdf_bytes: bytes) -> list[Image.Image]:
|
||||
"""
|
||||
Converts PDF bytes to JPG images at different scales.
|
||||
"""
|
||||
pil_images = convert_from_bytes(pdf_bytes, dpi=self._dpi)
|
||||
return pil_images
|
||||
|
||||
|
||||
def main(
|
||||
data_dir: str | Path,
|
||||
text: str = "ID card. Carte Vitale. Bank details. Human face.",
|
||||
@@ -82,7 +37,7 @@ def main(
|
||||
for img_path in tqdm(
|
||||
data_dir.glob("*.pdf"), total=len(list(data_dir.glob("*.pdf")))
|
||||
):
|
||||
pdf_convertor = PdfConverterService(120)
|
||||
pdf_convertor = PdfConverter(120)
|
||||
if img_path.suffix == ".pdf":
|
||||
imgs = pdf_convertor.convert_pdf_to_jpg(str(img_path))
|
||||
img = imgs[0]
|
||||
|
Reference in New Issue
Block a user