Grosch commited on
Commit
4c8156b
1 Parent(s): 72bd9af

Initial setup

Browse files
Files changed (5) hide show
  1. .gitattributes +1 -0
  2. README.md +4 -4
  3. app.py +194 -0
  4. eynollah-flow.png +0 -0
  5. requirements.txt +6 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+
README.md CHANGED
@@ -1,13 +1,13 @@
1
  ---
2
  title: Eynollah Demo
3
- emoji: 🌖
4
- colorFrom: blue
5
  colorTo: red
6
  sdk: gradio
7
- sdk_version: 4.27.0
8
  app_file: app.py
9
  pinned: false
10
- license: apache-2.0
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
  title: Eynollah Demo
3
+ emoji: 👁
4
+ colorFrom: green
5
  colorTo: red
6
  sdk: gradio
7
+ sdk_version: 4.26.0
8
  app_file: app.py
9
  pinned: false
10
+ license: mit
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,194 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ import cv2
5
+ from huggingface_hub import from_pretrained_keras
6
+
7
+ def resize_image(img_in,input_height,input_width):
8
+ return cv2.resize( img_in, ( input_width,input_height) ,interpolation=cv2.INTER_NEAREST)
9
+
10
+ def do_prediction(model_name, img):
11
+ model = from_pretrained_keras(model_name)
12
+
13
+ match model_name:
14
+ # numerical output
15
+ case "SBB/eynollah-column-classifier":
16
+
17
+ num_col=model.layers[len(model.layers)-1].output_shape[1]
18
+ return "Found {} columns".format(num_col), None
19
+
20
+ # bitmap output
21
+ case "SBB/eynollah-binarization" | "SBB/eynollah-page-extraction" | "SBB/eynollah-textline" | "SBB/eynollah-textline_light" | "SBB/eynollah-enhancement" | "SBB/eynollah-tables" | "SBB/eynollah-main-regions" | "SBB/eynollah-main-regions-aug-rotation" | "SBB/eynollah-main-regions-aug-scaling" | "SBB/eynollah-main-regions-ensembled" | "SBB/eynollah-full-regions-1column" | "SBB/eynollah-full-regions-3pluscolumn":
22
+
23
+ img_height_model=model.layers[len(model.layers)-1].output_shape[1]
24
+ img_width_model=model.layers[len(model.layers)-1].output_shape[2]
25
+ n_classes=model.layers[len(model.layers)-1].output_shape[3]
26
+
27
+ if img.shape[0] < img_height_model:
28
+ img = resize_image(img, img_height_model, img.shape[1])
29
+
30
+ if img.shape[1] < img_width_model:
31
+ img = resize_image(img, img.shape[0], img_width_model)
32
+
33
+ marginal_of_patch_percent = 0.1
34
+ margin = int(marginal_of_patch_percent * img_height_model)
35
+ width_mid = img_width_model - 2 * margin
36
+ height_mid = img_height_model - 2 * margin
37
+ img = img / float(255.0)
38
+ img = img.astype(np.float16)
39
+ img_h = img.shape[0]
40
+ img_w = img.shape[1]
41
+ prediction_true = np.zeros((img_h, img_w, 3))
42
+ mask_true = np.zeros((img_h, img_w))
43
+ nxf = img_w / float(width_mid)
44
+ nyf = img_h / float(height_mid)
45
+ nxf = int(nxf) + 1 if nxf > int(nxf) else int(nxf)
46
+ nyf = int(nyf) + 1 if nyf > int(nyf) else int(nyf)
47
+
48
+ for i in range(nxf):
49
+ for j in range(nyf):
50
+ if i == 0:
51
+ index_x_d = i * width_mid
52
+ index_x_u = index_x_d + img_width_model
53
+ else:
54
+ index_x_d = i * width_mid
55
+ index_x_u = index_x_d + img_width_model
56
+ if j == 0:
57
+ index_y_d = j * height_mid
58
+ index_y_u = index_y_d + img_height_model
59
+ else:
60
+ index_y_d = j * height_mid
61
+ index_y_u = index_y_d + img_height_model
62
+ if index_x_u > img_w:
63
+ index_x_u = img_w
64
+ index_x_d = img_w - img_width_model
65
+ if index_y_u > img_h:
66
+ index_y_u = img_h
67
+ index_y_d = img_h - img_height_model
68
+
69
+ img_patch = img[index_y_d:index_y_u, index_x_d:index_x_u, :]
70
+ label_p_pred = model.predict(img_patch.reshape(1, img_patch.shape[0], img_patch.shape[1], img_patch.shape[2]),
71
+ verbose=0)
72
+
73
+ seg = np.argmax(label_p_pred, axis=3)[0]
74
+
75
+ seg_color = np.repeat(seg[:, :, np.newaxis], 3, axis=2)
76
+
77
+ if i == 0 and j == 0:
78
+ seg_color = seg_color[0 : seg_color.shape[0] - margin, 0 : seg_color.shape[1] - margin, :]
79
+ #seg = seg[0 : seg.shape[0] - margin, 0 : seg.shape[1] - margin]
80
+ #mask_true[index_y_d + 0 : index_y_u - margin, index_x_d + 0 : index_x_u - margin] = seg
81
+ prediction_true[index_y_d + 0 : index_y_u - margin, index_x_d + 0 : index_x_u - margin, :] = seg_color
82
+ elif i == nxf - 1 and j == nyf - 1:
83
+ seg_color = seg_color[margin : seg_color.shape[0] - 0, margin : seg_color.shape[1] - 0, :]
84
+ #seg = seg[margin : seg.shape[0] - 0, margin : seg.shape[1] - 0]
85
+ #mask_true[index_y_d + margin : index_y_u - 0, index_x_d + margin : index_x_u - 0] = seg
86
+ prediction_true[index_y_d + margin : index_y_u - 0, index_x_d + margin : index_x_u - 0, :] = seg_color
87
+ elif i == 0 and j == nyf - 1:
88
+ seg_color = seg_color[margin : seg_color.shape[0] - 0, 0 : seg_color.shape[1] - margin, :]
89
+ #seg = seg[margin : seg.shape[0] - 0, 0 : seg.shape[1] - margin]
90
+ #mask_true[index_y_d + margin : index_y_u - 0, index_x_d + 0 : index_x_u - margin] = seg
91
+ prediction_true[index_y_d + margin : index_y_u - 0, index_x_d + 0 : index_x_u - margin, :] = seg_color
92
+ elif i == nxf - 1 and j == 0:
93
+ seg_color = seg_color[0 : seg_color.shape[0] - margin, margin : seg_color.shape[1] - 0, :]
94
+ #seg = seg[0 : seg.shape[0] - margin, margin : seg.shape[1] - 0]
95
+ #mask_true[index_y_d + 0 : index_y_u - margin, index_x_d + margin : index_x_u - 0] = seg
96
+ prediction_true[index_y_d + 0 : index_y_u - margin, index_x_d + margin : index_x_u - 0, :] = seg_color
97
+ elif i == 0 and j != 0 and j != nyf - 1:
98
+ seg_color = seg_color[margin : seg_color.shape[0] - margin, 0 : seg_color.shape[1] - margin, :]
99
+ #seg = seg[margin : seg.shape[0] - margin, 0 : seg.shape[1] - margin]
100
+ #mask_true[index_y_d + margin : index_y_u - margin, index_x_d + 0 : index_x_u - margin] = seg
101
+ prediction_true[index_y_d + margin : index_y_u - margin, index_x_d + 0 : index_x_u - margin, :] = seg_color
102
+ elif i == nxf - 1 and j != 0 and j != nyf - 1:
103
+ seg_color = seg_color[margin : seg_color.shape[0] - margin, margin : seg_color.shape[1] - 0, :]
104
+ #seg = seg[margin : seg.shape[0] - margin, margin : seg.shape[1] - 0]
105
+ #mask_true[index_y_d + margin : index_y_u - margin, index_x_d + margin : index_x_u - 0] = seg
106
+ prediction_true[index_y_d + margin : index_y_u - margin, index_x_d + margin : index_x_u - 0, :] = seg_color
107
+ elif i != 0 and i != nxf - 1 and j == 0:
108
+ seg_color = seg_color[0 : seg_color.shape[0] - margin, margin : seg_color.shape[1] - margin, :]
109
+ #seg = seg[0 : seg.shape[0] - margin, margin : seg.shape[1] - margin]
110
+ #mask_true[index_y_d + 0 : index_y_u - margin, index_x_d + margin : index_x_u - margin] = seg
111
+ prediction_true[index_y_d + 0 : index_y_u - margin, index_x_d + margin : index_x_u - margin, :] = seg_color
112
+ elif i != 0 and i != nxf - 1 and j == nyf - 1:
113
+ seg_color = seg_color[margin : seg_color.shape[0] - 0, margin : seg_color.shape[1] - margin, :]
114
+ #seg = seg[margin : seg.shape[0] - 0, margin : seg.shape[1] - margin]
115
+ #mask_true[index_y_d + margin : index_y_u - 0, index_x_d + margin : index_x_u - margin] = seg
116
+ prediction_true[index_y_d + margin : index_y_u - 0, index_x_d + margin : index_x_u - margin, :] = seg_color
117
+ else:
118
+ seg_color = seg_color[margin : seg_color.shape[0] - margin, margin : seg_color.shape[1] - margin, :]
119
+ #seg = seg[margin : seg.shape[0] - margin, margin : seg.shape[1] - margin]
120
+ #mask_true[index_y_d + margin : index_y_u - margin, index_x_d + margin : index_x_u - margin] = seg
121
+ prediction_true[index_y_d + margin : index_y_u - margin, index_x_d + margin : index_x_u - margin, :] = seg_color
122
+
123
+ prediction_true = prediction_true.astype(np.uint8)
124
+
125
+ '''
126
+ img = img / float(255.0)
127
+ image = resize_image(image, 224,448)
128
+ prediction = model.predict(image.reshape(1,224,448,image.shape[2]))
129
+ prediction = tf.squeeze(tf.round(prediction))
130
+
131
+ prediction = np.argmax(prediction,axis=2)
132
+
133
+ prediction = np.repeat(prediction[:, :, np.newaxis]*255, 3, axis=2)
134
+ print(prediction.shape)
135
+
136
+ '''
137
+ prediction_true = prediction_true * -1
138
+ prediction_true = prediction_true + 1
139
+ return "No numerical output", prediction_true * 255
140
+
141
+ # catch-all (we should not reach this)
142
+ case _:
143
+ return None, None
144
+
145
+ title = "Welcome to the Eynollah Demo page! 👁️"
146
+ description = """
147
+ <div class="row" style="display: flex">
148
+ <div class="column" style="flex: 50%; font-size: 17px">
149
+ This Space demonstrates the functionality of various Eynollah models developed at <a rel="nofollow" href="https://huggingface.co/SBB">SBB</a>.
150
+ <br><br>
151
+ The Eynollah suite introduces an <u>end-to-end pipeline</u> to extract layout, text lines and reading order for historic documents, where the output can be used as an input for OCR engines.
152
+ Please keep in mind that with this demo you can just use <u>one of the 13 sub-modules</u> of the whole Eynollah system <u>at a time</u>.
153
+ </div>
154
+ <div class="column" style="flex: 5%; font-size: 17px"></div>
155
+ <div class="column" style="flex: 45%; font-size: 17px">
156
+ <strong style="font-size: 19px">Resources for more information:</strong>
157
+ <ul>
158
+ <li>The GitHub Repo can be found <a rel="nofollow" href="https://github.com/qurator-spk/eynollah">here</a></li>
159
+ <li>Associated Paper: <a rel="nofollow" href="https://doi.org/10.1145/3604951.3605513">Document Layout Analysis with Deep Learning and Heuristics</a></li>
160
+ <li>The full Eynollah pipeline can be viewed <a rel="nofollow" href="https://huggingface.co/spaces/SBB/eynollah-demo-test/blob/main/eynollah-flow.png">here</a></li>
161
+ </ul>
162
+ </li>
163
+ </div>
164
+ </div>
165
+ """
166
+ iface = gr.Interface(
167
+ title=title,
168
+ description=description,
169
+ fn=do_prediction,
170
+ inputs=[
171
+ gr.Dropdown([
172
+ "SBB/eynollah-binarization",
173
+ "SBB/eynollah-enhancement",
174
+ "SBB/eynollah-page-extraction",
175
+ "SBB/eynollah-column-classifier",
176
+ "SBB/eynollah-tables",
177
+ "SBB/eynollah-textline",
178
+ "SBB/eynollah-textline_light",
179
+ "SBB/eynollah-main-regions",
180
+ "SBB/eynollah-main-regions-aug-rotation",
181
+ "SBB/eynollah-main-regions-aug-scaling",
182
+ "SBB/eynollah-main-regions-ensembled",
183
+ "SBB/eynollah-full-regions-1column",
184
+ "SBB/eynollah-full-regions-3pluscolumn"
185
+ ], label="Select one model of the Eynollah suite 👇", info=""),
186
+ gr.Image()
187
+ ],
188
+ outputs=[
189
+ gr.Textbox(label="Output of model (numerical or bitmap) ⬇️"),
190
+ gr.Image()
191
+ ],
192
+ #examples=[['example-1.jpg']]
193
+ )
194
+ iface.launch()
eynollah-flow.png ADDED
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ tensorflow == 2.12
2
+ opencv-python
3
+ tqdm
4
+ pandas
5
+ seaborn
6
+ huggingface_hub