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 sys | |
| import pilecaps_adr | |
| import base64 | |
| from db import dropbox_connect | |
| import db | |
| import cv2 | |
| import pandas as pd | |
| import ast | |
| 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 urllib.request | |
| import base64 | |
| import io | |
| from urllib.parse import unquote | |
| import API | |
| app = Flask(__name__) | |
| # out_file = open("static/data/myfile.json", "w") | |
| def getprjnamesfromTestAPI(): | |
| prjnames=API.getPrjNames() | |
| return jsonify(prjnames) | |
| def getprjpartsfromTestAPI(jsdata): | |
| prjparts=API.getprjParts(jsdata) | |
| return jsonify(prjparts) | |
| def getprjSectionsfromTestAPI(jsdata): | |
| sections=API.getprjSections(jsdata) | |
| return jsonify(sections) | |
| #retrieves project parts | |
| def get_javascript_data(jsdata): | |
| #get project from the first dropdown | |
| print(jsdata) | |
| matches=update_dropdown(jsdata) #list f of projects | |
| print(matches) | |
| return jsonify (list(matches)) | |
| #--------------------------------------------------------------- | |
| #get projects of 'project name' | |
| def update_dropdown(section): | |
| if section.startswith('1.0'): | |
| plans_inrepo=os.listdir('dropbox_plans/1.0') | |
| elif section.startswith('3.2'): | |
| plans_inrepo=os.listdir('dropbox_plans/3.2') | |
| elif section.startswith('2.2'): | |
| plans_inrepo=os.listdir('dropbox_plans/2.2') | |
| elif section.startswith('2.1'): | |
| plans_inrepo=os.listdir('dropbox_plans/2.1') | |
| else: | |
| return '' | |
| #if 'foundation' in radio and project in plans_inrepo | |
| matches=[] | |
| if section==None: | |
| print('no proj') | |
| else: | |
| for x in plans_inrepo: | |
| matches.append(x) | |
| print(matches) | |
| return matches | |
| #_________________________________________________________________________________________________________________________ | |
| #_________________________________________________________________________________________________________________________ | |
| #NOT IMP NOW | |
| #retrieve all info of elements after clicking 'measure' button | |
| def getInfotoMeasure(): | |
| # db.create_folder() | |
| mylist=[1,2,3,4] | |
| return render_template("proposed-GUI.html",mylist=mylist) | |
| #_________________________________________________________________________________________________________________________ | |
| #_________________________________________________________________________________________________________________________ | |
| #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') | |
| 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: | |
| opencv_img = plan2img('dropbox_plans/1.0/'+str(vv[0]) ) | |
| arr=measureproject(vv,opencv_img) | |
| # # print(opencv_img) | |
| # cv2.imwrite('img.png', opencv_img) | |
| # return 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,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) | |
| 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) | |
| _, 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('dropbox_plans/1.0/'+jsdata) | |
| # print('imgdone','dropbox_plans/1.0/'+jsdata ) | |
| _, 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(path): | |
| doc = fitz.open(path) | |
| page=doc[0] | |
| pix = page.get_pixmap(dpi=200) # render page to an image | |
| pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples) | |
| img=np.array(pl) | |
| print(img.shape) | |
| img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) | |
| return img | |
| #_________________________________________________________________________________________________________________________ | |
| #_________________________________________________________________________________________________________________________ | |
| #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,guessednamesfinal=pilecaps_adr.mapnametoLegend(result) | |
| allreturns=[summaryid,guessednamesfinal] | |
| 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): | |
| db.create_folder() | |
| print(jsdata) | |
| jsdata=eval(jsdata) | |
| if jsdata[2].startswith('1.0'): | |
| doc = fitz.open('dropbox_plans/1.0/'+str(jsdata[3]) ) | |
| elif jsdata[2].startswith('3.2'): | |
| doc = fitz.open('dropbox_plans/3.2/'+str(jsdata[3]) ) | |
| elif jsdata[2].startswith('2.2'): | |
| doc = fitz.open('dropbox_plans/2.2/'+str(jsdata[3]) ) | |
| elif jsdata[2].startswith('2.1'): | |
| doc = fitz.open('dropbox_plans/2.1/'+str(jsdata[3]) ) | |
| else: | |
| return '' | |
| for page in doc: | |
| pix = page.get_pixmap() # render page to an image | |
| pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples) | |
| img=np.array(pl) | |
| print(img.shape) | |
| img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) | |
| if jsdata[2].startswith('1.0'): | |
| doc=pixelconversion.openDrawPDF(path='dropbox_plans/1.0/'+str(jsdata[3])) | |
| elif jsdata[2].startswith('3.2'): | |
| doc=pixelconversion.openDrawPDF(path='dropbox_plans/3.2/'+str(jsdata[3])) | |
| elif jsdata[2].startswith('2.2'): | |
| doc=pixelconversion.openDrawPDF(path='dropbox_plans/2.2/'+str(jsdata[3])) | |
| elif jsdata[2].startswith('2.1'): | |
| doc=pixelconversion.openDrawPDF(path='dropbox_plans/2.1/'+str(jsdata[3])) | |
| pdfpath='/'+jsdata[0]+'/'+jsdata[1]+'/'+jsdata[2]+'/'+'Scale Document' +'/' | |
| dburl=db.dropbox_upload_file(doc=doc,pdfname=str(jsdata[3]) ,pdfpath=pdfpath,flag=1) | |
| areaPixel,perimeterPixel= pixelconversion.getAreaPerimeter(pdfpath, str(jsdata[3])) | |
| print(areaPixel,perimeterPixel) | |
| outputs=[areaPixel,perimeterPixel , dburl] | |
| return jsonify(outputs) | |
| #_________________________________________________________________________________________________________________________ | |
| #_________________________________________________________________________________________________________________________ | |
| #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+='/' | |
| deletedrows1=pilecaps_adr.deletemarkups(list1=list1,pdfpath=pdfpth,path=path) | |
| print(deletedrows1) | |
| 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): | |
| pdfpth='' | |
| for word in eval(jsdata): | |
| pdfpth+='/' +word | |
| print(pdfpth) | |
| pdfurl=db.dropbox_shareableLink(pdfpth) | |
| 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(len(titles)): | |
| if not titles[i].startswith('API'): | |
| 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) | |
| #_________________________________________________________________________________________________________________________ | |
| #_________________________________________________________________________________________________________________________ | |
| #_________________________________________________________________________________________________________________________ | |
| #_________________________________________________________________________________________________________________________ | |
| #_________________________________________________________________________________________________________________________ | |
| #_________________________________________________________________________________________________________________________ | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=7860) | |