Spaces:
Sleeping
Sleeping
Update pilecaps_adr.py
Browse files- pilecaps_adr.py +32 -71
pilecaps_adr.py
CHANGED
|
@@ -651,31 +651,6 @@ def FillDictionary(green2,SimilarAreaDictionary,img,number,ratioarea,ratioperim,
|
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
-
def rotate_point(point, angle, center_point=(0, 0)):
|
| 655 |
-
"""Rotates a point around center_point(origin by default)
|
| 656 |
-
Angle is in degrees.
|
| 657 |
-
Rotation is counter-clockwise
|
| 658 |
-
"""
|
| 659 |
-
angle_rad = radians(angle % 360)
|
| 660 |
-
# Shift the point so that center_point becomes the origin
|
| 661 |
-
new_point = (point[0] - center_point[0], point[1] - center_point[1])
|
| 662 |
-
new_point = (new_point[0] * cos(angle_rad) - new_point[1] * sin(angle_rad),
|
| 663 |
-
new_point[0] * sin(angle_rad) + new_point[1] * cos(angle_rad))
|
| 664 |
-
# Reverse the shifting we have done
|
| 665 |
-
new_point = (new_point[0] + center_point[0], new_point[1] + center_point[1])
|
| 666 |
-
return new_point
|
| 667 |
-
|
| 668 |
-
def rotate_polygon(polygon, angle, center_point=(0, 0)):
|
| 669 |
-
"""Rotates the given polygon which consists of corners represented as (x,y)
|
| 670 |
-
around center_point (origin by default)
|
| 671 |
-
Rotation is counter-clockwise
|
| 672 |
-
Angle is in degrees
|
| 673 |
-
"""
|
| 674 |
-
rotated_polygon = []
|
| 675 |
-
for corner in polygon:
|
| 676 |
-
rotated_corner = rotate_point(corner, angle, center_point)
|
| 677 |
-
rotated_polygon.append(rotated_corner)
|
| 678 |
-
return rotated_polygon
|
| 679 |
|
| 680 |
def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,pdfpath):
|
| 681 |
green2=allpreSteps(img)
|
|
@@ -690,14 +665,8 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
|
|
| 690 |
allpoints=[]
|
| 691 |
|
| 692 |
if number ==220:
|
| 693 |
-
# finalColorArray= colorOrder(img,finalColorArray)
|
| 694 |
-
# if flag== 'area':
|
| 695 |
-
# SimilarAreaDictionary= pd.DataFrame(columns=['Color','Rounded','Width','Height','Area','Occurences','Total Area' , 'R','G','B']) #
|
| 696 |
-
# else:
|
| 697 |
-
# SimilarAreaDictionary= pd.DataFrame(columns=['Color','Rounded','Width','Height','Perimeter','Occurences','Total Perimeter' ,'R','G','B'])
|
| 698 |
SimilarAreaDictionary= pd.DataFrame(columns=['Color','Rounded','Width','Height','Occurences','Area','Total Area','Perimeter','Total Perimeter','Length','Total Length','R','G','B'])
|
| 699 |
firstcolor=finalColorArray[0]
|
| 700 |
-
# print(lastcolor)
|
| 701 |
counter=0
|
| 702 |
maskDone=img.copy()
|
| 703 |
for eachcolor in finalColorArray:
|
|
@@ -710,11 +679,6 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
|
|
| 710 |
contourssA,rgbcolor,invertedmask=findContoursFullImage(green2,maskDone,number,finalColorArray,ratioarea,ratioperim,'perimeter',eachcolor)
|
| 711 |
SimilarAreaDictionary, colorsUsed , areas_Perimeters= FillDictionary(green2,SimilarAreaDictionary,maskDone,number,ratioarea,ratioperim,flag,finalColorArray,rgbcolor,eachcolor)
|
| 712 |
|
| 713 |
-
a = SimilarAreaDictionary.to_numpy()
|
| 714 |
-
|
| 715 |
-
# for component in zip(contourss,hierarchy):
|
| 716 |
-
# contour = component[0]
|
| 717 |
-
# currentHierarchy = component[1]
|
| 718 |
for contour in range(len(contourss)):
|
| 719 |
shape=[]
|
| 720 |
|
|
@@ -739,18 +703,19 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
|
|
| 739 |
area1 = cv2.contourArea(approx)
|
| 740 |
approx = cv2.approxPolyDP(contourss[contour][0], 0.01 * perimeter, True) #0.0009
|
| 741 |
perimeter1 = cv2.arcLength(approx, True)
|
|
|
|
| 742 |
for point in approx:
|
| 743 |
x1, y1 = point[0]
|
| 744 |
p1 = fitz.Point(x1*ratio,y1*ratio)
|
| 745 |
p1=p1*page.derotation_matrix
|
| 746 |
shape.append([p1[0],p1[1]])
|
| 747 |
-
|
| 748 |
if (angleR != 90.0 and angleR != -90.0 and angleR != 0.0 and angleR != -0.0 ): #inclined b ay degree
|
| 749 |
width=widthR
|
| 750 |
height=heightR
|
| 751 |
if width>height:
|
| 752 |
lengthShape = width
|
| 753 |
-
|
| 754 |
lengthShape = height
|
| 755 |
widthMin= width-10
|
| 756 |
widthMax= width+10
|
|
@@ -780,7 +745,7 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
|
|
| 780 |
passed=0
|
| 781 |
for i, row in masked.iterrows():
|
| 782 |
if passed ==0:
|
| 783 |
-
if
|
| 784 |
if (SimilarAreaDictionary['Width'].loc[i] <=widthMax and SimilarAreaDictionary['Width'].loc[i] >= widthMin) and (SimilarAreaDictionary['Height'].loc[i] <= heightMax and SimilarAreaDictionary['Height'].loc[i] >= heightMin ) or (SimilarAreaDictionary['Width'].loc[i] <=heightMax and SimilarAreaDictionary['Width'].loc[i] >= heightMin) and (SimilarAreaDictionary['Height'].loc[i] <= widthMax and SimilarAreaDictionary['Height'].loc[i] >= widthMin ) :
|
| 785 |
SimilarAreaDictionary['Total Area'].loc[i]+=areaa
|
| 786 |
SimilarAreaDictionary['Area'].loc[i]=areaa
|
|
@@ -844,11 +809,10 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
|
|
| 844 |
approx = cv2.approxPolyDP(contourss[contour][0], 0.01 * perimeter, True) #0.0009
|
| 845 |
perimeter1 = cv2.arcLength(approx, True)
|
| 846 |
for point in approx:
|
| 847 |
-
|
| 848 |
-
|
| 849 |
-
|
| 850 |
-
|
| 851 |
-
|
| 852 |
if (angleR != 90.0 and angleR != -90.0 and angleR != 0.0 and angleR != -0.0 ): #inclined b ay degree
|
| 853 |
width=widthR
|
| 854 |
height=heightR
|
|
@@ -909,8 +873,8 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
|
|
| 909 |
|
| 910 |
# annotationsSave
|
| 911 |
|
| 912 |
-
|
| 913 |
pdflink= db.dropbox_upload_file(doc=doc,pdfname=path,pdfpath=pdfpath+'Measured Plan/')
|
|
|
|
| 914 |
dbx=db.dropbox_connect()
|
| 915 |
md, res =dbx.files_download(path= pdfpath+path)
|
| 916 |
data = res.content
|
|
@@ -1132,11 +1096,9 @@ def legendGoogleSheets(SimilarAreaDictionary,path ,pdfpath, spreadsheetId=0):
|
|
| 1132 |
ws.create_developer_metadata('path',pdfpath)
|
| 1133 |
splittedpdfpath=re.split(r'[`\-=~!@#$%^&*()_+\[\]{};\'\\:"|<,/<>?]', pdfpath)
|
| 1134 |
namepathArr=[legendTitle , spreadsheetId,ws.get_developer_metadata('path')[0].value]
|
| 1135 |
-
if splittedpdfpath[-2].startswith('2.2'):
|
| 1136 |
-
df=
|
| 1137 |
-
|
| 1138 |
-
|
| 1139 |
-
|
| 1140 |
else:
|
| 1141 |
top_header_format = [
|
| 1142 |
{'mergeCells': { #areas
|
|
@@ -1198,23 +1160,24 @@ def legendGoogleSheets(SimilarAreaDictionary,path ,pdfpath, spreadsheetId=0):
|
|
| 1198 |
worksheet.cell((2,6)).value='Perimeters'
|
| 1199 |
worksheet.cell((2,8)).value='Lengths'
|
| 1200 |
second_row_data=['Nr','m2','Total','m','Total','m','Total']
|
| 1201 |
-
|
| 1202 |
-
|
| 1203 |
-
|
| 1204 |
-
|
| 1205 |
-
|
| 1206 |
-
|
| 1207 |
-
|
| 1208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1209 |
|
| 1210 |
-
worksheet.update_col(8,list(SimilarAreaDictionary['Length']),row_offset=3)
|
| 1211 |
-
worksheet.update_col(9,list(SimilarAreaDictionary['Total Length']),row_offset=3)
|
| 1212 |
-
if splittedpdfpath[-2].startswith('1.0'):
|
| 1213 |
-
colorsUsed=[]
|
| 1214 |
-
for i in range(len(SimilarAreaDictionary)):
|
| 1215 |
-
colorsUsed.append([SimilarAreaDictionary['R'].iloc[i] ,SimilarAreaDictionary['G'].iloc[i] , SimilarAreaDictionary['B'].iloc[i]] )
|
| 1216 |
-
elif splittedpdfpath[-2].startswith('3.2'):
|
| 1217 |
-
colorsUsed=list(SimilarAreaDictionary['Color'])
|
| 1218 |
#legend specs here
|
| 1219 |
rowsLen=len(SimilarAreaDictionary.values.tolist()) #kam row -- last row = rowsLen +1
|
| 1220 |
lastcell=worksheet.cell((rowsLen+2,1)) #row,col
|
|
@@ -1361,9 +1324,9 @@ def legendGoogleSheets(SimilarAreaDictionary,path ,pdfpath, spreadsheetId=0):
|
|
| 1361 |
model_cell.set_horizontal_alignment( pygsheets.custom_types.HorizontalAlignment.CENTER )
|
| 1362 |
model_cell.color = (213/255, 219/255 ,255/255)
|
| 1363 |
pygsheets.DataRange('A2','I2', worksheet=worksheet).apply_format(model_cell)
|
| 1364 |
-
|
| 1365 |
-
|
| 1366 |
-
|
| 1367 |
|
| 1368 |
|
| 1369 |
return gc,spreadsheet_service,spreadsheetId ,spreadsheet_url , namepathArr
|
|
@@ -1449,8 +1412,6 @@ def mapnametoLegend(McTName):
|
|
| 1449 |
allgbnames+= name +' +'
|
| 1450 |
indices = [o for o, x in enumerate(guessednames) if x == name]
|
| 1451 |
print(indices)
|
| 1452 |
-
# roww=worksheetw.find(str(name))
|
| 1453 |
-
# print(roww)
|
| 1454 |
for j in range(len(indices)):
|
| 1455 |
# print('kjjjj',roww[j])
|
| 1456 |
ar+=float(worksheetw.cell((indices[j]+1 ,rowvalue)).value)
|
|
|
|
| 651 |
|
| 652 |
|
| 653 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 654 |
|
| 655 |
def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,pdfpath):
|
| 656 |
green2=allpreSteps(img)
|
|
|
|
| 665 |
allpoints=[]
|
| 666 |
|
| 667 |
if number ==220:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 668 |
SimilarAreaDictionary= pd.DataFrame(columns=['Color','Rounded','Width','Height','Occurences','Area','Total Area','Perimeter','Total Perimeter','Length','Total Length','R','G','B'])
|
| 669 |
firstcolor=finalColorArray[0]
|
|
|
|
| 670 |
counter=0
|
| 671 |
maskDone=img.copy()
|
| 672 |
for eachcolor in finalColorArray:
|
|
|
|
| 679 |
contourssA,rgbcolor,invertedmask=findContoursFullImage(green2,maskDone,number,finalColorArray,ratioarea,ratioperim,'perimeter',eachcolor)
|
| 680 |
SimilarAreaDictionary, colorsUsed , areas_Perimeters= FillDictionary(green2,SimilarAreaDictionary,maskDone,number,ratioarea,ratioperim,flag,finalColorArray,rgbcolor,eachcolor)
|
| 681 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 682 |
for contour in range(len(contourss)):
|
| 683 |
shape=[]
|
| 684 |
|
|
|
|
| 703 |
area1 = cv2.contourArea(approx)
|
| 704 |
approx = cv2.approxPolyDP(contourss[contour][0], 0.01 * perimeter, True) #0.0009
|
| 705 |
perimeter1 = cv2.arcLength(approx, True)
|
| 706 |
+
|
| 707 |
for point in approx:
|
| 708 |
x1, y1 = point[0]
|
| 709 |
p1 = fitz.Point(x1*ratio,y1*ratio)
|
| 710 |
p1=p1*page.derotation_matrix
|
| 711 |
shape.append([p1[0],p1[1]])
|
| 712 |
+
|
| 713 |
if (angleR != 90.0 and angleR != -90.0 and angleR != 0.0 and angleR != -0.0 ): #inclined b ay degree
|
| 714 |
width=widthR
|
| 715 |
height=heightR
|
| 716 |
if width>height:
|
| 717 |
lengthShape = width
|
| 718 |
+
else:
|
| 719 |
lengthShape = height
|
| 720 |
widthMin= width-10
|
| 721 |
widthMax= width+10
|
|
|
|
| 745 |
passed=0
|
| 746 |
for i, row in masked.iterrows():
|
| 747 |
if passed ==0:
|
| 748 |
+
if ( SimilarAreaDictionary['Rounded'].loc[i] <= areaPerimeterMax and SimilarAreaDictionary['Rounded'].loc[i] >= areaPerimeterMin) :
|
| 749 |
if (SimilarAreaDictionary['Width'].loc[i] <=widthMax and SimilarAreaDictionary['Width'].loc[i] >= widthMin) and (SimilarAreaDictionary['Height'].loc[i] <= heightMax and SimilarAreaDictionary['Height'].loc[i] >= heightMin ) or (SimilarAreaDictionary['Width'].loc[i] <=heightMax and SimilarAreaDictionary['Width'].loc[i] >= heightMin) and (SimilarAreaDictionary['Height'].loc[i] <= widthMax and SimilarAreaDictionary['Height'].loc[i] >= widthMin ) :
|
| 750 |
SimilarAreaDictionary['Total Area'].loc[i]+=areaa
|
| 751 |
SimilarAreaDictionary['Area'].loc[i]=areaa
|
|
|
|
| 809 |
approx = cv2.approxPolyDP(contourss[contour][0], 0.01 * perimeter, True) #0.0009
|
| 810 |
perimeter1 = cv2.arcLength(approx, True)
|
| 811 |
for point in approx:
|
| 812 |
+
x1, y1 = point[0]
|
| 813 |
+
p1 = fitz.Point(x1*ratio,y1*ratio)
|
| 814 |
+
p1=p1*page.derotation_matrix
|
| 815 |
+
shape.append([p1[0],p1[1]])
|
|
|
|
| 816 |
if (angleR != 90.0 and angleR != -90.0 and angleR != 0.0 and angleR != -0.0 ): #inclined b ay degree
|
| 817 |
width=widthR
|
| 818 |
height=heightR
|
|
|
|
| 873 |
|
| 874 |
# annotationsSave
|
| 875 |
|
|
|
|
| 876 |
pdflink= db.dropbox_upload_file(doc=doc,pdfname=path,pdfpath=pdfpath+'Measured Plan/')
|
| 877 |
+
print('pdfpath,link',pdfpath,pdflink)
|
| 878 |
dbx=db.dropbox_connect()
|
| 879 |
md, res =dbx.files_download(path= pdfpath+path)
|
| 880 |
data = res.content
|
|
|
|
| 1096 |
ws.create_developer_metadata('path',pdfpath)
|
| 1097 |
splittedpdfpath=re.split(r'[`\-=~!@#$%^&*()_+\[\]{};\'\\:"|<,/<>?]', pdfpath)
|
| 1098 |
namepathArr=[legendTitle , spreadsheetId,ws.get_developer_metadata('path')[0].value]
|
| 1099 |
+
if splittedpdfpath[-2].startswith('2.2') or splittedpdfpath[-2].startswith('2.1') :
|
| 1100 |
+
worksheet.set_dataframe(start='A1',df=SimilarAreaDictionary)
|
| 1101 |
+
print(SimilarAreaDictionary)
|
|
|
|
|
|
|
| 1102 |
else:
|
| 1103 |
top_header_format = [
|
| 1104 |
{'mergeCells': { #areas
|
|
|
|
| 1160 |
worksheet.cell((2,6)).value='Perimeters'
|
| 1161 |
worksheet.cell((2,8)).value='Lengths'
|
| 1162 |
second_row_data=['Nr','m2','Total','m','Total','m','Total']
|
| 1163 |
+
if splittedpdfpath[-2].startswith('1.0') or splittedpdfpath[-2].startswith('3.2'):
|
| 1164 |
+
worksheet.update_row(3,second_row_data,col_offset=2)
|
| 1165 |
+
|
| 1166 |
+
worksheet.update_col(3,list(SimilarAreaDictionary['Occurences']),row_offset=3)
|
| 1167 |
+
worksheet.update_col(4,list(SimilarAreaDictionary['Area']),row_offset=3)
|
| 1168 |
+
worksheet.update_col(5,list(SimilarAreaDictionary['Total Area']),row_offset=3)
|
| 1169 |
+
worksheet.update_col(6,list(SimilarAreaDictionary['Perimeter']),row_offset=3)
|
| 1170 |
+
worksheet.update_col(7,list(SimilarAreaDictionary['Total Perimeter']),row_offset=3)
|
| 1171 |
+
|
| 1172 |
+
worksheet.update_col(8,list(SimilarAreaDictionary['Length']),row_offset=3)
|
| 1173 |
+
worksheet.update_col(9,list(SimilarAreaDictionary['Total Length']),row_offset=3)
|
| 1174 |
+
if splittedpdfpath[-2].startswith('1.0'):
|
| 1175 |
+
colorsUsed=[]
|
| 1176 |
+
for i in range(len(SimilarAreaDictionary)):
|
| 1177 |
+
colorsUsed.append([SimilarAreaDictionary['R'].iloc[i] ,SimilarAreaDictionary['G'].iloc[i] , SimilarAreaDictionary['B'].iloc[i]] )
|
| 1178 |
+
elif splittedpdfpath[-2].startswith('3.2'):
|
| 1179 |
+
colorsUsed=list(SimilarAreaDictionary['Color'])
|
| 1180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1181 |
#legend specs here
|
| 1182 |
rowsLen=len(SimilarAreaDictionary.values.tolist()) #kam row -- last row = rowsLen +1
|
| 1183 |
lastcell=worksheet.cell((rowsLen+2,1)) #row,col
|
|
|
|
| 1324 |
model_cell.set_horizontal_alignment( pygsheets.custom_types.HorizontalAlignment.CENTER )
|
| 1325 |
model_cell.color = (213/255, 219/255 ,255/255)
|
| 1326 |
pygsheets.DataRange('A2','I2', worksheet=worksheet).apply_format(model_cell)
|
| 1327 |
+
spreadsheet_url = "https://docs.google.com/spreadsheets/d/%s" % spreadsheetId
|
| 1328 |
+
print(spreadsheet_url)
|
| 1329 |
+
drive_service.permissions().update(transferOwnership=True , fileId=spreadsheetId,permissionId='11OfoB4Z6wOVII8mYmbnCbbqTQs7rYA65')
|
| 1330 |
|
| 1331 |
|
| 1332 |
return gc,spreadsheet_service,spreadsheetId ,spreadsheet_url , namepathArr
|
|
|
|
| 1412 |
allgbnames+= name +' +'
|
| 1413 |
indices = [o for o, x in enumerate(guessednames) if x == name]
|
| 1414 |
print(indices)
|
|
|
|
|
|
|
| 1415 |
for j in range(len(indices)):
|
| 1416 |
# print('kjjjj',roww[j])
|
| 1417 |
ar+=float(worksheetw.cell((indices[j]+1 ,rowvalue)).value)
|