|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +import pytest |
| 4 | +from paddleocr import PaddleOCR, PPStructure |
| 5 | + |
| 6 | + |
| 7 | +# Test image paths |
| 8 | +IMAGE_PATHS_OCR = ["./doc/imgs_en/254.jpg", "./doc/imgs_en/img_10.jpg"] |
| 9 | +IMAGE_PATHS_STRUCTURE = [ |
| 10 | + "./ppstructure/docs/table/layout.jpg", |
| 11 | + "./ppstructure/docs/table/1.png", |
| 12 | +] |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture(params=["en", "ch"]) |
| 16 | +def ocr_engine(request: Any) -> PaddleOCR: |
| 17 | + """ |
| 18 | + Initialize PaddleOCR engine with different languages. |
| 19 | +
|
| 20 | + Args: |
| 21 | + request: pytest fixture request object. |
| 22 | +
|
| 23 | + Returns: |
| 24 | + An instance of PaddleOCR. |
| 25 | + """ |
| 26 | + return PaddleOCR(lang=request.param) |
| 27 | + |
| 28 | + |
| 29 | +def test_ocr_initialization(ocr_engine: PaddleOCR) -> None: |
| 30 | + """ |
| 31 | + Test PaddleOCR initialization. |
| 32 | +
|
| 33 | + Args: |
| 34 | + ocr_engine: An instance of PaddleOCR. |
| 35 | + """ |
| 36 | + assert ocr_engine is not None |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.parametrize("image_path", IMAGE_PATHS_OCR) |
| 40 | +def test_ocr_function(ocr_engine: PaddleOCR, image_path: str) -> None: |
| 41 | + """ |
| 42 | + Test PaddleOCR OCR functionality with different images. |
| 43 | +
|
| 44 | + Args: |
| 45 | + ocr_engine: An instance of PaddleOCR. |
| 46 | + image_path: Path to the image to be processed. |
| 47 | + """ |
| 48 | + result = ocr_engine.ocr(image_path) |
| 49 | + assert result is not None |
| 50 | + assert isinstance(result, list) |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.parametrize("image_path", IMAGE_PATHS_OCR) |
| 54 | +def test_ocr_det_only(ocr_engine: PaddleOCR, image_path: str) -> None: |
| 55 | + """ |
| 56 | + Test PaddleOCR OCR functionality with detection only. |
| 57 | +
|
| 58 | + Args: |
| 59 | + ocr_engine: An instance of PaddleOCR. |
| 60 | + image_path: Path to the image to be processed. |
| 61 | + """ |
| 62 | + result = ocr_engine.ocr(image_path, det=True, rec=False) |
| 63 | + assert result is not None |
| 64 | + assert isinstance(result, list) |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.parametrize("image_path", IMAGE_PATHS_OCR) |
| 68 | +def test_ocr_rec_only(ocr_engine: PaddleOCR, image_path: str) -> None: |
| 69 | + """ |
| 70 | + Test PaddleOCR OCR functionality with recognition only. |
| 71 | +
|
| 72 | + Args: |
| 73 | + ocr_engine: An instance of PaddleOCR. |
| 74 | + image_path: Path to the image to be processed. |
| 75 | + """ |
| 76 | + result = ocr_engine.ocr(image_path, det=False, rec=True) |
| 77 | + assert result is not None |
| 78 | + assert isinstance(result, list) |
| 79 | + |
| 80 | + |
| 81 | +@pytest.fixture(params=["en", "ch"]) |
| 82 | +def structure_engine(request: Any) -> PPStructure: |
| 83 | + """ |
| 84 | + Initialize PPStructure engine with different languages. |
| 85 | +
|
| 86 | + Args: |
| 87 | + request: pytest fixture request object. |
| 88 | +
|
| 89 | + Returns: |
| 90 | + An instance of PPStructure. |
| 91 | + """ |
| 92 | + return PPStructure(lang=request.param) |
| 93 | + |
| 94 | + |
| 95 | +def test_structure_initialization(structure_engine: PPStructure) -> None: |
| 96 | + """ |
| 97 | + Test PPStructure initialization. |
| 98 | +
|
| 99 | + Args: |
| 100 | + structure_engine: An instance of PPStructure. |
| 101 | + """ |
| 102 | + assert structure_engine is not None |
| 103 | + |
| 104 | + |
| 105 | +@pytest.mark.parametrize("image_path", IMAGE_PATHS_STRUCTURE) |
| 106 | +def test_structure_function(structure_engine: PPStructure, image_path: str) -> None: |
| 107 | + """ |
| 108 | + Test PPStructure structure analysis functionality with different images. |
| 109 | +
|
| 110 | + Args: |
| 111 | + structure_engine: An instance of PPStructure. |
| 112 | + image_path: Path to the image to be processed. |
| 113 | + """ |
| 114 | + result = structure_engine(image_path) |
| 115 | + assert result is not None |
| 116 | + assert isinstance(result, list) |
0 commit comments