Spaces:
Sleeping
Sleeping
Update pilecaps_adr.py
Browse files- pilecaps_adr.py +39 -35
pilecaps_adr.py
CHANGED
|
@@ -218,17 +218,21 @@ def ChangeBrightness(img,k):
|
|
| 218 |
|
| 219 |
def preprocessold(img,number):
|
| 220 |
|
| 221 |
-
blurG =
|
| 222 |
imgGry = cv2.cvtColor(blurG, cv2.COLOR_BGR2GRAY)
|
| 223 |
kernel=np.ones((3,3),np.uint8)
|
| 224 |
er1=cv2.dilate(imgGry,kernel, iterations=2) #thinning
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
if number == 0:
|
| 226 |
-
ret3, thresh = cv2.threshold(er1,
|
| 227 |
else:
|
| 228 |
ret3, thresh = cv2.threshold(er1, 220, 255, cv2.THRESH_BINARY_INV) #`140 - 141
|
| 229 |
-
|
| 230 |
return thresh
|
| 231 |
|
|
|
|
| 232 |
def preprocess(imgOriginal,number,green2,flag,layeredflag,BlackmaskDetected1=0):
|
| 233 |
#first preprocessing ( old method - black img with white shapes)
|
| 234 |
img1=preprocessold(imgOriginal,number)
|
|
@@ -248,12 +252,12 @@ def preprocess(imgOriginal,number,green2,flag,layeredflag,BlackmaskDetected1=0):
|
|
| 248 |
x, y , width, height = cv2.boundingRect(cnt)
|
| 249 |
if flag.startswith('perimeter'):
|
| 250 |
kernel=np.ones((2,2),np.uint8)
|
| 251 |
-
cv2.drawContours(Blackmask,[cnt],0,(255,255,255),
|
| 252 |
-
Blackmask=cv2.dilate(Blackmask,kernel, iterations=
|
| 253 |
elif flag.startswith('area'):
|
| 254 |
kernel=np.ones((2,2),np.uint8)
|
| 255 |
-
cv2.drawContours(Blackmask,[cnt],0,(255,255,255),
|
| 256 |
-
Blackmask=cv2.
|
| 257 |
contours1, hier1 = cv2.findContours(Blackmask, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
|
| 258 |
finalcnts.append(contours1)
|
| 259 |
finalcnts=tuple(finalcnts)
|
|
@@ -591,8 +595,8 @@ def FillDictionary(green2,SimilarAreaDictionary,img,number,ratioarea,ratioperim,
|
|
| 591 |
widthMax= width1+10
|
| 592 |
heightMin=height1-10
|
| 593 |
heightMax= height1+10
|
| 594 |
-
areaPerimeterMin= round(item1,1) - 0.
|
| 595 |
-
areaPerimeterMax= round(item1,1) + 0.
|
| 596 |
# print (areaMin, areaMax)
|
| 597 |
if color != [0,0,0]: #colored images
|
| 598 |
|
|
@@ -683,7 +687,7 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
|
|
| 683 |
shape=[]
|
| 684 |
|
| 685 |
# cv2_imshow(imgStraight)
|
| 686 |
-
area1 = cv2.contourArea(
|
| 687 |
|
| 688 |
# perimeter1 = cv2.arcLength(contourss[contour][0], True)
|
| 689 |
if (area1 > 2000 ): #check perimeter kman fl condition -- 2800
|
|
@@ -699,9 +703,9 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
|
|
| 699 |
|
| 700 |
x, y , width, height = cv2.boundingRect(contourss[contour][0])
|
| 701 |
perimeter = cv2.arcLength(contourss[contour][0], True)
|
| 702 |
-
approx = cv2.approxPolyDP(contourssA[contour][0], 0.0002* perimeter, True)
|
| 703 |
-
area1 = cv2.contourArea(approx)
|
| 704 |
-
approx = cv2.approxPolyDP(contourss[contour][0], 0.
|
| 705 |
perimeter1 = cv2.arcLength(approx, True)
|
| 706 |
|
| 707 |
for point in approx:
|
|
@@ -735,8 +739,8 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
|
|
| 735 |
perimeterr=perimeter1
|
| 736 |
|
| 737 |
# if flag=='area':
|
| 738 |
-
areaPerimeterMin= round(areaa,1) - 0.
|
| 739 |
-
areaPerimeterMax= round(areaa,1) + 0.
|
| 740 |
# areaPerimeterMin= round(perimeterr,1) - 0.3
|
| 741 |
# areaPerimeterMax= round(perimeterr,1) + 0.3
|
| 742 |
masked=SimilarAreaDictionary.loc[SimilarAreaDictionary.index[((SimilarAreaDictionary['Rounded'] >=areaPerimeterMin) &(SimilarAreaDictionary['Rounded']<=areaPerimeterMax) )]]
|
|
@@ -745,7 +749,7 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
|
|
| 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
|
|
@@ -765,11 +769,11 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
|
|
| 765 |
annot.set_colors( fill=( int(SimilarAreaDictionary['R'].loc[i])/255 , int(SimilarAreaDictionary['G'].loc[i])/255 , int(SimilarAreaDictionary['B'].loc[i])/255 ) )
|
| 766 |
# annot.set_colors( fill=(1,0,1) )
|
| 767 |
annot.set_opacity(0.5)
|
| 768 |
-
annot.set_info(content='Area='+str(areaa)+' m2' +'\n \nPerimeter='+str(perimeterr)+' m',subject='ADR Team')#,title='uuum')
|
| 769 |
# annot.set_line_ends(fitz.PDF_ANNOT_LE_DIAMOND, fitz.PDF_ANNOT_LE_CIRCLE)
|
| 770 |
annot.update()
|
| 771 |
|
| 772 |
-
cv2.putText(imgtransparent1,'Area= '+str(area1) , (x+50,y-10) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
|
| 773 |
# cv2.putText(imgtransparent1,'Width= '+str(width) , (x+50,y-10) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
|
| 774 |
# cv2.putText(imgtransparent1,'Length= '+str(height) , (x+50,y-20) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
|
| 775 |
areasinImage.append(areaa)
|
|
@@ -778,12 +782,12 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
|
|
| 778 |
cv2.putText(imgPerimeter1,'Area: '+str(areaa)+' m2', (x+50,y-40) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
|
| 779 |
|
| 780 |
|
| 781 |
-
for i,row in SimilarAreaDictionary.iterrows():
|
| 782 |
-
|
| 783 |
-
|
| 784 |
-
|
| 785 |
|
| 786 |
-
print(SimilarAreaDictionary)
|
| 787 |
# display(totaldf)
|
| 788 |
#########################
|
| 789 |
else:
|
|
@@ -795,7 +799,7 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
|
|
| 795 |
# print('filled')
|
| 796 |
for contour in range(len(contourss)):
|
| 797 |
# imgArea1=img.copy()
|
| 798 |
-
area1 = cv2.contourArea(
|
| 799 |
if (area1 >2000 ):
|
| 800 |
perimeter = cv2.arcLength(contourss[contour][0],True)
|
| 801 |
|
|
@@ -804,9 +808,9 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
|
|
| 804 |
x, y , width, height = cv2.boundingRect(contourss[contour][0])
|
| 805 |
# approxo = cv2.approxPolyDP(contourss[contour],0.1*cv2.arcLength(contourss[contour],True),True)
|
| 806 |
# approx = cv2.approxPolyDP(contourss[contour][0], 0.005 * perimeter, True)
|
| 807 |
-
approxa = cv2.approxPolyDP(contourssA[contour][0], 0.0002* perimeter, True)
|
| 808 |
-
area1 = cv2.contourArea(approxa)
|
| 809 |
-
approx = cv2.approxPolyDP(contourss[contour][0], 0.
|
| 810 |
perimeter1 = cv2.arcLength(approx, True)
|
| 811 |
for point in approx:
|
| 812 |
x1, y1 = point[0]
|
|
@@ -821,19 +825,19 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
|
|
| 821 |
if height>width:
|
| 822 |
lengthShape = height
|
| 823 |
|
| 824 |
-
widthMin= width-
|
| 825 |
-
widthMax= width+
|
| 826 |
-
heightMin=height-
|
| 827 |
-
heightMax= height+
|
| 828 |
|
| 829 |
if ratioarea !=0 and ratioperim!=0:
|
| 830 |
-
areaa=area1* ratioarea # true value of area of any shape/ area px value of same shape
|
| 831 |
perimeterr=round(perimeter1* ratioperim,3)
|
| 832 |
lengthShape=round(lengthShape* ratioperim,3)
|
| 833 |
else:
|
| 834 |
perimeterr=perimeter1
|
| 835 |
-
areaPerimeterMin= round(areaa,1) - 0.
|
| 836 |
-
areaPerimeterMax= round(areaa,1) + 0.
|
| 837 |
masked=SimilarAreaDictionary.loc[SimilarAreaDictionary.index[((SimilarAreaDictionary['Rounded'] >=areaPerimeterMin) & (SimilarAreaDictionary['Rounded']<=areaPerimeterMax) )]]
|
| 838 |
passed=0
|
| 839 |
for i, row in masked.iterrows():
|
|
@@ -858,7 +862,7 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
|
|
| 858 |
annot.set_colors( fill=( int(SimilarAreaDictionary['R'].loc[i])/255 , int(SimilarAreaDictionary['G'].loc[i])/255 , int(SimilarAreaDictionary['B'].loc[i])/255 ) )
|
| 859 |
# annot.set_colors( fill=(1,0,1) )
|
| 860 |
annot.set_opacity(0.5)
|
| 861 |
-
annot.set_info(content='Area='+str(areaa)+' m2' +'\n \nPerimeter='+str(perimeterr)+' m',subject='ADR Team')#,title='uuum')
|
| 862 |
# annot.set_line_ends(fitz.PDF_ANNOT_LE_DIAMOND, fitz.PDF_ANNOT_LE_CIRCLE)
|
| 863 |
annot.update()
|
| 864 |
|
|
|
|
| 218 |
|
| 219 |
def preprocessold(img,number):
|
| 220 |
|
| 221 |
+
blurG = ChangeBrightness(img,3)
|
| 222 |
imgGry = cv2.cvtColor(blurG, cv2.COLOR_BGR2GRAY)
|
| 223 |
kernel=np.ones((3,3),np.uint8)
|
| 224 |
er1=cv2.dilate(imgGry,kernel, iterations=2) #thinning
|
| 225 |
+
kernel=np.ones((2,2),np.uint8)
|
| 226 |
+
er1=cv2.erode(er1,kernel, iterations=3) #thicken
|
| 227 |
+
# er1=cv2.dilate(er1,kernel, iterations=1) #thicken
|
| 228 |
+
# er1=cv2.dilate(er1,kernel, iterations=1) #thicken
|
| 229 |
if number == 0:
|
| 230 |
+
ret3, thresh = cv2.threshold(er1, 254, 255, cv2.THRESH_BINARY_INV )
|
| 231 |
else:
|
| 232 |
ret3, thresh = cv2.threshold(er1, 220, 255, cv2.THRESH_BINARY_INV) #`140 - 141
|
|
|
|
| 233 |
return thresh
|
| 234 |
|
| 235 |
+
|
| 236 |
def preprocess(imgOriginal,number,green2,flag,layeredflag,BlackmaskDetected1=0):
|
| 237 |
#first preprocessing ( old method - black img with white shapes)
|
| 238 |
img1=preprocessold(imgOriginal,number)
|
|
|
|
| 252 |
x, y , width, height = cv2.boundingRect(cnt)
|
| 253 |
if flag.startswith('perimeter'):
|
| 254 |
kernel=np.ones((2,2),np.uint8)
|
| 255 |
+
cv2.drawContours(Blackmask,[cnt],0,(255,255,255), 5)
|
| 256 |
+
Blackmask=cv2.dilate(Blackmask,kernel, iterations=2)
|
| 257 |
elif flag.startswith('area'):
|
| 258 |
kernel=np.ones((2,2),np.uint8)
|
| 259 |
+
cv2.drawContours(Blackmask,[cnt],0,(255,255,255), 4)
|
| 260 |
+
Blackmask=cv2.dilate(Blackmask,kernel, iterations=1)
|
| 261 |
contours1, hier1 = cv2.findContours(Blackmask, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
|
| 262 |
finalcnts.append(contours1)
|
| 263 |
finalcnts=tuple(finalcnts)
|
|
|
|
| 595 |
widthMax= width1+10
|
| 596 |
heightMin=height1-10
|
| 597 |
heightMax= height1+10
|
| 598 |
+
areaPerimeterMin= round(item1,1) - 0.2
|
| 599 |
+
areaPerimeterMax= round(item1,1) + 0.2
|
| 600 |
# print (areaMin, areaMax)
|
| 601 |
if color != [0,0,0]: #colored images
|
| 602 |
|
|
|
|
| 687 |
shape=[]
|
| 688 |
|
| 689 |
# cv2_imshow(imgStraight)
|
| 690 |
+
area1 = cv2.contourArea(contourssA[contour][0])
|
| 691 |
|
| 692 |
# perimeter1 = cv2.arcLength(contourss[contour][0], True)
|
| 693 |
if (area1 > 2000 ): #check perimeter kman fl condition -- 2800
|
|
|
|
| 703 |
|
| 704 |
x, y , width, height = cv2.boundingRect(contourss[contour][0])
|
| 705 |
perimeter = cv2.arcLength(contourss[contour][0], True)
|
| 706 |
+
# approx = cv2.approxPolyDP(contourssA[contour][0], 0.0002* perimeter, True)
|
| 707 |
+
# area1 = cv2.contourArea(approx)
|
| 708 |
+
approx = cv2.approxPolyDP(contourss[contour][0], 0.03 * perimeter, True) #0.0009
|
| 709 |
perimeter1 = cv2.arcLength(approx, True)
|
| 710 |
|
| 711 |
for point in approx:
|
|
|
|
| 739 |
perimeterr=perimeter1
|
| 740 |
|
| 741 |
# if flag=='area':
|
| 742 |
+
areaPerimeterMin= round(areaa,1) - 0.2
|
| 743 |
+
areaPerimeterMax= round(areaa,1) + 0.2
|
| 744 |
# areaPerimeterMin= round(perimeterr,1) - 0.3
|
| 745 |
# areaPerimeterMax= round(perimeterr,1) + 0.3
|
| 746 |
masked=SimilarAreaDictionary.loc[SimilarAreaDictionary.index[((SimilarAreaDictionary['Rounded'] >=areaPerimeterMin) &(SimilarAreaDictionary['Rounded']<=areaPerimeterMax) )]]
|
|
|
|
| 749 |
passed=0
|
| 750 |
for i, row in masked.iterrows():
|
| 751 |
if passed ==0:
|
| 752 |
+
if SimilarAreaDictionary['Color'].loc[i] == [rgbcolor[0],rgbcolor[1],rgbcolor[2]] and ( SimilarAreaDictionary['Rounded'].loc[i] <= areaPerimeterMax and SimilarAreaDictionary['Rounded'].loc[i] >= areaPerimeterMin) :
|
| 753 |
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 ) :
|
| 754 |
SimilarAreaDictionary['Total Area'].loc[i]+=areaa
|
| 755 |
SimilarAreaDictionary['Area'].loc[i]=areaa
|
|
|
|
| 769 |
annot.set_colors( fill=( int(SimilarAreaDictionary['R'].loc[i])/255 , int(SimilarAreaDictionary['G'].loc[i])/255 , int(SimilarAreaDictionary['B'].loc[i])/255 ) )
|
| 770 |
# annot.set_colors( fill=(1,0,1) )
|
| 771 |
annot.set_opacity(0.5)
|
| 772 |
+
annot.set_info(content='Area= '+str(areaa)+' m2' +'\n \nPerimeter= '+str(perimeterr)+' m' +'\n \nLength= '+str(lengthShape)+' m',subject='ADR Team')#,title='uuum')
|
| 773 |
# annot.set_line_ends(fitz.PDF_ANNOT_LE_DIAMOND, fitz.PDF_ANNOT_LE_CIRCLE)
|
| 774 |
annot.update()
|
| 775 |
|
| 776 |
+
# cv2.putText(imgtransparent1,'Area= '+str(area1) , (x+50,y-10) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
|
| 777 |
# cv2.putText(imgtransparent1,'Width= '+str(width) , (x+50,y-10) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
|
| 778 |
# cv2.putText(imgtransparent1,'Length= '+str(height) , (x+50,y-20) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
|
| 779 |
areasinImage.append(areaa)
|
|
|
|
| 782 |
cv2.putText(imgPerimeter1,'Area: '+str(areaa)+' m2', (x+50,y-40) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
|
| 783 |
|
| 784 |
|
| 785 |
+
# for i,row in SimilarAreaDictionary.iterrows():
|
| 786 |
+
# # print(row)
|
| 787 |
+
# if row[5] not in areasinImage: # column of area
|
| 788 |
+
# SimilarAreaDictionary = SimilarAreaDictionary.drop(SimilarAreaDictionary.loc[SimilarAreaDictionary.index==i].index)
|
| 789 |
|
| 790 |
+
# print(SimilarAreaDictionary)
|
| 791 |
# display(totaldf)
|
| 792 |
#########################
|
| 793 |
else:
|
|
|
|
| 799 |
# print('filled')
|
| 800 |
for contour in range(len(contourss)):
|
| 801 |
# imgArea1=img.copy()
|
| 802 |
+
area1 = cv2.contourArea(contourssA[contour][0])
|
| 803 |
if (area1 >2000 ):
|
| 804 |
perimeter = cv2.arcLength(contourss[contour][0],True)
|
| 805 |
|
|
|
|
| 808 |
x, y , width, height = cv2.boundingRect(contourss[contour][0])
|
| 809 |
# approxo = cv2.approxPolyDP(contourss[contour],0.1*cv2.arcLength(contourss[contour],True),True)
|
| 810 |
# approx = cv2.approxPolyDP(contourss[contour][0], 0.005 * perimeter, True)
|
| 811 |
+
# approxa = cv2.approxPolyDP(contourssA[contour][0], 0.0002* perimeter, True)
|
| 812 |
+
# area1 = cv2.contourArea(approxa)
|
| 813 |
+
approx = cv2.approxPolyDP(contourss[contour][0], 0.03 * perimeter, True) #0.0009
|
| 814 |
perimeter1 = cv2.arcLength(approx, True)
|
| 815 |
for point in approx:
|
| 816 |
x1, y1 = point[0]
|
|
|
|
| 825 |
if height>width:
|
| 826 |
lengthShape = height
|
| 827 |
|
| 828 |
+
widthMin= width-10
|
| 829 |
+
widthMax= width+10
|
| 830 |
+
heightMin=height-10
|
| 831 |
+
heightMax= height+10
|
| 832 |
|
| 833 |
if ratioarea !=0 and ratioperim!=0:
|
| 834 |
+
areaa= round(area1* ratioarea,3) # true value of area of any shape/ area px value of same shape
|
| 835 |
perimeterr=round(perimeter1* ratioperim,3)
|
| 836 |
lengthShape=round(lengthShape* ratioperim,3)
|
| 837 |
else:
|
| 838 |
perimeterr=perimeter1
|
| 839 |
+
areaPerimeterMin= round(areaa,1) - 0.2
|
| 840 |
+
areaPerimeterMax= round(areaa,1) + 0.2
|
| 841 |
masked=SimilarAreaDictionary.loc[SimilarAreaDictionary.index[((SimilarAreaDictionary['Rounded'] >=areaPerimeterMin) & (SimilarAreaDictionary['Rounded']<=areaPerimeterMax) )]]
|
| 842 |
passed=0
|
| 843 |
for i, row in masked.iterrows():
|
|
|
|
| 862 |
annot.set_colors( fill=( int(SimilarAreaDictionary['R'].loc[i])/255 , int(SimilarAreaDictionary['G'].loc[i])/255 , int(SimilarAreaDictionary['B'].loc[i])/255 ) )
|
| 863 |
# annot.set_colors( fill=(1,0,1) )
|
| 864 |
annot.set_opacity(0.5)
|
| 865 |
+
annot.set_info(content='Area= '+str(areaa)+' m2' +'\n \nPerimeter= '+str(perimeterr)+' m' +'\n \nLength= '+str(lengthShape)+' m',subject='ADR Team')#,title='uuum')
|
| 866 |
# annot.set_line_ends(fitz.PDF_ANNOT_LE_DIAMOND, fitz.PDF_ANNOT_LE_CIRCLE)
|
| 867 |
annot.update()
|
| 868 |
|