cimo001 commited on
Commit
44d0d80
·
verified ·
1 Parent(s): c24af8a
Files changed (1) hide show
  1. Docling-Layout-Heron/src/example.py +85 -0
Docling-Layout-Heron/src/example.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = "./Docling-Layout-Heron/"
11
+
12
+ imageSizeModel = 640
13
+
14
+ scoreThreshold = 0.5
15
+
16
+ labelList = [
17
+ "caption",
18
+ "footnote",
19
+ "formula",
20
+ "list_item",
21
+ "page_footer",
22
+ "page_header",
23
+ "picture",
24
+ "section_header",
25
+ "table",
26
+ "text",
27
+ "title",
28
+ "document_index",
29
+ "code",
30
+ "checkbox_selected",
31
+ "checkbox_unselected",
32
+ "form",
33
+ "key_value_region"
34
+ ]
35
+
36
+ onnxSession = onnxSessionBuild(f"{pathModel}onnx/docling-layout-heron.onnx")
37
+
38
+ def inference(imageRgb):
39
+ resultList = []
40
+
41
+ imageHeight, imageWidth = imageRgb.shape[0:2]
42
+ imageResized = cv2.resize(imageRgb, (imageSizeModel, imageSizeModel), interpolation=cv2.INTER_LINEAR).astype(numpy.float32) / 255.0
43
+
44
+ tensor = numpy.expand_dims(imageResized.transpose((2, 0, 1)), axis=0).astype(numpy.float32)
45
+
46
+ tensorOutputList = onnxSession.run(None, {"pixel_values": tensor})
47
+
48
+ scoreList = 1.0 / (1.0 + numpy.exp(-tensorOutputList[0][0].astype(numpy.float64)))
49
+ boxList = tensorOutputList[1][0]
50
+
51
+ for a in range(scoreList.shape[0]):
52
+ for b in range(scoreList.shape[1]):
53
+ if scoreList[a][b] < scoreThreshold:
54
+ continue
55
+
56
+ centerX, centerY, width, height = boxList[a]
57
+
58
+ x1 = max(0.0, float(centerX - width / 2)) * imageWidth
59
+ y1 = max(0.0, float(centerY - height / 2)) * imageHeight
60
+ x2 = min(1.0, float(centerX + width / 2)) * imageWidth
61
+ y2 = min(1.0, float(centerY + height / 2)) * imageHeight
62
+
63
+ if x2 > x1 and y2 > y1:
64
+ resultList.append({
65
+ "label": labelList[b],
66
+ "score": float(scoreList[a][b]),
67
+ "coordinate": [x1, y1, x2, y2]
68
+ })
69
+
70
+ resultList.sort(key=lambda itemObject: itemObject["score"], reverse=True)
71
+
72
+ return resultList
73
+
74
+ image = cv2.imread(sys.argv[1])
75
+ imageRgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
76
+
77
+ itemList = inference(imageRgb)
78
+
79
+ for a in range(len(itemList)):
80
+ coordinateList = []
81
+
82
+ for b in range(len(itemList[a]["coordinate"])):
83
+ coordinateList.append(int(round(itemList[a]["coordinate"][b])))
84
+
85
+ print(f"{itemList[a]['score']:.6f} | {itemList[a]['label']} | {coordinateList}")