Spaces:
Sleeping
Sleeping
Update pilecaps_adr.py
Browse files- pilecaps_adr.py +335 -238
pilecaps_adr.py
CHANGED
|
@@ -22,6 +22,27 @@ import db
|
|
| 22 |
import ast
|
| 23 |
import Dropbox_TSA_API
|
| 24 |
import tsadropboxretrieval
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
#Prepare preprocessing
|
| 26 |
def detectCircles(imgOriginal ):
|
| 27 |
im=imgOriginal.copy()
|
|
@@ -62,10 +83,8 @@ def detectSmallCircles(img ):
|
|
| 62 |
#DRAW CIRCLES
|
| 63 |
for (x, y, r) in detected_circles:
|
| 64 |
cv2.circle(im, (x, y), r+3, (255, 255, 255), -1)
|
| 65 |
-
# cv2_imshow(im)
|
| 66 |
return im
|
| 67 |
|
| 68 |
-
|
| 69 |
def DashedPreprocessing(imgOriginal,imgnoSmall):
|
| 70 |
h,w=imgOriginal.shape[0:2]
|
| 71 |
#remove the gray contours from the plan
|
|
@@ -94,17 +113,12 @@ def DashedPreprocessing(imgOriginal,imgnoSmall):
|
|
| 94 |
return img4, imgBW, max,min
|
| 95 |
|
| 96 |
def removeDashedLines(img4, imgBW ,max,min):
|
| 97 |
-
|
| 98 |
imgLines= cv2.HoughLinesP(img4,1,np.pi/310,30,minLineLength=(max-min)//1.8,maxLineGap = 120) #was w-h , gap=150 0.99
|
| 99 |
-
#1 120
|
| 100 |
-
|
| 101 |
for i in range(len(imgLines)):
|
| 102 |
for x1,y1,x2,y2 in imgLines[i]:
|
| 103 |
cv2.line(imgBW,(x1,y1),(x2,y2),(0,255,0),3)
|
| 104 |
-
|
| 105 |
im_copy=imgBW.copy()
|
| 106 |
green=im_copy[:,:,1]
|
| 107 |
-
# cv2_imshow(im_copy)
|
| 108 |
return green
|
| 109 |
|
| 110 |
def removeSmallDashes(imgOriginal,green):
|
|
@@ -143,20 +157,15 @@ def euclidian_distance(point1, point2):
|
|
| 143 |
return sum([(point1[x] - point2[x]) ** 2 for x in range(len(point1))]) ** 0.5
|
| 144 |
|
| 145 |
def removeDashedLinesSmall(img4, imgBW ,max,min):
|
| 146 |
-
|
| 147 |
imgBW=cv2.cvtColor(imgBW,cv2.COLOR_GRAY2BGR)
|
| 148 |
imgLines= cv2.HoughLinesP(img4,1,np.pi/100,20,minLineLength=(max-min)//3.5,maxLineGap = 70) #2.1
|
| 149 |
-
|
| 150 |
for i in range(len(imgLines)):
|
| 151 |
for x1,y1,x2,y2 in imgLines[i]:
|
| 152 |
dist=euclidian_distance((x1,y1), (x2,y2))
|
| 153 |
-
# if dist > 1300 and dist <1450:
|
| 154 |
if dist >= (max-min)//2.3 and dist < (max-min)//1.9: #1.4
|
| 155 |
cv2.line(imgBW,(x1,y1),(x2,y2),(0,255,0),3)
|
| 156 |
-
|
| 157 |
im_copy=imgBW.copy()
|
| 158 |
green=im_copy[:,:,1]
|
| 159 |
-
# cv2_imshow(im_copy)
|
| 160 |
return green
|
| 161 |
|
| 162 |
def ConnectBeamLines(smalldashesOut):
|
|
@@ -180,51 +189,148 @@ def allpreSteps(imgOriginal):
|
|
| 180 |
# cv2_imshow(green2)
|
| 181 |
return green2
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
def ChangeBrightness(img,k):
|
| 184 |
imgdarker = 255 * (img/255)**k # k>1 darker , k <1 lighter
|
| 185 |
imgdarker = imgdarker.astype('uint8')
|
| 186 |
return imgdarker
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
#OLD method (White shapes)
|
| 188 |
def preprocessold(img,number):
|
| 189 |
-
blurG = ChangeBrightness(img,3)
|
| 190 |
imgGry = cv2.cvtColor(blurG, cv2.COLOR_BGR2GRAY)
|
| 191 |
-
|
| 192 |
-
er1=cv2.dilate(imgGry,
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
return thresh
|
|
|
|
| 198 |
#anding of old method output with solid lines img
|
| 199 |
-
def preprocess(
|
| 200 |
#first preprocessing ( old method - black img with white shapes)
|
| 201 |
-
|
|
|
|
| 202 |
anding=cv2.bitwise_and(green2,green2,mask=img1)
|
| 203 |
anding = cv2.cvtColor(anding, cv2.COLOR_BGR2GRAY)
|
| 204 |
ret3, thresh2 = cv2.threshold(anding, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
|
|
|
| 205 |
if layeredflag.startswith('layer'):
|
| 206 |
thresh2=cv2.bitwise_and(thresh2,thresh2,mask=BlackmaskDetected1)
|
| 207 |
threshcontours, threshHier = cv2.findContours(thresh2, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
|
| 208 |
-
|
|
|
|
| 209 |
finalcnts=[]
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
area1 = cv2.contourArea(cnt)
|
| 214 |
if (area1 > 2000 ):
|
| 215 |
x, y , width, height = cv2.boundingRect(cnt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
if flag.startswith('perimeter'):
|
| 217 |
kernel=np.ones((2,2),np.uint8)
|
| 218 |
-
cv2.drawContours(Blackmask,[cnt],0,(255,255,255),
|
| 219 |
Blackmask=cv2.dilate(Blackmask,kernel, iterations=1)
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
kernel=np.ones((2,2),np.uint8)
|
| 222 |
-
cv2.drawContours(Blackmask,[cnt],0,(255,255,255),
|
| 223 |
-
Blackmask=cv2.
|
|
|
|
| 224 |
contours1, hier1 = cv2.findContours(Blackmask, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
|
| 225 |
finalcnts.append(contours1)
|
| 226 |
finalcnts=tuple(finalcnts)
|
| 227 |
-
|
|
|
|
|
|
|
| 228 |
|
| 229 |
"""# ROI (levels)
|
| 230 |
## Detect regions with specific color and mask them
|
|
@@ -404,201 +510,139 @@ def getColoredContour(mask,img,finalColorArray,ratioarea,ratioperim,flag,eachcol
|
|
| 404 |
|
| 405 |
"""# contours"""
|
| 406 |
|
| 407 |
-
def findContoursFullImage(green2,img,number,finalColorArray,ratioarea,ratioperim,flag,color=[0,0,0]):
|
| 408 |
if number == 0:
|
| 409 |
-
contourss=preprocess(img,number,green2,flag,'nolayer')
|
| 410 |
-
|
| 411 |
-
# contourss, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 412 |
-
return contourss
|
| 413 |
else:
|
| 414 |
mask, detectedColors, rgbcolor =DetectColor(img,color)
|
| 415 |
-
# print(rgbcolor)
|
| 416 |
-
|
| 417 |
pil_image=Image.fromarray(mask)
|
| 418 |
-
|
| 419 |
extrema = pil_image.convert("L").getextrema()
|
| 420 |
if extrema != (0, 0): # if image is not black --> has a colored mask within
|
| 421 |
coloredregions,invertedmask,BlackmaskDetected1=getColoredContour(mask,img,finalColorArray,ratioarea,ratioperim,flag,color)
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
else:
|
| 426 |
-
contourss=preprocess(img,number,green2,flag,'nolayer')
|
| 427 |
-
|
| 428 |
-
return contourss,color ,mask
|
| 429 |
|
| 430 |
#Straighten tilted shapes
|
| 431 |
def StraightenImage(contour,imgArea):
|
| 432 |
rect = cv2.minAreaRect(contour)
|
| 433 |
-
|
| 434 |
(center, (width, height), angleR) = cv2.minAreaRect(contour)
|
| 435 |
-
|
| 436 |
box = cv2.boxPoints(rect)
|
| 437 |
box = np.int0(box)
|
| 438 |
-
|
| 439 |
# get width and height of the detected rectangle
|
| 440 |
width = int(rect[1][0])
|
| 441 |
height = int(rect[1][1])
|
| 442 |
-
|
| 443 |
-
# src_pts = box.astype("float32")
|
| 444 |
-
# dst_pts = np.array([[0, height-1],
|
| 445 |
-
# [0, 0],
|
| 446 |
-
# [width-1, 0],
|
| 447 |
-
# [width-1, height-1]], dtype="float32")
|
| 448 |
-
|
| 449 |
-
# # the perspective transformation matrix
|
| 450 |
-
# M = cv2.getPerspectiveTransform(src_pts, dst_pts)
|
| 451 |
-
|
| 452 |
-
# # directly warp the rotated rectangle to get the straightened rectangle
|
| 453 |
-
|
| 454 |
-
# warped = cv2.warpPerspective(imgArea, M, (width, height))
|
| 455 |
-
##############
|
| 456 |
return angleR,width,height
|
|
|
|
| 457 |
#get all areas and perimeter present
|
| 458 |
-
def getAreasPerimeter(
|
| 459 |
appended=[]
|
| 460 |
-
if number==0:
|
| 461 |
-
contourss=findContoursFullImage(green2,img,number,finalColorArray,ratioarea,ratioperim,flag,color)
|
| 462 |
-
else:
|
| 463 |
-
contourss=findContoursFullImage(green2,img,number,finalColorArray,ratioarea,ratioperim,flag,color)[0]
|
| 464 |
-
|
| 465 |
for contour in range(len(contourss)):
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
if ratioarea!=0 and ratioperim!=0:
|
| 484 |
-
# print('numss')
|
| 485 |
-
# if flag=='area':
|
| 486 |
-
# addedMargin=area1+perimeter1*2
|
| 487 |
-
areaa=round(area1* ratioarea,3)# true value of area of any shape/ area px value of same shape
|
| 488 |
-
appended.append([areaa,width,height])
|
| 489 |
-
|
| 490 |
-
#else:
|
| 491 |
-
#perimeter=round(perimeter1*(ratioarea/ratioperim),3)
|
| 492 |
-
#appended.append([perimeter,width,height])
|
| 493 |
-
|
| 494 |
return appended
|
| 495 |
#fill dictionary with areas and perimeters and occurences
|
| 496 |
-
def FillDictionary(
|
| 497 |
#fills dictionary with key areas and number of occurences
|
| 498 |
-
areas_Perimeters=sorted(getAreasPerimeter(
|
| 499 |
-
|
| 500 |
indices=[]
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
colorRanges=[[255,0,0],[0,0,255],[0,255,255],[0,64,0],[255,204,0],[255,128,64],[255,0,128],[255,128,192],[128,128,255],[128,64,0],[0,255,0],[179,106,179],[115,52,179],[0,128,192],[128,0,128],[128,0,0],[0,128,255],[255,182,128],[255,0,255],[0,0,128],[0,128,64],[255,255,0],[128,0,64],[203,203,106],[128,255,166],[255,128,0],[255,98,98],[90,105,138],[114,10,72],[36,82,78],[225,105,29],[108,62,40],[11,35,75],[42,176,203],[255,153,153],[129,74,138],[99,123,137],[159,179,30]]
|
| 504 |
-
# print(colorRanges[0])
|
| 505 |
-
# print(colorRanges[0][0],colorRanges[0][1], colorRanges[0][2])
|
| 506 |
colorsUsed=[]
|
| 507 |
for i in range(len(areas_Perimeters)):
|
| 508 |
-
|
| 509 |
# colorRGB=hexRGB(color)
|
| 510 |
item1 = areas_Perimeters[i][0]
|
| 511 |
width1 = areas_Perimeters[i][1]
|
| 512 |
height1 = areas_Perimeters[i][2]
|
| 513 |
-
widthMin= width1-
|
| 514 |
-
widthMax= width1+
|
| 515 |
-
heightMin=height1-
|
| 516 |
-
heightMax= height1+
|
| 517 |
-
areaPerimeterMin= round(item1,1) - 0.
|
| 518 |
-
areaPerimeterMax= round(item1,1) + 0.
|
| 519 |
# print (areaMin, areaMax)
|
| 520 |
-
if color != [0,0,0]: #colored images
|
| 521 |
-
|
| 522 |
-
mydata=[[rgbcolor[0],rgbcolor[1],rgbcolor[2] ],round(item1,1),width1,height1,1, 0,0,0,0,0,0,0,0,0]
|
| 523 |
-
# mydata=[round(item1,1),width1,height1,0, 1,0,[rgbcolor[0],rgbcolor[1],rgbcolor[2] ],colorRanges[0][2],colorRanges[0][1],colorRanges[0][0]]
|
| 524 |
-
# colorRanges.pop(0)
|
| 525 |
else:
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
mydata=[' ', round(item1,1),width1,height1,1, 0,0,0,0,0,0,0,0,0]
|
| 529 |
-
|
| 530 |
-
# if (( round(item1,1) in SimilarAreaDictionary['Rounded'].values) or (areaMin in SimilarAreaDictionary['Rounded'].values )or (areaMax in SimilarAreaDictionary['Rounded'].values )):
|
| 531 |
-
|
| 532 |
-
# myindex= SimilarAreaDictionary.index[( SimilarAreaDictionary['Rounded']== round(item1,1) ) ].tolist()
|
| 533 |
myindex= SimilarAreaDictionary.index[((SimilarAreaDictionary['Rounded'] >=areaPerimeterMin) &(SimilarAreaDictionary['Rounded']<=areaPerimeterMax) )].tolist()
|
| 534 |
-
# for i in myindex:
|
| 535 |
-
# SimilarAreaDictionary['Rounded'].loc[i]
|
| 536 |
if color!= [0,0,0]: #leveled image
|
| 537 |
-
|
| 538 |
checkifColorExists=0 # to check whether this row was found or not( area and color )
|
| 539 |
for i in myindex: # loop on indices that were found --> rows containing this area to check its color and add occ.
|
| 540 |
-
if SimilarAreaDictionary['Color'].loc[i]==[rgbcolor[0],rgbcolor[1],rgbcolor[2]] and ( SimilarAreaDictionary['Rounded'].loc[i] >= areaPerimeterMin and SimilarAreaDictionary['Rounded'].loc[i] <= areaPerimeterMax) :
|
| 541 |
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 ) :
|
| 542 |
checkifColorExists=1 #found and incremented
|
| 543 |
SimilarAreaDictionary['Occurences'].loc[i]+=1
|
| 544 |
-
if checkifColorExists==0: #couldnt find the color , doesnt exist so add it
|
| 545 |
SimilarAreaDictionary.loc[len(SimilarAreaDictionary)] =mydata
|
| 546 |
-
|
| 547 |
else: #full image
|
| 548 |
-
#
|
| 549 |
-
#same code bs mgher color
|
| 550 |
checkifColorExists=0
|
| 551 |
-
for i in myindex:
|
| 552 |
if ( SimilarAreaDictionary['Rounded'].loc[i] <= areaPerimeterMax and SimilarAreaDictionary['Rounded'].loc[i] >= areaPerimeterMin) :
|
| 553 |
-
# print(SimilarAreaDictionary['Rounded'].loc[i] ,'in rng if', areaMin,areaMax)
|
| 554 |
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 ) :
|
| 555 |
checkifColorExists=1 #found and incremented
|
| 556 |
SimilarAreaDictionary['Occurences'].loc[i]+=1
|
| 557 |
-
|
| 558 |
-
# SimilarAreaDictionary['G'].loc[i] =colorRanges[i][1]
|
| 559 |
-
# SimilarAreaDictionary['B'].loc[i] = colorRanges[i][2]
|
| 560 |
-
|
| 561 |
-
# colorRanges.pop(0)
|
| 562 |
-
|
| 563 |
-
if checkifColorExists==0: #couldnt find the color , doesnt exist so add it
|
| 564 |
SimilarAreaDictionary.loc[len(SimilarAreaDictionary)] =mydata
|
| 565 |
# s= SimilarAreaDictionary
|
| 566 |
for i in range(len(SimilarAreaDictionary)):
|
| 567 |
-
SimilarAreaDictionary
|
| 568 |
-
SimilarAreaDictionary
|
| 569 |
-
SimilarAreaDictionary
|
| 570 |
colorsUsed.append(colorRanges[i])
|
| 571 |
|
| 572 |
-
|
| 573 |
return SimilarAreaDictionary, colorsUsed , areas_Perimeters
|
| 574 |
#detect and draw and measure
|
| 575 |
def drawAllContours(dataDoc,img,number,finalColorArray,ratioarea,ratioperim,flag , path,pdfpath):
|
| 576 |
green2=allpreSteps(img)
|
| 577 |
doc = fitz.open("pdf",dataDoc)
|
| 578 |
page = doc[0]
|
|
|
|
| 579 |
pix=page.get_pixmap()
|
| 580 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 581 |
areasinImage=[]
|
| 582 |
imgArea1= img.copy()
|
| 583 |
imgPerimeter1=img.copy()
|
| 584 |
imgtransparent1=img.copy()
|
| 585 |
-
allpoints=[]
|
| 586 |
|
| 587 |
if number ==220:
|
| 588 |
-
SimilarAreaDictionary= pd.DataFrame(columns=['Color','Rounded','Width','Height','Occurences','Area','Total Area','Perimeter','Total Perimeter','Length','Total Length','R','G','B'])
|
| 589 |
firstcolor=finalColorArray[0]
|
| 590 |
counter=0
|
| 591 |
maskDone=img.copy()
|
| 592 |
for eachcolor in finalColorArray:
|
| 593 |
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
counter+=1
|
| 597 |
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
SimilarAreaDictionary, colorsUsed , areas_Perimeters= FillDictionary(green2,SimilarAreaDictionary,maskDone,number,ratioarea,ratioperim,flag,finalColorArray,rgbcolor,eachcolor)
|
| 601 |
-
|
| 602 |
for contour in range(len(contourss)):
|
| 603 |
shape=[]
|
| 604 |
|
|
@@ -607,29 +651,21 @@ def drawAllContours(dataDoc,img,number,finalColorArray,ratioarea,ratioperim,flag
|
|
| 607 |
|
| 608 |
# perimeter1 = cv2.arcLength(contourss[contour][0], True)
|
| 609 |
if (area1 > 3500 ): #check perimeter kman fl condition -- 2800
|
| 610 |
-
|
| 611 |
-
|
| 612 |
angleR,widthR ,heightR= StraightenImage(contourss[contour][0],imgArea1)
|
| 613 |
x, y , width, height = cv2.boundingRect(contourss[contour][0])
|
| 614 |
-
|
|
|
|
| 615 |
|
| 616 |
-
|
| 617 |
-
box = box.astype('int')
|
| 618 |
-
# print(box)
|
| 619 |
-
|
| 620 |
-
x, y , width, height = cv2.boundingRect(contourss[contour][0])
|
| 621 |
-
perimeter = cv2.arcLength(contourss[contour][0], True)
|
| 622 |
-
# approx = cv2.approxPolyDP(contourssA[contour][0], 0.0002* perimeter, True)
|
| 623 |
-
# area1 = cv2.contourArea(approx)
|
| 624 |
-
approx = cv2.approxPolyDP(contourss[contour][0], 0.007 * perimeter, True) #0.0009
|
| 625 |
perimeter1 = cv2.arcLength(approx, True)
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
if (angleR != 90.0 and angleR != -90.0 and angleR != 0.0 and angleR != -0.0 ): #inclined b ay degree
|
| 634 |
width=widthR
|
| 635 |
height=heightR
|
|
@@ -642,9 +678,6 @@ def drawAllContours(dataDoc,img,number,finalColorArray,ratioarea,ratioperim,flag
|
|
| 642 |
heightMin=height-15
|
| 643 |
heightMax= height+15
|
| 644 |
if ratioarea !=0 and ratioperim!=0:
|
| 645 |
-
widthh=round(width*ratioperim,3)
|
| 646 |
-
heightt=round(height*ratioperim,3)
|
| 647 |
-
# if flag=='area':
|
| 648 |
areaa=round(area1* ratioarea, 3) # true value of area of any shape/ area px value of same shape
|
| 649 |
perimeterr=round(perimeter1* ratioperim,3)
|
| 650 |
lengthShape=round(lengthShape* ratioperim,3)
|
|
@@ -652,46 +685,62 @@ def drawAllContours(dataDoc,img,number,finalColorArray,ratioarea,ratioperim,flag
|
|
| 652 |
areaa=area1
|
| 653 |
perimeterr=perimeter1
|
| 654 |
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
areaPerimeterMax= round(areaa,1) + 0.25
|
| 658 |
-
# areaPerimeterMin= round(perimeterr,1) - 0.3
|
| 659 |
-
# areaPerimeterMax= round(perimeterr,1) + 0.3
|
| 660 |
masked=SimilarAreaDictionary.loc[SimilarAreaDictionary.index[((SimilarAreaDictionary['Rounded'] >=areaPerimeterMin) &(SimilarAreaDictionary['Rounded']<=areaPerimeterMax) )]]
|
| 661 |
-
# masked=SimilarAreaDictionary.loc[SimilarAreaDictionary['Rounded'] ==round(areaa,1)]
|
| 662 |
-
# if (round(areaa,1) in masked['Rounded'].values ) :
|
| 663 |
passed=0
|
| 664 |
for i, row in masked.iterrows():
|
| 665 |
if passed ==0:
|
| 666 |
if SimilarAreaDictionary['Color'].loc[i] == [rgbcolor[0],rgbcolor[1],rgbcolor[2]] and ( SimilarAreaDictionary['Rounded'].loc[i] <= areaPerimeterMax and SimilarAreaDictionary['Rounded'].loc[i] >= areaPerimeterMin) :
|
| 667 |
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 ) :
|
| 668 |
-
|
| 669 |
-
|
| 670 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 671 |
SimilarAreaDictionary['Total Perimeter'].loc[i]+=perimeterr
|
| 672 |
-
SimilarAreaDictionary['Perimeter'].loc[i]=perimeterr
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
|
| 679 |
-
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
annot.set_opacity(0.5)
|
| 686 |
-
annot.set_info(content='Area= '+str(areaa)+' m2' +'\n \nPerimeter= '+str(perimeterr)+' m' +'\n \nLength= '+str(lengthShape)+' m',subject='ADR Team')#,title='uuum')
|
| 687 |
-
# annot.set_line_ends(fitz.PDF_ANNOT_LE_DIAMOND, fitz.PDF_ANNOT_LE_CIRCLE)
|
| 688 |
-
annot.update()
|
| 689 |
|
| 690 |
-
#
|
| 691 |
-
# cv2.putText(imgtransparent1,'Width= '+str(width) , (x+50,y-10) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
|
| 692 |
-
# cv2.putText(imgtransparent1,'Length= '+str(height) , (x+50,y-20) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
|
| 693 |
-
areasinImage.append(areaa)
|
| 694 |
-
|
| 695 |
cv2.putText(imgPerimeter1,'Perimeter: '+str(perimeterr)+ ' m', (x+70,y-30) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
|
| 696 |
cv2.putText(imgPerimeter1,'Area: '+str(areaa)+' m2', (x+50,y-40) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
|
| 697 |
|
|
@@ -706,27 +755,25 @@ def drawAllContours(dataDoc,img,number,finalColorArray,ratioarea,ratioperim,flag
|
|
| 706 |
#########################
|
| 707 |
else:
|
| 708 |
|
| 709 |
-
SimilarAreaDictionary= pd.DataFrame(columns=['Color','Rounded','Width','Height','Occurences','Area','Total Area','Perimeter','Total Perimeter','Length','Total Length','R','G','B'])
|
| 710 |
-
contourss=findContoursFullImage(green2,img,number,finalColorArray,ratioarea,ratioperim,'perimeter')
|
| 711 |
-
contourssA=findContoursFullImage(green2,img,number,finalColorArray,ratioarea,ratioperim,'area')
|
| 712 |
-
SimilarAreaDictionary,colorsUsed , areas_Perimeters= FillDictionary(
|
| 713 |
-
|
| 714 |
for contour in range(len(contourss)):
|
| 715 |
-
# imgArea1=img.copy()
|
| 716 |
area1 = cv2.contourArea(contourssA[contour][0])
|
| 717 |
-
if (area1 >
|
| 718 |
-
perimeter
|
|
|
|
| 719 |
|
| 720 |
shape=[]
|
| 721 |
angleR,widthR ,heightR= StraightenImage(contourss[contour][0],imgArea1)
|
| 722 |
x, y , width, height = cv2.boundingRect(contourss[contour][0])
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
# area1 = cv2.contourArea(approxa)
|
| 727 |
-
approx = cv2.approxPolyDP(contourss[contour][0], 0.007 * perimeter, True) #0.0009
|
| 728 |
perimeter1 = cv2.arcLength(approx, True)
|
| 729 |
-
for point in
|
| 730 |
x1, y1 = point[0]
|
| 731 |
p1 = fitz.Point(x1*ratio,y1*ratio)
|
| 732 |
p1=p1*page.derotation_matrix
|
|
@@ -750,8 +797,8 @@ def drawAllContours(dataDoc,img,number,finalColorArray,ratioarea,ratioperim,flag
|
|
| 750 |
lengthShape=round(lengthShape* ratioperim,3)
|
| 751 |
else:
|
| 752 |
perimeterr=perimeter1
|
| 753 |
-
areaPerimeterMin= round(areaa,1) - 0.
|
| 754 |
-
areaPerimeterMax= round(areaa,1) + 0.
|
| 755 |
masked=SimilarAreaDictionary.loc[SimilarAreaDictionary.index[((SimilarAreaDictionary['Rounded'] >=areaPerimeterMin) & (SimilarAreaDictionary['Rounded']<=areaPerimeterMax) )]]
|
| 756 |
passed=0
|
| 757 |
for i, row in masked.iterrows():
|
|
@@ -760,29 +807,79 @@ def drawAllContours(dataDoc,img,number,finalColorArray,ratioarea,ratioperim,flag
|
|
| 760 |
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 ) :
|
| 761 |
SimilarAreaDictionary['Total Area'].loc[i]+=areaa
|
| 762 |
SimilarAreaDictionary['Area'].loc[i]=areaa
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
SimilarAreaDictionary['
|
| 766 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 767 |
SimilarAreaDictionary['Total Length'].loc[i]+=lengthShape
|
| 768 |
SimilarAreaDictionary['Length'].loc[i]=lengthShape
|
| 769 |
|
| 770 |
passed=1
|
| 771 |
cv2.drawContours(imgArea1, [contourss[contour][0]], 0, ( int(SimilarAreaDictionary['B'].loc[i]), int(SimilarAreaDictionary['G'].loc[i]), int(SimilarAreaDictionary['R'].loc[i])), -1)
|
| 772 |
-
|
| 773 |
-
annot
|
| 774 |
-
|
| 775 |
-
annot.set_border(width=0.3, dashes=[2])
|
| 776 |
-
annot.set_colors( fill=( int(SimilarAreaDictionary['R'].loc[i])/255 , int(SimilarAreaDictionary['G'].loc[i])/255 , int(SimilarAreaDictionary['B'].loc[i])/255 ) )
|
| 777 |
-
# annot.set_colors( fill=(1,0,1) )
|
| 778 |
annot.set_opacity(0.5)
|
| 779 |
-
annot.set_info(content='Area=
|
| 780 |
# annot.set_line_ends(fitz.PDF_ANNOT_LE_DIAMOND, fitz.PDF_ANNOT_LE_CIRCLE)
|
| 781 |
annot.update()
|
| 782 |
-
|
| 783 |
-
cv2.putText(imgtransparent1,'Area: '+str(areaa)+' m2'
|
| 784 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 785 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 786 |
alpha = 0.4 # Transparency factor.
|
| 787 |
image_new1 = cv2.addWeighted(imgArea1, alpha, imgtransparent1, 1 - alpha, 0)
|
| 788 |
|
|
@@ -1036,7 +1133,7 @@ def legendGoogleSheets(SimilarAreaDictionary,path ,pdfpath, spreadsheetId=0):
|
|
| 1036 |
second_row_data=['Nr','m2','Total','m','Total','m','Total']
|
| 1037 |
if splittedpdfpath[-2].startswith('1.0') or splittedpdfpath[-2].startswith('3.2'):
|
| 1038 |
worksheet.update_row(3,second_row_data,col_offset=2)
|
| 1039 |
-
|
| 1040 |
worksheet.update_col(3,list(SimilarAreaDictionary['Occurences']),row_offset=3)
|
| 1041 |
worksheet.update_col(4,list(SimilarAreaDictionary['Area']),row_offset=3)
|
| 1042 |
worksheet.update_col(5,list(SimilarAreaDictionary['Total Area']),row_offset=3)
|
|
|
|
| 22 |
import ast
|
| 23 |
import Dropbox_TSA_API
|
| 24 |
import tsadropboxretrieval
|
| 25 |
+
from collections import Counter
|
| 26 |
+
|
| 27 |
+
def textLists(img,dataDoc):
|
| 28 |
+
allTexts = texts_from_pdf(dataDoc)
|
| 29 |
+
doc = fitz.open('pdf',dataDoc)
|
| 30 |
+
page=doc[0]
|
| 31 |
+
if page.rotation!=0:
|
| 32 |
+
page.set_rotation(0)
|
| 33 |
+
pix = page.get_pixmap() # render page to an image
|
| 34 |
+
ratio = pix.width/ img.shape[1]
|
| 35 |
+
listall=[]
|
| 36 |
+
pc_coor = []
|
| 37 |
+
for tpl in allTexts:
|
| 38 |
+
if "GB" in tpl[4] or "RC" in tpl[4] or "PC" in tpl[4]:
|
| 39 |
+
p1 = fitz.Point((tpl[2]/ratio),(tpl[3]/ratio))
|
| 40 |
+
if page.rotation==0:
|
| 41 |
+
p1=p1*page.derotation_matrix
|
| 42 |
+
pc_coor.append((p1[0],p1[1]))
|
| 43 |
+
listall.append((p1[0],p1[1],tpl[4]))
|
| 44 |
+
return pc_coor, listall
|
| 45 |
+
|
| 46 |
#Prepare preprocessing
|
| 47 |
def detectCircles(imgOriginal ):
|
| 48 |
im=imgOriginal.copy()
|
|
|
|
| 83 |
#DRAW CIRCLES
|
| 84 |
for (x, y, r) in detected_circles:
|
| 85 |
cv2.circle(im, (x, y), r+3, (255, 255, 255), -1)
|
|
|
|
| 86 |
return im
|
| 87 |
|
|
|
|
| 88 |
def DashedPreprocessing(imgOriginal,imgnoSmall):
|
| 89 |
h,w=imgOriginal.shape[0:2]
|
| 90 |
#remove the gray contours from the plan
|
|
|
|
| 113 |
return img4, imgBW, max,min
|
| 114 |
|
| 115 |
def removeDashedLines(img4, imgBW ,max,min):
|
|
|
|
| 116 |
imgLines= cv2.HoughLinesP(img4,1,np.pi/310,30,minLineLength=(max-min)//1.8,maxLineGap = 120) #was w-h , gap=150 0.99
|
|
|
|
|
|
|
| 117 |
for i in range(len(imgLines)):
|
| 118 |
for x1,y1,x2,y2 in imgLines[i]:
|
| 119 |
cv2.line(imgBW,(x1,y1),(x2,y2),(0,255,0),3)
|
|
|
|
| 120 |
im_copy=imgBW.copy()
|
| 121 |
green=im_copy[:,:,1]
|
|
|
|
| 122 |
return green
|
| 123 |
|
| 124 |
def removeSmallDashes(imgOriginal,green):
|
|
|
|
| 157 |
return sum([(point1[x] - point2[x]) ** 2 for x in range(len(point1))]) ** 0.5
|
| 158 |
|
| 159 |
def removeDashedLinesSmall(img4, imgBW ,max,min):
|
|
|
|
| 160 |
imgBW=cv2.cvtColor(imgBW,cv2.COLOR_GRAY2BGR)
|
| 161 |
imgLines= cv2.HoughLinesP(img4,1,np.pi/100,20,minLineLength=(max-min)//3.5,maxLineGap = 70) #2.1
|
|
|
|
| 162 |
for i in range(len(imgLines)):
|
| 163 |
for x1,y1,x2,y2 in imgLines[i]:
|
| 164 |
dist=euclidian_distance((x1,y1), (x2,y2))
|
|
|
|
| 165 |
if dist >= (max-min)//2.3 and dist < (max-min)//1.9: #1.4
|
| 166 |
cv2.line(imgBW,(x1,y1),(x2,y2),(0,255,0),3)
|
|
|
|
| 167 |
im_copy=imgBW.copy()
|
| 168 |
green=im_copy[:,:,1]
|
|
|
|
| 169 |
return green
|
| 170 |
|
| 171 |
def ConnectBeamLines(smalldashesOut):
|
|
|
|
| 189 |
# cv2_imshow(green2)
|
| 190 |
return green2
|
| 191 |
|
| 192 |
+
def texts_from_pdf(input_pdf):
|
| 193 |
+
pdf_document = fitz.open("pdf",input_pdf)
|
| 194 |
+
|
| 195 |
+
for page_num in range(pdf_document.page_count):
|
| 196 |
+
page = pdf_document[page_num]
|
| 197 |
+
text_instances = page.get_text("words")
|
| 198 |
+
page.apply_redactions()
|
| 199 |
+
return text_instances
|
| 200 |
+
|
| 201 |
+
def nearestText(a,b , pc_coor):
|
| 202 |
+
nearest_point=min(pc_coor,key=lambda x:euclidian_distance((a,b),x))
|
| 203 |
+
dist = euclidian_distance(nearest_point, (a,b))
|
| 204 |
+
if dist < 400: #distance threshold
|
| 205 |
+
return nearest_point
|
| 206 |
+
else:
|
| 207 |
+
return 'none'
|
| 208 |
+
|
| 209 |
def ChangeBrightness(img,k):
|
| 210 |
imgdarker = 255 * (img/255)**k # k>1 darker , k <1 lighter
|
| 211 |
imgdarker = imgdarker.astype('uint8')
|
| 212 |
return imgdarker
|
| 213 |
+
|
| 214 |
+
def getOutlinesDotIN(img):
|
| 215 |
+
cc=detectSmallCircles(img)
|
| 216 |
+
cc = cv2.cvtColor(cc, cv2.COLOR_BGR2GRAY)
|
| 217 |
+
kernel=np.ones((3,3),np.uint8)
|
| 218 |
+
er1=cv2.dilate(cc,kernel, iterations=2) #thinning
|
| 219 |
+
blurG = ChangeBrightness(er1,10)
|
| 220 |
+
ret3, thresh = cv2.threshold(blurG, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
|
| 221 |
+
threshCnt, threshHier2 = cv2.findContours(thresh, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
|
| 222 |
+
outlinesDotIN = np.zeros(img.shape[:2], dtype="uint8")
|
| 223 |
+
for cnt in threshCnt:
|
| 224 |
+
area1 = cv2.contourArea(cnt)
|
| 225 |
+
if (area1 > 2000 ):
|
| 226 |
+
cv2.drawContours(outlinesDotIN,[cnt],0,(255,255,255),2)
|
| 227 |
+
return outlinesDotIN
|
| 228 |
+
|
| 229 |
+
def connectsmallDot(blackwithNoDot):
|
| 230 |
+
imgLines= cv2.HoughLinesP(blackwithNoDot,0.05,np.pi/180,8,minLineLength=30,maxLineGap = 30) #was w-h , gap=150 #50
|
| 231 |
+
for i in range(len(imgLines)):
|
| 232 |
+
for x1,y1,x2,y2 in imgLines[i]:
|
| 233 |
+
cv2.line(blackwithNoDot,(x1,y1),(x2,y2),(255,255,255),2)
|
| 234 |
+
return blackwithNoDot
|
| 235 |
#OLD method (White shapes)
|
| 236 |
def preprocessold(img,number):
|
| 237 |
+
blurG = cv2.GaussianBlur(ChangeBrightness(img,6),(3,3),0 )
|
| 238 |
imgGry = cv2.cvtColor(blurG, cv2.COLOR_BGR2GRAY)
|
| 239 |
+
kernel=np.ones((3,3),np.uint8)
|
| 240 |
+
er1=cv2.dilate(imgGry,kernel, iterations=2) #thinning
|
| 241 |
+
if number == 0:
|
| 242 |
+
ret3, thresh = cv2.threshold(er1, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
|
| 243 |
+
else:
|
| 244 |
+
ret3, thresh = cv2.threshold(er1, 220, 255, cv2.THRESH_BINARY_INV) #`140 - 141
|
| 245 |
return thresh
|
| 246 |
+
|
| 247 |
#anding of old method output with solid lines img
|
| 248 |
+
def preprocess(dataDoc,imgtransparent1,img,number,green2,flag,layeredflag,BlackmaskDetected1=0):
|
| 249 |
#first preprocessing ( old method - black img with white shapes)
|
| 250 |
+
alltxts=[]
|
| 251 |
+
img1=preprocessold(img,number)
|
| 252 |
anding=cv2.bitwise_and(green2,green2,mask=img1)
|
| 253 |
anding = cv2.cvtColor(anding, cv2.COLOR_BGR2GRAY)
|
| 254 |
ret3, thresh2 = cv2.threshold(anding, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 255 |
+
|
| 256 |
if layeredflag.startswith('layer'):
|
| 257 |
thresh2=cv2.bitwise_and(thresh2,thresh2,mask=BlackmaskDetected1)
|
| 258 |
threshcontours, threshHier = cv2.findContours(thresh2, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
|
| 259 |
+
threshHier= threshHier[0]
|
| 260 |
+
outlinesDotIN=getOutlinesDotIN(img)
|
| 261 |
finalcnts=[]
|
| 262 |
+
perimeters=[]
|
| 263 |
+
openClosedFlag=0
|
| 264 |
+
threshCnt, threshHier2 = cv2.findContours(img1, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
|
| 265 |
+
outlines = np.zeros(img.shape[:2], dtype="uint8")
|
| 266 |
+
for cnt in threshCnt:
|
| 267 |
+
area1 = cv2.contourArea(cnt)
|
| 268 |
+
if (area1 > 2000 ):
|
| 269 |
+
cv2.drawContours(outlines,[cnt],0,(255,255,255),2)
|
| 270 |
+
pc_coor, listall=textLists(img,dataDoc)
|
| 271 |
+
perimeter=0
|
| 272 |
+
for cnt in threshcontours:
|
| 273 |
+
Blackmask = np.zeros(img.shape[:2], dtype="uint8")
|
| 274 |
area1 = cv2.contourArea(cnt)
|
| 275 |
if (area1 > 2000 ):
|
| 276 |
x, y , width, height = cv2.boundingRect(cnt)
|
| 277 |
+
textPoint=nearestText(int(x+(width/2)), int(y+(height/2)) , pc_coor)
|
| 278 |
+
txt=''
|
| 279 |
+
if(textPoint!='none'):
|
| 280 |
+
for textTuple in listall:
|
| 281 |
+
if textPoint[0]==textTuple[0] and textPoint[1]==textTuple[1]:
|
| 282 |
+
txt=textTuple[2]
|
| 283 |
+
alltxts.append(txt)
|
| 284 |
+
elif textPoint=='none':
|
| 285 |
+
alltxts.append('none')
|
| 286 |
if flag.startswith('perimeter'):
|
| 287 |
kernel=np.ones((2,2),np.uint8)
|
| 288 |
+
cv2.drawContours(Blackmask,[cnt],0,(255,255,255), 6)
|
| 289 |
Blackmask=cv2.dilate(Blackmask,kernel, iterations=1)
|
| 290 |
+
if 'GB' in txt:
|
| 291 |
+
cBlk,_= cv2.findContours(Blackmask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 292 |
+
xcBlk, ycBlk , width, height = cv2.boundingRect(cBlk[0])
|
| 293 |
+
xx=cv2.bitwise_and(outlines,outlines,mask=Blackmask)
|
| 294 |
+
xx = cv2.threshold(xx, 250, 255, cv2.THRESH_BINARY)[1]
|
| 295 |
+
cntsx,hierx= cv2.findContours(xx, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
|
| 296 |
+
if len(cntsx)>0:
|
| 297 |
+
hierx=hierx[0]
|
| 298 |
+
xx=cv2.bitwise_and(outlinesDotIN,outlinesDotIN,mask=xx)
|
| 299 |
+
cntsx,hierx= cv2.findContours(xx, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
|
| 300 |
+
if len(cntsx)>0:
|
| 301 |
+
xx=connectsmallDot(xx)
|
| 302 |
+
cntsx,hierx= cv2.findContours(xx, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
|
| 303 |
+
if len(cntsx)>0:
|
| 304 |
+
hierx=hierx[0]
|
| 305 |
+
for comp in zip(cntsx,hierx):
|
| 306 |
+
c=comp[0]
|
| 307 |
+
h=comp[1]
|
| 308 |
+
xc, yc , width, height = cv2.boundingRect(c)
|
| 309 |
+
perimeter=cv2.arcLength(c,True)
|
| 310 |
+
shape=[]
|
| 311 |
+
approx = cv2.approxPolyDP(c, 0.003* perimeter, True)
|
| 312 |
+
if h[2]<0 and h[3] <0:
|
| 313 |
+
perimeter1 = cv2.arcLength(approx, True)
|
| 314 |
+
perimeter=perimeter1/2
|
| 315 |
+
# cv2_imshow(xx)
|
| 316 |
+
openClosedFlag='open'
|
| 317 |
+
imgtransparent1=cv2.polylines(imgtransparent1, [approx], False, (0,255,0), thickness=4,lineType=8)
|
| 318 |
+
|
| 319 |
+
perimeters.append([xc, yc ,xcBlk, ycBlk ,perimeter ,openClosedFlag , txt , approx])
|
| 320 |
+
else:
|
| 321 |
+
if h[2] >0:
|
| 322 |
+
openClosedFlag='closed'
|
| 323 |
+
if flag.startswith('area'):
|
| 324 |
kernel=np.ones((2,2),np.uint8)
|
| 325 |
+
cv2.drawContours(Blackmask,[cnt],0,(255,255,255), 5)
|
| 326 |
+
Blackmask=cv2.erode(Blackmask,kernel, iterations=1)
|
| 327 |
+
openClosedFlag='none'
|
| 328 |
contours1, hier1 = cv2.findContours(Blackmask, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
|
| 329 |
finalcnts.append(contours1)
|
| 330 |
finalcnts=tuple(finalcnts)
|
| 331 |
+
# ###############
|
| 332 |
+
|
| 333 |
+
return finalcnts, perimeters , alltxts , imgtransparent1
|
| 334 |
|
| 335 |
"""# ROI (levels)
|
| 336 |
## Detect regions with specific color and mask them
|
|
|
|
| 510 |
|
| 511 |
"""# contours"""
|
| 512 |
|
| 513 |
+
def findContoursFullImage(dataDoc,imgtransparent1,green2,img,number,finalColorArray,ratioarea,ratioperim,flag,color=[0,0,0]):
|
| 514 |
if number == 0:
|
| 515 |
+
contourss, perimeters ,alltxts , imgtransparent1=preprocess(dataDoc,imgtransparent1,img,number,green2,flag, 'nolayer')
|
| 516 |
+
return contourss , perimeters, alltxts , imgtransparent1
|
|
|
|
|
|
|
| 517 |
else:
|
| 518 |
mask, detectedColors, rgbcolor =DetectColor(img,color)
|
|
|
|
|
|
|
| 519 |
pil_image=Image.fromarray(mask)
|
|
|
|
| 520 |
extrema = pil_image.convert("L").getextrema()
|
| 521 |
if extrema != (0, 0): # if image is not black --> has a colored mask within
|
| 522 |
coloredregions,invertedmask,BlackmaskDetected1=getColoredContour(mask,img,finalColorArray,ratioarea,ratioperim,flag,color)
|
| 523 |
+
|
| 524 |
+
contourss,perimeters , alltxts , imgtransparent1=preprocess(dataDoc,imgtransparent1,coloredregions,number,green2,flag,'layer',BlackmaskDetected1)
|
| 525 |
+
return contourss,rgbcolor ,invertedmask , perimeters , alltxts , imgtransparent1
|
| 526 |
else:
|
| 527 |
+
contourss, perimeters , alltxts , imgtransparent1=preprocess(dataDoc,imgtransparent1,img,number,green2,flag,'nolayer')
|
| 528 |
+
return contourss,color ,mask , perimeters , alltxts
|
|
|
|
| 529 |
|
| 530 |
#Straighten tilted shapes
|
| 531 |
def StraightenImage(contour,imgArea):
|
| 532 |
rect = cv2.minAreaRect(contour)
|
|
|
|
| 533 |
(center, (width, height), angleR) = cv2.minAreaRect(contour)
|
|
|
|
| 534 |
box = cv2.boxPoints(rect)
|
| 535 |
box = np.int0(box)
|
|
|
|
| 536 |
# get width and height of the detected rectangle
|
| 537 |
width = int(rect[1][0])
|
| 538 |
height = int(rect[1][1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 539 |
return angleR,width,height
|
| 540 |
+
|
| 541 |
#get all areas and perimeter present
|
| 542 |
+
def getAreasPerimeter(img,ratioarea,ratioperim,contourss,contourssA):
|
| 543 |
appended=[]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 544 |
for contour in range(len(contourss)):
|
| 545 |
+
area1 = cv2.contourArea(contourss[contour][0])
|
| 546 |
+
if (area1 >2000 ):
|
| 547 |
+
perimeter= cv2.arcLength(contourss[contour][0],True)
|
| 548 |
+
approx = cv2.approxPolyDP(contourss[contour][0], 0.01* perimeter, True)
|
| 549 |
+
perimeter1 = cv2.arcLength(approx, True)
|
| 550 |
+
approx = cv2.approxPolyDP(contourssA[contour][0], 0.0002* perimeter, True)
|
| 551 |
+
area1 = cv2.contourArea(approx)
|
| 552 |
+
x, y , width, height = cv2.boundingRect(contourss[contour][0])
|
| 553 |
+
angleR,widthR ,heightR= StraightenImage(contourss[contour][0],img)
|
| 554 |
+
|
| 555 |
+
if (angleR != 90.0 and angleR != -90.0 and angleR != 0.0 and angleR != -0.0 ): #inclined b ay degree
|
| 556 |
+
width=widthR
|
| 557 |
+
height=heightR
|
| 558 |
+
if (area1 > 2000 ): #check perimeter kman fl condition -- 2800
|
| 559 |
+
if ratioarea!=0 and ratioperim!=0:
|
| 560 |
+
areaa=round(area1* ratioarea , 2) # true value of area of any shape/ area px value of same shape
|
| 561 |
+
appended.append([areaa,width,height])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 562 |
return appended
|
| 563 |
#fill dictionary with areas and perimeters and occurences
|
| 564 |
+
def FillDictionary(SimilarAreaDictionary,img,ratioarea,ratioperim,contourss,contourssA,rgbcolor=[0,0,0],color=[0,0,0]):
|
| 565 |
#fills dictionary with key areas and number of occurences
|
| 566 |
+
areas_Perimeters=sorted(getAreasPerimeter(img,ratioarea,ratioperim,contourss,contourssA))
|
| 567 |
+
|
| 568 |
indices=[]
|
| 569 |
+
colorRanges=[[255,0,0],[0,0,255],[0,255,255],[0,64,0],[255,204,0],[255,128,64],[255,0,128],[255,128,192],[128,128,255],[128,64,0],[0,255,0],[179,106,179],[115,52,179],[0,128,192],[128,0,128],[128,0,0],[0,128,255],[255,182,128],[255,0,255],[0,0,128],[0,128,64],[255,255,0],[128,0,64],[203,203,106],[128,255,166],[255,128,0],[255,98,98],[90,105,138],[114,10,72],[36,82,78],[225,105,29],[108,62,40],[11,35,75],[42,176,203],[255,153,153],[129,74,138],[99,123,137],[159,179,30],[255,0,0],[0,0,255],[0,255,255],[0,64,0],[255,204,0],[255,128,64],[255,0,128],[255,128,192],[128,128,255],[128,64,0],[0,255,0],[179,106,179],[115,52,179],[0,128,192],[128,0,128],[128,0,0],[0,128,255],[255,182,128],[255,0,255],[0,0,128],[0,128,64],[255,255,0],[128,0,64],[203,203,106],[128,255,166],[255,128,0],[255,98,98],[90,105,138],[114,10,72],[36,82,78],[225,105,29],[108,62,40],[11,35,75],[42,176,203],[255,153,153],[129,74,138],[99,123,137],[159,179,30]]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 570 |
colorsUsed=[]
|
| 571 |
for i in range(len(areas_Perimeters)):
|
| 572 |
+
|
| 573 |
# colorRGB=hexRGB(color)
|
| 574 |
item1 = areas_Perimeters[i][0]
|
| 575 |
width1 = areas_Perimeters[i][1]
|
| 576 |
height1 = areas_Perimeters[i][2]
|
| 577 |
+
widthMin= width1-15
|
| 578 |
+
widthMax= width1+15
|
| 579 |
+
heightMin=height1-15
|
| 580 |
+
heightMax= height1+15
|
| 581 |
+
areaPerimeterMin= round(item1,1) - 0.4
|
| 582 |
+
areaPerimeterMax= round(item1,1) + 0.4
|
| 583 |
# print (areaMin, areaMax)
|
| 584 |
+
if color != [0,0,0]: #colored images
|
| 585 |
+
mydata=[[],[rgbcolor[0],rgbcolor[1],rgbcolor[2] ],round(item1,1),width1,height1,1, 0,0,0,0,0,0,0,0,0]
|
|
|
|
|
|
|
|
|
|
| 586 |
else:
|
| 587 |
+
mydata=[[],' ', round(item1,1),width1,height1,1, 0,0,0,0,0,0,0,0,0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 588 |
myindex= SimilarAreaDictionary.index[((SimilarAreaDictionary['Rounded'] >=areaPerimeterMin) &(SimilarAreaDictionary['Rounded']<=areaPerimeterMax) )].tolist()
|
|
|
|
|
|
|
| 589 |
if color!= [0,0,0]: #leveled image
|
|
|
|
| 590 |
checkifColorExists=0 # to check whether this row was found or not( area and color )
|
| 591 |
for i in myindex: # loop on indices that were found --> rows containing this area to check its color and add occ.
|
| 592 |
+
if SimilarAreaDictionary['Color'].loc[i]==[rgbcolor[0],rgbcolor[1],rgbcolor[2]] and ( SimilarAreaDictionary['Rounded'].loc[i] >= areaPerimeterMin and SimilarAreaDictionary['Rounded'].loc[i] <= areaPerimeterMax) :
|
| 593 |
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 ) :
|
| 594 |
checkifColorExists=1 #found and incremented
|
| 595 |
SimilarAreaDictionary['Occurences'].loc[i]+=1
|
| 596 |
+
if checkifColorExists==0: #couldnt find the color , doesnt exist so add it
|
| 597 |
SimilarAreaDictionary.loc[len(SimilarAreaDictionary)] =mydata
|
|
|
|
| 598 |
else: #full image
|
| 599 |
+
#same code bs mgher color
|
|
|
|
| 600 |
checkifColorExists=0
|
| 601 |
+
for i in myindex:
|
| 602 |
if ( SimilarAreaDictionary['Rounded'].loc[i] <= areaPerimeterMax and SimilarAreaDictionary['Rounded'].loc[i] >= areaPerimeterMin) :
|
|
|
|
| 603 |
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 ) :
|
| 604 |
checkifColorExists=1 #found and incremented
|
| 605 |
SimilarAreaDictionary['Occurences'].loc[i]+=1
|
| 606 |
+
if checkifColorExists==0: #couldnt find the color , doesnt exist so add it
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 607 |
SimilarAreaDictionary.loc[len(SimilarAreaDictionary)] =mydata
|
| 608 |
# s= SimilarAreaDictionary
|
| 609 |
for i in range(len(SimilarAreaDictionary)):
|
| 610 |
+
SimilarAreaDictionary.loc[i, "R"] = colorRanges[i][0]
|
| 611 |
+
SimilarAreaDictionary.loc[i, "G"] = colorRanges[i][1]
|
| 612 |
+
SimilarAreaDictionary.loc[i, "B"] = colorRanges[i][2]
|
| 613 |
colorsUsed.append(colorRanges[i])
|
| 614 |
|
|
|
|
| 615 |
return SimilarAreaDictionary, colorsUsed , areas_Perimeters
|
| 616 |
#detect and draw and measure
|
| 617 |
def drawAllContours(dataDoc,img,number,finalColorArray,ratioarea,ratioperim,flag , path,pdfpath):
|
| 618 |
green2=allpreSteps(img)
|
| 619 |
doc = fitz.open("pdf",dataDoc)
|
| 620 |
page = doc[0]
|
| 621 |
+
rotationOld=page.rotation
|
| 622 |
pix=page.get_pixmap()
|
| 623 |
+
if page.rotation!=0:
|
| 624 |
+
page.set_rotation(0)
|
| 625 |
+
ratio = pix.width/ img.shape[0]
|
| 626 |
+
else:
|
| 627 |
+
ratio = pix.width/ img.shape[1]
|
| 628 |
+
|
| 629 |
areasinImage=[]
|
| 630 |
imgArea1= img.copy()
|
| 631 |
imgPerimeter1=img.copy()
|
| 632 |
imgtransparent1=img.copy()
|
|
|
|
| 633 |
|
| 634 |
if number ==220:
|
| 635 |
+
SimilarAreaDictionary= pd.DataFrame(columns=['Guess','Color','Rounded','Width','Height','Occurences','Area','Total Area','Perimeter','Total Perimeter','Length','Total Length','R','G','B'])
|
| 636 |
firstcolor=finalColorArray[0]
|
| 637 |
counter=0
|
| 638 |
maskDone=img.copy()
|
| 639 |
for eachcolor in finalColorArray:
|
| 640 |
|
| 641 |
+
contourss,rgbcolor,invertedmask , perimeters , alltxts,imgtransparent1=findContoursFullImage(dataDoc,imgtransparent1,green2,maskDone,number,finalColorArray,ratioarea,ratioperim,'perimeter',eachcolor)
|
| 642 |
+
contourssA,rgbcolor,invertedmask, perimeters , alltxts,imgtransparent1=findContoursFullImage(dataDoc,imgtransparent1,green2,maskDone,number,finalColorArray,ratioarea,ratioperim,'area',eachcolor)
|
|
|
|
| 643 |
|
| 644 |
+
SimilarAreaDictionary, colorsUsed , areas_Perimeters= FillDictionary(SimilarAreaDictionary,maskDone,ratioarea,ratioperim,contourss,contourssA,rgbcolor,eachcolor)
|
| 645 |
+
perimTotal=0
|
|
|
|
|
|
|
| 646 |
for contour in range(len(contourss)):
|
| 647 |
shape=[]
|
| 648 |
|
|
|
|
| 651 |
|
| 652 |
# perimeter1 = cv2.arcLength(contourss[contour][0], True)
|
| 653 |
if (area1 > 3500 ): #check perimeter kman fl condition -- 2800
|
| 654 |
+
perimeter=cv2.arcLength(contourss[contour][0],True)
|
| 655 |
+
shape=[]
|
| 656 |
angleR,widthR ,heightR= StraightenImage(contourss[contour][0],imgArea1)
|
| 657 |
x, y , width, height = cv2.boundingRect(contourss[contour][0])
|
| 658 |
+
approxA = cv2.approxPolyDP(contourssA[contour][0], 0.0002* perimeter, True)
|
| 659 |
+
area1 = cv2.contourArea(approxA)
|
| 660 |
|
| 661 |
+
approx = cv2.approxPolyDP(contourss[contour][0], 0.01 * perimeter, True) #0.0009
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 662 |
perimeter1 = cv2.arcLength(approx, True)
|
| 663 |
+
for point in approxA:
|
| 664 |
+
x1, y1 = point[0]
|
| 665 |
+
p1 = fitz.Point(x1*ratio,y1*ratio)
|
| 666 |
+
p1=p1*page.derotation_matrix
|
| 667 |
+
shape.append([p1[0],p1[1]])
|
| 668 |
+
|
|
|
|
| 669 |
if (angleR != 90.0 and angleR != -90.0 and angleR != 0.0 and angleR != -0.0 ): #inclined b ay degree
|
| 670 |
width=widthR
|
| 671 |
height=heightR
|
|
|
|
| 678 |
heightMin=height-15
|
| 679 |
heightMax= height+15
|
| 680 |
if ratioarea !=0 and ratioperim!=0:
|
|
|
|
|
|
|
|
|
|
| 681 |
areaa=round(area1* ratioarea, 3) # true value of area of any shape/ area px value of same shape
|
| 682 |
perimeterr=round(perimeter1* ratioperim,3)
|
| 683 |
lengthShape=round(lengthShape* ratioperim,3)
|
|
|
|
| 685 |
areaa=area1
|
| 686 |
perimeterr=perimeter1
|
| 687 |
|
| 688 |
+
areaPerimeterMin= round(areaa,1) - 0.4
|
| 689 |
+
areaPerimeterMax= round(areaa,1) + 0.4
|
|
|
|
|
|
|
|
|
|
| 690 |
masked=SimilarAreaDictionary.loc[SimilarAreaDictionary.index[((SimilarAreaDictionary['Rounded'] >=areaPerimeterMin) &(SimilarAreaDictionary['Rounded']<=areaPerimeterMax) )]]
|
|
|
|
|
|
|
| 691 |
passed=0
|
| 692 |
for i, row in masked.iterrows():
|
| 693 |
if passed ==0:
|
| 694 |
if SimilarAreaDictionary['Color'].loc[i] == [rgbcolor[0],rgbcolor[1],rgbcolor[2]] and ( SimilarAreaDictionary['Rounded'].loc[i] <= areaPerimeterMax and SimilarAreaDictionary['Rounded'].loc[i] >= areaPerimeterMin) :
|
| 695 |
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 ) :
|
| 696 |
+
SimilarAreaDictionary['Total Area'].loc[i]+=areaa
|
| 697 |
+
SimilarAreaDictionary['Area'].loc[i]=areaa
|
| 698 |
+
SimilarAreaDictionary['Guess'].loc[i].append(str(alltxts[contour]))
|
| 699 |
+
pFlagDF=0
|
| 700 |
+
color= (int(SimilarAreaDictionary['R'].loc[i])/255 , int(SimilarAreaDictionary['G'].loc[i])/255 , int(SimilarAreaDictionary['B'].loc[i])/255 )
|
| 701 |
+
|
| 702 |
+
|
| 703 |
+
for p in perimeters:
|
| 704 |
+
if p[2]==x and p[3]==y and p[5]=='open':
|
| 705 |
+
# if areaa >=5.15 and areaa<=5.25:
|
| 706 |
+
shapee=[]
|
| 707 |
+
SimilarAreaDictionary['Total Perimeter'].loc[i]+=round((p[4]-1)*ratioperim,3)
|
| 708 |
+
SimilarAreaDictionary['Perimeter'].loc[i]=round((p[4]-1)*ratioperim,3)
|
| 709 |
+
for poi in p[7]:
|
| 710 |
+
x1, y1 = poi[0]
|
| 711 |
+
p1 = fitz.Point(x1*ratio,y1*ratio)
|
| 712 |
+
p1=p1*page.derotation_matrix
|
| 713 |
+
shapee.append([p1[0],p1[1]])
|
| 714 |
+
|
| 715 |
+
annot11 = page.add_polyline_annot( points=shapee) # 'Polygon'
|
| 716 |
+
annot11.set_border(width=0.2, dashes=[3])
|
| 717 |
+
annot1.set_colors(stroke=color ,fill=None)
|
| 718 |
+
annot11.set_info(content='Perimeter='+str(round((p[4]-1)*ratioperim,3))+' m',subject='ADR Team',title=str(alltxts[contour]))
|
| 719 |
+
# annot.set_line_ends(fitz.PDF_ANNOT_LE_DIAMOND, fitz.PDF_ANNOT_LE_CIRCLE)
|
| 720 |
+
annot11.update()
|
| 721 |
+
pFlagDF=1
|
| 722 |
+
if pFlagDF==0:
|
| 723 |
+
annot1 = page.add_polyline_annot( points=shape) # 'Polygon'
|
| 724 |
+
annot1.set_border(width=0.2 ,dashes=[3])
|
| 725 |
+
annot1.set_colors(stroke=color , fill=None)
|
| 726 |
+
|
| 727 |
+
annot1.set_info(content='Perimeter='+str(perimeterr)+' m',subject='ADR Team',title=str(alltxts[contour]))
|
| 728 |
SimilarAreaDictionary['Total Perimeter'].loc[i]+=perimeterr
|
| 729 |
+
SimilarAreaDictionary['Perimeter'].loc[i]=perimeterr
|
| 730 |
+
SimilarAreaDictionary['Total Length'].loc[i]+=lengthShape
|
| 731 |
+
SimilarAreaDictionary['Length'].loc[i]=lengthShape
|
| 732 |
+
|
| 733 |
+
passed=1
|
| 734 |
+
cv2.drawContours(imgArea1, [contourss[contour][0]], 0, ( int(SimilarAreaDictionary['B'].loc[i]), int(SimilarAreaDictionary['G'].loc[i]), int(SimilarAreaDictionary['R'].loc[i])), -1)
|
| 735 |
+
annot = page.add_polygon_annot( points=shape) # 'Polygon'
|
| 736 |
+
annot.set_border(width=0.2)
|
| 737 |
+
annot.set_colors(stroke=color, fill= color )
|
| 738 |
+
annot.set_opacity(0.5)
|
| 739 |
+
annot.set_info(content='Area='+str(areaa)+' m2' +'\n \n Length='+str(lengthShape)+' m',subject='ADR Team',title=str(alltxts[contour]))
|
| 740 |
+
# annot.set_line_ends(fitz.PDF_ANNOT_LE_DIAMOND, fitz.PDF_ANNOT_LE_CIRCLE)
|
| 741 |
+
annot.update()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 742 |
|
| 743 |
+
# areasinImage.append(areaa)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 744 |
cv2.putText(imgPerimeter1,'Perimeter: '+str(perimeterr)+ ' m', (x+70,y-30) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
|
| 745 |
cv2.putText(imgPerimeter1,'Area: '+str(areaa)+' m2', (x+50,y-40) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
|
| 746 |
|
|
|
|
| 755 |
#########################
|
| 756 |
else:
|
| 757 |
|
| 758 |
+
SimilarAreaDictionary= pd.DataFrame(columns=['Guess','Color','Rounded','Width','Height','Occurences','Area','Total Area','Perimeter','Total Perimeter','Length','Total Length','R','G','B'])
|
| 759 |
+
contourss , perimeters , alltxts , imgtransparent1=findContoursFullImage(dataDoc,imgtransparent1,green2,img,number,finalColorArray,ratioarea,ratioperim,'perimeter')
|
| 760 |
+
contourssA , perimetersA , alltxts , imgtransparent1=findContoursFullImage(dataDoc,imgtransparent1,green2,img,number,finalColorArray,ratioarea,ratioperim,'area')
|
| 761 |
+
SimilarAreaDictionary,colorsUsed , areas_Perimeters= FillDictionary(SimilarAreaDictionary,img,ratioarea,ratioperim,contourss,contourssA)
|
| 762 |
+
|
| 763 |
for contour in range(len(contourss)):
|
|
|
|
| 764 |
area1 = cv2.contourArea(contourssA[contour][0])
|
| 765 |
+
if (area1 >4000 ):
|
| 766 |
+
perimeter=cv2.arcLength(contourss[contour][0],True)
|
| 767 |
+
|
| 768 |
|
| 769 |
shape=[]
|
| 770 |
angleR,widthR ,heightR= StraightenImage(contourss[contour][0],imgArea1)
|
| 771 |
x, y , width, height = cv2.boundingRect(contourss[contour][0])
|
| 772 |
+
approxA = cv2.approxPolyDP(contourssA[contour][0], 0.0002* perimeter, True)
|
| 773 |
+
area1 = cv2.contourArea(approxA)
|
| 774 |
+
approx = cv2.approxPolyDP(contourss[contour][0], 0.01 * perimeter, True) #0.0009
|
|
|
|
|
|
|
| 775 |
perimeter1 = cv2.arcLength(approx, True)
|
| 776 |
+
for point in approxA:
|
| 777 |
x1, y1 = point[0]
|
| 778 |
p1 = fitz.Point(x1*ratio,y1*ratio)
|
| 779 |
p1=p1*page.derotation_matrix
|
|
|
|
| 797 |
lengthShape=round(lengthShape* ratioperim,3)
|
| 798 |
else:
|
| 799 |
perimeterr=perimeter1
|
| 800 |
+
areaPerimeterMin= round(areaa,1) - 0.4
|
| 801 |
+
areaPerimeterMax= round(areaa,1) + 0.4
|
| 802 |
masked=SimilarAreaDictionary.loc[SimilarAreaDictionary.index[((SimilarAreaDictionary['Rounded'] >=areaPerimeterMin) & (SimilarAreaDictionary['Rounded']<=areaPerimeterMax) )]]
|
| 803 |
passed=0
|
| 804 |
for i, row in masked.iterrows():
|
|
|
|
| 807 |
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 ) :
|
| 808 |
SimilarAreaDictionary['Total Area'].loc[i]+=areaa
|
| 809 |
SimilarAreaDictionary['Area'].loc[i]=areaa
|
| 810 |
+
SimilarAreaDictionary['Guess'].loc[i].append(str(alltxts[contour]))
|
| 811 |
+
pFlagDF=0
|
| 812 |
+
color= (int(SimilarAreaDictionary['R'].loc[i])/255 , int(SimilarAreaDictionary['G'].loc[i])/255 , int(SimilarAreaDictionary['B'].loc[i])/255 )
|
| 813 |
+
|
| 814 |
+
|
| 815 |
+
for p in perimeters:
|
| 816 |
+
if p[2]==x and p[3]==y and p[5]=='open':
|
| 817 |
+
# if areaa >=5.15 and areaa<=5.25:
|
| 818 |
+
shapee=[]
|
| 819 |
+
SimilarAreaDictionary['Total Perimeter'].loc[i]+=round((p[4]-1)*ratioperim,3)
|
| 820 |
+
SimilarAreaDictionary['Perimeter'].loc[i]=round((p[4]-1)*ratioperim,3)
|
| 821 |
+
for poi in p[7]:
|
| 822 |
+
x1, y1 = poi[0]
|
| 823 |
+
p1 = fitz.Point(x1*ratio,y1*ratio)
|
| 824 |
+
p1=p1*page.derotation_matrix
|
| 825 |
+
shapee.append([p1[0],p1[1]])
|
| 826 |
+
|
| 827 |
+
annot11 = page.add_polyline_annot( points=shapee) # 'Polygon'
|
| 828 |
+
annot11.set_border(width=0.2, dashes=[3])
|
| 829 |
+
annot1.set_colors(stroke=color ,fill=None)
|
| 830 |
+
annot11.set_info(content='Perimeter='+str(round((p[4]-1)*ratioperim,3))+' m',subject='ADR Team',title=str(alltxts[contour]))
|
| 831 |
+
# annot.set_line_ends(fitz.PDF_ANNOT_LE_DIAMOND, fitz.PDF_ANNOT_LE_CIRCLE)
|
| 832 |
+
annot11.update()
|
| 833 |
+
pFlagDF=1
|
| 834 |
+
if pFlagDF==0:
|
| 835 |
+
annot1 = page.add_polyline_annot( points=shape) # 'Polygon'
|
| 836 |
+
annot1.set_border(width=0.2 ,dashes=[3])
|
| 837 |
+
annot1.set_colors(stroke=color ,fill=None)
|
| 838 |
+
annot1.set_info(content='Perimeter='+str(perimeterr)+' m',subject='ADR Team',title=str(alltxts[contour]))
|
| 839 |
+
SimilarAreaDictionary['Total Perimeter'].loc[i]+=perimeterr
|
| 840 |
+
SimilarAreaDictionary['Perimeter'].loc[i]=perimeterr
|
| 841 |
SimilarAreaDictionary['Total Length'].loc[i]+=lengthShape
|
| 842 |
SimilarAreaDictionary['Length'].loc[i]=lengthShape
|
| 843 |
|
| 844 |
passed=1
|
| 845 |
cv2.drawContours(imgArea1, [contourss[contour][0]], 0, ( int(SimilarAreaDictionary['B'].loc[i]), int(SimilarAreaDictionary['G'].loc[i]), int(SimilarAreaDictionary['R'].loc[i])), -1)
|
| 846 |
+
annot = page.add_polygon_annot( points=shape) # 'Polygon'
|
| 847 |
+
annot.set_border(width=0.2)
|
| 848 |
+
annot.set_colors(stroke=color, fill= color )
|
|
|
|
|
|
|
|
|
|
| 849 |
annot.set_opacity(0.5)
|
| 850 |
+
annot.set_info(content='Area='+str(areaa)+' m2' +'\n \n Length='+str(lengthShape)+' m',subject='ADR Team',title=str(alltxts[contour]))
|
| 851 |
# annot.set_line_ends(fitz.PDF_ANNOT_LE_DIAMOND, fitz.PDF_ANNOT_LE_CIRCLE)
|
| 852 |
annot.update()
|
| 853 |
+
|
| 854 |
+
cv2.putText(imgtransparent1,'Area: '+str(areaa) +' m2', (x+50,y-10) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
|
| 855 |
+
pFlag=0
|
| 856 |
+
|
| 857 |
+
for p in perimeters:
|
| 858 |
+
if p[2]==x and p[3]==y and p[5]=='open':
|
| 859 |
+
# if areaa >=5.15 and areaa<=5.25:
|
| 860 |
+
perimTotal+=round((p[4]-1)*ratioperim,3)
|
| 861 |
+
cv2.putText(imgtransparent1,'Perimeter: '+str(round((p[4])*ratioperim,3))+ ' m', (p[0]+50,p[1]-40) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
|
| 862 |
+
pFlag=1
|
| 863 |
+
if pFlag==0:
|
| 864 |
+
cv2.putText(imgtransparent1,'Perimeter: '+str(perimeterr)+' m', (x+50,y-40) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
|
| 865 |
+
for i, row in SimilarAreaDictionary.iterrows():
|
| 866 |
+
c = Counter( SimilarAreaDictionary['Guess'].loc[i])
|
| 867 |
+
if len(c) >0:
|
| 868 |
+
value, count = c.most_common()[0]
|
| 869 |
+
SimilarAreaDictionary['Guess'].loc[i]= value
|
| 870 |
+
else:
|
| 871 |
+
SimilarAreaDictionary['Guess'].loc[i]= 'none'
|
| 872 |
+
|
| 873 |
|
| 874 |
+
# cv2.circle (imgtransparent1, (img.shape[0],img.shape[0]1), 5, 255, 5)
|
| 875 |
+
alpha = 0.4 # Transparency factor.
|
| 876 |
+
image_new1 = cv2.addWeighted(imgArea1, alpha, imgtransparent1, 1 - alpha, 0)
|
| 877 |
+
if rotationOld==90:
|
| 878 |
+
image_new1 = cv2.rotate(image_new1, cv2.ROTATE_90_CLOCKWISE)
|
| 879 |
+
if rotationOld==180:
|
| 880 |
+
image_new1 = cv2.rotate(image_new1, cv2.ROTATE_180)
|
| 881 |
+
if rotationOld==270:
|
| 882 |
+
image_new1 = cv2.rotate(image_new1, cv2.ROTATE_90_COUNTERCLOCKWISE)
|
| 883 |
alpha = 0.4 # Transparency factor.
|
| 884 |
image_new1 = cv2.addWeighted(imgArea1, alpha, imgtransparent1, 1 - alpha, 0)
|
| 885 |
|
|
|
|
| 1133 |
second_row_data=['Nr','m2','Total','m','Total','m','Total']
|
| 1134 |
if splittedpdfpath[-2].startswith('1.0') or splittedpdfpath[-2].startswith('3.2'):
|
| 1135 |
worksheet.update_row(3,second_row_data,col_offset=2)
|
| 1136 |
+
worksheet.update_col(1,list(SimilarAreaDictionary['Guess']),row_offset=3)
|
| 1137 |
worksheet.update_col(3,list(SimilarAreaDictionary['Occurences']),row_offset=3)
|
| 1138 |
worksheet.update_col(4,list(SimilarAreaDictionary['Area']),row_offset=3)
|
| 1139 |
worksheet.update_col(5,list(SimilarAreaDictionary['Total Area']),row_offset=3)
|