image_grid.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import numpy as np
  5. from module.atom.image import RuleImage
  6. class ImageGrid:
  7. def __init__(self, images: list[RuleImage]):
  8. self.images = images
  9. def find_anyone(self, img: np.array) -> RuleImage or None:
  10. """
  11. 在这些图片中找到其中一个
  12. :param img:
  13. :return: 如果没有找到返回None
  14. """
  15. for image in self.images:
  16. if image.match(img):
  17. return image
  18. return None
  19. def find_everyone(self, img: np.array) -> list or None:
  20. """
  21. 自下而上查找所有匹配项,返回带对应image对象的排序结果
  22. :param img: 待匹配图像
  23. :return: 排序后的列表,每个元素为(image对象, (x, y, w, h)),无匹配返回None
  24. """
  25. matched = []
  26. # 收集匹配结果时保留来源image
  27. for image in self.images:
  28. matches = image.match_all_any(img, threshold=0.8, nms_threshold=0.3)
  29. for (score, x, y, w, h) in matches:
  30. matched.append( (image, score, (x, y, w, h)) )
  31. # 按y坐标升序排列(屏幕坐标系从上到下)
  32. sorted_results = sorted(
  33. matched,
  34. key=lambda item: item[2][1] # item[1]是坐标元组,取y值
  35. )
  36. return sorted_results if sorted_results else None