forked from fadyazizz/FloorPlanTo3D-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.py
More file actions
176 lines (133 loc) · 3.8 KB
/
application.py
File metadata and controls
176 lines (133 loc) · 3.8 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
import os
import PIL
import numpy
from numpy.lib.function_base import average
from numpy import zeros
from numpy import asarray
from mrcnn.config import Config
from mrcnn.model import MaskRCNN
from skimage.draw import polygon2mask
from skimage.io import imread
from datetime import datetime
from io import BytesIO
from mrcnn.utils import extract_bboxes
from numpy import expand_dims
from matplotlib import pyplot
from matplotlib.patches import Rectangle
from keras.backend import clear_session
import json
from flask import Flask, flash, request,jsonify, redirect, url_for
from werkzeug.utils import secure_filename
from skimage.io import imread
from mrcnn.model import mold_image
import tensorflow as tf
import sys
from PIL import Image
global _model
global _graph
global cfg
ROOT_DIR = os.path.abspath("./")
WEIGHTS_FOLDER = "./weights"
from flask_cors import CORS, cross_origin
sys.path.append(ROOT_DIR)
MODEL_NAME = "mask_rcnn_hq"
WEIGHTS_FILE_NAME = 'maskrcnn_15_epochs.h5'
application=Flask(__name__)
cors = CORS(application, resources={r"/*": {"origins": "*"}})
class PredictionConfig(Config):
# define the name of the configuration
NAME = "floorPlan_cfg"
# number of classes (background + door + wall + window)
NUM_CLASSES = 1 + 3
# simplify GPU config
GPU_COUNT = 1
IMAGES_PER_GPU = 1
@application.before_first_request
def load_model():
global cfg
global _model
model_folder_path = os.path.abspath("./") + "/mrcnn"
weights_path= os.path.join(WEIGHTS_FOLDER, WEIGHTS_FILE_NAME)
cfg=PredictionConfig()
print(cfg.IMAGE_RESIZE_MODE)
print('==============before loading model=========')
_model = MaskRCNN(mode='inference', model_dir=model_folder_path,config=cfg)
print('=================after loading model==============')
_model.load_weights(weights_path, by_name=True)
global _graph
_graph = tf.get_default_graph()
def myImageLoader(imageInput):
image = numpy.asarray(imageInput)
h,w,c=image.shape
if image.ndim != 3:
image = skimage.color.gray2rgb(image)
if image.shape[-1] == 4:
image = image[..., :3]
return image,w,h
def getClassNames(classIds):
result=list()
for classid in classIds:
data={}
if classid==1:
data['name']='wall'
if classid==2:
data['name']='window'
if classid==3:
data['name']='door'
result.append(data)
return result
def normalizePoints(bbx,classNames):
normalizingX=1
normalizingY=1
result=list()
doorCount=0
index=-1
doorDifference=0
for bb in bbx:
index=index+1
if(classNames[index]==3):
doorCount=doorCount+1
if(abs(bb[3]-bb[1])>abs(bb[2]-bb[0])):
doorDifference=doorDifference+abs(bb[3]-bb[1])
else:
doorDifference=doorDifference+abs(bb[2]-bb[0])
result.append([bb[0]*normalizingY,bb[1]*normalizingX,bb[2]*normalizingY,bb[3]*normalizingX])
return result,(doorDifference/doorCount)
def turnSubArraysToJson(objectsArr):
result=list()
for obj in objectsArr:
data={}
data['x1']=obj[1]
data['y1']=obj[0]
data['x2']=obj[3]
data['y2']=obj[2]
result.append(data)
return result
@application.route('/',methods=['POST'])
def prediction():
global cfg
imagefile = PIL.Image.open(request.files['image'].stream)
image,w,h=myImageLoader(imagefile)
print(h,w)
scaled_image = mold_image(image, cfg)
sample = expand_dims(scaled_image, 0)
global _model
global _graph
with _graph.as_default():
r = _model.detect(sample, verbose=0)[0]
#output_data = model_api(imagefile)
data={}
bbx=r['rois'].tolist()
temp,averageDoor=normalizePoints(bbx,r['class_ids'])
temp=turnSubArraysToJson(temp)
data['points']=temp
data['classes']=getClassNames(r['class_ids'])
data['Width']=w
data['Height']=h
data['averageDoor']=averageDoor
return jsonify(data)
if __name__ =='__main__':
application.debug=True
print('===========before running==========')
application.run()
print('===========after running==========')