-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
415 lines (355 loc) · 15.9 KB
/
app.py
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# We need to import request to access the details of the POST request
# and render_template, to render our templates (form and response)
# we'll use url_for to get some URLs for the app on the templates
import os
from datetime import datetime
from flask import Flask, render_template, request, redirect, flash, Response, send_file
#from flask_sslify import SSLify
from werkzeug.utils import secure_filename
from fileProcessor import processFile, check_file_extension, createResultView, findLatest, coordinatesLookup, filterCropFiles, updateVisualizeResults
from logWriter import writeLog, writeUserLog, updateHTML
from pathParameter import LOCDB, detectronDir, outputPath, modelPath, imagesDir, annotationsDir, debugMode
import zipfile
from io import BytesIO
from werkzeug.serving import WSGIRequestHandler
import json
from bs4 import BeautifulSoup
import subprocess
from rq import Queue
from rq.job import Job
from worker import conn
import natsort
UPLOAD_FOLDER = 'upload/'
OUTPUT_FOLDER = 'output/'
ALLOWED_EXTENSIONS = set([ 'pdf', 'png', 'jpg', 'jpeg','tif','xml','odt','doc','docx','txt', 'htm', 'html', 'xhtml'])
Settings = ["Unknown", "True"]
MAX_PROCESSES = 1
#refresh userlogLOCDB
if os.path.exists("visibleUserLog.txt"):
os.remove("visibleUserLog.txt")
with open("visibleUserLog.txt", "w") as f:
f.write("To get information if a uploaded file is finished press the update button.\nIt updates on every request. \nThis is the user log.\n")
# Initialize the Flask application
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.secret_key = "super secret key"
q = Queue(connection=conn)
# Define a route for the default URL, which loads the form
@app.route('/')
def form():
writeUserLog("App started")
return updateHTML()
#updates the user log
@app.route('/logupdate/', methods=['GET','POST'])
def logupdate():
return updateHTML()
@app.route('/fileupload/', methods=['GET','POST'])
def fileupload():
if debugMode.lower() == "yes":
print ""
print "####################################"
print "In fileupload()"
print "####################################"
print ""
#checks the upload request parameters and stores it to the upload folder
if request.method == 'POST':
if 'files' not in request.files:
flash('No file part')
return redirect(request.url)
#requested files and pdf mode are retrieved from flask
fileList = request.files.getlist('files')
if request.form.get('pdfFlag'):
Settings[0] = "IMG"
else:
Settings[0] = "TXT"
if request.form.get('Txt_Dummy'):
Settings[1] = "True"
else:
Settings[1] = "False"
autoview = False
if request.form.get('autoviewResults'):
autoview = True
filenameFP_List = []
filenameString =""
for uploadedFile in fileList:
if uploadedFile.filename == '':
flash('No selected inputfile')
return redirect(request.url)
if uploadedFile and check_file_extension(uploadedFile.filename, ALLOWED_EXTENSIONS):
filenameFP = secure_filename(uploadedFile.filename)
print 'Uploaded inputfile : '+filenameFP
print ""
writeUserLog("Uploaded inputfile : " + filenameFP)
#adding timestamp
ts= datetime.now().strftime('%Y%m%d%H%M%S')
filenameFP= ts+"_" + filenameFP
writeLog(filenameFP,Settings, False)
uploadedFile.save(os.path.join(LOCDB+UPLOAD_FOLDER, filenameFP))
filenameFP_List.append(filenameFP)
filenameString += filenameFP + "\n"
else:
return "Error: Invalid file extension..."
try:
job = q.enqueue_call(
func=processFile, args=(UPLOAD_FOLDER, OUTPUT_FOLDER, MAX_PROCESSES, Settings, filenameFP_List,), result_ttl=8000, timeout=80000
)
print(job.get_id())
return job.get_id()
#sync process
filenameFP_List = natsort.natsorted(filenameFP_List)
result = createResultView(OUTPUT_FOLDER, filenameFP_List)
return Response(result, content_type='text/xml; charset=utf-8')
except:
return "An Error occured during file processing..."
if autoview:
return render_template("form_submitocr.html",waiting="1",filesText = filenameString)
else:
return updateHTML()
return "Error"
@app.route("/results/<job_key>", methods=['GET'])
def get_results(job_key):
job = Job.fetch(job_key, connection=conn)
if job.is_finished:
return job.result, 200
else:
return "In Processing!", 202
#method that checks if result is ready and after that returs it
@app.route('/pollresult/', methods=['GET','POST'])
def pollresult():
fileString = request.form.get('filenames_field')
resultList = []
file_list = fileString.strip().splitlines()
for file in file_list:
for folder in os.listdir("upload"):
if file in folder:
resultList.append(folder)
if len(resultList) == 0:
file_list = natsort.natsorted(file_list)
result = createResultView(OUTPUT_FOLDER, file_list)
return Response(result, content_type='text/xml; charset=utf-8')
else:
return render_template("form_submitocr.html",waiting="1",filesText = fileString)
# Method used for the file browser to view already finished files
@app.route('/fileview/', methods=['GET','POST'])
def fileview():
#checks the upload and stores it to the upload folder
if request.method == 'POST':
if 'files' not in request.files:
flash('No file part')
return redirect(request.url)
#requested files and pdf mode are retrieved from flask
fileList = request.files.getlist('files')
filenameFP_List = []
for uploadedFile in fileList:
if uploadedFile.filename == '':
flash('No selected inputfile')
return redirect(request.url)
if uploadedFile and check_file_extension(uploadedFile.filename, ALLOWED_EXTENSIONS):
filenameFP = secure_filename(uploadedFile.filename)
print "filenameFP:",filenameFP
filenameFP_List.append(filenameFP)
if debugMode.lower() == "yes":
print "filenameFP_List:",filenameFP_List
print "OUTPUT_FOLDER:",OUTPUT_FOLDER
filenameFP_List = natsort.natsorted(filenameFP_List)
result = createResultView(OUTPUT_FOLDER, filenameFP_List, 2)
return Response(result, content_type='text/xml; charset=utf-8')
return "Error: Results not found"
# method used for the file browser to view already finished files
@app.route('/getimage/', methods=['GET', 'POST'])
def getimage():
# checks the upload and stores it to the upload folder
if request.method == 'POST':
if 'files' not in request.files:
flash('No file part')
return redirect(request.url)
# requested files and pdf mode are retrieved from flask
fileList = request.files.getlist('files')
securefname = secure_filename(fileList[0].filename)
if os.path.splitext(securefname)[1].lower() == ".pdf":
filenameFP_List = []
for uploadedFile in fileList:
if uploadedFile.filename == '':
flash('No selected inputfile')
return redirect(request.url)
if uploadedFile and check_file_extension(uploadedFile.filename, ALLOWED_EXTENSIONS):
filenameFP = secure_filename(uploadedFile.filename)
filenameFP_List.append(filenameFP)
allFilenames3 = os.listdir("images")
allFilenames3 = natsort.natsorted(allFilenames3)
filenameFP_List = natsort.natsorted(filenameFP_List)
tempFilesList = []
for currFile1 in filenameFP_List:
for currFile2 in allFilenames3:
if currFile1[:-4] in currFile2:
tempFilesList.append(currFile2)
if debugMode.lower() == "yes":
print "tempFilesList:",len(tempFilesList)
tempFilesList = filterCropFiles(tempFilesList)
if len(tempFilesList) != 0:
latestFileNames = findLatest(tempFilesList)
if debugMode.lower() == "yes":
print "latestFileNames:",latestFileNames
currDir = os.getcwd()
os.chdir(LOCDB+"images/")
memory_file = BytesIO()
with zipfile.ZipFile(memory_file, 'w') as zf:
for individualFile in latestFileNames:
zf.write(individualFile)
os.chdir(currDir)
memory_file.seek(0)
return send_file(memory_file, attachment_filename=currFile1[:-4]+'.zip', as_attachment=True)
else:
return "Error: No Files Found..."
else:
return "Wrong File Type: Please upload a pdf file"
return "Error: Results not found"
@app.route('/segmentReference/', methods = ['POST', 'GET'])
def segmentReference():
if request.method == 'POST':
filename = request.form.get('filename').encode('utf8')
coordinates = request.form.get('coordinates')
if debugMode.lower() == "yes":
print "filename:",filename
print "coordinates:",type(coordinates)
coordinates = coordinates.strip().split(' ')
if debugMode.lower() == "yes":
print "coordinates:",coordinates
coordinates = map(int, coordinates)
if filename and coordinates:
# For single filename only
filename = [filename]
tempFilesList = []
for currFile1 in filename:
for currFile2 in os.listdir("images"):
if currFile1[:-4] in currFile2:
tempFilesList.append(currFile2)
latest_filename = findLatest(tempFilesList)
try:
result = coordinatesLookup(latest_filename,coordinates)
return Response(result, content_type='text/xml; charset=utf-8')
except ValueError:
return "Error processing the given coordinates"
return 'Error: Results not found'
@app.route('/uploadCorrections/', methods = ['POST'])
def upload_corrections():
if request.method == 'POST':
allowed_extensions = ['jpg','png','jpeg','tif']
f = request.files.getlist('files')
if debugMode.lower() == "yes":
print "files:",f
for curr in f:
securedFilename = secure_filename(curr.filename)
if securedFilename[-3:].lower() == "xml":
curr.save(os.path.join(annotationsDir+securedFilename))
elif securedFilename[-3:].lower() in allowed_extensions:
curr.save(os.path.join(imagesDir+securedFilename))
else:
return "Unsupported File Found: Please use jpg, png, jpeg, tif and xml extensions only."
return 'File uploaded successfully'
@app.route('/triggerTraining/', methods = ['POST'])
def trigger_training():
outputFile = "annotations-finetune.json"
filenames_unique = []
width = []
height = []
filenames = []
x1 =[]
y1 =[]
x2 =[]
y2 =[]
allFilenames = []
for (dirpath, dirnames, filenames1) in os.walk(annotationsDir):
allFilenames.extend(filenames1)
break
allFilenames2 = []
for (dirpath, dirnames, filenames1) in os.walk(imagesDir):
allFilenames2.extend(filenames1)
break
if len(allFilenames) == 0:
return "No files available for training..."
if len(allFilenames) != len(allFilenames2):
return "Error: Missing annotation or image files..."
if os.path.isfile(modelPath+"model_final.pkl"):
ts = datetime.now().strftime('%Y%m%d%H%M%S')
os.system("mv "+modelPath+"model_final.pkl "+ modelPath+ts+"_"+"model_final.pkl")
for currFile in allFilenames:
with open(annotationsDir+currFile) as f:
xmldata = f.read().encode('utf-8')
parsXmlsoup = BeautifulSoup(xmldata,'xml')
tempname = parsXmlsoup.filename.string
tempname = tempname.replace('.-','-')
if (tempname[-3:]).lower() != "jpg":
if tempname[-1] == ".":
tempname = tempname + "jpg"
else:
tempname = tempname + ".jpg"
if not (parsXmlsoup.filename.string in filenames_unique):
filenames_unique.append(tempname)
width.append(int(parsXmlsoup.size.width.string))
height.append(int(parsXmlsoup.size.height.string))
xmltags= parsXmlsoup.find_all('bndbox')
for curr in xmltags:
filenames.append(tempname)
x1.append(int(curr.xmin.string))
y1.append(int(curr.ymin.string))
x2.append(int(curr.xmax.string))
y2.append(int(curr.ymax.string))
data = {}
images_list = []
annotations_list = []
categories_list = []
for index, currImage in enumerate(filenames_unique):
images_dict = dict()
images_dict["file_name"] = currImage
images_dict["height"] = height[index]
images_dict["width"] = width[index]
images_dict["id"] = index+1
images_list.append(images_dict)
annotation_id = 1
for temp_image,tempx1,tempy1,tempx2,tempy2 in zip(filenames,x1,y1,x2,y2):
temp_id = filenames_unique.index(temp_image)
temp_box_height = tempy2 - tempy1
temp_box_width = tempx2 - tempx1
segmentation_dict = dict()
segmentation_dict["segmentation"] = [[tempx1,tempy1,tempx1,tempy1+temp_box_height,tempx1+temp_box_width,tempy1+temp_box_height,tempx1+temp_box_width,tempy1]]
segmentation_dict["area"] = temp_box_height * temp_box_width
segmentation_dict["iscrowd"] = 0
segmentation_dict["image_id"] = temp_id+1
segmentation_dict["id"] = annotation_id
segmentation_dict["bbox"] = [tempx1,tempy1,temp_box_width,temp_box_height]
segmentation_dict["category_id"] = 1 # Fixed (Reference Class)
segmentation_dict["ignore"] = 0
annotations_list.append(segmentation_dict)
annotation_id += 1
categories_dict = dict()
categories_dict["supercategory"] = "none"
categories_dict["id"] = 1
categories_dict["name"] = "reference"
categories_list.append(categories_dict)
data['images'] = images_list
data["type"] = "instances"
data["annotations"] = annotations_list
data["categories"] = categories_list
with open(outputPath+outputFile, 'w') as outfile:
json.dump(data, outfile)
try:
subprocess.call(["python2 tools/train_net.py --cfg configs/custom/e2e_mask_rcnn_R-50-C4_1x-LOCDB-finetune.yaml"], shell=True, cwd=detectronDir)
except:
return "An Error occured during network training..."
os.system("rm "+annotationsDir+"*")
os.system("rm "+imagesDir+"*")
return 'Training Finished...'
@app.route('/visualizeResults/')
def visualizeResults():
tags = updateVisualizeResults()
#return render_template('visualize_results.html')
return render_template('visualize_results.html', divTags = tags)
# Run the app :)
if __name__ == '__main__':
WSGIRequestHandler.protocol_version = "HTTP/1.1"
app.run(
host="0.0.0.0",
port=int("8000")
)
#sslify = SSLify(app)