refact: add log

This commit is contained in:
liuyuanchuang
2026-02-05 20:50:04 +08:00
parent 15986c8966
commit c93eba2839
4 changed files with 74 additions and 7 deletions

View File

@@ -22,7 +22,7 @@ class Settings(BaseSettings):
debug: bool = False
# PaddleOCR-VL Settings
paddleocr_vl_url: str = "http://127.0.0.1:8000/v1"
paddleocr_vl_url: str = "http://127.0.0.1:8001/v1"
# MinerOCR Settings
miner_ocr_api_url: str = "http://127.0.0.1:8000/file_parse"

View File

@@ -49,10 +49,11 @@ def get_converter() -> Converter:
def get_mineru_ocr_service() -> MineruOCRService:
"""Get a MinerOCR service instance."""
settings = get_settings()
api_url = getattr(settings, 'miner_ocr_api_url', 'http://127.0.0.1:8000/file_parse')
api_url = getattr(settings, "miner_ocr_api_url", "http://127.0.0.1:8000/file_parse")
paddleocr_vl_url = getattr(settings, "paddleocr_vl_url", "http://localhost:8001/v1")
return MineruOCRService(
api_url=api_url,
converter=get_converter(),
image_processor=get_image_processor(),
paddleocr_vl_url=paddleocr_vl_url,
)

View File

@@ -547,27 +547,35 @@ class MineruOCRService(OCRServiceBase):
Returns:
Markdown content with formulas recognized by PaddleOCR-VL.
"""
# Pattern to match image references: ![](images/xxx.png)
# Pattern to match image references: ![](images/xxx.png) or ![](images/xxx.jpg)
image_pattern = re.compile(r"!\[\]\(images/[^)]+\)")
if not image_pattern.search(markdown_content):
return markdown_content
print(f"[DEBUG] Found image reference in markdown, triggering PaddleOCR-VL recognition")
try:
# For now, use the entire image for formula recognition
# TODO: Extract specific regions if image paths contain coordinates
formula_text = self._recognize_formula_with_paddleocr_vl(original_image)
print(f"[DEBUG] PaddleOCR-VL recognized formula: {formula_text[:100] if formula_text else 'Empty'}...")
# Replace image references with recognized formulas
# Wrap in display math delimiters if not already wrapped
if not formula_text.startswith("$$"):
if formula_text and not formula_text.startswith("$$"):
formula_text = f"$${formula_text}$$"
markdown_content = image_pattern.sub(formula_text, markdown_content)
print(f"[DEBUG] Formula recognition successful, updated markdown")
except Exception as e:
# If formula recognition fails, keep original content
print(f"Warning: Formula recognition failed: {e}")
# If formula recognition fails, keep original content and log error
import traceback
print(f"[ERROR] Formula recognition failed: {e}")
print(f"[ERROR] Traceback: {traceback.format_exc()}")
return markdown_content
@@ -622,10 +630,15 @@ class MineruOCRService(OCRServiceBase):
if "results" in result and "image" in result["results"]:
markdown_content = result["results"]["image"].get("md_content", "")
print(f"[DEBUG] Markdown content from Mineru: {markdown_content[:200]}...")
# Check if markdown contains formula image references
if "![](images/" in markdown_content:
print(f"[DEBUG] Detected image reference, calling PaddleOCR-VL...")
# Use PaddleOCR-VL to recognize the formula
markdown_content = self._extract_and_recognize_formulas(markdown_content, image)
else:
print(f"[DEBUG] No image reference found in markdown")
# Apply postprocessing to fix OCR errors
markdown_content = _postprocess_markdown(markdown_content)

View File

@@ -0,0 +1,53 @@
"""Test script for PaddleOCR-VL integration in MineruOCRService."""
import cv2
import numpy as np
from app.services.ocr_service import MineruOCRService
from app.services.converter import Converter
from app.services.image_processor import ImageProcessor
def test_paddleocr_vl_integration():
"""Test that PaddleOCR-VL is called when image references are found."""
# Create a simple test image (white background with black text)
test_image = np.ones((100, 300, 3), dtype=np.uint8) * 255
cv2.putText(test_image, "x^2 + y^2 = 1", (50, 50),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2)
# Initialize service
service = MineruOCRService(
api_url="http://127.0.0.1:8000/file_parse",
converter=Converter(),
image_processor=ImageProcessor(),
paddleocr_vl_url="http://localhost:8000/v1" # Your PaddleOCR-VL server
)
# Simulate markdown with image reference (this is what Mineru returns)
test_markdown = "![](images/af7f211f671f16f57d346e8e17611e68e0f4671bd1ae52ed59013c10eecef589.jpg)"
print("Testing formula extraction...")
result = service._extract_and_recognize_formulas(test_markdown, test_image)
print(f"\nOriginal markdown: {test_markdown}")
print(f"Processed markdown: {result}")
# Check if the image reference was replaced
if "![](images/" in result:
print("\n❌ FAILED: Image reference was not replaced")
else:
print("\n✅ SUCCESS: Image reference was replaced with formula")
if __name__ == "__main__":
print("=" * 60)
print("PaddleOCR-VL Integration Test")
print("=" * 60)
print("\nMake sure your PaddleOCR-VL server is running at:")
print("http://localhost:8000/v1")
print("\n" + "=" * 60 + "\n")
try:
test_paddleocr_vl_integration()
except Exception as e:
print(f"\n❌ Test failed with error: {e}")
import traceback
traceback.print_exc()