cimo001 commited on
Commit
613cc17
·
verified ·
1 Parent(s): aac359b
RT-DETR-L_wireless_table_cell_det/src/example.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_wireless_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_wireless_table_cell_det.onnx")
19
+
20
+ def overlapCalculate(coordinateFirstList, coordinateSecondList):
21
+ areaFirst = (coordinateFirstList[2] - coordinateFirstList[0]) * (coordinateFirstList[3] - coordinateFirstList[1])
22
+ areaSecond = (coordinateSecondList[2] - coordinateSecondList[0]) * (coordinateSecondList[3] - coordinateSecondList[1])
23
+
24
+ x1 = max(coordinateFirstList[0], coordinateSecondList[0])
25
+ y1 = max(coordinateFirstList[1], coordinateSecondList[1])
26
+ x2 = min(coordinateFirstList[2], coordinateSecondList[2])
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):
40
+ resultList = []
41
+
42
+ itemSortedList = sorted(itemList, key=lambda itemObject: itemObject["score"], reverse=True)
43
+
44
+ for a in range(len(itemSortedList)):
45
+ isKeep = True
46
+
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
57
+
58
+ if isKeep:
59
+ resultList.append(itemSortedList[a])
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)
78
+
79
+ boxCount = int(tensorOutputList[1][0]) if len(tensorOutputList) > 1 else len(tensorOutputList[0])
80
+
81
+ for a in range(boxCount):
82
+ value = tensorOutputList[0][a]
83
+
84
+ score = float(value[1])
85
+ x1 = max(0.0, min(float(value[2]), float(imageWidth)))
86
+ y1 = max(0.0, min(float(value[3]), float(imageHeight)))
87
+ x2 = max(0.0, min(float(value[4]), float(imageWidth)))
88
+ y2 = max(0.0, min(float(value[5]), float(imageHeight)))
89
+
90
+ if score >= scoreThreshold and x2 > x1 and y2 > y1:
91
+ resultList.append({
92
+ "score": score,
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)
100
+
101
+ itemList = inference(imageRgb)
102
+
103
+ for a in range(len(itemList)):
104
+ coordinateList = []
105
+
106
+ for b in range(len(itemList[a]["coordinate"])):
107
+ coordinateList.append(int(round(itemList[a]["coordinate"][b])))
108
+
109
+ print(f"{itemList[a]['score']:.6f} | {coordinateList}")