main.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. """入口:裁剪模板、匹配关键点并在截图上标点;由 ``start.bat`` 调用本脚本。"""
  2. from __future__ import annotations
  3. import sys
  4. from pathlib import Path
  5. # 仓库根:``routing``、``thirdparty``;``img-match``:单应等;``img-match/superpoint-match``:AKAZE / LoFTR / LightGlue。
  6. _REPO_ROOT = Path(__file__).resolve().parent
  7. _IMG_MATCH = _REPO_ROOT / "img-match"
  8. _SUPERPOINT_MATCH = _IMG_MATCH / "superpoint-match"
  9. for _p in (_REPO_ROOT, _IMG_MATCH, _SUPERPOINT_MATCH):
  10. if str(_p) not in sys.path:
  11. sys.path.insert(0, str(_p))
  12. from thirdparty.crop_img import CropImg
  13. from thirdparty.draw_point import DrawPoint
  14. from keypoints_matching import KeypointsMatching
  15. from akaze_match import AkazeMatch
  16. from lightglue_match import LightglueMatch
  17. from loftr_match import LoftrMatch
  18. from fit_homography import fit_homography
  19. from corner_points_on_match_img import corner_points_on_match_img
  20. from thirdparty.draw_rect import draw_rect
  21. from thirdparty.round_corners import round_corner
  22. if __name__ == "__main__":
  23. CropImg().crop_edges(
  24. 0.2,
  25. 0.2,
  26. 0.0,
  27. 0.0,
  28. "input/template.webp",
  29. "output/crop-img.png"
  30. )
  31. # ``degree`` 实为圆角半径(像素),贴近网页卡片约 8~12px;模板更大时可酌增至 20~40。
  32. # round_corner(
  33. # degree=20,
  34. # image="input/template.webp",
  35. # output_path="output/round-corner-img.png",
  36. # )
  37. # keypoints_obj = AkazeMatch(
  38. # template="output/crop-img.png",
  39. # match_image="input/screenshot.png",
  40. # )
  41. # keypoints_obj = LightglueMatch(
  42. # template="output/crop-img.png",
  43. # match_image="input/screenshot.png",
  44. # )
  45. keypoints_obj = LoftrMatch(
  46. template="output/crop-img.png",
  47. match_image="input/screenshot.png",
  48. )
  49. kp_match = KeypointsMatching(keypoints_obj)
  50. keypoints_arr = kp_match.get_keypoints()
  51. DrawPoint(
  52. keypoints_arr,
  53. "input/screenshot.png",
  54. "output/keypoints_on_screenshot.png").draw_point()
  55. homography_obj = fit_homography(
  56. keypoints_arr,
  57. correspondence=kp_match.correspondence(),
  58. )
  59. corner_points_arr = corner_points_on_match_img(
  60. homography_obj,
  61. keypoints_obj.template,
  62. keypoints_obj.match_image,
  63. )
  64. draw_rect(
  65. corner_points_arr,
  66. match_image="input/screenshot.png",
  67. output_path="output/template_rect_on_screenshot.png",
  68. )