cimo001 commited on
Commit
7cc4a75
·
verified ·
1 Parent(s): 974b100
RT-DETR-L_wired_table_cell_det/src/example.py CHANGED
@@ -1,19 +1,20 @@
1
  import sys
2
- sys.dont_write_bytecode = True
3
-
4
  import cv2
5
  import numpy
6
 
 
 
 
7
  from helper import onnxSessionBuild
8
 
9
  pathModel = "./RT-DETR-L_wired_table_cell_det/"
10
 
11
- imageSizeDetection = 640
12
 
13
  scoreThreshold = 0.3
14
-
15
  levelBoxNms = 0.5
16
  levelBoxContained = 0.9
 
17
 
18
  onnxSession = onnxSessionBuild(f"{pathModel}onnx/rt-detr-l_wired_table_cell_det.onnx")
19
 
@@ -27,13 +28,15 @@ def overlapCalculate(coordinateFirstList, coordinateSecondList):
27
  y2 = min(coordinateFirstList[3], coordinateSecondList[3])
28
 
29
  if x2 <= x1 or y2 <= y1:
30
- return {"intersectionOverUnion": 0.0, "containment": 0.0}
31
 
32
  areaIntersection = (x2 - x1) * (y2 - y1)
33
 
34
  return {
35
  "intersectionOverUnion": areaIntersection / float(areaFirst + areaSecond - areaIntersection),
36
- "containment": areaIntersection / float(min(areaFirst, areaSecond))
 
 
37
  }
38
 
39
  def suppressionNonMaximum(itemList):
@@ -47,10 +50,7 @@ def suppressionNonMaximum(itemList):
47
  for b in range(len(resultList)):
48
  overlapObject = overlapCalculate(itemSortedList[a]["coordinate"], resultList[b]["coordinate"])
49
 
50
- isSameRegion = overlapObject["intersectionOverUnion"] >= levelBoxNms
51
- isInside = overlapObject["containment"] >= levelBoxContained
52
-
53
- if isSameRegion or isInside:
54
  isKeep = False
55
 
56
  break
@@ -60,18 +60,38 @@ def suppressionNonMaximum(itemList):
60
 
61
  return resultList
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  def inference(imageRgb):
64
  resultList = []
65
 
66
  imageHeight, imageWidth = imageRgb.shape[0:2]
67
- imageResized = cv2.resize(imageRgb, (imageSizeDetection, imageSizeDetection), interpolation=cv2.INTER_CUBIC).astype(numpy.float32) / 255.0
68
 
69
  tensor = numpy.expand_dims(imageResized.transpose((2, 0, 1)), axis=0).astype(numpy.float32)
70
 
71
  tensorFeedObject = {
72
  "image": tensor,
73
- "im_shape": numpy.array([[imageSizeDetection, imageSizeDetection]], dtype=numpy.float32),
74
- "scale_factor": numpy.array([[imageSizeDetection / float(imageHeight), imageSizeDetection / float(imageWidth)]], dtype=numpy.float32)
75
  }
76
 
77
  tensorOutputList = onnxSession.run(None, tensorFeedObject)
@@ -93,7 +113,10 @@ def inference(imageRgb):
93
  "coordinate": [x1, y1, x2, y2]
94
  })
95
 
96
- return suppressionNonMaximum(resultList)
 
 
 
97
 
98
  image = cv2.imread(sys.argv[1])
99
  imageRgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 
1
  import sys
 
 
2
  import cv2
3
  import numpy
4
 
5
+ sys.dont_write_bytecode = True
6
+
7
+ # Source
8
  from helper import onnxSessionBuild
9
 
10
  pathModel = "./RT-DETR-L_wired_table_cell_det/"
11
 
12
+ imageSizeModel = 640
13
 
14
  scoreThreshold = 0.3
 
15
  levelBoxNms = 0.5
16
  levelBoxContained = 0.9
17
+ countContainedMinimum = 2
18
 
19
  onnxSession = onnxSessionBuild(f"{pathModel}onnx/rt-detr-l_wired_table_cell_det.onnx")
20
 
 
28
  y2 = min(coordinateFirstList[3], coordinateSecondList[3])
29
 
30
  if x2 <= x1 or y2 <= y1:
31
+ return {"intersectionOverUnion": 0.0, "containment": 0.0, "areaFirst": areaFirst, "areaSecond": areaSecond}
32
 
33
  areaIntersection = (x2 - x1) * (y2 - y1)
34
 
35
  return {
36
  "intersectionOverUnion": areaIntersection / float(areaFirst + areaSecond - areaIntersection),
37
+ "containment": areaIntersection / float(min(areaFirst, areaSecond)),
38
+ "areaFirst": areaFirst,
39
+ "areaSecond": areaSecond
40
  }
41
 
42
  def suppressionNonMaximum(itemList):
 
50
  for b in range(len(resultList)):
51
  overlapObject = overlapCalculate(itemSortedList[a]["coordinate"], resultList[b]["coordinate"])
52
 
53
+ if overlapObject["intersectionOverUnion"] >= levelBoxNms:
 
 
 
54
  isKeep = False
55
 
56
  break
 
60
 
61
  return resultList
62
 
63
+ def suppressionContainer(itemList):
64
+ resultList = []
65
+
66
+ for a in range(len(itemList)):
67
+ countContained = 0
68
+
69
+ for b in range(len(itemList)):
70
+ if a == b:
71
+ continue
72
+
73
+ overlapObject = overlapCalculate(itemList[a]["coordinate"], itemList[b]["coordinate"])
74
+
75
+ if overlapObject["containment"] >= levelBoxContained and overlapObject["areaFirst"] > overlapObject["areaSecond"]:
76
+ countContained += 1
77
+
78
+ if countContained < countContainedMinimum:
79
+ resultList.append(itemList[a])
80
+
81
+ return resultList
82
+
83
  def inference(imageRgb):
84
  resultList = []
85
 
86
  imageHeight, imageWidth = imageRgb.shape[0:2]
87
+ imageResized = cv2.resize(imageRgb, (imageSizeModel, imageSizeModel), interpolation=cv2.INTER_CUBIC).astype(numpy.float32) / 255.0
88
 
89
  tensor = numpy.expand_dims(imageResized.transpose((2, 0, 1)), axis=0).astype(numpy.float32)
90
 
91
  tensorFeedObject = {
92
  "image": tensor,
93
+ "im_shape": numpy.array([[imageSizeModel, imageSizeModel]], dtype=numpy.float32),
94
+ "scale_factor": numpy.array([[imageSizeModel / float(imageHeight), imageSizeModel / float(imageWidth)]], dtype=numpy.float32)
95
  }
96
 
97
  tensorOutputList = onnxSession.run(None, tensorFeedObject)
 
113
  "coordinate": [x1, y1, x2, y2]
114
  })
115
 
116
+ resultList = suppressionNonMaximum(resultList)
117
+ resultList = suppressionContainer(resultList)
118
+
119
+ return resultList
120
 
121
  image = cv2.imread(sys.argv[1])
122
  imageRgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)