Marthee commited on
Commit
a5d81a0
·
verified ·
1 Parent(s): c1745f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -44
app.py CHANGED
@@ -813,8 +813,12 @@ def stringToRGB():
813
  page.set_rotation(0)
814
  pix = page.get_pixmap(dpi=300) # render page to an image
815
  pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
816
-
817
- arr=measureproject(result=vv,img=pl,pdf_content=pdf_content)
 
 
 
 
818
 
819
  else:
820
  if ( vv[5][2].startswith('3.2') or vv[5][2].startswith('3.3') or vv[5][2].startswith('2.7')) :
@@ -884,7 +888,16 @@ def stringToRGB():
884
  pix = page.get_pixmap(dpi=300) # render page to an image
885
  pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
886
  pls.append(pl)
887
- arr=measureproject(vv,dataDocs,pls)
 
 
 
 
 
 
 
 
 
888
  #pdfpath,pdflink=tsadropboxretrieval.getPathtoPDF_File(nameofPDF= str(vv[0]))
889
  #dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user')
890
  #md, res =dbxTeam.files_download(path=pdfpath)
@@ -934,7 +947,7 @@ def measure2_1():
934
  return jsonify({"error": error_msg}), 500
935
  #MAIN FUNCTION -- calls python code to measure the chosen plan from the interface
936
  # @app.route('/projecttomeasure/<jsdata>',methods=["GET","POST"])
937
- def measureproject(result,dataDoc=0,img=0,dxffile=0,pdf_content=0):
938
  try:
939
  colorarr=[]
940
  global pdflink
@@ -1044,9 +1057,9 @@ def measureproject(result,dataDoc=0,img=0,dxffile=0,pdf_content=0):
1044
  print("SearchArray = ",SearchArray)
1045
  global hatched_areas2_7
1046
  if pdf_content:
1047
- doc,outputimg, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas,bax_pretty_xml,column_xml=Code_2_7.mainFunctionDrawImgPdf(dataDoc,temp_filename,result[4],SearchArray,CorrectionRatio,points_Of_drawing_Canvas,Thickness, pdfpath,result[0],pdf_content)
1048
  else:
1049
- doc,outputimg, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas,bax_pretty_xml,column_xml=Code_2_7.mainFunctionDrawImgPdf(dataDoc,temp_filename,result[4],SearchArray,CorrectionRatio,points_Of_drawing_Canvas,Thickness, pdfpath,result[0])
1050
  # global colorsused
1051
  hatched_areas2_7=hatched_areas
1052
  colorsused=list(SimilarAreaDictionary['Color'])
@@ -1073,7 +1086,7 @@ def measureproject(result,dataDoc=0,img=0,dxffile=0,pdf_content=0):
1073
  listofmarkups=[]
1074
  SearchArray=result[7]
1075
  print('searchhh:',SearchArray)
1076
-
1077
  # annotatedimg,pdf_document,spreadsheet_url, list1 , df_doors=doors_fasterrcnn.main_run(img,dataDoc,'separated_classes_all.pth',result[0],pdfpath, result[4]) #single_double.pth
1078
  if pdf_content:
1079
  link = urllib.parse.unquote(result[6].strip('"'))
@@ -1182,23 +1195,15 @@ def measureproject(result,dataDoc=0,img=0,dxffile=0,pdf_content=0):
1182
  #_________________________________________________________________________________________________________________________
1183
  #_________________________________________________________________________________________________________________________
1184
  ######################################################### HELPER FUNCTIONS: MARTHE/OMAR #################################################################################################################################
1185
-
1186
-
1187
- def drawonpdf(nameofpdf, coords):
1188
  try:
1189
- print()
1190
- pdfpath, pdflink = tsadropboxretrieval.getPathtoPDF_File(nameofPDF=nameofpdf)
1191
- dbxTeam = tsadropboxretrieval.ADR_Access_DropboxTeam('user')
1192
- md, res = dbxTeam.files_download(path=pdfpath)
1193
  data = res.content
1194
- doc = fitz.open("pdf", data)
1195
- page = doc[0]
1196
-
1197
- # 1. Initialize a list to hold all your shape dictionaries
1198
- canvas_points_list = []
1199
-
1200
- # Use enumerate to get a 1-based shape index
1201
- for index, shape in enumerate(coords, start=1):
1202
  if not isinstance(shape, dict):
1203
  continue
1204
 
@@ -1211,38 +1216,19 @@ def drawonpdf(nameofpdf, coords):
1211
 
1212
  # Convert to fitz.Point objects and apply derotation
1213
  points = [fitz.Point(x, y) * page.derotation_matrix for x, y in vertices]
1214
-
1215
- # --- Convert to your desired format ---
1216
- # 1. Create the list of {'x': ..., 'y': ...} dictionaries
1217
- coordinates_list = [{"x": p.x, "y": p.y} for p in points]
1218
-
1219
- # 2. Create the final dictionary for this shape
1220
- shape_data = {
1221
- 'shapeIndex': index,
1222
- 'coordinates': coordinates_list
1223
- }
1224
-
1225
- # 3. Append this shape's dictionary to the main list
1226
- canvas_points_list.append(shape_data)
1227
- # --- End of conversion ---
1228
 
1229
  # --- 🟢 Use polygon annotation ---
1230
  if len(points) > 2:
1231
  annot = page.add_polygon_annot(points)
1232
  else:
1233
  annot = page.add_polyline_annot(points)
1234
-
1235
  # Style annotation
1236
  annot.set_colors(stroke=(1, 0, 0)) # red border
1237
  annot.set_border(width=1)
1238
  annot.update()
1239
-
1240
  # doc.save("output.pdf")
1241
-
1242
- # # 4. Return the list of dictionaries
1243
- # print('pointsomar,',canvas_points_list)
1244
- return canvas_points_list
1245
-
1246
  except (ConnectionError, TimeoutError) as e:
1247
  # Use app context when logging
1248
  from flask import current_app
@@ -1256,8 +1242,8 @@ def drawonpdf(nameofpdf, coords):
1256
  error_details = traceback.format_exc()
1257
  error_msg = log_error(error_details, issue_type="backend")
1258
  return jsonify({"error": error_msg}), 500
 
1259
 
1260
-
1261
  ######################################################### GUI: CANVAS #################################################################################################################################
1262
  @app.route("/canvaspdftoimgBackground/<jsdata>",methods=["GET", "POST"])
1263
  def pdftoimgCanvas(jsdata):
 
813
  page.set_rotation(0)
814
  pix = page.get_pixmap(dpi=300) # render page to an image
815
  pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
816
+ if 'file' not in request.files:
817
+ print('error, No file part in the request')
818
+ else:
819
+ csvfile = request.files['csvfile']
820
+ print('csvfile done, measuring')
821
+ arr=measureproject(result=vv,img=pl,pdf_content=pdf_content,csvfile=csvfile)
822
 
823
  else:
824
  if ( vv[5][2].startswith('3.2') or vv[5][2].startswith('3.3') or vv[5][2].startswith('2.7')) :
 
888
  pix = page.get_pixmap(dpi=300) # render page to an image
889
  pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
890
  pls.append(pl)
891
+ arr=measureproject(vv,dataDocs,pls)
892
+
893
+ # ---- CSV FILE ----#
894
+ #if 'file' not in request.files:
895
+ # print('error, No file part in the request')
896
+ # arr=measureproject(vv,dataDocs,pls)
897
+ #else:
898
+ # csvfile = request.files['csvfile']
899
+ # print('csvfile done, measuring')
900
+ #arr=measureproject(vv,dataDocs,pls,csvfile=csvfile)
901
  #pdfpath,pdflink=tsadropboxretrieval.getPathtoPDF_File(nameofPDF= str(vv[0]))
902
  #dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user')
903
  #md, res =dbxTeam.files_download(path=pdfpath)
 
947
  return jsonify({"error": error_msg}), 500
948
  #MAIN FUNCTION -- calls python code to measure the chosen plan from the interface
949
  # @app.route('/projecttomeasure/<jsdata>',methods=["GET","POST"])
950
+ def measureproject(result,dataDoc=0,img=0,dxffile=0,pdf_content=0,csvfile=0):
951
  try:
952
  colorarr=[]
953
  global pdflink
 
1057
  print("SearchArray = ",SearchArray)
1058
  global hatched_areas2_7
1059
  if pdf_content:
1060
+ doc,outputimg, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas,bax_pretty_xml,column_xml=Code_2_7.mainFunctionDrawImgPdf(dataDoc,temp_filename,result[4],SearchArray,CorrectionRatio,result[8],Thickness, pdfpath,result[0],pdf_content)
1061
  else:
1062
+ doc,outputimg, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas,bax_pretty_xml,column_xml=Code_2_7.mainFunctionDrawImgPdf(dataDoc,temp_filename,result[4],SearchArray,CorrectionRatio,result[8],Thickness, pdfpath,result[0])
1063
  # global colorsused
1064
  hatched_areas2_7=hatched_areas
1065
  colorsused=list(SimilarAreaDictionary['Color'])
 
1086
  listofmarkups=[]
1087
  SearchArray=result[7]
1088
  print('searchhh:',SearchArray)
1089
+ print('csv',csvfile)
1090
  # annotatedimg,pdf_document,spreadsheet_url, list1 , df_doors=doors_fasterrcnn.main_run(img,dataDoc,'separated_classes_all.pth',result[0],pdfpath, result[4]) #single_double.pth
1091
  if pdf_content:
1092
  link = urllib.parse.unquote(result[6].strip('"'))
 
1195
  #_________________________________________________________________________________________________________________________
1196
  #_________________________________________________________________________________________________________________________
1197
  ######################################################### HELPER FUNCTIONS: MARTHE/OMAR #################################################################################################################################
1198
+ def drawonpdf(nameofpdf,coords):
 
 
1199
  try:
1200
+ pdfpath,pdflink=tsadropboxretrieval.getPathtoPDF_File(nameofPDF=nameofpdf)
1201
+ dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user')
1202
+ md, res =dbxTeam.files_download(path=pdfpath)
 
1203
  data = res.content
1204
+ doc = fitz.open("pdf",data)
1205
+ page=doc[0]
1206
+ for shape in coords:
 
 
 
 
 
1207
  if not isinstance(shape, dict):
1208
  continue
1209
 
 
1216
 
1217
  # Convert to fitz.Point objects and apply derotation
1218
  points = [fitz.Point(x, y) * page.derotation_matrix for x, y in vertices]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1219
 
1220
  # --- 🟢 Use polygon annotation ---
1221
  if len(points) > 2:
1222
  annot = page.add_polygon_annot(points)
1223
  else:
1224
  annot = page.add_polyline_annot(points)
1225
+
1226
  # Style annotation
1227
  annot.set_colors(stroke=(1, 0, 0)) # red border
1228
  annot.set_border(width=1)
1229
  annot.update()
 
1230
  # doc.save("output.pdf")
1231
+ return annot
 
 
 
 
1232
  except (ConnectionError, TimeoutError) as e:
1233
  # Use app context when logging
1234
  from flask import current_app
 
1242
  error_details = traceback.format_exc()
1243
  error_msg = log_error(error_details, issue_type="backend")
1244
  return jsonify({"error": error_msg}), 500
1245
+
1246
 
 
1247
  ######################################################### GUI: CANVAS #################################################################################################################################
1248
  @app.route("/canvaspdftoimgBackground/<jsdata>",methods=["GET", "POST"])
1249
  def pdftoimgCanvas(jsdata):