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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import print_function
|
| 2 |
+
from flask import Flask, render_template,request,flash , json, url_for,g , redirect
|
| 3 |
+
import os
|
| 4 |
+
import json
|
| 5 |
+
import fitz
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import cv2
|
| 8 |
+
import numpy as np
|
| 9 |
+
import sys
|
| 10 |
+
import pilecaps_adr
|
| 11 |
+
import base64
|
| 12 |
+
import imutils
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
app = Flask(__name__)
|
| 16 |
+
|
| 17 |
+
out_file = open("static/data/myfile.json", "w")
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# @app.route("/")
|
| 22 |
+
# def home():
|
| 23 |
+
# #lists of the projects el httb3t lel dropdowns w tro7 lel html through the return b nfs el esm hna w hnak
|
| 24 |
+
# mylist=[1,2,3,4]
|
| 25 |
+
# return render_template("proposed-GUI.html",mylist=mylist, matches=[1,2])
|
| 26 |
+
|
| 27 |
+
#info from javascript to python
|
| 28 |
+
@app.route('/getmethod/<jsdata>',methods=['GET','POST'])
|
| 29 |
+
def get_javascript_data(jsdata):
|
| 30 |
+
#get project from the first dropdown
|
| 31 |
+
print(jsdata)
|
| 32 |
+
matches=update_dropdown(jsdata) #list f of projects
|
| 33 |
+
d =[ {'project':(jsdata , matches)} ]
|
| 34 |
+
|
| 35 |
+
with open('static/data/myfile.json', 'w', encoding ='utf8') as json_file:
|
| 36 |
+
json.dump(d, json_file, ensure_ascii = False)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
#get projects bl esm da ex: BMW job1
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
return render_template("proposed-GUI.html",matches=matches)
|
| 43 |
+
#info from python to js
|
| 44 |
+
@app.route('/getpythondata')
|
| 45 |
+
def get_python_data():
|
| 46 |
+
SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
|
| 47 |
+
json_url = os.path.join(SITE_ROOT, "static/data", "myfile.json")
|
| 48 |
+
data = json.load(open(json_url))
|
| 49 |
+
#data[0]['project']
|
| 50 |
+
#matches=update_dropdown(data[0]['project']) #list f of projects
|
| 51 |
+
|
| 52 |
+
return data[0]['project']
|
| 53 |
+
|
| 54 |
+
#get projects of 'project name'
|
| 55 |
+
def update_dropdown(project):
|
| 56 |
+
|
| 57 |
+
plans_inrepo=os.listdir('dropbox_plans')
|
| 58 |
+
#if 'foundation' in radio and project in plans_inrepo
|
| 59 |
+
matches=[]
|
| 60 |
+
if project==None:
|
| 61 |
+
print('no proj')
|
| 62 |
+
# drop=gr.Dropdown.update(choices=matches)
|
| 63 |
+
else:
|
| 64 |
+
for x in plans_inrepo:
|
| 65 |
+
if (project in x): #project name and section in a plan
|
| 66 |
+
matches.append(x)
|
| 67 |
+
print(matches)
|
| 68 |
+
#drop=gr.Dropdown.update(choices=matches)
|
| 69 |
+
return matches
|
| 70 |
+
#make the img global using(g) from flask
|
| 71 |
+
@app.before_request
|
| 72 |
+
def measureImg(img='aa'):
|
| 73 |
+
if type(img) is str:
|
| 74 |
+
g.img='aa'
|
| 75 |
+
print('striiing')
|
| 76 |
+
g.measuredimg='aa'
|
| 77 |
+
else:
|
| 78 |
+
imgPerimeter1,image_new1,SimilarAreaDictionary , colorsUsed=pilecaps_adr.drawAllContours(img,0,[],0,0,'area')
|
| 79 |
+
|
| 80 |
+
g.image_new1 =image_new1
|
| 81 |
+
return image_new1
|
| 82 |
+
|
| 83 |
+
#retrieve all info of elements after clicking 'measure' button
|
| 84 |
+
@app.route("/",methods=["GET", "POST"])
|
| 85 |
+
def getInfotoMeasure():
|
| 86 |
+
|
| 87 |
+
vals=[]
|
| 88 |
+
mylist=[1,2,3,4]
|
| 89 |
+
if request.method == "POST":
|
| 90 |
+
for key,value in request.form.items(): #akhdna kol m3lomatna mn el html
|
| 91 |
+
#nnady 3la el functions el 3yzenha bl info el 3yzen nb3tha
|
| 92 |
+
vals.append([key,value])
|
| 93 |
+
|
| 94 |
+
if key== 'tomeasure':
|
| 95 |
+
img = plan2img('dropbox_plans/'+str(value) )
|
| 96 |
+
#image_new1=measureImg(img)
|
| 97 |
+
|
| 98 |
+
#print('weeeeeeeeeeeeeee',g.image_new1)
|
| 99 |
+
|
| 100 |
+
return
|
| 101 |
+
return render_template("proposed-GUI.html",mylist=mylist)
|
| 102 |
+
|
| 103 |
+
@app.route("/getoutputimg")
|
| 104 |
+
def sendimgJs():
|
| 105 |
+
measuredimg=measureImg(g.img)
|
| 106 |
+
g.measuredimg=measuredimg
|
| 107 |
+
print(type( g.measuredimg))
|
| 108 |
+
print('measuredddddd')
|
| 109 |
+
return str(measuredimg)
|
| 110 |
+
#info from javascript to python
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def hexRGB(color):
|
| 114 |
+
color=color.lstrip('#')
|
| 115 |
+
|
| 116 |
+
color= tuple(int(color[i:i+2], 16) for i in (0, 2, 4)) #hex to rgb
|
| 117 |
+
color=list(color) #rgb to bgr
|
| 118 |
+
return color
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
@app.route('/projecttomeasure/<jsdata>',methods=["GET","POST"])
|
| 122 |
+
def measureproject(jsdata):
|
| 123 |
+
colorarr=[]
|
| 124 |
+
result = json.loads(jsdata)
|
| 125 |
+
print('RESULT HEREEE', result)
|
| 126 |
+
|
| 127 |
+
img = plan2img('dropbox_plans/'+str(result[0]) )
|
| 128 |
+
# g.img=img
|
| 129 |
+
for item in result[2]:
|
| 130 |
+
item1 ='#'+item
|
| 131 |
+
c=hexRGB(item1)
|
| 132 |
+
colorarr.append(c)
|
| 133 |
+
print(colorarr)
|
| 134 |
+
# return colorarr
|
| 135 |
+
# print(result[0],result[1],colorarr)
|
| 136 |
+
# # return colorarr
|
| 137 |
+
imgPerimeter1,image_new1,SimilarAreaDictionary , colorsUsed,spreadsheet_url=pilecaps_adr.drawAllContours(img,220,[[253,163,47]],1.11,25500,'area')
|
| 138 |
+
print('measuredddddd')
|
| 139 |
+
|
| 140 |
+
img2 = cv2.cvtColor(image_new1, cv2.COLOR_RGB2BGR)
|
| 141 |
+
_, buffer = cv2.imencode('.png', img2)
|
| 142 |
+
|
| 143 |
+
# return SimilarAreaDictionary.to_dict()
|
| 144 |
+
#return
|
| 145 |
+
arr=[base64.b64encode(buffer).decode('utf-8'), SimilarAreaDictionary.to_dict() ,spreadsheet_url]
|
| 146 |
+
return arr#'{}, {} '.format(base64.b64encode(buffer).decode('utf-8') , SimilarAreaDictionary )
|
| 147 |
+
# return Flask.jsonify(data=base64.b64encode(buffer).decode('utf-8'), data1=SimilarAreaDictionary)
|
| 148 |
+
# return str(measuredimg)
|
| 149 |
+
# return render_template("proposed-GUI.html")
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
#retrieve all info of elements after clicking 'done' button
|
| 153 |
+
@app.route("/forward/",methods=["GET", "POST"])
|
| 154 |
+
def move_forward():
|
| 155 |
+
#Moving forward code
|
| 156 |
+
vals=[]
|
| 157 |
+
forward_message = "Moving Forward..."
|
| 158 |
+
if request.method == "POST":
|
| 159 |
+
for key,value in request.form.items():
|
| 160 |
+
#last_name = request.form.get("select2")
|
| 161 |
+
vals.append([key,value])
|
| 162 |
+
return vals
|
| 163 |
+
return render_template("home.html")
|
| 164 |
+
|
| 165 |
+
#####################
|
| 166 |
+
#pdf to img
|
| 167 |
+
def plan2img(path):
|
| 168 |
+
|
| 169 |
+
doc = fitz.open(path)
|
| 170 |
+
for page in doc:
|
| 171 |
+
|
| 172 |
+
pix = page.get_pixmap(dpi=200) # render page to an image
|
| 173 |
+
pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
|
| 174 |
+
img=np.array(pl)
|
| 175 |
+
print(img)
|
| 176 |
+
#img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 177 |
+
return img
|
| 178 |
+
|
| 179 |
+
@app.route("/mctnametoGoogleSheet/<jsdata>",methods=["GET", "POST"])
|
| 180 |
+
def sendmctnametoLegend(jsdata):
|
| 181 |
+
result = json.loads(jsdata)
|
| 182 |
+
print(result)
|
| 183 |
+
pilecaps_adr.mapnametoLegend(result)
|
| 184 |
+
return jsdata
|
| 185 |
+
|
| 186 |
+
if __name__ == "__main__":
|
| 187 |
+
app.run(debug=True)
|
| 188 |
+
|
| 189 |
+
|