scan, split, OCR, prepare for LLM
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. from __future__ import annotations
  2. import tempfile
  3. from pathlib import Path
  4. import cv2
  5. import numpy as np
  6. import pytest
  7. from src.split.slicer import (
  8. SUPPORTED_EXTENSIONS,
  9. VALID_ROTATIONS,
  10. add_border,
  11. crop_to_content,
  12. find_content_bounds,
  13. generate_output_paths,
  14. load_image,
  15. parse_slice,
  16. process_image,
  17. rotate_image,
  18. save_page,
  19. slice_grid,
  20. )
  21. def _create_test_image(width: int = 400, height: int = 300) -> np.ndarray:
  22. image = np.zeros((height, width, 3), dtype=np.uint8)
  23. image[:, :] = (200, 200, 200)
  24. return image
  25. def _image_to_path(image: np.ndarray, suffix: str = ".png") -> Path:
  26. with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as tmp:
  27. cv2.imwrite(tmp.name, image)
  28. return Path(tmp.name)
  29. class TestParseSlice:
  30. def test_simple_2x2(self) -> None:
  31. assert parse_slice("2:2") == (2, 2)
  32. def test_2x4(self) -> None:
  33. assert parse_slice("2:4") == (2, 4)
  34. def test_1x1(self) -> None:
  35. assert parse_slice("1:1") == (1, 1)
  36. def test_large(self) -> None:
  37. assert parse_slice("10:10") == (10, 10)
  38. def test_invalid_format_missing_colon(self) -> None:
  39. with pytest.raises(ValueError, match="Неверный формат --slice"):
  40. parse_slice("22")
  41. def test_invalid_format_extra_colon(self) -> None:
  42. with pytest.raises(ValueError, match="Неверный формат --slice"):
  43. parse_slice("2:3:4")
  44. def test_non_integer_value(self) -> None:
  45. with pytest.raises(ValueError, match="Неверный формат --slice"):
  46. parse_slice("a:b")
  47. def test_non_integer_col(self) -> None:
  48. with pytest.raises(ValueError, match="Неверный формат --slice"):
  49. parse_slice("x:2")
  50. def test_zero_cols(self) -> None:
  51. with pytest.raises(ValueError, match="Неверное значение --slice"):
  52. parse_slice("0:2")
  53. def test_zero_rows(self) -> None:
  54. with pytest.raises(ValueError, match="Неверное значение --slice"):
  55. parse_slice("2:0")
  56. def test_negative_cols(self) -> None:
  57. with pytest.raises(ValueError, match="Неверное значение --slice"):
  58. parse_slice("-1:2")
  59. def test_blank_string(self) -> None:
  60. with pytest.raises(ValueError):
  61. parse_slice("")
  62. class TestSliceGrid:
  63. def test_2x2_exact(self) -> None:
  64. image = _create_test_image(400, 300)
  65. pages = slice_grid(image, 2, 2)
  66. assert len(pages) == 4
  67. assert pages[0].shape == (150, 200, 3)
  68. assert pages[1].shape == (150, 200, 3)
  69. assert pages[2].shape == (150, 200, 3)
  70. assert pages[3].shape == (150, 200, 3)
  71. def test_1x1(self) -> None:
  72. image = _create_test_image(100, 100)
  73. pages = slice_grid(image, 1, 1)
  74. assert len(pages) == 1
  75. assert pages[0].shape == (100, 100, 3)
  76. def test_2x3(self) -> None:
  77. image = _create_test_image(300, 200)
  78. pages = slice_grid(image, 2, 3)
  79. assert len(pages) == 6
  80. for page in pages:
  81. assert page.shape == (100, 100, 3)
  82. def test_uneven_division_width(self) -> None:
  83. image = _create_test_image(width=403, height=300)
  84. pages = slice_grid(image, 1, 2)
  85. assert len(pages) == 2
  86. assert pages[0].shape == (300, 201, 3)
  87. assert pages[1].shape == (300, 202, 3)
  88. def test_uneven_division_height(self) -> None:
  89. image = _create_test_image(width=400, height=301)
  90. pages = slice_grid(image, 2, 1)
  91. assert len(pages) == 2
  92. assert pages[0].shape == (150, 400, 3)
  93. assert pages[1].shape == (151, 400, 3)
  94. def test_uneven_both(self) -> None:
  95. image = _create_test_image(width=401, height=301)
  96. pages = slice_grid(image, 2, 2)
  97. assert len(pages) == 4
  98. assert pages[0].shape == (150, 200, 3)
  99. assert pages[1].shape == (150, 201, 3)
  100. assert pages[2].shape == (151, 200, 3)
  101. assert pages[3].shape == (151, 201, 3)
  102. def test_pixel_content_preserved(self) -> None:
  103. image = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8)
  104. pages = slice_grid(image, 2, 2)
  105. assert np.array_equal(image[0:50, 0:50], pages[0])
  106. assert np.array_equal(image[0:50, 50:100], pages[1])
  107. assert np.array_equal(image[50:100, 0:50], pages[2])
  108. assert np.array_equal(image[50:100, 50:100], pages[3])
  109. def test_total_pixels_preserved(self) -> None:
  110. image = _create_test_image(width=401, height=301)
  111. pages = slice_grid(image, 3, 3)
  112. total = sum(page.shape[0] * page.shape[1] for page in pages)
  113. assert total == 401 * 301
  114. def test_order_2x2(self) -> None:
  115. image = _create_test_image(200, 200)
  116. image[0:100, 0:100] = (255, 0, 0)
  117. image[0:100, 100:200] = (0, 255, 0)
  118. image[100:200, 0:100] = (0, 0, 255)
  119. image[100:200, 100:200] = (255, 255, 0)
  120. pages = slice_grid(image, 2, 2)
  121. assert pages[0][0, 0].tolist() == [255, 0, 0]
  122. assert pages[1][0, 0].tolist() == [0, 255, 0]
  123. assert pages[2][0, 0].tolist() == [0, 0, 255]
  124. assert pages[3][0, 0].tolist() == [255, 255, 0]
  125. def test_order_2x4(self) -> None:
  126. image = _create_test_image(400, 200)
  127. pages = slice_grid(image, 2, 4)
  128. assert len(pages) == 8
  129. assert pages[0].shape == (100, 100, 3)
  130. assert pages[7].shape == (100, 100, 3)
  131. def test_4x4(self) -> None:
  132. image = _create_test_image(400, 400)
  133. pages = slice_grid(image, 4, 4)
  134. assert len(pages) == 16
  135. for page in pages:
  136. assert page.shape == (100, 100, 3)
  137. class TestAddBorder:
  138. def test_default_border(self) -> None:
  139. image = _create_test_image(100, 100)
  140. result = add_border(image, 20)
  141. assert result.shape == (140, 140, 3)
  142. def test_zero_border(self) -> None:
  143. image = _create_test_image(100, 100)
  144. result = add_border(image, 0)
  145. assert np.array_equal(result, image)
  146. def test_border_is_white(self) -> None:
  147. image = _create_test_image(100, 100)
  148. border = 10
  149. result = add_border(image, border)
  150. assert np.all(result[0:border, :] == 255)
  151. assert np.all(result[-border:, :] == 255)
  152. assert np.all(result[:, 0:border] == 255)
  153. assert np.all(result[:, -border:] == 255)
  154. def test_large_border(self) -> None:
  155. image = _create_test_image(10, 10)
  156. result = add_border(image, 50)
  157. assert result.shape == (110, 110, 3)
  158. class TestRotateImage:
  159. def test_rotate_90(self) -> None:
  160. image = _create_test_image(200, 100)
  161. result = rotate_image(image, 90)
  162. assert result.shape == (200, 100, 3)
  163. def test_rotate_180(self) -> None:
  164. image = _create_test_image(200, 100)
  165. result = rotate_image(image, 180)
  166. assert result.shape == (100, 200, 3)
  167. def test_rotate_270(self) -> None:
  168. image = _create_test_image(200, 100)
  169. result = rotate_image(image, 270)
  170. assert result.shape == (200, 100, 3)
  171. def test_invalid_angle(self) -> None:
  172. image = _create_test_image()
  173. with pytest.raises(ValueError, match="Недопустимый угол поворота"):
  174. rotate_image(image, 45)
  175. def test_invalid_angle_zero(self) -> None:
  176. image = _create_test_image()
  177. with pytest.raises(ValueError):
  178. rotate_image(image, 0)
  179. def test_valid_rotations_set(self) -> None:
  180. assert VALID_ROTATIONS == frozenset({90, 180, 270})
  181. class TestGenerateOutputPaths:
  182. def test_simple(self) -> None:
  183. paths = generate_output_paths(Path("scan001.jpg"), 2, Path("/tmp"))
  184. assert paths == [Path("/tmp/scan001_01.jpg"), Path("/tmp/scan001_02.jpg")]
  185. def test_tiff_extension(self) -> None:
  186. paths = generate_output_paths(Path("doc.tiff"), 3, Path("out"))
  187. assert paths == [
  188. Path("out/doc_01.tiff"),
  189. Path("out/doc_02.tiff"),
  190. Path("out/doc_03.tiff"),
  191. ]
  192. def test_png_extension(self) -> None:
  193. paths = generate_output_paths(Path("img.PNG"), 1, Path("out"))
  194. assert paths == [Path("out/img_01.png")]
  195. def test_zero_padding(self) -> None:
  196. paths = generate_output_paths(Path("scan.jpg"), 12, Path("out"))
  197. assert paths[0] == Path("out/scan_01.jpg")
  198. assert paths[9] == Path("out/scan_10.jpg")
  199. assert paths[11] == Path("out/scan_12.jpg")
  200. def test_many_pages(self) -> None:
  201. paths = generate_output_paths(Path("scan.jpg"), 100, Path("out"))
  202. assert paths[0] == Path("out/scan_001.jpg")
  203. assert paths[99] == Path("out/scan_100.jpg")
  204. def test_dot_in_filename(self) -> None:
  205. paths = generate_output_paths(Path("scan.001.jpg"), 2, Path("out"))
  206. assert paths == [Path("out/scan.001_01.jpg"), Path("out/scan.001_02.jpg")]
  207. class TestLoadImage:
  208. def test_valid_jpeg(self) -> None:
  209. image = _create_test_image()
  210. path = _image_to_path(image, ".jpg")
  211. loaded = load_image(path)
  212. assert loaded.shape == (300, 400, 3)
  213. def test_valid_png(self) -> None:
  214. image = _create_test_image()
  215. path = _image_to_path(image, ".png")
  216. loaded = load_image(path)
  217. assert loaded.shape == (300, 400, 3)
  218. def test_valid_tiff(self) -> None:
  219. image = _create_test_image()
  220. path = _image_to_path(image, ".tiff")
  221. loaded = load_image(path)
  222. assert loaded.shape == (300, 400, 3)
  223. def test_file_not_found(self) -> None:
  224. with pytest.raises(FileNotFoundError, match="Файл не найден"):
  225. load_image(Path("/tmp/nonexistent_scan2html_test.jpg"))
  226. def test_unsupported_extension(self) -> None:
  227. path = Path("/tmp/test.bmp")
  228. with pytest.raises(ValueError, match="Неподдерживаемый формат"):
  229. load_image(path)
  230. def test_unsupported_extension_no_dot(self) -> None:
  231. path = Path("/tmp/test")
  232. with pytest.raises(ValueError, match="Неподдерживаемый формат"):
  233. load_image(path)
  234. def test_supported_extensions_set(self) -> None:
  235. assert SUPPORTED_EXTENSIONS == frozenset({".jpg", ".jpeg", ".png", ".tiff", ".tif"})
  236. class TestSavePage:
  237. def test_saves_file(self) -> None:
  238. image = _create_test_image()
  239. with tempfile.TemporaryDirectory() as tmp:
  240. path = Path(tmp) / "test.png"
  241. save_page(image, path)
  242. assert path.exists()
  243. loaded = cv2.imread(str(path))
  244. assert loaded is not None
  245. assert loaded.shape == (300, 400, 3)
  246. def test_creates_parent_dir(self) -> None:
  247. image = _create_test_image()
  248. with tempfile.TemporaryDirectory() as tmp:
  249. path = Path(tmp) / "subdir" / "test.png"
  250. save_page(image, path)
  251. assert path.exists()
  252. class TestProcessImage:
  253. def test_end_to_end_2x2(self) -> None:
  254. image = _create_test_image(400, 300)
  255. path = _image_to_path(image, ".jpg")
  256. with tempfile.TemporaryDirectory() as tmp:
  257. result = process_image(Path(path), 2, 2, Path(tmp))
  258. assert len(result) == 4
  259. for p in result:
  260. assert p.exists()
  261. loaded = cv2.imread(str(p))
  262. assert loaded.shape == (250, 300, 3)
  263. def test_end_to_end_with_border(self) -> None:
  264. image = _create_test_image(200, 200)
  265. path = _image_to_path(image, ".png")
  266. with tempfile.TemporaryDirectory() as tmp:
  267. result = process_image(Path(path), 2, 2, Path(tmp), border_px=10)
  268. assert len(result) == 4
  269. for p in result:
  270. assert p.exists()
  271. loaded = cv2.imread(str(p))
  272. assert loaded.shape == (120, 120, 3)
  273. def test_end_to_end_tiff(self) -> None:
  274. image = _create_test_image(400, 400)
  275. path = _image_to_path(image, ".tiff")
  276. with tempfile.TemporaryDirectory() as tmp:
  277. result = process_image(Path(path), 2, 2, Path(tmp))
  278. assert len(result) == 4
  279. for p in result:
  280. assert p.suffix.lower() == ".tiff"
  281. assert p.exists()
  282. def test_end_to_end_with_rotation(self) -> None:
  283. image = _create_test_image(200, 400)
  284. path = _image_to_path(image, ".png")
  285. with tempfile.TemporaryDirectory() as tmp:
  286. result = process_image(Path(path), 2, 2, Path(tmp), pre_rotate=90)
  287. assert len(result) == 4
  288. for p in result:
  289. assert p.exists()
  290. loaded = cv2.imread(str(result[0]))
  291. assert loaded.shape[0] > 0
  292. class TestFindContentBounds:
  293. def test_uniform_image(self) -> None:
  294. image = _create_test_image(200, 150)
  295. image[:, :] = (255, 255, 255)
  296. x1, y1, x2, y2 = find_content_bounds(image)
  297. assert (x1, y1, x2, y2) == (0, 0, 200, 150)
  298. def test_single_dot_center(self) -> None:
  299. image = _create_test_image(200, 150)
  300. image[:, :] = (255, 255, 255)
  301. image[75, 100] = (0, 0, 0)
  302. x1, y1, x2, y2 = find_content_bounds(image)
  303. assert abs(x1 - 100) <= 1 and abs(x2 - 100) <= 1
  304. assert abs(y1 - 75) <= 1 and abs(y2 - 75) <= 1
  305. def test_rectangle_content(self) -> None:
  306. image = _create_test_image(200, 150)
  307. image[:, :] = (255, 255, 255)
  308. image[30:100, 50:140] = (0, 0, 0)
  309. x1, y1, x2, y2 = find_content_bounds(image)
  310. assert x1 == 50
  311. assert y1 == 30
  312. assert x2 == 139
  313. assert y2 == 99
  314. def test_content_at_edges(self) -> None:
  315. image = _create_test_image(200, 150)
  316. image[:, :] = (255, 255, 255)
  317. image[0:10, :] = (0, 0, 0)
  318. image[:, 0:10] = (0, 0, 0)
  319. x1, y1, _x2, _y2 = find_content_bounds(image)
  320. assert y1 == 0
  321. assert x1 == 0
  322. def test_grayscale_content(self) -> None:
  323. image = _create_test_image(100, 100)
  324. image[:, :] = (255, 255, 255)
  325. image[20:30, 20:30] = (128, 128, 128)
  326. x1, y1, x2, y2 = find_content_bounds(image)
  327. assert x1 <= 29 and x2 >= 20
  328. assert y1 <= 29 and y2 >= 20
  329. class TestCropToContent:
  330. def test_crop_centered_content(self) -> None:
  331. image = _create_test_image(200, 150)
  332. image[:, :] = (255, 255, 255)
  333. image[30:50, 40:60] = (0, 0, 0)
  334. result = crop_to_content(image, border_px=10)
  335. assert result.shape[0] >= 20
  336. assert result.shape[1] >= 20
  337. def test_crop_uniform_image_no_change(self) -> None:
  338. image = _create_test_image(100, 100)
  339. image[:, :] = (255, 255, 255)
  340. result = crop_to_content(image, border_px=10)
  341. assert result.shape == (100, 100, 3)
  342. def test_border_capped_by_image_edge(self) -> None:
  343. image = _create_test_image(100, 100)
  344. image[:, :] = (255, 255, 255)
  345. image[10:30, 10:30] = (0, 0, 0)
  346. result = crop_to_content(image, border_px=200)
  347. assert result.shape == (100, 100, 3)
  348. def test_border_equals_distance_to_edge(self) -> None:
  349. image = _create_test_image(100, 100)
  350. image[:, :] = (255, 255, 255)
  351. image[40:60, 30:70] = (0, 0, 0)
  352. result = crop_to_content(image, border_px=50)
  353. assert result.shape[1] <= 100
  354. assert result.shape[0] <= 100
  355. def test_dark_bg_no_crop_uniform(self) -> None:
  356. image = _create_test_image(100, 100)
  357. image[:, :] = (0, 0, 0)
  358. result = crop_to_content(image, border_px=0)
  359. assert result.shape == (100, 100, 3)
  360. def test_zero_border_exact_crop(self) -> None:
  361. image = _create_test_image(200, 150)
  362. image[:, :] = (255, 255, 255)
  363. image[20:100, 30:170] = (0, 0, 0)
  364. result = crop_to_content(image, border_px=0)
  365. assert result.shape == (80, 140, 3)
  366. class TestProcessImagePostCrop:
  367. def test_post_crop_reduces_size(self) -> None:
  368. image = _create_test_image(400, 400)
  369. image[:, :] = (255, 255, 255)
  370. image[100:200, 100:200] = (0, 0, 0)
  371. path = _image_to_path(image, ".png")
  372. with tempfile.TemporaryDirectory() as tmp:
  373. result = process_image(Path(path), 1, 1, Path(tmp), border_px=30, post_crop=True)
  374. assert len(result) == 1
  375. loaded = cv2.imread(str(result[0]))
  376. assert loaded.shape[0] < 400 or loaded.shape[1] < 400
  377. def test_post_crop_off_no_crop(self) -> None:
  378. image = _create_test_image(400, 300)
  379. path = _image_to_path(image, ".png")
  380. with tempfile.TemporaryDirectory() as tmp:
  381. result = process_image(Path(path), 1, 1, Path(tmp), post_crop=False)
  382. assert len(result) == 1
  383. loaded = cv2.imread(str(result[0]))
  384. assert loaded.shape == (400, 500, 3)
  385. def test_post_crop_e2e_2x2(self) -> None:
  386. image = _create_test_image(400, 400)
  387. image[:, :] = (255, 255, 255)
  388. image[50:150, 50:150] = (0, 0, 0)
  389. image[50:150, 250:350] = (0, 0, 0)
  390. image[250:350, 50:150] = (0, 0, 0)
  391. image[250:350, 250:350] = (0, 0, 0)
  392. path = _image_to_path(image, ".png")
  393. with tempfile.TemporaryDirectory() as tmp:
  394. result_crop = process_image(Path(path), 2, 2, Path(tmp) / "crop", border_px=30, post_crop=True)
  395. result_no = process_image(Path(path), 2, 2, Path(tmp) / "no", border_px=30, post_crop=False)
  396. assert len(result_crop) == 4
  397. for cp, np in zip(result_crop, result_no):
  398. c = cv2.imread(str(cp))
  399. n = cv2.imread(str(np))
  400. assert c.shape[0] < n.shape[0]
  401. assert c.shape[1] < n.shape[1]