-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
287 lines (229 loc) · 10.7 KB
/
server.py
File metadata and controls
287 lines (229 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import base64
import hashlib
import os
import time
import sys
from io import BytesIO
from PIL import Image
from flask import Flask, request, jsonify
from flask_cors import CORS, cross_origin
import json
import uuid
from src import contrastAdjustor, noiseRemove, detectLines, toGrayscale, toBlackWhite, toBoxes, utilities, resizeImage, \
dilation, convertPDF2img
from src.utilities import ImageToFile, FileToImage
app = Flask(__name__)
CORS(app)
@app.route('/')
def hello():
return "OMAGA IT WfORKS!"
@app.route('/addImage', methods=['GET'])
def get():
return "Post only"
"""The microservice responsable for managing requests regarding getting an image(JPEG/PNG) and returning the processed
image.
The microservice recives a name for the image, the image's payload, the boolean parameters for applying the noise
reduction and dilation(expansion), two floats representing the contrastFactor and the segmentationFactor and one int
for the separationFactor.
The microservice process the image according given parameters and returns the name of the image converted to
black-white, it's payload and the coordinates for each detected character(upper-left and lower-right).
"""
@app.route('/addImage', methods=['POST', 'OPTIONS'])
def addImage():
sys.setrecursionlimit(2000000)
name = request.json['name']
payload = request.json['payload']
contrastFactor = request.json['contrastFactor']
applyDilation = request.json['applyDilation']
applyNoiseReduction = request.json['applyNoiseReduction']
segmentationFactor = request.json['segmentationFactor']
separationFactor = request.json['separationFactor']
tempBlackWhite = ''
print(
'Received image {}\n\tcontrastFactor:{}\n\tapplyDilation:{}\n\tapplyNoiseReduction:{}\n\tsegmentationFactor:{}\n\tseparationFactor:{}\n\t'.format(
name, contrastFactor, applyDilation, applyNoiseReduction, segmentationFactor, separationFactor))
try:
originalImage = Image.open(BytesIO(base64.b64decode(payload)))
id = int(round(time.time() * 1000))
tempBlackWhite = str(id) + "_tempBW.png"
originalImage = resizeImage.resizeImg(originalImage, 2000, 1900)
originalImage = toGrayscale.ToGrayscale(originalImage)
if applyDilation:
originalImage = dilation.dilation(originalImage)
originalImage = dilation.erosion(originalImage)
if applyNoiseReduction:
absoluteImage = noiseRemove.remove_noise(originalImage)
else:
absoluteImage = contrastAdjustor.AdjustContrast(originalImage, float(contrastFactor))
absoluteImage = toBlackWhite.ToBlackAndWhite(absoluteImage)
ImageToFile(absoluteImage, tempBlackWhite)
lines, linesCoord = detectLines.DetectLines(absoluteImage, float(segmentationFactor))
toBoxes.prepareDebug(tempBlackWhite) # remove this if you don't want an image with the rectangles
toBoxes.GetPixels(absoluteImage)
output = toBoxes.fullFlood(linesCoord, separationFactor)
newPayload = base64.b64encode(open(tempBlackWhite, "rb").read())
# cleanup
if os.path.exists(tempBlackWhite):
os.remove(tempBlackWhite)
if os.path.exists("output_image.jpg"):
os.remove("output_image.jpg")
data = {
"name": name,
"payload": str(newPayload),
"coords": output
}
# return json.dumps(data)
return jsonify(json.dumps(data)), 200, {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
}
except Exception as err:
print("!!!Error: ", err)
if os.path.exists(tempBlackWhite):
os.remove(tempBlackWhite)
if os.path.exists("output_image.jpg"):
os.remove("output_image.jpg")
return jsonify(json.dumps(str(err))), 500, {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
}
"""The microservice responsable for managing requests regarding getting a PDF and returning the processed images
The microservice recives a name for the PDF, the PDF's payload, and the boolean parameters for applying the noise reduction and the segmentation factor
The microservice reconstructs the PDF localy, and then converts each page of the PDF to a JPG image, with an unique ID in its name.
For the first image, all needed processing is done (segmentationm noise remove - if specified), returning the payload of the
processed image, as well as the coordinates from the segmentation, and the name of the image
For the rest, it returns an array of payloads and names of the images
"""
@app.route('/addPdf', methods=['POST', 'OPTIONS'])
def convert_pdf_to_image():
sys.setrecursionlimit(2000000)
pdf_file_name = request.json['name']
pdf_encoded_content = request.json['payload']
apply_dilation = request.json['applyDilation']
contrast_factor = request.json['contrastFactor']
apply_noise_reduction = request.json['applyNoiseReduction']
segmentation_factor = request.json['segmentationFactor']
separation_factor = request.json['separationFactor']
print(
'Received pdf {}\n\tcontrastFactor:{}\n\tapplyDilation:{}\n\tapplyNoiseReduction:{}\n\tsegmentationFactor:{}\n\tseparationFactor:{}\n\t'.format(
pdf_file_name, contrast_factor, apply_dilation, apply_noise_reduction, segmentation_factor,
separation_factor))
in_memory_pdf_file = base64.b64decode(pdf_encoded_content)
open(pdf_file_name, 'wb').write(in_memory_pdf_file)
images_uid_prefix = str(uuid.uuid4())
nr_pages = convertPDF2img.convertToJPG(pdf_file_name, images_uid_prefix)
image_index = 0
image_filenames = []
images_encoded_content = []
image_to_process_filename = ''
for i in range(1, nr_pages + 1):
file = './images' + images_uid_prefix + '_' + str(i) + '.jpg'
print(file)
image_index = image_index + 1
image_filenames.append(file)
images_encoded_content.append((base64.b64encode(open('./images/' + file, 'rb').read())).decode('utf-8'))
if image_index == 1:
image_to_process_filename = file
else:
# delete temp img
os.remove('./images/' + file)
# delete pdf
os.remove(pdf_file_name)
temporary_black_white_image = images_uid_prefix + "_temporary_black_white.png"
'''
temporary_grayscale = images_uid_prefix + "_temporary_grayscale.png"
temporary_contrast = images_uid_prefix + "_temporary_contrast.png"
temporary_noise = images_uid_prefix + "_temporary_noise.png"
temporary_detect_lines = images_uid_prefix + "_temporary_detect_lines.png"
'''
originalImage = Image.open('./images/' + image_to_process_filename)
originalImage = resizeImage.resizeImg(originalImage, 2000, 1900)
originalImage = toGrayscale.ToGrayscale(originalImage)
if apply_dilation:
originalImage = dilation.dilation(originalImage)
originalImage = dilation.erosion(originalImage)
if apply_noise_reduction:
absoluteImage = noiseRemove.remove_noise(originalImage)
else:
absoluteImage = contrastAdjustor.AdjustContrast(originalImage, float(contrast_factor))
absoluteImage = toBlackWhite.ToBlackAndWhite(absoluteImage)
utilities.ImageToFile(absoluteImage, './images/' + temporary_black_white_image)
print("Saved black-white image ", temporary_black_white_image)
lines, linesCoord = detectLines.DetectLines(absoluteImage, float(segmentation_factor))
toBoxes.prepareDebug(temporary_black_white_image) # remove this if you don't want an image with the rectangles
toBoxes.GetPixels(absoluteImage)
coordinates = toBoxes.fullFlood(linesCoord, separation_factor)
with open('./images/' + temporary_black_white_image, 'rb') as file:
processed_image_payload = (base64.b64encode(file.read())).decode('utf-8')
# cleanup
if os.path.exists('./images/' + temporary_black_white_image):
os.remove('./images/' + temporary_black_white_image)
if os.path.exists("output_image.jpg"):
os.remove("output_image.jpg")
os.remove('./images/' + image_to_process_filename)
return_data = {
'names': image_filenames,
'payloads': images_encoded_content,
'pName': image_to_process_filename,
'pPayload': processed_image_payload,
'coords': coordinates
}
if os.path.exists('./images/' + temporary_black_white_image):
os.remove('./images/' + temporary_black_white_image)
# return json.dumps(return_data)
return jsonify(json.dumps(return_data)), 200, {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
}
"""The microservice responsable for managing requests regarding getting a PDF and returning the each page of the PDF
as an JPEG image.
The microservice recives a name for the PDF and the PDF's payload.
The microservice reconstructs the PDF localy, and then converts each page of the PDF to a JPG image, with an unique ID
in its name.
"""
@app.route('/convertPdf', methods=['POST', 'OPTIONS'])
def convert_pdf():
if request.method == 'OPTIONS':
return ''
pdf_file_name = request.json['name']
pdf_encoded_content = request.json['payload']
print('Received pdf {}\n'.format(pdf_file_name))
in_memory_pdf_file = base64.b64decode(pdf_encoded_content)
open(pdf_file_name, 'wb').write(in_memory_pdf_file)
images_uid_prefix = str(uuid.uuid4())
nr_pages = convertPDF2img.convertToJPG(pdf_file_name, images_uid_prefix)
image_index = 0
image_filenames = []
images_encoded_content = []
files = [f for f in os.listdir('./images') if f.startswith(images_uid_prefix + "_")]
for i in range(1, nr_pages + 1):
file = './images/' + images_uid_prefix + '_' + str(i) + '.jpg'
print(file)
image_index = image_index + 1
image_filenames.append(file)
images_encoded_content.append((base64.b64encode(open(file, 'rb').read())).decode('utf-8'))
# delete temp img
os.remove(file)
# delete pdf
os.remove(pdf_file_name)
images_b64_frecv = {}
for b64 in images_encoded_content:
md5 = hashlib.md5()
md5.update(b64.encode())
b64_hash = md5.hexdigest()
if b64_hash in images_b64_frecv:
images_b64_frecv[b64_hash] += 1
else:
images_b64_frecv[b64_hash] = 1
print(images_b64_frecv)
return_data = {
'names': image_filenames,
'payloads': images_encoded_content
}
return jsonify(json.dumps(return_data)), 200, {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
}
if __name__ == '__main__':
sys.setrecursionlimit(2000000)
app.run(host='0.0.0.0', debug=True)