-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
121 lines (100 loc) · 2.97 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
from flask import Flask, render_template, jsonify, request, send_from_directory, send_file, abort
from flask_cors import CORS, cross_origin
from base64 import encodebytes
import glob
import io
import os
from PIL import Image
import json
import numpy as np
cwd = os.getcwd()
# configuration
DEBUG = True
DATA_FOLDER = f"{cwd}/data"
# instantiate the app
app = Flask(__name__, static_folder="dist/static", template_folder="dist", static_url_path="/static")
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
app.config.from_object(__name__)
# definitions
SITE = {
'logo': 'Stackoptica',
'version': '2.0.0'
}
OWNER = {
'name': 'Royal Belgian Institute of Natural Sciences',
}
# pass data to the frontend
site_data = {
'site': SITE,
'owner': OWNER
}
# landing page
@app.route('/<id>')
def welcome(id):
print(f"id : {id}")
return render_template('index.html', **site_data)
# send full image
@app.route('/<id>/<image_id>/full-image')
@cross_origin()
def image(id,image_id):
return send_from_directory(f"{DATA_FOLDER}/{id}", image_id)
# send thumbnail
@app.route('/<id>/<image_id>/thumbnail')
@cross_origin()
def thumbnail(id,image_id):
return send_from_directory(f"{DATA_FOLDER}/{id}/thumbnails", image_id)
# send StackData
@app.route('/<id>/images')
@cross_origin()
def images(id):
directory = f"{DATA_FOLDER}/{id}"
if not os.path.exists(directory):
abort(404)
with open(f"{directory}/stack.json", "r") as f:
stack_file = json.load(f)
to_jsonify = {}
encoded_images = []
for image in stack_file["stack"]:
try:
print(image)
encoded_images.append(image)
except Exception as error:
print(error)
continue
encoded_images.sort(key=lambda image_name : stack_file["stack"][image_name]["SlicePosition"][2])
encoded_images = [[x,x] for x in encoded_images]
stackedImages = dict()
for image in stack_file["Stacked_images"]:
try:
# file name of stacked image
stackedImages[image] = [stack_file['Stacked_images'][image], stack_file['Stacked_images'][image]]
except Exception as error:
print(error)
continue
to_jsonify["stackImages"] = encoded_images
to_jsonify["individualImages"] = stackedImages
to_jsonify["size"] = {
"width" : stack_file["width"],
"height" : stack_file["height"]
}
return jsonify(to_jsonify)
@app.route('/<id>/<image_id>/position')
@cross_origin()
def compute_landmark(id, image_id):
x = float(request.args.get("x"))
y = float(request.args.get("y"))
directory = f"{DATA_FOLDER}/{id}"
if not os.path.exists(directory):
abort(404)
with open(f"{directory}/stack.json", "r") as f:
stack_file = json.load(f)
image_data = stack_file["stack"][image_id]
position = {
"x" : x*image_data["PixelRatio"][0] + image_data["SlicePosition"][0],
"y" : y*image_data["PixelRatio"][1] + image_data["SlicePosition"][1],
"z" : image_data["SlicePosition"][2]
}
return jsonify(position)
if __name__ == '__main__':
app.run()