|
13 | 13 | Image = pytest.importorskip("PIL.Image") |
14 | 14 |
|
15 | 15 |
|
16 | | -@pytest.fixture |
17 | | -def crops_single_page_path(): |
18 | | - return V2_PRODUCT_DATA_DIR / "crop" / "default_sample.jpg" |
19 | | - |
20 | | - |
21 | | -@pytest.fixture |
22 | | -def crops_multi_page_path(): |
23 | | - return V2_PRODUCT_DATA_DIR / "crop" / "multipage_sample.pdf" |
24 | | - |
25 | | - |
26 | | -@pytest.fixture |
27 | | -def crops_single_page_json_path(): |
28 | | - return V2_PRODUCT_DATA_DIR / "crop" / "crop_single.json" |
29 | | - |
30 | | - |
31 | | -@pytest.fixture |
32 | | -def crops_multi_page_json_path(): |
33 | | - return V2_PRODUCT_DATA_DIR / "crop" / "crop_multiple.json" |
34 | | - |
35 | | - |
36 | 16 | @pytest.mark.pillow |
37 | 17 | @pytest.mark.pypdfium2 |
38 | | -def test_single_page_crop_split(crops_single_page_path, crops_single_page_json_path): |
39 | | - input_sample = PathInput(crops_single_page_path) |
40 | | - with open(crops_single_page_json_path, "rb") as f: |
| 18 | +def test_single_page_crop(): |
| 19 | + input_sample = PathInput(V2_PRODUCT_DATA_DIR / "crop" / "default_sample.jpg") |
| 20 | + with open(V2_PRODUCT_DATA_DIR / "crop" / "default_sample.json", "rb") as f: |
41 | 21 | response = CropResponse(json.load(f)) |
42 | 22 | extracted_crops = response.inference.result.extract_from_input_source(input_sample) |
43 | | - assert len(extracted_crops) == 1 |
| 23 | + assert len(extracted_crops) == 2 |
| 24 | + |
| 25 | + crop0 = extracted_crops[0] |
| 26 | + assert crop0.page_id == 0 |
| 27 | + assert crop0.element_id == 0 |
| 28 | + assert crop0.filename == "default_sample_page-001-item-001.jpg" |
| 29 | + assert Image.open(crop0.buffer).size == (1057, 2071) |
44 | 30 |
|
45 | | - assert extracted_crops[0].page_id == 0 |
46 | | - assert extracted_crops[0].element_id == 0 |
47 | | - image_buffer_0 = Image.open(extracted_crops[0].buffer) |
48 | | - assert image_buffer_0.size == (2823, 1571) |
| 31 | + crop1 = extracted_crops[1] |
| 32 | + assert crop1.page_id == 0 |
| 33 | + assert crop1.element_id == 1 |
| 34 | + assert crop1.filename == "default_sample_page-001-item-002.jpg" |
| 35 | + assert Image.open(crop1.buffer).size == (1298, 1869) |
49 | 36 |
|
50 | 37 |
|
51 | 38 | @pytest.mark.pillow |
52 | 39 | @pytest.mark.pypdfium2 |
53 | | -def test_multi_page_receipt_crop(crops_multi_page_path, crops_multi_page_json_path): |
54 | | - input_sample = PathInput(crops_multi_page_path) |
55 | | - with open(crops_multi_page_json_path, "rb") as f: |
| 40 | +def test_multi_page_crop(): |
| 41 | + input_sample = PathInput(V2_PRODUCT_DATA_DIR / "crop" / "multipage_sample.pdf") |
| 42 | + with open(V2_PRODUCT_DATA_DIR / "crop" / "multipage_sample.json", "rb") as f: |
56 | 43 | response = CropResponse(json.load(f)) |
57 | 44 | extracted_crops = response.inference.result.extract_from_input_source(input_sample) |
58 | | - assert len(extracted_crops) == 2 |
59 | | - |
60 | | - assert extracted_crops[0].page_id == 0 |
61 | | - assert extracted_crops[0].element_id == 0 |
62 | | - image_buffer_0 = Image.open(extracted_crops[0].buffer) |
63 | | - assert image_buffer_0.size == (156, 758) |
64 | | - |
65 | | - assert extracted_crops[1].page_id == 0 |
66 | | - assert extracted_crops[1].element_id == 1 |
67 | | - image_buffer_1 = Image.open(extracted_crops[1].buffer) |
68 | | - assert image_buffer_1.size == (187, 690) |
| 45 | + assert len(extracted_crops) == 5 |
| 46 | + |
| 47 | + crop0 = extracted_crops[0] |
| 48 | + assert crop0.page_id == 0 |
| 49 | + assert crop0.element_id == 0 |
| 50 | + assert crop0.filename == "multipage_sample_page-001-item-001.jpg" |
| 51 | + assert Image.open(crop0.buffer).size == (200, 553) |
| 52 | + |
| 53 | + crop1 = extracted_crops[1] |
| 54 | + assert crop1.page_id == 0 |
| 55 | + assert crop1.element_id == 1 |
| 56 | + assert crop1.filename == "multipage_sample_page-001-item-002.jpg" |
| 57 | + assert Image.open(crop1.buffer).size == (203, 333) |
| 58 | + |
| 59 | + crop4 = extracted_crops[4] |
| 60 | + assert crop4.page_id == 1 |
| 61 | + assert crop4.element_id == 1 |
| 62 | + assert crop4.filename == "multipage_sample_page-002-item-002.jpg" |
| 63 | + assert Image.open(crop4.buffer).size == (197, 520) |
0 commit comments