test_unit.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. import pytest
  2. from magic_pdf.libs.boxbase import _is_in_or_part_overlap, _is_in_or_part_overlap_with_area_ratio, _is_in, \
  3. _is_part_overlap, _left_intersect, _right_intersect, _is_vertical_full_overlap, _is_bottom_full_overlap, \
  4. _is_left_overlap, __is_overlaps_y_exceeds_threshold, calculate_iou, calculate_overlap_area_2_minbox_area_ratio, \
  5. calculate_overlap_area_in_bbox1_area_ratio, get_minbox_if_overlap_by_ratio, get_bbox_in_boundry, \
  6. find_top_nearest_text_bbox, find_bottom_nearest_text_bbox, find_left_nearest_text_bbox, \
  7. find_right_nearest_text_bbox, bbox_relative_pos, bbox_distance
  8. from magic_pdf.libs.commons import mymax, join_path, get_top_percent_list
  9. from magic_pdf.libs.config_reader import get_s3_config
  10. from magic_pdf.libs.path_utils import parse_s3path
  11. # 输入一个列表,如果列表空返回0,否则返回最大元素
  12. @pytest.mark.parametrize("list_input, target_num",
  13. [
  14. ([0, 0, 0, 0], 0),
  15. ([0], 0),
  16. ([1, 2, 5, 8, 4], 8),
  17. ([], 0),
  18. ([1.1, 7.6, 1.009, 9.9], 9.9),
  19. ([1.0 * 10 ** 2, 3.5 * 10 ** 3, 0.9 * 10 ** 6], 0.9 * 10 ** 6),
  20. ])
  21. def test_list_max(list_input: list, target_num) -> None:
  22. """
  23. list_input: 输入列表元素,元素均为数字类型
  24. """
  25. assert target_num == mymax(list_input)
  26. # 连接多个参数生成路径信息,使用"/"作为连接符,生成的结果需要是一个合法路径
  27. @pytest.mark.parametrize("path_input, target_path", [
  28. (['https:', '', 'www.baidu.com'], 'https://www.baidu.com'),
  29. (['https:', 'www.baidu.com'], 'https:/www.baidu.com'),
  30. (['D:', 'file', 'pythonProject', 'demo' + '.py'], 'D:/file/pythonProject/demo.py'),
  31. ])
  32. def test_join_path(path_input: list, target_path: str) -> None:
  33. """
  34. path_input: 输入path的列表,列表元素均为字符串
  35. """
  36. assert target_path == join_path(*path_input)
  37. # 获取列表中前百分之多少的元素
  38. @pytest.mark.parametrize("num_list, percent, target_num_list", [
  39. ([], 0.75, []),
  40. ([-5, -10, 9, 3, 7, -7, 0, 23, -1, -11], 0.8, [23, 9, 7, 3, 0, -1, -5, -7]),
  41. ([-5, -10, 9, 3, 7, -7, 0, 23, -1, -11], 0, []),
  42. ([-5, -10, 9, 3, 7, -7, 0, 23, -1, -11, 28], 0.8, [28, 23, 9, 7, 3, 0, -1, -5])
  43. ])
  44. def test_get_top_percent_list(num_list: list, percent: float, target_num_list: list) -> None:
  45. """
  46. num_list: 数字列表,列表元素为数字
  47. percent: 占比,float, 向下取证
  48. """
  49. assert target_num_list == get_top_percent_list(num_list, percent)
  50. # 输入一个s3路径,返回bucket名字和其余部分(key)
  51. @pytest.mark.parametrize("s3_path, target_data", [
  52. ("s3://bucket/path/to/my/file.txt", "bucket"),
  53. ("/path/to/my/file1.txt", "path"),
  54. ("bucket/path/to/my/file2.txt", "bucket"),
  55. ])
  56. def test_parse_s3path(s3_path: str, target_data: str):
  57. """
  58. s3_path: s3路径
  59. 如果为无效路径,则返回对应的bucket名字和其余部分
  60. 如果为异常路径 例如:file2.txt,则报异常
  61. """
  62. out_keys = parse_s3path(s3_path)
  63. assert target_data == out_keys[0]
  64. # 2个box是否处于包含或者部分重合关系。
  65. # 如果某边界重合算重合。
  66. # 部分边界重合,其他在内部也算包含
  67. @pytest.mark.parametrize("box1, box2, target_bool", [
  68. ((120, 133, 223, 248), (128, 168, 269, 295), True),
  69. ((137, 53, 245, 157), (134, 11, 200, 147), True), # 部分重合
  70. ((137, 56, 211, 116), (140, 66, 202, 199), True), # 部分重合
  71. ((42, 34, 69, 65), (42, 34, 69, 65), True), # 部分重合
  72. ((39, 63, 87, 106), (37, 66, 85, 109), True), # 部分重合
  73. ((13, 37, 55, 66), (7, 46, 49, 75), True), # 部分重合
  74. ((56, 83, 85, 104), (64, 85, 93, 106), True), # 部分重合
  75. ((12, 53, 48, 94), (14, 53, 50, 94), True), # 部分重合
  76. ((43, 54, 93, 131), (55, 82, 77, 106), True), # 包含
  77. ((63, 2, 134, 71), (72, 43, 104, 78), True), # 包含
  78. ((25, 57, 109, 127), (26, 73, 49, 95), True), # 包含
  79. ((24, 47, 111, 115), (34, 81, 58, 106), True), # 包含
  80. ((34, 8, 105, 83), (76, 20, 116, 45), True), # 包含
  81. ])
  82. def test_is_in_or_part_overlap(box1: tuple, box2: tuple, target_bool: bool) -> None:
  83. """
  84. box1: 坐标数组
  85. box2: 坐标数组
  86. """
  87. assert target_bool == _is_in_or_part_overlap(box1, box2)
  88. # 如果box1在box2内部,返回True
  89. # 如果是部分重合的,则重合面积占box1的比例大于阈值时候返回True
  90. @pytest.mark.parametrize("box1, box2, target_bool", [
  91. ((35, 28, 108, 90), (47, 60, 83, 96), False), # 包含 box1 up box2, box2 多半,box1少半
  92. ((65, 151, 92, 177), (49, 99, 105, 198), True), # 包含 box1 in box2
  93. ((80, 62, 112, 84), (74, 40, 144, 111), True), # 包含 box1 in box2
  94. ((65, 88, 127, 144), (92, 102, 131, 139), False), # 包含 box2 多半,box1约一半
  95. ((92, 102, 131, 139), (65, 88, 127, 144), True), # 包含 box1 多半
  96. ((100, 93, 199, 168), (169, 126, 198, 165), False), # 包含 box2 in box1
  97. ((26, 75, 106, 172), (65, 108, 90, 128), False), # 包含 box2 in box1
  98. ((28, 90, 77, 126), (35, 84, 84, 120), True), # 相交 box1多半,box2多半
  99. ((37, 6, 69, 52), (28, 3, 60, 49), True), # 相交 box1多半,box2多半
  100. ((94, 29, 133, 60), (84, 30, 123, 61), True), # 相交 box1多半,box2多半
  101. ])
  102. def test_is_in_or_part_overlap_with_area_ratio(box1: tuple, box2: tuple, target_bool: bool) -> None:
  103. out_bool = _is_in_or_part_overlap_with_area_ratio(box1, box2)
  104. assert target_bool == out_bool
  105. # box1在box2内部或者box2在box1内部返回True。如果部分边界重合也算作包含。
  106. @pytest.mark.parametrize("box1, box2, target_bool", [
  107. # ((), (), "Error"), # Error
  108. ((65, 151, 92, 177), (49, 99, 105, 198), True), # 包含 box1 in box2
  109. ((80, 62, 112, 84), (74, 40, 144, 111), True), # 包含 box1 in box2
  110. ((76, 140, 154, 277), (121, 326, 192, 384), False), # 分离
  111. ((65, 88, 127, 144), (92, 102, 131, 139), False), # 包含 box2 多半,box1约一半
  112. ((92, 102, 131, 139), (65, 88, 127, 144), False), # 包含 box1 多半
  113. ((68, 94, 118, 120), (68, 90, 118, 122), True), # 包含,box1 in box2 两边x相切
  114. ((69, 94, 118, 120), (68, 90, 118, 122), True), # 包含,box1 in box2 一边x相切
  115. ((69, 114, 118, 122), (68, 90, 118, 122), True), # 包含,box1 in box2 一边y相切
  116. # ((100, 93, 199, 168), (169, 126, 198, 165), True), # 包含 box2 in box1 Error
  117. # ((26, 75, 106, 172), (65, 108, 90, 128), True), # 包含 box2 in box1 Error
  118. # ((38, 94, 122, 120), (68, 94, 118, 120), True), # 包含,box2 in box1 两边y相切 Error
  119. # ((68, 34, 118, 158), (68, 94, 118, 120), True), # 包含,box2 in box1 两边x相切 Error
  120. # ((68, 34, 118, 158), (68, 94, 84, 120), True), # 包含,box2 in box1 一边x相切 Error
  121. # ((27, 94, 118, 158), (68, 94, 84, 120), True), # 包含,box2 in box1 一边y相切 Error
  122. ])
  123. def test_is_in(box1: tuple, box2: tuple, target_bool: bool) -> None:
  124. assert target_bool == _is_in(box1, box2)
  125. # 仅仅是部分包含关系,返回True,如果是完全包含关系则返回False
  126. @pytest.mark.parametrize("box1, box2, target_bool", [
  127. ((65, 151, 92, 177), (49, 99, 105, 198), False), # 包含 box1 in box2
  128. ((80, 62, 112, 84), (74, 40, 144, 111), False), # 包含 box1 in box2
  129. # ((76, 140, 154, 277), (121, 326, 192, 384), False), # 分离 Error
  130. ((76, 140, 154, 277), (121, 277, 192, 384), True), # 外相切
  131. ((65, 88, 127, 144), (92, 102, 131, 139), True), # 包含 box2 多半,box1约一半
  132. ((92, 102, 131, 139), (65, 88, 127, 144), True), # 包含 box1 多半
  133. ((68, 94, 118, 120), (68, 90, 118, 122), False), # 包含,box1 in box2 两边x相切
  134. ((69, 94, 118, 120), (68, 90, 118, 122), False), # 包含,box1 in box2 一边x相切
  135. ((69, 114, 118, 122), (68, 90, 118, 122), False), # 包含,box1 in box2 一边y相切
  136. # ((26, 75, 106, 172), (65, 108, 90, 128), False), # 包含 box2 in box1 Error
  137. # ((38, 94, 122, 120), (68, 94, 118, 120), False), # 包含,box2 in box1 两边y相切 Error
  138. # ((68, 34, 118, 158), (68, 94, 84, 120), False), # 包含,box2 in box1 一边x相切 Error
  139. ])
  140. def test_is_part_overlap(box1: tuple, box2: tuple, target_bool: bool) -> None:
  141. assert target_bool == _is_part_overlap(box1, box2)
  142. # left_box右侧是否和right_box左侧有部分重叠
  143. @pytest.mark.parametrize("box1, box2, target_bool", [
  144. (None, None, False),
  145. ((88, 81, 222, 173), (60, 221, 123, 358), False), # 分离
  146. ((121, 149, 184, 289), (172, 130, 230, 268), True), # box1 left bottom box2 相交
  147. ((172, 130, 230, 268), (121, 149, 184, 289), False), # box2 left bottom box1 相交
  148. ((109, 68, 182, 146), (215, 188, 277, 253), False), # box1 top left box2 分离
  149. ((117, 53, 222, 176), (174, 142, 298, 276), True), # box1 left top box2 相交
  150. ((174, 142, 298, 276), (117, 53, 222, 176), False), # box2 left top box1 相交
  151. ((65, 88, 127, 144), (92, 102, 131, 139), True), # box1 left box2 y:box2 in box1
  152. ((92, 102, 131, 139), (65, 88, 127, 144), False), # box2 left box1 y:box1 in box2
  153. ((182, 130, 230, 268), (121, 149, 174, 289), False), # box2 left box1 分离
  154. ((1, 10, 26, 45), (3, 4, 20, 39), True), # box1 bottom box2 x:box2 in box1
  155. ])
  156. def test_left_intersect(box1: tuple, box2: tuple, target_bool: bool) -> None:
  157. assert target_bool == _left_intersect(box1, box2)
  158. # left_box左侧是否和right_box右侧部分重叠
  159. @pytest.mark.parametrize("box1, box2, target_bool", [
  160. (None, None, False),
  161. ((88, 81, 222, 173), (60, 221, 123, 358), False), # 分离
  162. ((121, 149, 184, 289), (172, 130, 230, 268), False), # box1 left bottom box2 相交
  163. ((172, 130, 230, 268), (121, 149, 184, 289), True), # box2 left bottom box1 相交
  164. ((109, 68, 182, 146), (215, 188, 277, 253), False), # box1 top left box2 分离
  165. ((117, 53, 222, 176), (174, 142, 298, 276), False), # box1 left top box2 相交
  166. ((174, 142, 298, 276), (117, 53, 222, 176), True), # box2 left top box1 相交
  167. ((65, 88, 127, 144), (92, 102, 131, 139), False), # box1 left box2 y:box2 in box1
  168. # ((92, 102, 131, 139), (65, 88, 127, 144), True), # box2 left box1 y:box1 in box2 Error
  169. ((182, 130, 230, 268), (121, 149, 174, 289), False), # box2 left box1 分离
  170. # ((1, 10, 26, 45), (3, 4, 20, 39), False), # box1 bottom box2 x:box2 in box1 Error
  171. ])
  172. def test_right_intersect(box1: tuple, box2: tuple, target_bool: bool) -> None:
  173. assert target_bool == _right_intersect(box1, box2)
  174. # x方向上:要么box1包含box2, 要么box2包含box1。不能部分包含
  175. # y方向上:box1和box2有重叠
  176. @pytest.mark.parametrize("box1, box2, target_bool", [
  177. # (None, None, False), # Error
  178. ((35, 28, 108, 90), (47, 60, 83, 96), True), # box1 top box2, x:box2 in box1, y:有重叠
  179. ((35, 28, 98, 90), (27, 60, 103, 96), True), # box1 top box2, x:box1 in box2, y:有重叠
  180. ((57, 77, 130, 210), (59, 219, 119, 293), False), # box1 top box2, x: box2 in box1, y:无重叠
  181. ((47, 60, 83, 96), (35, 28, 108, 90), True), # box2 top box1, x:box1 in box2, y:有重叠
  182. ((27, 60, 103, 96), (35, 28, 98, 90), True), # box2 top box1, x:box2 in box1, y:有重叠
  183. ((59, 219, 119, 293), (57, 77, 130, 210), False), # box2 top box1, x: box1 in box2, y:无重叠
  184. ((35, 28, 55, 90), (57, 60, 83, 96), False), # box1 top box2, x:无重叠, y:有重叠
  185. ((47, 60, 63, 96), (65, 28, 108, 90), False), # box2 top box1, x:无重叠, y:有重叠
  186. ])
  187. def test_is_vertical_full_overlap(box1: tuple, box2: tuple, target_bool: bool) -> None:
  188. assert target_bool == _is_vertical_full_overlap(box1, box2)
  189. # 检查box1下方和box2的上方有轻微的重叠,轻微程度收到y_tolerance的限制
  190. @pytest.mark.parametrize("box1, box2, target_bool", [
  191. (None, None, False),
  192. ((35, 28, 108, 90), (47, 89, 83, 116), True), # box1 top box2, y:有重叠
  193. ((35, 28, 108, 90), (47, 60, 83, 96), False), # box1 top box2, y:有重叠且过多
  194. ((57, 77, 130, 210), (59, 219, 119, 293), False), # box1 top box2, y:无重叠
  195. ((47, 60, 83, 96), (35, 28, 108, 90), False), # box2 top box1, y:有重叠且过多
  196. ((27, 89, 103, 116), (35, 28, 98, 90), False), # box2 top box1, y:有重叠
  197. ((59, 219, 119, 293), (57, 77, 130, 210), False), # box2 top box1, y:无重叠
  198. ])
  199. def test_is_bottom_full_overlap(box1: tuple, box2: tuple, target_bool: bool) -> None:
  200. assert target_bool == _is_bottom_full_overlap(box1, box2)
  201. # 检查box1的左侧是否和box2有重叠
  202. @pytest.mark.parametrize("box1, box2, target_bool", [
  203. (None, None, False),
  204. ((88, 81, 222, 173), (60, 221, 123, 358), False), # 分离
  205. # ((121, 149, 184, 289), (172, 130, 230, 268), False), # box1 left bottom box2 相交 Error
  206. # ((172, 130, 230, 268), (121, 149, 184, 289), True), # box2 left bottom box1 相交 Error
  207. ((109, 68, 182, 146), (215, 188, 277, 253), False), # box1 top left box2 分离
  208. ((117, 53, 222, 176), (174, 142, 298, 276), False), # box1 left top box2 相交
  209. # ((174, 142, 298, 276), (117, 53, 222, 176), True), # box2 left top box1 相交 Error
  210. # ((65, 88, 127, 144), (92, 102, 131, 139), False), # box1 left box2 y:box2 in box1 Error
  211. ((1, 10, 26, 45), (3, 4, 20, 39), True), # box1 middle bottom box2 x:box2 in box1
  212. ])
  213. def test_is_left_overlap(box1: tuple, box2: tuple, target_bool: bool) -> None:
  214. assert target_bool == _is_left_overlap(box1, box2)
  215. # 查两个bbox在y轴上是否有重叠,并且该重叠区域的高度占两个bbox高度更低的那个超过阈值
  216. @pytest.mark.parametrize("box1, box2, target_bool", [
  217. # (None, None, "Error"), # Error
  218. ((51, 69, 192, 147), (75, 48, 132, 187), True), # y: box1 in box2
  219. ((51, 39, 192, 197), (75, 48, 132, 187), True), # y: box2 in box1
  220. ((88, 81, 222, 173), (60, 221, 123, 358), False), # y: box1 top box2
  221. ((109, 68, 182, 196), (215, 188, 277, 253), False), # y: box1 top box2 little
  222. ((109, 68, 182, 196), (215, 78, 277, 253), True), # y: box1 top box2 more
  223. ((109, 68, 182, 196), (215, 138, 277, 213), False), # y: box1 top box2 more but lower overlap_ratio_threshold
  224. ((109, 68, 182, 196), (215, 138, 277, 203), True), # y: box1 top box2 more and more overlap_ratio_threshold
  225. ])
  226. def test_is_overlaps_y_exceeds_threshold(box1: tuple, box2: tuple, target_bool: bool) -> None:
  227. assert target_bool == __is_overlaps_y_exceeds_threshold(box1, box2)
  228. # Determine the coordinates of the intersection rectangle
  229. @pytest.mark.parametrize("box1, box2, target_num", [
  230. # (None, None, "Error"), # Error
  231. ((88, 81, 222, 173), (60, 221, 123, 358), 0.0), # 分离
  232. ((76, 140, 154, 277), (121, 326, 192, 384), 0.0), # 分离
  233. ((142, 109, 238, 164), (134, 211, 224, 270), 0.0), # 分离
  234. ((109, 68, 182, 196), (175, 138, 277, 213), 0.024475524475524476), # 相交
  235. ((56, 90, 170, 219), (103, 212, 171, 304), 0.02288586346557361), # 相交
  236. ((109, 126, 204, 245), (130, 127, 232, 186), 0.33696071621517326), # 相交
  237. ((109, 126, 204, 245), (110, 127, 232, 206), 0.5493822593770807), # 相交
  238. ((76, 140, 154, 277), (121, 277, 192, 384), 0.0) # 相切
  239. ])
  240. def test_calculate_iou(box1: tuple, box2: tuple, target_num: float) -> None:
  241. assert target_num == calculate_iou(box1, box2)
  242. # 计算box1和box2的重叠面积占最小面积的box的比例
  243. @pytest.mark.parametrize("box1, box2, target_num", [
  244. # (None, None, "Error"), # Error
  245. ((142, 109, 238, 164), (134, 211, 224, 270), 0.0), # 分离
  246. ((88, 81, 222, 173), (60, 221, 123, 358), 0.0), # 分离
  247. ((76, 140, 154, 277), (121, 326, 192, 384), 0.0), # 分离
  248. ((76, 140, 154, 277), (121, 277, 192, 384), 0.0), # 相切
  249. ((109, 126, 204, 245), (110, 127, 232, 206), 0.7704918032786885), # 相交
  250. ((56, 90, 170, 219), (103, 212, 171, 304), 0.07496803069053709), # 相交
  251. ((121, 149, 184, 289), (172, 130, 230, 268), 0.17841079460269865), # 相交
  252. ((51, 69, 192, 147), (75, 48, 132, 187), 0.5611510791366906), # 相交
  253. ((117, 53, 222, 176), (174, 142, 298, 276), 0.12636469221835075), # 相交
  254. ((102, 60, 233, 203), (70, 190, 220, 319), 0.08188757807078417), # 相交
  255. ((109, 126, 204, 245), (130, 127, 232, 186), 0.7254901960784313), # 相交
  256. ])
  257. def test_calculate_overlap_area_2_minbox_area_ratio(box1: tuple, box2: tuple, target_num: float) -> None:
  258. assert target_num == calculate_overlap_area_2_minbox_area_ratio(box1, box2)
  259. # 计算box1和box2的重叠面积占bbox1的比例
  260. @pytest.mark.parametrize("box1, box2, target_num", [
  261. # (None, None, "Error"), # Error
  262. ((142, 109, 238, 164), (134, 211, 224, 270), 0.0), # 分离
  263. ((88, 81, 222, 173), (60, 221, 123, 358), 0.0), # 分离
  264. ((76, 140, 154, 277), (121, 326, 192, 384), 0.0), # 分离
  265. ((76, 140, 154, 277), (121, 277, 192, 384), 0.0), # 相切
  266. ((142, 109, 238, 164), (134, 164, 224, 270), 0.0), # 相切
  267. ((109, 126, 204, 245), (110, 127, 232, 206), 0.6568774878372402), # 相交
  268. ((56, 90, 170, 219), (103, 212, 171, 304), 0.03189174486604107), # 相交
  269. ((121, 149, 184, 289), (172, 130, 230, 268), 0.1619047619047619), # 相交
  270. ((51, 69, 192, 147), (75, 48, 132, 187), 0.40425531914893614), # 相交
  271. ((117, 53, 222, 176), (174, 142, 298, 276), 0.12636469221835075), # 相交
  272. ((102, 60, 233, 203), (70, 190, 220, 319), 0.08188757807078417), # 相交
  273. ((109, 126, 204, 245), (130, 127, 232, 186), 0.38620079610791685), # 相交
  274. ])
  275. def test_calculate_overlap_area_in_bbox1_area_ratio(box1: tuple, box2: tuple, target_num: float) -> None:
  276. assert target_num == calculate_overlap_area_in_bbox1_area_ratio(box1, box2)
  277. # 计算两个bbox重叠的面积占最小面积的box的比例,如果比例大于ratio,则返回小的那个bbox,否则返回None
  278. @pytest.mark.parametrize("box1, box2, ratio, target_box", [
  279. # (None, None, 0.8, "Error"), # Error
  280. ((142, 109, 238, 164), (134, 211, 224, 270), 0.0, None), # 分离
  281. ((109, 126, 204, 245), (110, 127, 232, 206), 0.5, (110, 127, 232, 206)),
  282. ((56, 90, 170, 219), (103, 212, 171, 304), 0.5, None),
  283. ((121, 149, 184, 289), (172, 130, 230, 268), 0.5, None),
  284. ((51, 69, 192, 147), (75, 48, 132, 187), 0.5, (75, 48, 132, 187)),
  285. ((117, 53, 222, 176), (174, 142, 298, 276), 0.5, None),
  286. ((102, 60, 233, 203), (70, 190, 220, 319), 0.5, None),
  287. ((109, 126, 204, 245), (130, 127, 232, 186), 0.5, (130, 127, 232, 186)),
  288. ])
  289. def test_get_minbox_if_overlap_by_ratio(box1: tuple, box2: tuple, ratio: float, target_box: list) -> None:
  290. assert target_box == get_minbox_if_overlap_by_ratio(box1, box2, ratio)
  291. # 根据boundry获取在这个范围内的所有的box的列表,完全包含关系
  292. @pytest.mark.parametrize("boxes, boundry, target_boxs", [
  293. # ([], (), "Error"), # Error
  294. ([], (110, 340, 209, 387), []),
  295. ([(142, 109, 238, 164)], (134, 211, 224, 270), []), # 分离
  296. ([(109, 126, 204, 245), (110, 127, 232, 206)], (105, 116, 258, 300), [(109, 126, 204, 245), (110, 127, 232, 206)]),
  297. ([(109, 126, 204, 245), (110, 127, 232, 206)], (105, 116, 258, 230), [(110, 127, 232, 206)]),
  298. ([(81, 280, 123, 315), (282, 203, 342, 247), (183, 100, 300, 155), (46, 99, 133, 148), (33, 156, 97, 211),
  299. (137, 29, 287, 87)], (80, 90, 249, 200), []),
  300. ([(81, 280, 123, 315), (282, 203, 342, 247), (183, 100, 300, 155), (46, 99, 133, 148), (33, 156, 97, 211),
  301. (137, 29, 287, 87)], (30, 20, 349, 320),
  302. [(81, 280, 123, 315), (282, 203, 342, 247), (183, 100, 300, 155), (46, 99, 133, 148), (33, 156, 97, 211),
  303. (137, 29, 287, 87)]),
  304. ([(81, 280, 123, 315), (282, 203, 342, 247), (183, 100, 300, 155), (46, 99, 133, 148), (33, 156, 97, 211),
  305. (137, 29, 287, 87)], (30, 20, 200, 320),
  306. [(81, 280, 123, 315), (46, 99, 133, 148), (33, 156, 97, 211)]),
  307. ])
  308. def test_get_bbox_in_boundry(boxes: list, boundry: tuple, target_boxs: list) -> None:
  309. assert target_boxs == get_bbox_in_boundry(boxes, boundry)
  310. # 寻找上方距离最近的box,margin 4个单位, x方向有重合,y方向最近的
  311. @pytest.mark.parametrize("pymu_blocks, obj_box, target_boxs", [
  312. ([{"bbox": (81, 280, 123, 315)}, {"bbox": (282, 203, 342, 247)}, {"bbox": (183, 100, 300, 155)},
  313. {"bbox": (46, 99, 133, 148)}, {"bbox": (33, 156, 97, 211)},
  314. {"bbox": (137, 29, 287, 87)}], (81, 280, 123, 315), {"bbox": (33, 156, 97, 211)}),
  315. # ([{"bbox": (168, 120, 263, 159)},
  316. # {"bbox": (231, 61, 279, 159)},
  317. # {"bbox": (35, 85, 136, 110)},
  318. # {"bbox": (228, 193, 347, 225)},
  319. # {"bbox": (144, 264, 188, 323)},
  320. # {"bbox": (62, 37, 126, 64)}], (228, 193, 347, 225),
  321. # [{"bbox": (168, 120, 263, 159)}, {"bbox": (231, 61, 279, 159)}]), # y:方向最近的有两个,x: 两个均有重合 Error
  322. ([{"bbox": (35, 85, 136, 159)},
  323. {"bbox": (168, 120, 263, 159)},
  324. {"bbox": (231, 61, 279, 118)},
  325. {"bbox": (228, 193, 347, 225)},
  326. {"bbox": (144, 264, 188, 323)},
  327. {"bbox": (62, 37, 126, 64)}], (228, 193, 347, 225),
  328. {"bbox": (168, 120, 263, 159)},), # y:方向最近的有两个,x:只有一个有重合
  329. ([{"bbox": (239, 115, 379, 167)},
  330. {"bbox": (33, 237, 104, 262)},
  331. {"bbox": (124, 288, 168, 325)},
  332. {"bbox": (242, 291, 379, 340)},
  333. {"bbox": (55, 117, 121, 154)},
  334. {"bbox": (266, 183, 384, 217)}, ], (124, 288, 168, 325), {'bbox': (55, 117, 121, 154)}),
  335. ([{"bbox": (239, 115, 379, 167)},
  336. {"bbox": (33, 237, 104, 262)},
  337. {"bbox": (124, 288, 168, 325)},
  338. {"bbox": (242, 291, 379, 340)},
  339. {"bbox": (55, 117, 119, 154)},
  340. {"bbox": (266, 183, 384, 217)}, ], (124, 288, 168, 325), None), # x没有重合
  341. ([{"bbox": (80, 90, 249, 200)},
  342. {"bbox": (183, 100, 240, 155)}, ], (183, 100, 240, 155), None), # 包含
  343. ])
  344. def test_find_top_nearest_text_bbox(pymu_blocks: list, obj_box: tuple, target_boxs: dict) -> None:
  345. assert target_boxs == find_top_nearest_text_bbox(pymu_blocks, obj_box)
  346. # 寻找下方距离自己最近的box, x方向有重合,y方向最近的
  347. @pytest.mark.parametrize("pymu_blocks, obj_box, target_boxs", [
  348. ([{"bbox": (165, 96, 300, 114)},
  349. {"bbox": (11, 157, 139, 201)},
  350. {"bbox": (124, 208, 265, 262)},
  351. {"bbox": (124, 283, 248, 306)},
  352. {"bbox": (39, 267, 84, 301)},
  353. {"bbox": (36, 89, 114, 145)}, ], (165, 96, 300, 114), {"bbox": (124, 208, 265, 262)}),
  354. ([{"bbox": (187, 37, 303, 49)},
  355. {"bbox": (2, 227, 90, 283)},
  356. {"bbox": (158, 174, 200, 212)},
  357. {"bbox": (259, 174, 324, 228)},
  358. {"bbox": (205, 61, 316, 97)},
  359. {"bbox": (295, 248, 374, 287)}, ], (205, 61, 316, 97), {"bbox": (259, 174, 324, 228)}), # y有两个最近的, x只有一个重合
  360. # ([{"bbox": (187, 37, 303, 49)},
  361. # {"bbox": (2, 227, 90, 283)},
  362. # {"bbox": (259, 174, 324, 228)},
  363. # {"bbox": (205, 61, 316, 97)},
  364. # {"bbox": (295, 248, 374, 287)},
  365. # {"bbox": (158, 174, 209, 212)}, ], (205, 61, 316, 97),
  366. # [{"bbox": (259, 174, 324, 228)}, {"bbox": (158, 174, 209, 212)}]), # x有重合,y有两个最近的 Error
  367. ([{"bbox": (287, 132, 398, 191)},
  368. {"bbox": (44, 141, 163, 188)},
  369. {"bbox": (132, 191, 240, 241)},
  370. {"bbox": (81, 25, 142, 67)},
  371. {"bbox": (74, 297, 116, 314)},
  372. {"bbox": (77, 84, 224, 107)}, ], (287, 132, 398, 191), None), # x没有重合
  373. ([{"bbox": (80, 90, 249, 200)},
  374. {"bbox": (183, 100, 240, 155)}, ], (183, 100, 240, 155), None), # 包含
  375. ])
  376. def test_find_bottom_nearest_text_bbox(pymu_blocks: list, obj_box: tuple, target_boxs: dict) -> None:
  377. assert target_boxs == find_bottom_nearest_text_bbox(pymu_blocks, obj_box)
  378. # 寻找左侧距离自己最近的box, y方向有重叠,x方向最近
  379. @pytest.mark.parametrize("pymu_blocks, obj_box, target_boxs", [
  380. ([{"bbox": (80, 90, 249, 200)}, {"bbox": (183, 100, 240, 155)}], (183, 100, 240, 155), None), # 包含
  381. ([{"bbox": (28, 90, 77, 126)}, {"bbox": (35, 84, 84, 120)}], (35, 84, 84, 120), None), # y:重叠,x:重叠大于2
  382. ([{"bbox": (28, 90, 77, 126)}, {"bbox": (75, 84, 134, 120)}], (75, 84, 134, 120), {"bbox": (28, 90, 77, 126)}),
  383. # y:重叠,x:重叠小于等于2
  384. ([{"bbox": (239, 115, 379, 167)},
  385. {"bbox": (33, 237, 104, 262)},
  386. {"bbox": (124, 288, 168, 325)},
  387. {"bbox": (242, 291, 379, 340)},
  388. {"bbox": (55, 113, 161, 154)},
  389. {"bbox": (266, 123, 384, 217)}], (266, 123, 384, 217), {"bbox": (55, 113, 161, 154)}), # y重叠,x left
  390. # ([{"bbox": (136, 219, 268, 240)},
  391. # {"bbox": (169, 115, 268, 181)},
  392. # {"bbox": (33, 237, 104, 262)},
  393. # {"bbox": (124, 288, 168, 325)},
  394. # {"bbox": (55, 117, 161, 154)},
  395. # {"bbox": (266, 183, 384, 217)}], (266, 183, 384, 217),
  396. # [{"bbox": (136, 219, 267, 240)}, {"bbox": (169, 115, 267, 181)}]), # y有重叠,x重叠小于2或者在left Error
  397. ])
  398. def test_find_left_nearest_text_bbox(pymu_blocks: list, obj_box: tuple, target_boxs: dict) -> None:
  399. assert target_boxs == find_left_nearest_text_bbox(pymu_blocks, obj_box)
  400. # 寻找右侧距离自己最近的box, y方向有重叠,x方向最近
  401. @pytest.mark.parametrize("pymu_blocks, obj_box, target_boxs", [
  402. ([{"bbox": (80, 90, 249, 200)}, {"bbox": (183, 100, 240, 155)}], (183, 100, 240, 155), None), # 包含
  403. ([{"bbox": (28, 90, 77, 126)}, {"bbox": (35, 84, 84, 120)}], (28, 90, 77, 126), None), # y:重叠,x:重叠大于2
  404. ([{"bbox": (28, 90, 77, 126)}, {"bbox": (75, 84, 134, 120)}], (28, 90, 77, 126), {"bbox": (75, 84, 134, 120)}),
  405. # y:重叠,x:重叠小于等于2
  406. ([{"bbox": (239, 115, 379, 167)},
  407. {"bbox": (33, 237, 104, 262)},
  408. {"bbox": (124, 288, 168, 325)},
  409. {"bbox": (242, 291, 379, 340)},
  410. {"bbox": (55, 113, 161, 154)},
  411. {"bbox": (266, 123, 384, 217)}], (55, 113, 161, 154), {"bbox": (239, 115, 379, 167)}), # y重叠,x right
  412. # ([{"bbox": (169, 115, 298, 181)},
  413. # {"bbox": (169, 219, 268, 240)},
  414. # {"bbox": (33, 177, 104, 262)},
  415. # {"bbox": (124, 288, 168, 325)},
  416. # {"bbox": (55, 117, 161, 154)},
  417. # {"bbox": (266, 183, 384, 217)}], (33, 177, 104, 262),
  418. # [{"bbox": (169, 115, 298, 181)}, {"bbox": (169, 219, 268, 240)}]), # y有重叠,x重叠小于2或者在right Error
  419. ])
  420. def test_find_right_nearest_text_bbox(pymu_blocks: list, obj_box: tuple, target_boxs: dict) -> None:
  421. assert target_boxs == find_right_nearest_text_bbox(pymu_blocks, obj_box)
  422. # 判断两个矩形框的相对位置关系 (left, right, bottom, top)
  423. @pytest.mark.parametrize("box1, box2, target_box", [
  424. # (None, None, "Error"), # Error
  425. ((80, 90, 249, 200), (183, 100, 240, 155), (False, False, False, False)), # 包含
  426. # ((124, 81, 222, 173), (60, 221, 123, 358), (False, True, False, True)), # 分离,右上 Error
  427. ((142, 109, 238, 164), (134, 211, 224, 270), (False, False, False, True)), # 分离,上
  428. # ((51, 69, 192, 147), (205, 198, 282, 297), (True, False, False, True)), # 分离,左上 Error
  429. # ((101, 149, 164, 289), (172, 130, 230, 268), (True, False, False, False)), # 分离,左 Error
  430. # ((69, 196, 124, 285), (130, 127, 232, 186), (True, False, True, False)), # 分离,左下 Error
  431. ((103, 212, 171, 304), (56, 90, 170, 209), (False, False, True, False)), # 分离,下
  432. # ((124, 367, 222, 415), (60, 221, 123, 358), (False, True, True, False)), # 分离,右下 Error
  433. # ((172, 130, 230, 268), (101, 149, 164, 289), (False, True, False, False)), # 分离,右 Error
  434. ])
  435. def test_bbox_relative_pos(box1: tuple, box2: tuple, target_box: tuple) -> None:
  436. assert target_box == bbox_relative_pos(box1, box2)
  437. # 计算两个矩形框的距离
  438. """
  439. 受bbox_relative_pos方法的影响,左右相反,这里计算结果全部受影响,在错误的基础上计算出了正确的结果
  440. """
  441. @pytest.mark.parametrize("box1, box2, target_num", [
  442. # (None, None, "Error"), # Error
  443. ((80, 90, 249, 200), (183, 100, 240, 155), 0.0), # 包含
  444. ((142, 109, 238, 164), (134, 211, 224, 270), 47.0), # 分离,上
  445. ((103, 212, 171, 304), (56, 90, 170, 209), 3.0), # 分离,下
  446. ((101, 149, 164, 289), (172, 130, 230, 268), 8.0), # 分离,左
  447. ((172, 130, 230, 268), (101, 149, 164, 289), 8.0), # 分离,右
  448. ((80.3, 90.8, 249.0, 200.5), (183.8, 100.6, 240.2, 155.1), 0.0), # 包含
  449. ((142.3, 109.5, 238.9, 164.2), (134.4, 211.2, 224.8, 270.1), 47.0), # 分离,上
  450. ((103.5, 212.6, 171.1, 304.8), (56.1, 90.9, 170.6, 209.2), 3.4), # 分离,下
  451. ((101.1, 149.3, 164.9, 289.0), (172.1, 130.1, 230.5, 268.5), 7.2), # 分离,左
  452. ((172.1, 130.3, 230.1, 268.1), (101.2, 149.9, 164.3, 289.1), 7.8), # 分离,右
  453. ((124.3, 81.1, 222.5, 173.8), (60.3, 221.5, 123.0, 358.9), 47.717711596429254), # 分离,右上
  454. ((51.2, 69.31, 192.5, 147.9), (205.0, 198.1, 282.98, 297.09), 51.73287156151299), # 分离,左上
  455. ((124.3, 367.1, 222.9, 415.7), (60.9, 221.4, 123.2, 358.6), 8.570880934886448), # 分离,右下
  456. ((69.9, 196.2, 124.1, 285.7), (130.0, 127.3, 232.6, 186.1), 11.69700816448377), # 分离,左下
  457. ])
  458. def test_bbox_distance(box1: tuple, box2: tuple, target_num: float) -> None:
  459. assert target_num - bbox_distance(box1, box2) < 1
  460. # 根据bucket_name获取s3配置ak,sk,endpoint
  461. def test_get_s3_config() -> None:
  462. with open("./s3_config_testdata.json") as f:
  463. contents = f.read()
  464. for content in eval(contents):
  465. bucket_name = content["bucket_name"]
  466. target_data = content["target_data"]
  467. assert target_data == list(get_s3_config(bucket_name))