eval_det_iou.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from collections import namedtuple
  4. import numpy as np
  5. from shapely.geometry import Polygon
  6. """
  7. reference from :
  8. https://github.com/MhLiao/DB/blob/3c32b808d4412680310d3d28eeb6a2d5bf1566c5/concern/icdar2015_eval/detection/iou.py#L8
  9. """
  10. class DetectionIoUEvaluator(object):
  11. def __init__(self, iou_constraint=0.5, area_precision_constraint=0.5):
  12. self.iou_constraint = iou_constraint
  13. self.area_precision_constraint = area_precision_constraint
  14. def evaluate_image(self, gt, pred):
  15. def get_union(pD, pG):
  16. return Polygon(pD).union(Polygon(pG)).area
  17. def get_intersection_over_union(pD, pG):
  18. return get_intersection(pD, pG) / get_union(pD, pG)
  19. def get_intersection(pD, pG):
  20. return Polygon(pD).intersection(Polygon(pG)).area
  21. def compute_ap(confList, matchList, numGtCare):
  22. correct = 0
  23. AP = 0
  24. if len(confList) > 0:
  25. confList = np.array(confList)
  26. matchList = np.array(matchList)
  27. sorted_ind = np.argsort(-confList)
  28. confList = confList[sorted_ind]
  29. matchList = matchList[sorted_ind]
  30. for n in range(len(confList)):
  31. match = matchList[n]
  32. if match:
  33. correct += 1
  34. AP += float(correct) / (n + 1)
  35. if numGtCare > 0:
  36. AP /= numGtCare
  37. return AP
  38. perSampleMetrics = {}
  39. matchedSum = 0
  40. Rectangle = namedtuple("Rectangle", "xmin ymin xmax ymax")
  41. numGlobalCareGt = 0
  42. numGlobalCareDet = 0
  43. arrGlobalConfidences = []
  44. arrGlobalMatches = []
  45. recall = 0
  46. precision = 0
  47. hmean = 0
  48. detMatched = 0
  49. iouMat = np.empty([1, 1])
  50. gtPols = []
  51. detPols = []
  52. gtPolPoints = []
  53. detPolPoints = []
  54. # Array of Ground Truth Polygons' keys marked as don't Care
  55. gtDontCarePolsNum = []
  56. # Array of Detected Polygons' matched with a don't Care GT
  57. detDontCarePolsNum = []
  58. pairs = []
  59. detMatchedNums = []
  60. arrSampleConfidences = []
  61. arrSampleMatch = []
  62. evaluationLog = ""
  63. for n in range(len(gt)):
  64. points = gt[n]["points"]
  65. dontCare = gt[n]["ignore"]
  66. if not Polygon(points).is_valid:
  67. continue
  68. gtPol = points
  69. gtPols.append(gtPol)
  70. gtPolPoints.append(points)
  71. if dontCare:
  72. gtDontCarePolsNum.append(len(gtPols) - 1)
  73. evaluationLog += (
  74. "GT polygons: "
  75. + str(len(gtPols))
  76. + (
  77. " (" + str(len(gtDontCarePolsNum)) + " don't care)\n"
  78. if len(gtDontCarePolsNum) > 0
  79. else "\n"
  80. )
  81. )
  82. for n in range(len(pred)):
  83. points = pred[n]["points"]
  84. if not Polygon(points).is_valid:
  85. continue
  86. detPol = points
  87. detPols.append(detPol)
  88. detPolPoints.append(points)
  89. if len(gtDontCarePolsNum) > 0:
  90. for dontCarePol in gtDontCarePolsNum:
  91. dontCarePol = gtPols[dontCarePol]
  92. intersected_area = get_intersection(dontCarePol, detPol)
  93. pdDimensions = Polygon(detPol).area
  94. precision = (
  95. 0 if pdDimensions == 0 else intersected_area / pdDimensions
  96. )
  97. if precision > self.area_precision_constraint:
  98. detDontCarePolsNum.append(len(detPols) - 1)
  99. break
  100. evaluationLog += (
  101. "DET polygons: "
  102. + str(len(detPols))
  103. + (
  104. " (" + str(len(detDontCarePolsNum)) + " don't care)\n"
  105. if len(detDontCarePolsNum) > 0
  106. else "\n"
  107. )
  108. )
  109. if len(gtPols) > 0 and len(detPols) > 0:
  110. # Calculate IoU and precision matrixs
  111. outputShape = [len(gtPols), len(detPols)]
  112. iouMat = np.empty(outputShape)
  113. gtRectMat = np.zeros(len(gtPols), np.int8)
  114. detRectMat = np.zeros(len(detPols), np.int8)
  115. for gtNum in range(len(gtPols)):
  116. for detNum in range(len(detPols)):
  117. pG = gtPols[gtNum]
  118. pD = detPols[detNum]
  119. iouMat[gtNum, detNum] = get_intersection_over_union(pD, pG)
  120. for gtNum in range(len(gtPols)):
  121. for detNum in range(len(detPols)):
  122. if (
  123. gtRectMat[gtNum] == 0
  124. and detRectMat[detNum] == 0
  125. and gtNum not in gtDontCarePolsNum
  126. and detNum not in detDontCarePolsNum
  127. ):
  128. if iouMat[gtNum, detNum] > self.iou_constraint:
  129. gtRectMat[gtNum] = 1
  130. detRectMat[detNum] = 1
  131. detMatched += 1
  132. pairs.append({"gt": gtNum, "det": detNum})
  133. detMatchedNums.append(detNum)
  134. evaluationLog += (
  135. "Match GT #"
  136. + str(gtNum)
  137. + " with Det #"
  138. + str(detNum)
  139. + "\n"
  140. )
  141. numGtCare = len(gtPols) - len(gtDontCarePolsNum)
  142. numDetCare = len(detPols) - len(detDontCarePolsNum)
  143. if numGtCare == 0:
  144. recall = float(1)
  145. precision = float(0) if numDetCare > 0 else float(1)
  146. else:
  147. recall = float(detMatched) / numGtCare
  148. precision = 0 if numDetCare == 0 else float(detMatched) / numDetCare
  149. hmean = (
  150. 0
  151. if (precision + recall) == 0
  152. else 2.0 * precision * recall / (precision + recall)
  153. )
  154. matchedSum += detMatched
  155. numGlobalCareGt += numGtCare
  156. numGlobalCareDet += numDetCare
  157. perSampleMetrics = {
  158. "gtCare": numGtCare,
  159. "detCare": numDetCare,
  160. "detMatched": detMatched,
  161. }
  162. return perSampleMetrics
  163. def combine_results(self, results):
  164. numGlobalCareGt = 0
  165. numGlobalCareDet = 0
  166. matchedSum = 0
  167. for result in results:
  168. numGlobalCareGt += result["gtCare"]
  169. numGlobalCareDet += result["detCare"]
  170. matchedSum += result["detMatched"]
  171. methodRecall = (
  172. 0 if numGlobalCareGt == 0 else float(matchedSum) / numGlobalCareGt
  173. )
  174. methodPrecision = (
  175. 0 if numGlobalCareDet == 0 else float(matchedSum) / numGlobalCareDet
  176. )
  177. methodHmean = (
  178. 0
  179. if methodRecall + methodPrecision == 0
  180. else 2 * methodRecall * methodPrecision / (methodRecall + methodPrecision)
  181. )
  182. methodMetrics = {
  183. "precision": methodPrecision,
  184. "recall": methodRecall,
  185. "hmean": methodHmean,
  186. }
  187. return methodMetrics
  188. if __name__ == "__main__":
  189. evaluator = DetectionIoUEvaluator()
  190. gts = [
  191. [
  192. {
  193. "points": [(0, 0), (1, 0), (1, 1), (0, 1)],
  194. "text": 1234,
  195. "ignore": False,
  196. },
  197. {
  198. "points": [(2, 2), (3, 2), (3, 3), (2, 3)],
  199. "text": 5678,
  200. "ignore": False,
  201. },
  202. ]
  203. ]
  204. preds = [
  205. [
  206. {
  207. "points": [(0.1, 0.1), (1, 0), (1, 1), (0, 1)],
  208. "text": 123,
  209. "ignore": False,
  210. }
  211. ]
  212. ]
  213. results = []
  214. for gt, pred in zip(gts, preds):
  215. results.append(evaluator.evaluate_image(gt, pred))
  216. metrics = evaluator.combine_results(results)
  217. print(metrics)