Spaces:
Sleeping
Sleeping
| from __future__ import print_function | |
| from flask import Flask, render_template,request,flash , json, url_for,g , redirect , jsonify | |
| import os | |
| import json | |
| import fitz | |
| from PIL import Image | |
| import cv2 | |
| import numpy as np | |
| import pilecaps_adr | |
| import base64 | |
| from db import dropbox_connect | |
| import db | |
| import cv2 | |
| import pandas as pd | |
| import time | |
| from io import BytesIO | |
| from flask import Flask, Response | |
| from werkzeug.wsgi import wrap_file | |
| import tameem3_2 | |
| import pypdfium2 as pdfium | |
| import pixelconversion | |
| import tameem2_1 | |
| import base64 | |
| import io | |
| from urllib.parse import unquote | |
| import API | |
| import MC_Templates_API | |
| import tsadropboxretrieval | |
| import doc_search | |
| app = Flask(__name__) | |
| def getInfotoMeasure(): | |
| return render_template("proposed-GUI.html") | |
| def getprjnamesfromTestAPI(): | |
| prjnames=API.getPrjNames() | |
| return jsonify(prjnames) | |
| def getprjpartsfromTestAPI(jsdata): | |
| prjparts=API.getprjParts(jsdata) | |
| return prjparts | |
| def getprjSectionsfromTestAPI(jsdata): | |
| data=json.loads(jsdata) | |
| sections=API.getprjSections(data[0],data[1]) | |
| return jsonify(sections) | |
| #retrieves projects | |
| def get_javascript_data(jsdata): | |
| #get project from the first dropdown | |
| # jsdata=eval(jsdata) | |
| documnetsToMeasure,RelevantDocuments=tsadropboxretrieval.retrieveProjects(jsdata) | |
| if documnetsToMeasure: | |
| return jsonify ([documnetsToMeasure, RelevantDocuments]) | |
| else: | |
| return ['No projects found'] | |
| #--------------------------------------------------------------- | |
| def searchDocument(): | |
| return render_template('wordSearch.html') | |
| def getSearchinDocs(): | |
| print('cvbnm,') | |
| arr=[] | |
| values = request.get_json() | |
| keyword=values.get('keyword') | |
| listpfProjs=values.get('listofprojs') | |
| print(keyword,listpfProjs) | |
| df,img_list=doc_search.search_docs(keyword,listpfProjs) | |
| for img in img_list: | |
| _, buffer = cv2.imencode('.png', img) | |
| arr.append(base64.b64encode(buffer).decode('utf-8')) | |
| return jsonify([df.to_html(index=False, escape=False),arr]) | |
| def getSearchinFolder(): | |
| arr=[] | |
| values = request.get_json() | |
| keyword=values.get('keyword') | |
| projname=values.get('ProjectName') | |
| df,img_list=doc_search.slow_search(keyword=keyword,project=projname) | |
| for img in img_list: | |
| _, buffer = cv2.imencode('.png', img) | |
| arr.append(base64.b64encode(buffer).decode('utf-8')) | |
| return jsonify([df.to_html(index=False, escape=False),arr]) | |
| def measurementConsoleFn(): | |
| return render_template("proposed-GUI.html") | |
| def CallAPIforMCTNames(jsdata): | |
| # print(jsdata) | |
| DictionaryOfTemplates=MC_Templates_API.RetrieveMC_Templates_API() | |
| jsdata=jsdata.replace('"', '') | |
| return jsonify(DictionaryOfTemplates[str(jsdata).lower()]) | |
| #_________________________________________________________________________________________________________________________ | |
| #_________________________________________________________________________________________________________________________ | |
| #Hex value to RGB value | |
| def hexRGB(color): | |
| color=color.lstrip('#') | |
| color= tuple(int(color[i:i+2], 16) for i in (0, 2, 4)) #hex to rgb | |
| color=list(color) #rgb to bgr | |
| return color | |
| #_________________________________________________________________________________________________________________________ | |
| #_________________________________________________________________________________________________________________________ | |
| def stringToRGB(): | |
| name = request.get_json() | |
| vv=name.get('allvalues') | |
| dataDoc = plan2img( str(vv[0]) )[1] | |
| # vv[0] ->proj name, vv[1] (0/220),vv[2] ->colors , vv[3]->ratio1 , vv[4]->ratio2 , vv[5] -> pdfpath , vv[6] ->imgDrawnOn | |
| if vv[1]==220: | |
| imgdata = base64.b64decode(vv[6]) | |
| img=Image.open(io.BytesIO(imgdata)) | |
| opencv_img= cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) | |
| else: | |
| # vv[0] ->proj name, vv[1] (0/220),vv[2] ->colors , vv[3]->ratio1 , vv[4]->ratio2 , vv[5] -> pdfpath | |
| opencv_img = plan2img( str(vv[0]) )[0] | |
| arr=measureproject(vv,dataDoc,opencv_img) | |
| return jsonify(arr) | |
| def measure2_1(): | |
| name = request.get_json() | |
| result=name.get('allvalues') | |
| arr=measureproject(result) | |
| return arr | |
| #MAIN FUNCTION -- calls python code to measure the chosen plan from the interface | |
| # @app.route('/projecttomeasure/<jsdata>',methods=["GET","POST"]) | |
| def measureproject(result,dataDoc,img=0): | |
| colorarr=[] | |
| # result = json.loads(jsdata) | |
| pdfpath='/' | |
| ################################# -1.0- ################################# | |
| for word in result[5]: | |
| pdfpath+=word +'/' | |
| arr=[] | |
| if result[5][2].startswith('1.0'): #section value - 1.0 substructure- pile caps | |
| for item in result[2]: | |
| # item1 ='#'+item | |
| c=hexRGB(item) | |
| colorarr.append(c) | |
| print('RATIOS=',result[3], result[4]) | |
| imgPerimeter1,image_new1,SimilarAreaDictionary , colorsUsed,spreadsheet_url, spreadsheetId,list1, pdflink, areas_Perimeters, namepathArr =pilecaps_adr.drawAllContours(dataDoc,img,result[1],colorarr, result[3], result[4],'area', result[0],pdfpath) | |
| _, buffer = cv2.imencode('.png', image_new1) | |
| arr=[base64.b64encode(buffer).decode('utf-8'),SimilarAreaDictionary.to_dict(),spreadsheet_url , spreadsheetId,colorsUsed,list1.to_dict(), pdflink, areas_Perimeters, namepathArr] | |
| ################################# -3.2- ################################# | |
| elif result[5][2].startswith('3.2'): #section value - 3.2 floor finishes | |
| outputimg, df_merged , dropboxpdflink , spreadsheetId, spreadsheet_url , list1 = tameem3_2.mainFunction(str(result[0]),result[3], result[4] , pdfpath) #pass arearatio,perimratio | |
| _, buffer = cv2.imencode('.png', outputimg) | |
| arr=[ base64.b64encode(buffer).decode('utf-8') , df_merged.to_dict() , dropboxpdflink , spreadsheetId, spreadsheet_url , list1.to_dict()] | |
| ################################# -2.2- ################################# | |
| # elif result[5][2].startswith('2.2'): #section value - 2.2 rc slabs | |
| #add here python code | |
| # | |
| #link (add this to ur code) | |
| # pdflink= db.dropbox_upload_file(doc=doc,pdfname=path,pdfpath=pdfpath) | |
| # gc,spreadsheet_service,spreadsheetId, spreadsheet_url , namepathArr= pilecaps_adr.legendGoogleSheets(df,plan ,pdfpath) | |
| #output img | |
| # _, buffer = cv2.imencode('.png', outputimg) | |
| #let first result to be the img | |
| #return results in arr=[base64.b64encode(buffer).decode('utf-8'),pdflink,spreadsheetId,spreadsheet_url] like the previous sections in the above lines | |
| elif result[5][2].startswith('2.1'): #section value - 2.1 frames | |
| url = tameem2_1.mainfun( result[0], pdfpath) | |
| return jsonify([url]) | |
| return arr | |
| #_________________________________________________________________________________________________________________________ | |
| #_________________________________________________________________________________________________________________________ | |
| def pdftoimgCanvas(jsdata): | |
| img=plan2img(jsdata)[0] | |
| _, buffer = cv2.imencode('.png', img) | |
| arr=[base64.b64encode(buffer).decode('utf-8') , img.shape[0],img.shape[1]] | |
| return jsonify(arr) | |
| #pdf to img | |
| def plan2img(nameofpdf): | |
| pdfpath,pdflink=tsadropboxretrieval.getPathtoPDF_File(nameofPDF=nameofpdf) | |
| dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user') | |
| md, res =dbxTeam.files_download(path=pdfpath) | |
| data = res.content | |
| doc = fitz.open("pdf",data) | |
| page=doc[0] | |
| pix = page.get_pixmap(dpi=300) # render page to an image | |
| pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples) | |
| img=np.array(pl) | |
| img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) | |
| return img ,data | |
| #_________________________________________________________________________________________________________________________ | |
| #_________________________________________________________________________________________________________________________ | |
| #not used in 1.0 | |
| def convert2img(path): | |
| pdf = pdfium.PdfDocument(path) | |
| page = pdf.get_page(0) | |
| pil_image = page.render().to_pil() | |
| pl1=np.array(pil_image) | |
| img = cv2.cvtColor(pl1, cv2.COLOR_RGB2BGR) | |
| return img | |
| #_________________________________________________________________________________________________________________________ | |
| #_________________________________________________________________________________________________________________________ | |
| #User-made MC-T NAME - in the second part of the interface | |
| def sendmctnametoLegend(jsdata): | |
| result = json.loads(jsdata) | |
| print(result) | |
| summaryid=pilecaps_adr.mapnametoLegend(result) | |
| allreturns=[summaryid] | |
| return jsonify(allreturns) | |
| #_________________________________________________________________________________________________________________________ | |
| #_________________________________________________________________________________________________________________________ | |
| #NOT USED (pixel conversion instead) | |
| def calcRef(img): | |
| blk = np.ones(img.shape, dtype="uint8") * [[[np.uint8(0), np.uint8(0), np.uint8(0)]]] | |
| start_point = (50, 100) | |
| end_point = (120, 200) | |
| color = (255, 255, 255) # white BGR | |
| thickness = -1 # Thickness of -1 will fill the entire shape | |
| blk = cv2.rectangle(blk, start_point, end_point, color, thickness) | |
| blk = cv2.cvtColor(blk, cv2.COLOR_BGR2GRAY) | |
| contourzz, hierarchy = cv2.findContours(image=blk, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_NONE) | |
| for i, cnt3 in enumerate(contourzz): | |
| M = cv2.moments(cnt3) | |
| if M['m00'] != 0.0: | |
| x2 = int(M['m10']/M['m00']) | |
| y2 = int(M['m01']/M['m00']) | |
| area = cv2.contourArea(cnt3) | |
| perimeter = cv2.arcLength(cnt3, True) | |
| return area,perimeter , blk | |
| #NOT USED (pixel conversion instead) | |
| def modifyingcalcRefDynamic(img): | |
| imgcopy = img.copy() | |
| blk = np.ones(img.shape, dtype="uint8") * [[[np.uint8(0), np.uint8(0), np.uint8(0)]]] | |
| x = 50 | |
| y = 100 | |
| xD = int(img.shape[1] * 0.10) | |
| yD = int(img.shape[0] * 0.10) | |
| start_point = (x, y) | |
| end_point = (x+xD, y+yD) | |
| blue = (255, 0, 0) # BGR | |
| white = (255, 255, 255) # BGR | |
| thickness = -1 # Thickness of -1 will fill the entire shape | |
| imgcopy = cv2.rectangle(imgcopy, start_point, end_point, blue, thickness) | |
| blk = cv2.rectangle(blk, start_point, end_point, white, thickness) | |
| blk = cv2.cvtColor(blk, cv2.COLOR_BGR2GRAY) | |
| contourzz, hierarchy = cv2.findContours(image=blk, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_NONE) | |
| for i, cnt3 in enumerate(contourzz): | |
| M = cv2.moments(cnt3) | |
| if M['m00'] != 0.0: | |
| x2 = int(M['m10']/M['m00']) | |
| y2 = int(M['m01']/M['m00']) | |
| area = cv2.contourArea(cnt3) | |
| perimeter = cv2.arcLength(cnt3, True) | |
| return area, perimeter, blk , imgcopy | |
| #_________________________________________________________________________________________________________________________ | |
| #_________________________________________________________________________________________________________________________ | |
| #PIXEL CONVERSION METHOD -- SAVES DOC ON DROPBOX TO BE MEASURED BY USER | |
| def getimg(jsdata): | |
| jsdata=eval(jsdata) | |
| pdfpath,pdflink=tsadropboxretrieval.getPathtoPDF_File(nameofPDF=jsdata[3]) | |
| dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user') | |
| md, res =dbxTeam.files_download(path=pdfpath) | |
| data = res.content | |
| doc=pixelconversion.openDrawPDF(data) | |
| dbPath='/TSA Team Folder/ADR Test/'+jsdata[0]+'/'+jsdata[1]+'/'+jsdata[2]+'/'+'Scale Document' +'/' | |
| dburl=tsadropboxretrieval.uploadanyFile(doc=doc,pdfname=str(jsdata[3]) ,path=dbPath) | |
| areaPixel,perimeterPixel= pixelconversion.getAreaPerimeter(dbPath, str(jsdata[3])) | |
| outputs=[areaPixel,perimeterPixel , dburl] | |
| return jsonify(outputs) | |
| #_________________________________________________________________________________________________________________________ | |
| #_________________________________________________________________________________________________________________________ | |
| #get sunburst from doc_search | |
| def getSunburst(): | |
| # df=tsadropboxretrieval.GetParquetDF() | |
| tree=doc_search.prepare_sunburst() | |
| return jsonify(tree.to_dict()) | |
| #_________________________________________________________________________________________________________________________ | |
| #_________________________________________________________________________________________________________________________ | |
| #DELETE MARKUPS (for comparison) | |
| def getnewlegend(): | |
| pdfpth='' | |
| list1 = request.args.get('dict1') | |
| path = request.args.get('path') | |
| spreadsheetId =request.args.get('spreadsheetId') | |
| pdfpathpath=request.args.get('pdfpathpath') | |
| pdfname=request.args.get('pdfname') | |
| for word in eval(pdfpathpath): | |
| pdfpth+='/' +word | |
| pdfpth+='/' | |
| dbPath='/TSA Team Folder/ADR Test'+pdfpth+'Measured Plan/' | |
| deletedrows1=pilecaps_adr.deletemarkups(list1=list1,dbPath=dbPath,path=path) | |
| arr1=[deletedrows1.to_dict()] | |
| return jsonify(arr1) | |
| #--------------------------------------------------------------------------- | |
| #if user wishes to delete | |
| def dltmarkupslegend(): | |
| pdfpth='' | |
| SimilarAreaDictionary = request.args.get('dict') | |
| deletedrows = request.args.get('deletedrows') | |
| path = request.args.get('path') | |
| spreadsheetId =request.args.get('spreadsheetId') | |
| areaPermArr=request.args.get('areaPermArr') | |
| section=request.args.get('section') | |
| pdfpathpath=request.args.get('pdfpathpath') | |
| for word in eval(pdfpathpath): | |
| pdfpth+='/' +word | |
| pdfpth+='/' | |
| myDict=eval(deletedrows) | |
| deletedrows=pd.DataFrame(myDict) | |
| if section.startswith('1.0'): | |
| newlgnd=pilecaps_adr.deletefromlegend(deletedrows=deletedrows,SimilarAreaDictionarycopy=SimilarAreaDictionary, section=section,areaPermArr=areaPermArr) | |
| else: | |
| newlgnd=pilecaps_adr.deletefromlegend(deletedrows=deletedrows,SimilarAreaDictionarycopy=SimilarAreaDictionary, section=section) | |
| try: | |
| newlgnd1=pilecaps_adr.legendGoogleSheets(SimilarAreaDictionary=newlgnd,path=path,spreadsheetId=spreadsheetId ,pdfpath=pdfpth) #new legend | |
| except: | |
| print("An exception occurred") | |
| time.sleep(10) | |
| newlgnd1=pilecaps_adr.legendGoogleSheets(SimilarAreaDictionary=newlgnd,path=path,spreadsheetId=spreadsheetId,pdfpath=pdfpth) | |
| return jsonify('donee') | |
| #_________________________________________________________________________________________________________________________ | |
| #_________________________________________________________________________________________________________________________ | |
| #get pdf dropbox url after measurement is done | |
| def calldropboxurl(jsdata): | |
| pdfurl=tsadropboxretrieval.getPathtoPDF_File(nameofPDF=jsdata)[1] | |
| return jsonify(pdfurl) | |
| #_________________________________________________________________________________________________________________________ | |
| #_________________________________________________________________________________________________________________________ | |
| #Google sheet links | |
| def getlinkscreated(jsdata): | |
| spreadsheet_service,drive_service,gc= pilecaps_adr.authorizeLegend() | |
| ids=gc.spreadsheet_ids() | |
| titles=gc.spreadsheet_titles() | |
| allpaths=[] | |
| # for title in titles: | |
| for i in range(0,len(titles)): | |
| if not (titles[i].startswith('API') or (titles[i].startswith('Dropbox')) ) : | |
| ws=gc.open(titles[i]) | |
| allpaths.append([titles[i], ws.get_developer_metadata('path')[0].value , drive_service.files().get(fileId=ids[i],fields="createdTime, modifiedTime").execute() ,ids[i] ]) | |
| return jsonify(allpaths) | |
| #_________________________________________________________________________________________________________________________ | |
| #_________________________________________________________________________________________________________________________ | |
| def returnAPITables(): | |
| # API.update_df() | |
| table1,table2,table3=API.GenerateTables() | |
| return jsonify([table1.to_dict(),table2.to_dict(),table3.to_dict()]) | |
| #_________________________________________________________________________________________________________________________ | |
| #_________________________________________________________________________________________________________________________ | |
| #_________________________________________________________________________________________________________________________ | |
| #_________________________________________________________________________________________________________________________ | |
| #_________________________________________________________________________________________________________________________ | |
| #_________________________________________________________________________________________________________________________ | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=7860) | |