record_reader.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. """Read complete visible record rows, anchored on the selected red/blue emblem."""
  2. from copy import copy
  3. import cv2
  4. def read_record_rows(image, assets):
  5. # Only the user's selected-side column; the enemy column is excluded.
  6. x0, y0, x1, y1 = 320, 145, 405, 625
  7. source = image[y0:y1, x0:x1]
  8. candidates = []
  9. for side, rule in (('LEFT', assets.I_FROG_LAST_SELECT_RED),
  10. ('RIGHT', assets.I_FROG_LAST_SELECT_BLUE)):
  11. scores = cv2.matchTemplate(source, rule.image, cv2.TM_CCOEFF_NORMED)
  12. while scores.size:
  13. _, score, _, (x, y) = cv2.minMaxLoc(scores)
  14. if score <= rule.threshold:
  15. break
  16. candidates.append((y + y0, side, score))
  17. scores[max(0, y - 25):y + 26, :] = -1
  18. rows = []
  19. for y, side, score in sorted(candidates):
  20. if any(abs(y - other[0]) < 25 for other in candidates if other[1] != side):
  21. continue
  22. offset = y - assets.I_FROG_LAST_SELECT_RED.roi_front[1]
  23. # Partial rows at either boundary are ignored to avoid mixing records.
  24. if 154 + offset < 145 or 270 + offset > 625:
  25. continue
  26. def matches(rule):
  27. x, ry, w, h = rule.roi_back
  28. crop = image[ry + offset:ry + offset + h, x:x + w]
  29. return cv2.minMaxLoc(cv2.matchTemplate(crop, rule.image, cv2.TM_CCOEFF_NORMED))[1] > rule.threshold
  30. won, lost = matches(assets.I_FROG_LAST_WIN), matches(assets.I_FROG_LAST_LOSE)
  31. if won == lost:
  32. continue
  33. ocr = copy(assets.O_FROG_LAST_TIME)
  34. x, ry, w, h = ocr.roi
  35. ocr.roi = (x, ry + offset, w, h)
  36. ocr.area = ocr.roi
  37. stamp = str(ocr.ocr(image)).strip()
  38. rows.append((stamp, won, side))
  39. return rows