Marthee commited on
Commit
4d37bdb
·
1 Parent(s): cafff62

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -15
app.py CHANGED
@@ -21,9 +21,15 @@ from werkzeug.wsgi import wrap_file
21
  import tameem3_2
22
  import pypdfium2 as pdfium
23
  import pixelconversion
 
 
 
 
 
24
 
25
  app = Flask(__name__)
26
 
 
27
 
28
  #retrieves project parts
29
  @app.route('/getmethod/<jsdata>',methods=['GET','POST'])
@@ -73,12 +79,28 @@ def hexRGB(color):
73
  return color
74
  #_________________________________________________________________________________________________________________________
75
  #_________________________________________________________________________________________________________________________
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
  #MAIN FUNCTION -- calls python code to measure the chosen plan from the interface
78
- @app.route('/projecttomeasure/<jsdata>',methods=["GET","POST"])
79
- def measureproject(jsdata):
80
  colorarr=[]
81
- result = json.loads(jsdata)
82
  pdfpath='/'
83
  ################################# -1.0- #################################
84
  for word in result[5]:
@@ -86,13 +108,18 @@ def measureproject(jsdata):
86
  arr=[]
87
 
88
  if result[5][2].startswith('1.0'): #section value - 1.0 substructure- pile caps
89
- img = plan2img('dropbox_plans/1.0/'+str(result[0]) )
 
 
 
 
 
90
 
91
  for item in result[2]:
92
- item1 ='#'+item
93
- c=hexRGB(item1)
94
  colorarr.append(c)
95
- imgPerimeter1,image_new1,SimilarAreaDictionary , colorsUsed,spreadsheet_url, spreadsheetId,list1, pdflink, areas_Perimeters, namepathArr =pilecaps_adr.drawAllContours(img,int(result[1]),colorarr, result[3], result[4],'area', result[0],pdfpath)
96
  _, buffer = cv2.imencode('.png', image_new1)
97
  arr=[base64.b64encode(buffer).decode('utf-8'),SimilarAreaDictionary.to_dict(),spreadsheet_url , spreadsheetId,colorsUsed,list1.to_dict(), pdflink, areas_Perimeters, namepathArr]
98
  ################################# -3.2- #################################
@@ -115,21 +142,30 @@ def measureproject(jsdata):
115
 
116
  #let first result to be the img
117
  #return results in arr=[base64.b64encode(buffer).decode('utf-8'),pdflink,spreadsheetId,spreadsheet_url] like the previous sections in the above lines
118
- return jsonify(arr)
119
  #_________________________________________________________________________________________________________________________
120
  #_________________________________________________________________________________________________________________________
121
 
 
 
 
 
 
 
 
 
 
122
  #pdf to img
123
  def plan2img(path):
124
 
125
  doc = fitz.open(path)
126
- for page in doc:
127
- pix = page.get_pixmap(dpi=200) # render page to an image
128
- pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
129
- img=np.array(pl)
130
- print(img.shape)
131
- img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
132
- return img
133
  #_________________________________________________________________________________________________________________________
134
  #_________________________________________________________________________________________________________________________
135
 
 
21
  import tameem3_2
22
  import pypdfium2 as pdfium
23
  import pixelconversion
24
+ import urllib.request
25
+ import base64
26
+ import io
27
+ from urllib.parse import unquote
28
+
29
 
30
  app = Flask(__name__)
31
 
32
+ # out_file = open("static/data/myfile.json", "w")
33
 
34
  #retrieves project parts
35
  @app.route('/getmethod/<jsdata>',methods=['GET','POST'])
 
79
  return color
80
  #_________________________________________________________________________________________________________________________
81
  #_________________________________________________________________________________________________________________________
82
+ @app.route('/savebase64Img/',methods=["POST"])
83
+ def stringToRGB():
84
+ name = request.get_json()
85
+ vv=name.get('allvalues')
86
+ if vv[1]==220:
87
+ imgdata = base64.b64decode(vv[6])
88
+ img=Image.open(io.BytesIO(imgdata))
89
+ opencv_img= cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
90
+ else:
91
+ opencv_img = plan2img('dropbox_plans/1.0/'+str(vv[0]) )
92
+ arr=measureproject(vv,opencv_img)
93
+ # # print(opencv_img)
94
+ # cv2.imwrite('img.png', opencv_img)
95
+ # return opencv_img
96
+ return jsonify(arr)
97
+
98
 
99
  #MAIN FUNCTION -- calls python code to measure the chosen plan from the interface
100
+ # @app.route('/projecttomeasure/<jsdata>',methods=["GET","POST"])
101
+ def measureproject(result,img):
102
  colorarr=[]
103
+ # result = json.loads(jsdata)
104
  pdfpath='/'
105
  ################################# -1.0- #################################
106
  for word in result[5]:
 
108
  arr=[]
109
 
110
  if result[5][2].startswith('1.0'): #section value - 1.0 substructure- pile caps
111
+ # img0 = plan2img('dropbox_plans/1.0/'+str(result[0]) )
112
+
113
+ # if (result[1]!=0):
114
+ # img=stringToRGB(result[6], img0.shape)
115
+ # print('p',result[6])
116
+
117
 
118
  for item in result[2]:
119
+ # item1 ='#'+item
120
+ c=hexRGB(item)
121
  colorarr.append(c)
122
+ imgPerimeter1,image_new1,SimilarAreaDictionary , colorsUsed,spreadsheet_url, spreadsheetId,list1, pdflink, areas_Perimeters, namepathArr =pilecaps_adr.drawAllContours(img,result[1],colorarr, result[3], result[4],'area', result[0],pdfpath)
123
  _, buffer = cv2.imencode('.png', image_new1)
124
  arr=[base64.b64encode(buffer).decode('utf-8'),SimilarAreaDictionary.to_dict(),spreadsheet_url , spreadsheetId,colorsUsed,list1.to_dict(), pdflink, areas_Perimeters, namepathArr]
125
  ################################# -3.2- #################################
 
142
 
143
  #let first result to be the img
144
  #return results in arr=[base64.b64encode(buffer).decode('utf-8'),pdflink,spreadsheetId,spreadsheet_url] like the previous sections in the above lines
145
+ return arr
146
  #_________________________________________________________________________________________________________________________
147
  #_________________________________________________________________________________________________________________________
148
 
149
+ @app.route("/canvaspdftoimgBackground/<jsdata>",methods=["GET", "POST"])
150
+ def pdftoimgCanvas(jsdata):
151
+ img=plan2img('dropbox_plans/1.0/'+jsdata)
152
+ # print('imgdone','dropbox_plans/1.0/'+jsdata )
153
+ _, buffer = cv2.imencode('.png', img)
154
+ arr=[base64.b64encode(buffer).decode('utf-8') , img.shape[0],img.shape[1]]
155
+ return jsonify(arr)
156
+
157
+
158
  #pdf to img
159
  def plan2img(path):
160
 
161
  doc = fitz.open(path)
162
+ page=doc[0]
163
+ pix = page.get_pixmap(dpi=200) # render page to an image
164
+ pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
165
+ img=np.array(pl)
166
+ print(img.shape)
167
+ img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
168
+ return img
169
  #_________________________________________________________________________________________________________________________
170
  #_________________________________________________________________________________________________________________________
171