test_image_extractor.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from __future__ import annotations
  2. from app.core.data_research.extractors.base import ExtractionContext
  3. class FakeOcrService:
  4. def extract_pages(self, pages):
  5. from app.core.data_research.ocr.service import OcrResult
  6. assert pages == [b"image"]
  7. return [
  8. OcrResult(
  9. text="customer_id 客户编号",
  10. bbox=(0.1, 0.2, 0.7, 0.3),
  11. page=1,
  12. confidence=0.65,
  13. review_required=True,
  14. )
  15. ]
  16. def test_png_and_jpeg_are_supported_with_normalized_ocr_evidence():
  17. from app.core.data_research.extractors.image import ImageExtractor
  18. extractor = ImageExtractor(FakeOcrService())
  19. assert extractor.can_handle("image/png", "scan.png")
  20. assert extractor.can_handle("image/jpeg", "scan.jpg")
  21. batch = extractor.extract(
  22. b"image",
  23. ExtractionContext(filename="scan.png", media_type="image/png"),
  24. )
  25. assert batch.parser_version == "image-ocr-v1"
  26. assert batch.items[0].data["review_required"] is True
  27. assert batch.items[0].evidence[0].locator == {
  28. "kind": "image.ocr",
  29. "page": 1,
  30. "bbox": [0.1, 0.2, 0.7, 0.3],
  31. }
  32. assert batch.items[0].evidence[0].confidence == 0.65