feat: use paddle ocr v4 instead
This commit is contained in:
@@ -129,9 +129,6 @@ services:
|
|||||||
- MINIO_BUCKET_NAME=${MINIO_BUCKET_NAME}
|
- MINIO_BUCKET_NAME=${MINIO_BUCKET_NAME}
|
||||||
- MINIO_USE_SSL=${MINIO_USE_SSL}
|
- MINIO_USE_SSL=${MINIO_USE_SSL}
|
||||||
- WEB_PROXY=${WEB_PROXY}
|
- WEB_PROXY=${WEB_PROXY}
|
||||||
- GRPC_ENABLE_FORK_SUPPORT=1
|
|
||||||
- GRPC_WORKER_PROCESSES=1
|
|
||||||
- GRPC_MAX_WORKERS=4
|
|
||||||
networks:
|
networks:
|
||||||
- WeKnora-network
|
- WeKnora-network
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ urllib3
|
|||||||
markdownify
|
markdownify
|
||||||
mistletoe
|
mistletoe
|
||||||
goose3[all]
|
goose3[all]
|
||||||
paddleocr==3.2.0
|
paddleocr>=2.10.0,<3.0.0
|
||||||
markdown
|
markdown
|
||||||
pypdf
|
pypdf
|
||||||
cos-python-sdk-v5
|
cos-python-sdk-v5
|
||||||
@@ -25,7 +25,7 @@ ollama
|
|||||||
pdfplumber
|
pdfplumber
|
||||||
|
|
||||||
--extra-index-url https://www.paddlepaddle.org.cn/packages/stable/cpu/
|
--extra-index-url https://www.paddlepaddle.org.cn/packages/stable/cpu/
|
||||||
paddlepaddle==3.2.0
|
paddlepaddle>=3.0.0,<4.0.0
|
||||||
|
|
||||||
# --extra-index-url https://www.paddlepaddle.org.cn/packages/stable/cu126/
|
# --extra-index-url https://www.paddlepaddle.org.cn/packages/stable/cu126/
|
||||||
# paddlepaddle-gpu==3.0.0
|
# paddlepaddle-gpu==3.0.0
|
||||||
@@ -33,30 +33,40 @@ class PaddleOCRBackend(OCRBackend):
|
|||||||
"""Initialize PaddleOCR backend"""
|
"""Initialize PaddleOCR backend"""
|
||||||
self.ocr = None
|
self.ocr = None
|
||||||
try:
|
try:
|
||||||
|
import os
|
||||||
|
import paddle
|
||||||
|
|
||||||
|
# Set PaddlePaddle to use CPU and disable GPU
|
||||||
|
os.environ['CUDA_VISIBLE_DEVICES'] = ''
|
||||||
|
paddle.set_device('cpu')
|
||||||
|
|
||||||
from paddleocr import PaddleOCR
|
from paddleocr import PaddleOCR
|
||||||
# Default OCR configuration
|
# Simplified OCR configuration
|
||||||
ocr_config = {
|
ocr_config = {
|
||||||
"text_det_limit_type": "max", # Change from 'min' to 'max'
|
"use_gpu": False,
|
||||||
"text_det_limit_side_len": 960, # A standard and safe limit for the longest side
|
"text_det_limit_type": "max",
|
||||||
"use_doc_orientation_classify": False, # Do not use document image orientation classification
|
"text_det_limit_side_len": 960,
|
||||||
"use_doc_unwarping": False, # Do not use document unwarping
|
"use_doc_orientation_classify": False,
|
||||||
"use_textline_orientation": False, # Do not use textline orientation classification
|
"use_doc_unwarping": False,
|
||||||
"text_recognition_model_name": "PP-OCRv5_server_rec",
|
"use_textline_orientation": False,
|
||||||
"text_detection_model_name": "PP-OCRv5_server_det",
|
"text_recognition_model_name": "PP-OCRv4_server_rec",
|
||||||
"text_recognition_model_dir": "/root/.paddlex/official_models/PP-OCRv5_server_rec_infer",
|
"text_detection_model_name": "PP-OCRv4_server_det",
|
||||||
"text_detection_model_dir": "/root/.paddlex/official_models/PP-OCRv5_server_det_infer",
|
"text_det_thresh": 0.3,
|
||||||
"text_det_thresh": 0.3, # Text detection pixel threshold
|
"text_det_box_thresh": 0.6,
|
||||||
"text_det_box_thresh": 0.6, # Text detection box threshold
|
"text_det_unclip_ratio": 1.5,
|
||||||
"text_det_unclip_ratio": 1.5, # Text detection expansion ratio
|
"text_rec_score_thresh": 0.0,
|
||||||
"text_rec_score_thresh": 0.0, # Text recognition confidence threshold
|
"ocr_version": "PP-OCRv4",
|
||||||
"ocr_version": "PP-OCRv5", # Switch to PP-OCRv4 here to compare
|
|
||||||
"lang": "ch",
|
"lang": "ch",
|
||||||
|
"show_log": False,
|
||||||
|
"use_dilation": True, # improves accuracy
|
||||||
|
"det_db_score_mode": "slow", # improves accuracy
|
||||||
}
|
}
|
||||||
|
|
||||||
self.ocr = PaddleOCR(**ocr_config)
|
self.ocr = PaddleOCR(**ocr_config)
|
||||||
logger.info("PaddleOCR engine initialized successfully")
|
logger.info("PaddleOCR engine initialized successfully")
|
||||||
except ImportError:
|
|
||||||
logger.error("Failed to import paddleocr. Please install it with 'pip install paddleocr'")
|
except ImportError as e:
|
||||||
|
logger.error(f"Failed to import paddleocr: {str(e)}. Please install it with 'pip install paddleocr'")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to initialize PaddleOCR: {str(e)}")
|
logger.error(f"Failed to initialize PaddleOCR: {str(e)}")
|
||||||
|
|
||||||
@@ -71,50 +81,39 @@ class PaddleOCRBackend(OCRBackend):
|
|||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
# Ensure image is in RGB format
|
# Ensure image is in RGB format
|
||||||
|
if hasattr(image, "convert") and image.mode != "RGB":
|
||||||
|
image = image.convert("RGB")
|
||||||
|
|
||||||
|
# Convert to numpy array if needed
|
||||||
if hasattr(image, "convert"):
|
if hasattr(image, "convert"):
|
||||||
if image.mode == "RGBA":
|
image_array = np.array(image)
|
||||||
img_for_ocr = image.convert("RGB") # 尝试转换为 RGB
|
|
||||||
logger.info(f"Converted image from RGBA to RGB format for OCR.")
|
|
||||||
elif image.mode != "RGB": # 如果不是 RGBA 也不是 RGB,也尝试转 RGB
|
|
||||||
img_for_ocr = image.convert("RGB")
|
|
||||||
logger.info(f"Converted image from {image.mode} to RGB format for OCR.")
|
|
||||||
else:
|
|
||||||
img_for_ocr = image
|
|
||||||
logger.info(f"Image already in RGB format.")
|
|
||||||
else:
|
else:
|
||||||
img_for_ocr = image
|
image_array = image
|
||||||
logger.info(f"Image is not a PIL.Image object, assuming it's already suitable for OCR.")
|
|
||||||
|
|
||||||
# Convert to numpy array if not already
|
# Perform OCR
|
||||||
if hasattr(img_for_ocr, "convert"):
|
ocr_result = self.ocr.ocr(image_array, cls=False)
|
||||||
image_array = np.array(img_for_ocr)
|
|
||||||
else:
|
|
||||||
image_array = img_for_ocr
|
|
||||||
|
|
||||||
ocr_result = self.ocr.predict(image_array)
|
|
||||||
|
|
||||||
# Extract text
|
# Extract text
|
||||||
if ocr_result and any(ocr_result):
|
ocr_text = ""
|
||||||
ocr_text = ""
|
if ocr_result and ocr_result[0]:
|
||||||
for image_result in ocr_result:
|
for line in ocr_result[0]:
|
||||||
ocr_text = ocr_text + " ".join(image_result["rec_texts"])
|
if line and len(line) >= 2:
|
||||||
text_length = len(ocr_text)
|
text = line[1][0] if line[1] else ""
|
||||||
if text_length > 0:
|
if text:
|
||||||
logger.info(f"OCR extracted {text_length} characters")
|
ocr_text += text + " "
|
||||||
logger.info(
|
|
||||||
f"OCR text sample: {ocr_text[:100]}..."
|
text_length = len(ocr_text.strip())
|
||||||
if text_length > 100
|
if text_length > 0:
|
||||||
else f"OCR text: {ocr_text}"
|
logger.info(f"OCR extracted {text_length} characters")
|
||||||
)
|
return ocr_text.strip()
|
||||||
return ocr_text
|
|
||||||
else:
|
|
||||||
logger.warning("OCR returned empty result")
|
|
||||||
else:
|
else:
|
||||||
logger.warning("OCR did not return any result")
|
logger.warning("OCR returned empty result")
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"OCR recognition error: {str(e)}")
|
logger.error(f"OCR recognition error: {str(e)}")
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
class NanonetsOCRBackend(OCRBackend):
|
class NanonetsOCRBackend(OCRBackend):
|
||||||
"""Nanonets OCR backend implementation using OpenAI API format"""
|
"""Nanonets OCR backend implementation using OpenAI API format"""
|
||||||
|
|
||||||
|
|||||||
@@ -5,9 +5,7 @@ from concurrent import futures
|
|||||||
import traceback
|
import traceback
|
||||||
import grpc
|
import grpc
|
||||||
import uuid
|
import uuid
|
||||||
|
import atexit
|
||||||
# Enable gRPC fork support to avoid multiprocessing issues
|
|
||||||
os.environ.setdefault('GRPC_ENABLE_FORK_SUPPORT', '1')
|
|
||||||
|
|
||||||
# Add parent directory to Python path
|
# Add parent directory to Python path
|
||||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
@@ -327,104 +325,47 @@ def init_ocr_engine(ocr_backend, ocr_config):
|
|||||||
logger.error(f"Error initializing OCR engine: {str(e)}")
|
logger.error(f"Error initializing OCR engine: {str(e)}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def serve():
|
def serve():
|
||||||
|
|
||||||
init_ocr_engine(os.getenv("OCR_BACKEND", "paddle"), {
|
init_ocr_engine(os.getenv("OCR_BACKEND", "paddle"), {
|
||||||
"OCR_API_BASE_URL": os.getenv("OCR_API_BASE_URL", ""),
|
"OCR_API_BASE_URL": os.getenv("OCR_API_BASE_URL", ""),
|
||||||
})
|
})
|
||||||
# Set max number of worker threads and processes
|
|
||||||
|
# Set max number of worker threads
|
||||||
max_workers = int(os.environ.get("GRPC_MAX_WORKERS", "4"))
|
max_workers = int(os.environ.get("GRPC_MAX_WORKERS", "4"))
|
||||||
# Force single process mode to avoid gRPC multiprocessing issues
|
logger.info(f"Starting DocReader service with {max_workers} worker threads")
|
||||||
worker_processes = 1
|
|
||||||
logger.info(f"Starting DocReader service, max worker threads per process: {max_workers}, "
|
|
||||||
f"processes: {worker_processes} (forced single process mode)")
|
|
||||||
|
|
||||||
# Get port number
|
# Get port number
|
||||||
port = os.environ.get("GRPC_PORT", "50051")
|
port = os.environ.get("GRPC_PORT", "50051")
|
||||||
|
|
||||||
# Multi-process mode (disabled due to gRPC fork issues)
|
# Create server
|
||||||
if False and worker_processes > 1:
|
server = grpc.server(
|
||||||
import multiprocessing
|
futures.ThreadPoolExecutor(max_workers=max_workers),
|
||||||
processes = []
|
options=[
|
||||||
|
('grpc.max_send_message_length', MAX_MESSAGE_LENGTH),
|
||||||
|
('grpc.max_receive_message_length', MAX_MESSAGE_LENGTH),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
def run_server():
|
# Register service
|
||||||
# Create server
|
docreader_pb2_grpc.add_DocReaderServicer_to_server(DocReaderServicer(), server)
|
||||||
server = grpc.server(
|
|
||||||
futures.ThreadPoolExecutor(max_workers=max_workers),
|
|
||||||
options=[
|
|
||||||
('grpc.max_send_message_length', MAX_MESSAGE_LENGTH),
|
|
||||||
('grpc.max_receive_message_length', MAX_MESSAGE_LENGTH),
|
|
||||||
('grpc.enable_fork_support', 1),
|
|
||||||
('grpc.so_reuseport', 1),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
|
|
||||||
# Register service
|
# Set listen address
|
||||||
docreader_pb2_grpc.add_DocReaderServicer_to_server(DocReaderServicer(), server)
|
server.add_insecure_port(f"[::]:{port}")
|
||||||
|
|
||||||
# Set listen address
|
# Start service
|
||||||
server.add_insecure_port(f"[::]:{port}")
|
server.start()
|
||||||
|
|
||||||
# Start service
|
logger.info(f"Server started on port {port}")
|
||||||
server.start()
|
logger.info("Server is ready to accept connections")
|
||||||
|
|
||||||
logger.info(f"Worker process {os.getpid()} started on port {port}")
|
try:
|
||||||
|
# Wait for service termination
|
||||||
try:
|
server.wait_for_termination()
|
||||||
# Wait for service termination
|
except KeyboardInterrupt:
|
||||||
server.wait_for_termination()
|
logger.info("Received termination signal, shutting down server")
|
||||||
except KeyboardInterrupt:
|
server.stop(0)
|
||||||
logger.info(f"Worker process {os.getpid()} received termination signal")
|
|
||||||
server.stop(0)
|
|
||||||
|
|
||||||
# Start specified number of worker processes
|
|
||||||
for i in range(worker_processes):
|
|
||||||
process = multiprocessing.Process(target=run_server)
|
|
||||||
processes.append(process)
|
|
||||||
process.start()
|
|
||||||
logger.info(f"Started worker process {process.pid} ({i+1}/{worker_processes})")
|
|
||||||
|
|
||||||
# Wait for all processes to complete
|
|
||||||
try:
|
|
||||||
for process in processes:
|
|
||||||
process.join()
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
logger.info("Master process received termination signal")
|
|
||||||
for process in processes:
|
|
||||||
if process.is_alive():
|
|
||||||
logger.info(f"Terminating worker process {process.pid}")
|
|
||||||
process.terminate()
|
|
||||||
|
|
||||||
# Single-process mode
|
|
||||||
else:
|
|
||||||
# Create server
|
|
||||||
server = grpc.server(
|
|
||||||
futures.ThreadPoolExecutor(max_workers=max_workers),
|
|
||||||
options=[
|
|
||||||
('grpc.max_send_message_length', MAX_MESSAGE_LENGTH),
|
|
||||||
('grpc.max_receive_message_length', MAX_MESSAGE_LENGTH),
|
|
||||||
('grpc.enable_fork_support', 1),
|
|
||||||
('grpc.so_reuseport', 1),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
|
|
||||||
# Register service
|
|
||||||
docreader_pb2_grpc.add_DocReaderServicer_to_server(DocReaderServicer(), server)
|
|
||||||
|
|
||||||
# Set listen address
|
|
||||||
server.add_insecure_port(f"[::]:{port}")
|
|
||||||
|
|
||||||
# Start service
|
|
||||||
server.start()
|
|
||||||
|
|
||||||
logger.info(f"Server started on port {port} (single process mode)")
|
|
||||||
logger.info("Server is ready to accept connections")
|
|
||||||
|
|
||||||
try:
|
|
||||||
# Wait for service termination
|
|
||||||
server.wait_for_termination()
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
logger.info("Received termination signal, shutting down server")
|
|
||||||
server.stop(0)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
serve()
|
serve()
|
||||||
|
|||||||
Reference in New Issue
Block a user