Marthee commited on
Commit
800a3de
·
1 Parent(s): 4a67672

Update pilecaps_adr.py

Browse files
Files changed (1) hide show
  1. pilecaps_adr.py +139 -152
pilecaps_adr.py CHANGED
@@ -6,27 +6,18 @@ Automatically generated by Colaboratory.
6
  Original file is located at
7
  https://colab.research.google.com/drive/16RHtRae7VU_fqHMAjOUL4ET5slEFo3pf
8
  """
9
-
10
  # pip install pdf-annotate
11
-
12
  # pip install pdf-annotate
13
-
14
  #pip install pdf2image
15
-
16
  #!pip install -q gradio
17
-
18
  #pip install pygsheets
19
-
20
  # !apt-get install poppler-utils
21
-
22
  import numpy as np
23
  import cv2
24
  #from google.colab.patches import cv2_imshow
25
  from matplotlib import pyplot as plt
26
  #from pdf2image import convert_from_path
27
-
28
  import math
29
-
30
  import pandas as pd
31
  import random
32
  # import imutils
@@ -54,10 +45,10 @@ def detectCircles(imgOriginal ):
54
  er1=cv2.erode(imgGry1,kernel, iterations=2)
55
 
56
  er1=cv2.dilate(er1,kernel, iterations=1)
57
- gray_blurred = cv2.blur(er1, (3,3 ))
58
  # Apply Hough transform on the blurred image.
59
  # min distance between circles, Upper threshold for the internal Canny edge detector.
60
- detected_circles = cv2.HoughCircles( gray_blurred, cv2.HOUGH_GRADIENT, 1, 50, param1= 550,
61
  param2 =21, minRadius = 20, maxRadius = 40) #18 param2
62
 
63
  # Draw circles that are detected.
@@ -91,7 +82,7 @@ def detectSmallCircles(img ):
91
  detected_circles = np.round(detected_circles[0, :]).astype("int")
92
  #DRAW CIRCLES
93
  for (x, y, r) in detected_circles:
94
- cv2.circle(im, (x, y), r+1, (255, 255, 255), -1)
95
  # cv2_imshow(im)
96
  return im
97
  # c=detectCircles(img)
@@ -175,7 +166,6 @@ def euclidian_distance(point1, point2):
175
  def removeDashedLinesSmall(img4, imgBW ,max,min):
176
 
177
  imgBW=cv2.cvtColor(imgBW,cv2.COLOR_GRAY2BGR)
178
-
179
  imgLines= cv2.HoughLinesP(img4,1,np.pi/100,20,minLineLength=(max-min)//2.2,maxLineGap = 70) #2.1
180
 
181
  for i in range(len(imgLines)):
@@ -197,14 +187,14 @@ def ConnectBeamLines(smalldashesOut):
197
  imgLines= cv2.HoughLinesP(green1,0.05,np.pi/250,10,minLineLength=25,maxLineGap = 20) #was w-h , gap=150 #50
198
  for i in range(len(imgLines)):
199
  for x1,y1,x2,y2 in imgLines[i]:
200
- cv2.line(green2,(x1,y1),(x2,y2),(0,0,0),1)
201
 
202
- imgLines= cv2.HoughLinesP(green1,0.3,np.pi/360,10,minLineLength=25,maxLineGap = 20) #try 180
203
 
204
 
205
  for i in range(len(imgLines)):
206
  for x1,y1,x2,y2 in imgLines[i]:
207
- cv2.line(green2,(x1,y1),(x2,y2),(0,0,0),1)
208
  # cv2_imshow(green2)
209
  return green2
210
 
@@ -226,37 +216,47 @@ def ChangeBrightness(img,k):
226
  return imgdarker
227
 
228
  def preprocessold(img,number):
229
-
230
- # imcopy=detectCircles(img)
231
- blurG = cv2.GaussianBlur(ChangeBrightness(img,6),(3,3),0)
232
-
233
- imgGry = cv2.cvtColor(blurG, cv2.COLOR_BGR2GRAY)
234
 
 
 
235
  kernel=np.ones((3,3),np.uint8)
236
-
237
  er1=cv2.dilate(imgGry,kernel, iterations=2) #thinning
238
-
239
- er2=cv2.erode(er1,kernel, iterations=3) #thicken
240
- er3=cv2.dilate(er2,kernel, iterations=4)
241
-
242
  if number == 0:
243
- ret3, thresh = cv2.threshold(er3, 200, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
244
  else:
245
- ret3, thresh = cv2.threshold(er3, 220, 255, cv2.THRESH_BINARY_INV) #`140 - 141
246
- # cv2_imshow(thresh)
247
  return thresh
248
- # preprocessold(img,0)
249
 
250
- def preprocess(imgOriginal,number,green2):
251
  #first preprocessing ( old method - black img with white shapes)
252
  img1=preprocessold(imgOriginal,number)
253
- kernel0=np.ones((3,3),np.uint8)
254
- img1=cv2.dilate(img1,kernel0, iterations=3)
255
- green3=ChangeBrightness(green2,6)
256
- anding=cv2.bitwise_and(green3,green3,mask=img1)
257
- anding = cv2.cvtColor(anding , cv2.COLOR_BGR2GRAY)
258
-
259
- return anding
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
260
 
261
  """# ROI (levels)
262
  ## Detect regions with specific color and mask them
@@ -335,7 +335,7 @@ def colorOrder(img,finalColorArray):
335
 
336
  return res
337
 
338
- def getinnerColor(BlackmaskDetected,img,detectedColors,finalColorArray,num1,num2,flag,eachcolor):
339
 
340
  countBlackMasks=0
341
  xored=detectedColors
@@ -416,14 +416,7 @@ def allLevelsofColor(BlackmaskDetected,img,levelonly, invertedmask,color,finalCo
416
  firstLevel=cv2.bitwise_and(invertedmask,invertedmask,mask=Blackmask)
417
  ####remove black pixels and let them be all white
418
  # get (i, j) positions of all RGB pixels that are black (i.e. [0, 0, 0])
419
- black_pixels = np.where(
420
- (firstLevel[:, :, 0] == 0) &
421
- (firstLevel[:, :, 1] == 0) &
422
- (firstLevel[:, :, 2] == 0)
423
- )
424
-
425
- # set those pixels to white
426
- firstLevel[black_pixels] = [255, 255, 255]
427
  firstLevel1=cv2.bitwise_and(levelonly,firstLevel)
428
  # cv2_imshow(firstLevel1)
429
 
@@ -451,7 +444,7 @@ def allLevelsofColor(BlackmaskDetected,img,levelonly, invertedmask,color,finalCo
451
  # cv2_imshow(Blackmask)
452
  return firstLevel1, BlackmaskDetected
453
 
454
- def getColoredContour(mask,img,finalColorArray,num1,num2,flag,eachcolor):
455
  print('uuuuuuuuummmmmmmmmmmmm')
456
 
457
  ColoredContour, Coloredhierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
@@ -480,19 +473,17 @@ def getColoredContour(mask,img,finalColorArray,num1,num2,flag,eachcolor):
480
  if extrema == (0, 0) :#and extremaB==(0,0): # if image is not black --> has a colored mask within
481
  break
482
 
483
- levelOnly,invertedmask,BlackmaskDetected=getinnerColor(Blackmask,img,detectedColors,finalColorArray,num1,num2,flag,eachcolor) #mask inner levels b abyad
484
  firstLevel1, BlackmaskDetected1= allLevelsofColor(BlackmaskDetected,img,levelOnly, invertedmask,eachcolor,finalColorArray)
485
- # cv2.imshow('kk',firstLevel1)
486
- print('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA')
487
  return firstLevel1,invertedmask, BlackmaskDetected1
488
 
489
  """# contours"""
490
 
491
- def findContoursFullImage(green2,img,number,finalColorArray,num1,num2,flag,color=[0,0,0]):
492
  if number == 0:
493
- thresh=preprocess(img,number,green2)
494
 
495
- contourss, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
496
  return contourss
497
  else:
498
  mask, detectedColors, rgbcolor =DetectColor(img,color)
@@ -502,18 +493,13 @@ def findContoursFullImage(green2,img,number,finalColorArray,num1,num2,flag,color
502
 
503
  extrema = pil_image.convert("L").getextrema()
504
  if extrema != (0, 0): # if image is not black --> has a colored mask within
505
- coloredregions,invertedmask,BlackmaskDetected1=getColoredContour(mask,img,finalColorArray,num1,num2,flag,color)
506
- thresh=preprocess(coloredregions,number,green2)
507
- x=cv2.bitwise_and(thresh,thresh,mask=BlackmaskDetected1)
508
- contourss, hierarchy = cv2.findContours(x, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
509
  return contourss,rgbcolor ,invertedmask
510
 
511
  else:
512
- print('ELSEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE')
513
- thresh=preprocess(img,number,green2)
514
 
515
- contourss, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
516
- hierarchy = hierarchy[0]
517
  return contourss,color ,mask
518
 
519
 
@@ -545,43 +531,49 @@ def StraightenImage(contour,imgArea):
545
  ##############
546
  return angleR,width,height
547
 
548
- def getAreasPerimeter(green2,img,number,num1,num2,flag,finalColorArray,color=[0,0,0]):
549
  appended=[]
550
  if number==0:
551
- contourss=findContoursFullImage(green2,img,number,finalColorArray,num1,num2,flag,color)
552
  else:
553
- contourss=findContoursFullImage(green2,img,number,finalColorArray,num1,num2,flag,color)[0]
554
-
555
- for contour in contourss:
556
-
557
- area1 = cv2.contourArea(contour)
558
- perimeter1 = cv2.arcLength(contour, True)
559
- x, y , width, height = cv2.boundingRect(contour)
560
-
561
- angleR,widthR ,heightR= StraightenImage(contour,img)
562
-
563
- if (angleR != 90.0 and angleR != -90.0 and angleR != 0.0 and angleR != -0.0 ): #inclined b ay degree
564
- width=widthR
565
- height=heightR
566
- if (area1 > 4000 ): #check perimeter kman fl condition -- 2800
567
- if num1!=0 and num2!=0:
568
- #if flag=='area':
569
- # addedMargin=area1+perimeter1*2
570
- # areaa=round(addedMargin* (num1/(num2+perimeter1*2) ), 3) # true value of area of any shape/ area px value of same shape
571
- areaa=round(area1*num1,3)
572
- appended.append([areaa,width,height])
 
 
 
 
 
 
573
 
574
  #else:
575
- #perimeter=round(perimeter1*(num1/num2),3)
576
  #appended.append([perimeter,width,height])
577
 
578
  return appended
579
 
580
 
581
- def FillDictionary(green2,SimilarAreaDictionary,img,number,num1,num2,flag,finalColorArray,rgbcolor=[0,0,0],color=[0,0,0]):
582
  #fills dictionary with key areas and number of occurences
583
  print('wttttt')
584
- areas_Perimeters=sorted(getAreasPerimeter(green2,img,number,num1,num2,flag,finalColorArray,color) )
585
 
586
  indices=[]
587
  colorRanges=[[255,153,153],[51,255,51],[201,56,147],[255,0,0],[255,0,255],[0,102,204],[102,0,102],[153,0,76],[200,92,135],[52,161,99],[235,250,24],[40,30,170],[98,149,63],[100,30,179],[200,55,67],[150,80,200],[0,102,102],[250,28,191],[101,27,101],[230,150,76],[3,65,127],[114,39,39],[250,36,100],[180,30,40],[10,250,60],[140,30,253],[114,58,245],[47,255,255],[18,236,206],[225,105,29],[189,65,121],[206,204,48],[126,7,247],[3,168,251]]
@@ -691,7 +683,8 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
691
  if eachcolor==firstcolor: # 3shan a3rf el array of colors et3adet kam mara - to support embedded levels
692
  counter+=1
693
 
694
- contourss,rgbcolor,invertedmask=findContoursFullImage(green2,maskDone,number,finalColorArray,ratioarea,ratioperim,flag,eachcolor)
 
695
  SimilarAreaDictionary, colorsUsed , areas_Perimeters= FillDictionary(green2,SimilarAreaDictionary,maskDone,number,ratioarea,ratioperim,flag,finalColorArray,rgbcolor,eachcolor)
696
 
697
  a = SimilarAreaDictionary.to_numpy()
@@ -699,25 +692,31 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
699
  # for component in zip(contourss,hierarchy):
700
  # contour = component[0]
701
  # currentHierarchy = component[1]
702
- for contour in contourss:
703
  shape=[]
704
 
705
  # cv2_imshow(imgStraight)
706
- area1 = cv2.contourArea(contour)
707
- perimeter1 = cv2.arcLength(contour, True)
708
- if (area1 > 4000 ): #check perimeter kman fl condition -- 2800
709
- angleR,widthR ,heightR= StraightenImage(contour,imgArea1)
710
- rect = cv2.minAreaRect(contour)
711
-
712
- (center, (width, height), angleR) = cv2.minAreaRect(contour)
713
-
 
 
714
  box = cv2.boxPoints(rect)
715
  box = box.astype('int')
716
  print(box)
717
 
718
- x, y , width, height = cv2.boundingRect(contour)
719
- # cv2.drawContours(imgArea1,contours=[box], contourIdx=0 , color=(0, 0, 255), thickness=10)
720
- approx = cv2.approxPolyDP(contour, 0.005 * perimeter1, True)
 
 
 
 
721
  for point in approx:
722
  x1, y1 = point[0]
723
 
@@ -747,7 +746,8 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
747
  areaa=round(area1* ratioarea, 3) # true value of area of any shape/ area px value of same shape
748
 
749
  # elif flag=='perimeter':
750
- perimeterr=round(perimeter1* ratioperim, 3)
 
751
  else:
752
  areaa=area1
753
  perimeterr=perimeter1
@@ -776,7 +776,7 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
776
  passed=1
777
  # print(index)
778
  # cv2.drawContours(imgArea1, [contour], 0, (int(rgbcolor[2]), int(rgbcolor[1]), int(rgbcolor[0])), -1)
779
- cv2.drawContours(imgArea1, [contour], 0, ( int(SimilarAreaDictionary['B'].loc[i]), int(SimilarAreaDictionary['G'].loc[i]), int(SimilarAreaDictionary['R'].loc[i])), -1)
780
  annot = page.add_polygon_annot( points=shape ) # 'Polygon'
781
  annot.set_border(width=0.3, dashes=[2])
782
  annot.set_colors( fill=( int(SimilarAreaDictionary['R'].loc[i])/255 , int(SimilarAreaDictionary['G'].loc[i])/255 , int(SimilarAreaDictionary['B'].loc[i])/255 ) )
@@ -790,13 +790,14 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
790
  # cv2.putText(imgtransparent1,'Length= '+str(height) , (x+50,y-20) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
791
  areasinImage.append(areaa)
792
 
793
- cv2.putText(imgPerimeter1,'Perimeter'+str(perimeterr), (x+70,y-30) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
 
794
 
795
 
796
- for i,row in SimilarAreaDictionary.iterrows():
797
- # print(row)
798
- if row[5] not in areasinImage: # column of area
799
- SimilarAreaDictionary = SimilarAreaDictionary.drop(SimilarAreaDictionary.loc[SimilarAreaDictionary.index==i].index)
800
 
801
  print(SimilarAreaDictionary)
802
  # display(totaldf)
@@ -804,56 +805,54 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
804
  else:
805
 
806
  SimilarAreaDictionary= pd.DataFrame(columns=['Color','Rounded','Width','Height','Occurences','Area','Total Area','Perimeter','Total Perimeter','Length','Total Length','R','G','B'])
807
- contourss=findContoursFullImage(green2,img,number,finalColorArray,ratioarea,ratioperim,flag)
 
808
  SimilarAreaDictionary,colorsUsed , areas_Perimeters= FillDictionary(green2,SimilarAreaDictionary,img,number,ratioarea,ratioperim,flag,finalColorArray)
809
  # print('filled')
810
- for contour in contourss:
811
- # shape=[]
812
- area1 = cv2.contourArea(contour)
813
- perimeter1 = cv2.arcLength(contour, True)
814
- if (area1 >4000 ):
815
- shape=[]
816
- angleR,widthR ,heightR= StraightenImage(contour,imgArea1)
817
- x, y , width, height = cv2.boundingRect(contour)
818
 
819
- approx = cv2.approxPolyDP(contour, 0.005 * perimeter1, True)
 
 
 
 
 
 
 
 
820
  for point in approx:
821
  x1, y1 = point[0]
822
  shape.append([int(x1*ratio),int(y1*ratio)])
823
  allpoints.append(shape)
824
- if (angleR != 90.0 and angleR != -90.0 and angleR != 0.0 and angleR != -0.0 ): #inclined b ay degree
825
  width=widthR
826
  height=heightR
827
  if width>height:
828
  lengthShape = width
829
  if height>width:
830
  lengthShape = height
831
- widthMin= width-10 #5
 
832
  widthMax= width+10
833
  heightMin=height-10
834
  heightMax= height+10
835
 
836
-
837
  if ratioarea !=0 and ratioperim!=0:
838
- # if flag=='area':
839
- # addedMargin=area1+perimeter1*2
840
- # areaa=round(addedMargin* (num1/(num2+perimeter1*2) ), 3) # true value of area of any shape/ area px value of same shape
841
- # elif flag=='perimeter':
842
- areaa=round(area1* ratioarea, 3) # true value of area of any shape/ area px value of same shape
843
- perimeterr=round(perimeter1* ratioperim, 3)
844
  else:
845
- areaa=area1
846
  perimeterr=perimeter1
847
- # if flag=='area':
848
- areaPerimeterMin= round(areaa,1) - 0.3
849
- areaPerimeterMax= round(areaa,1) + 0.3
850
  masked=SimilarAreaDictionary.loc[SimilarAreaDictionary.index[((SimilarAreaDictionary['Rounded'] >=areaPerimeterMin) & (SimilarAreaDictionary['Rounded']<=areaPerimeterMax) )]]
851
  passed=0
852
- # if (round(areaa,1) in masked['Rounded'].values ) :
853
-
854
  for i, row in masked.iterrows():
855
  if passed ==0:
856
- # if SimilarAreaDictionary['Rounded'].loc[i] == round(areaa,1) :
857
  if ( SimilarAreaDictionary['Rounded'].loc[i] <= areaPerimeterMax and SimilarAreaDictionary['Rounded'].loc[i] >= areaPerimeterMin) :
858
  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 ) :
859
  SimilarAreaDictionary['Total Area'].loc[i]+=areaa
@@ -861,44 +860,32 @@ def drawAllContours(img,number,finalColorArray,ratioarea,ratioperim,flag , path,
861
 
862
  SimilarAreaDictionary['Total Perimeter'].loc[i]+=perimeterr
863
  SimilarAreaDictionary['Perimeter'].loc[i]=perimeterr
864
-
865
  SimilarAreaDictionary['Total Length'].loc[i]+=lengthShape
866
  SimilarAreaDictionary['Length'].loc[i]=lengthShape
 
867
  passed=1
868
- cv2.drawContours(imgArea1, [contour], 0, ( int(SimilarAreaDictionary['B'].loc[i]), int(SimilarAreaDictionary['G'].loc[i]), int(SimilarAreaDictionary['R'].loc[i])), -1)
869
-
870
  annot = page.add_polygon_annot( points=shape ) # 'Polygon'
871
  annot.set_border(width=0.3, dashes=[2])
872
- annot.set_colors( fill=( int(SimilarAreaDictionary['R'].loc[i])/255 , int(SimilarAreaDictionary['G'].loc[i])/255 , int(SimilarAreaDictionary['B'].loc[i])/255 ) )
873
  # annot.set_colors( fill=(1,0,1) )
874
  annot.set_opacity(0.5)
875
  annot.set_info(content='Area='+str(areaa)+' m2' +'\n \nPerimeter='+str(perimeterr)+' m',subject='ADR Team')#,title='uuum')
876
  # annot.set_line_ends(fitz.PDF_ANNOT_LE_DIAMOND, fitz.PDF_ANNOT_LE_CIRCLE)
877
  annot.update()
878
- cv2.putText(imgtransparent1,'area= '+str(area1) + ' m', (x+50,y-10) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
879
- # cv2.putText(imgtransparent1,'Width= '+str(width) , (x+50,y-10) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
880
- # cv2.putText(imgtransparent1,'Length= '+str(height) , (x+50,y-40) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
881
- cv2.drawContours(imgArea1, [contour], 0, (0, 0, 255),2)
882
- cv2.drawContours(imgPerimeter1, [contour], 0, (0, 0, 255), 4)
883
- cv2.putText(imgPerimeter1,'Perimeter='+str(perimeter1), (x+30,y-30) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
884
 
885
-
886
  alpha = 0.4 # Transparency factor.
887
  image_new1 = cv2.addWeighted(imgArea1, alpha, imgtransparent1, 1 - alpha, 0)
888
-
889
  # SimilarAreaDictionary.drop(['Rounded', 'Width','Height','R','G','B'], axis=1, inplace=True)
890
 
891
  print(SimilarAreaDictionary)
892
 
893
  # annotationsSave
894
- # doc.save('k.pdf', deflate=True)
895
  pdflink= db.dropbox_upload_file(doc=doc,pdfname=path,pdfpath=pdfpath)
896
- # list1=pd.DataFrame(columns=['content', 'creationDate', 'id', 'modDate', 'name', 'subject', 'title'])
897
- # doc1 = fitz.open('k.pdf')
898
- # for page in doc1:
899
- # for annot in page.annots():
900
- # list1.loc[len(list1)] =annot.info
901
- # print(list1)
902
  dbx=db.dropbox_connect()
903
  md, res =dbx.files_download(path= pdfpath+path)
904
  data = res.content
 
6
  Original file is located at
7
  https://colab.research.google.com/drive/16RHtRae7VU_fqHMAjOUL4ET5slEFo3pf
8
  """
 
9
  # pip install pdf-annotate
 
10
  # pip install pdf-annotate
 
11
  #pip install pdf2image
 
12
  #!pip install -q gradio
 
13
  #pip install pygsheets
 
14
  # !apt-get install poppler-utils
 
15
  import numpy as np
16
  import cv2
17
  #from google.colab.patches import cv2_imshow
18
  from matplotlib import pyplot as plt
19
  #from pdf2image import convert_from_path
 
20
  import math
 
21
  import pandas as pd
22
  import random
23
  # import imutils
 
45
  er1=cv2.erode(imgGry1,kernel, iterations=2)
46
 
47
  er1=cv2.dilate(er1,kernel, iterations=1)
48
+ # gray_blurred = cv2.blur(er1, (3,3 ))
49
  # Apply Hough transform on the blurred image.
50
  # min distance between circles, Upper threshold for the internal Canny edge detector.
51
+ detected_circles = cv2.HoughCircles( er1, cv2.HOUGH_GRADIENT, 1, 50, param1= 550,
52
  param2 =21, minRadius = 20, maxRadius = 40) #18 param2
53
 
54
  # Draw circles that are detected.
 
82
  detected_circles = np.round(detected_circles[0, :]).astype("int")
83
  #DRAW CIRCLES
84
  for (x, y, r) in detected_circles:
85
+ cv2.circle(im, (x, y), r+3, (255, 255, 255), -1)
86
  # cv2_imshow(im)
87
  return im
88
  # c=detectCircles(img)
 
166
  def removeDashedLinesSmall(img4, imgBW ,max,min):
167
 
168
  imgBW=cv2.cvtColor(imgBW,cv2.COLOR_GRAY2BGR)
 
169
  imgLines= cv2.HoughLinesP(img4,1,np.pi/100,20,minLineLength=(max-min)//2.2,maxLineGap = 70) #2.1
170
 
171
  for i in range(len(imgLines)):
 
187
  imgLines= cv2.HoughLinesP(green1,0.05,np.pi/250,10,minLineLength=25,maxLineGap = 20) #was w-h , gap=150 #50
188
  for i in range(len(imgLines)):
189
  for x1,y1,x2,y2 in imgLines[i]:
190
+ cv2.line(green2,(x1,y1),(x2,y2),(0,0,0),2)
191
 
192
+ imgLines= cv2.HoughLinesP(green1,1,np.pi/250,37,minLineLength=25,maxLineGap = 22) #try 180
193
 
194
 
195
  for i in range(len(imgLines)):
196
  for x1,y1,x2,y2 in imgLines[i]:
197
+ cv2.line(green2,(x1,y1),(x2,y2),(0,0,0),2)
198
  # cv2_imshow(green2)
199
  return green2
200
 
 
216
  return imgdarker
217
 
218
  def preprocessold(img,number):
 
 
 
 
 
219
 
220
+ blurG = cv2.GaussianBlur(ChangeBrightness(img,6),(3,3),0 )
221
+ imgGry = cv2.cvtColor(blurG, cv2.COLOR_BGR2GRAY)
222
  kernel=np.ones((3,3),np.uint8)
 
223
  er1=cv2.dilate(imgGry,kernel, iterations=2) #thinning
 
 
 
 
224
  if number == 0:
225
+ ret3, thresh = cv2.threshold(er1, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
226
  else:
227
+ ret3, thresh = cv2.threshold(er1, 220, 255, cv2.THRESH_BINARY_INV) #`140 - 141
228
+
229
  return thresh
 
230
 
231
+ def preprocess(imgOriginal,number,green2,flag,layeredflag,BlackmaskDetected1=0):
232
  #first preprocessing ( old method - black img with white shapes)
233
  img1=preprocessold(imgOriginal,number)
234
+ anding=cv2.bitwise_and(green2,green2,mask=img1)
235
+ anding = cv2.cvtColor(anding, cv2.COLOR_BGR2GRAY)
236
+ ret3, thresh2 = cv2.threshold(anding, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
237
+ if layeredflag.startswith('layer'):
238
+ thresh2=cv2.bitwise_and(thresh2,thresh2,mask=BlackmaskDetected1)
239
+ threshcontours, threshHier = cv2.findContours(thresh2, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
240
+
241
+ finalcnts=[]
242
+ BlackmaskALL = np.zeros(imgOriginal.shape[:2], dtype="uint8")
243
+ for cnt in threshcontours:
244
+ Blackmask = np.zeros(imgOriginal.shape[:2], dtype="uint8")
245
+ area1 = cv2.contourArea(cnt)
246
+ if (area1 > 2000 ):
247
+ x, y , width, height = cv2.boundingRect(cnt)
248
+ if flag.startswith('perimeter'):
249
+ kernel=np.ones((2,2),np.uint8)
250
+ cv2.drawContours(Blackmask,[cnt],0,(255,255,255), 6)
251
+ Blackmask=cv2.dilate(Blackmask,kernel, iterations=1)
252
+ elif flag.startswith('area'):
253
+ kernel=np.ones((2,2),np.uint8)
254
+ cv2.drawContours(Blackmask,[cnt],0,(255,255,255), 5)
255
+ Blackmask=cv2.erode(Blackmask,kernel, iterations=1)
256
+ contours1, hier1 = cv2.findContours(Blackmask, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
257
+ finalcnts.append(contours1)
258
+ finalcnts=tuple(finalcnts)
259
+ return finalcnts
260
 
261
  """# ROI (levels)
262
  ## Detect regions with specific color and mask them
 
335
 
336
  return res
337
 
338
+ def getinnerColor(BlackmaskDetected,img,detectedColors,finalColorArray,ratioarea,ratioperim,flag,eachcolor):
339
 
340
  countBlackMasks=0
341
  xored=detectedColors
 
416
  firstLevel=cv2.bitwise_and(invertedmask,invertedmask,mask=Blackmask)
417
  ####remove black pixels and let them be all white
418
  # get (i, j) positions of all RGB pixels that are black (i.e. [0, 0, 0])
419
+ firstLevel[np.all(firstLevel == (0,0,0), axis=-1)] = (255, 255, 255)
 
 
 
 
 
 
 
420
  firstLevel1=cv2.bitwise_and(levelonly,firstLevel)
421
  # cv2_imshow(firstLevel1)
422
 
 
444
  # cv2_imshow(Blackmask)
445
  return firstLevel1, BlackmaskDetected
446
 
447
+ def getColoredContour(mask,img,finalColorArray,ratioarea,ratioperim,flag,eachcolor):
448
  print('uuuuuuuuummmmmmmmmmmmm')
449
 
450
  ColoredContour, Coloredhierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
 
473
  if extrema == (0, 0) :#and extremaB==(0,0): # if image is not black --> has a colored mask within
474
  break
475
 
476
+ levelOnly,invertedmask,BlackmaskDetected=getinnerColor(Blackmask,img,detectedColors,finalColorArray,ratioarea,ratioperim,flag,eachcolor) #mask inner levels b abyad
477
  firstLevel1, BlackmaskDetected1= allLevelsofColor(BlackmaskDetected,img,levelOnly, invertedmask,eachcolor,finalColorArray)
 
 
478
  return firstLevel1,invertedmask, BlackmaskDetected1
479
 
480
  """# contours"""
481
 
482
+ def findContoursFullImage(green2,img,number,finalColorArray,ratioarea,ratioperim,flag,color=[0,0,0]):
483
  if number == 0:
484
+ contourss=preprocess(img,number,green2,flag,'nolayer')
485
 
486
+ # contourss, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
487
  return contourss
488
  else:
489
  mask, detectedColors, rgbcolor =DetectColor(img,color)
 
493
 
494
  extrema = pil_image.convert("L").getextrema()
495
  if extrema != (0, 0): # if image is not black --> has a colored mask within
496
+ coloredregions,invertedmask,BlackmaskDetected1=getColoredContour(mask,img,finalColorArray,ratioarea,ratioperim,flag,color)
497
+ contourss=preprocess(coloredregions,number,green2,flag,'layer',BlackmaskDetected1)
 
 
498
  return contourss,rgbcolor ,invertedmask
499
 
500
  else:
501
+ contourss=preprocess(img,number,green2,flag,'nolayer')
 
502
 
 
 
503
  return contourss,color ,mask
504
 
505
 
 
531
  ##############
532
  return angleR,width,height
533
 
534
+ def getAreasPerimeter(green2,img,number,ratioarea,ratioperim,flag,finalColorArray,color=[0,0,0]):
535
  appended=[]
536
  if number==0:
537
+ contourss=findContoursFullImage(green2,img,number,finalColorArray,ratioarea,ratioperim,flag,color)
538
  else:
539
+ contourss=findContoursFullImage(green2,img,number,finalColorArray,ratioarea,ratioperim,flag,color)[0]
540
+
541
+ for contour in range(len(contourss)):
542
+ # imgArea1=img.copy()
543
+ area1 = cv2.contourArea(contourss[contour][0])
544
+ if (area1 >2000 ):
545
+ perimeter = cv2.arcLength(contourss[contour][0],True)
546
+ approx = cv2.approxPolyDP(contourss[contour][0], 0.01* perimeter, True)
547
+ perimeter1 = cv2.arcLength(approx, True)
548
+ # approx = cv2.approxPolyDP(contourss[contour][0], 0.01* perimeter, True)
549
+ # area1 = cv2.contourArea(approx)
550
+ x, y , width, height = cv2.boundingRect(contourss[contour][0])
551
+
552
+ angleR,widthR ,heightR= StraightenImage(contourss[contour][0],img)
553
+
554
+ if (angleR != 90.0 and angleR != -90.0 and angleR != 0.0 and angleR != -0.0 ): #inclined b ay degree
555
+ width=widthR
556
+ height=heightR
557
+ if (area1 > 2000 ): #check perimeter kman fl condition -- 2800
558
+ # print('areass')
559
+ if ratioarea!=0 and ratioperim!=0:
560
+ # print('numss')
561
+ # if flag=='area':
562
+ # addedMargin=area1+perimeter1*2
563
+ areaa=round(area1* ratioarea , 2) # true value of area of any shape/ area px value of same shape
564
+ appended.append([areaa,width,height])
565
 
566
  #else:
567
+ #perimeter=round(perimeter1*(ratioarea/ratioperim),3)
568
  #appended.append([perimeter,width,height])
569
 
570
  return appended
571
 
572
 
573
+ def FillDictionary(green2,SimilarAreaDictionary,img,number,ratioarea,ratioperim,flag,finalColorArray,rgbcolor=[0,0,0],color=[0,0,0]):
574
  #fills dictionary with key areas and number of occurences
575
  print('wttttt')
576
+ areas_Perimeters=sorted(getAreasPerimeter(green2,img,number,ratioarea,ratioperim,flag,finalColorArray,color) )
577
 
578
  indices=[]
579
  colorRanges=[[255,153,153],[51,255,51],[201,56,147],[255,0,0],[255,0,255],[0,102,204],[102,0,102],[153,0,76],[200,92,135],[52,161,99],[235,250,24],[40,30,170],[98,149,63],[100,30,179],[200,55,67],[150,80,200],[0,102,102],[250,28,191],[101,27,101],[230,150,76],[3,65,127],[114,39,39],[250,36,100],[180,30,40],[10,250,60],[140,30,253],[114,58,245],[47,255,255],[18,236,206],[225,105,29],[189,65,121],[206,204,48],[126,7,247],[3,168,251]]
 
683
  if eachcolor==firstcolor: # 3shan a3rf el array of colors et3adet kam mara - to support embedded levels
684
  counter+=1
685
 
686
+ contourss,rgbcolor,invertedmask=findContoursFullImage(green2,maskDone,number,finalColorArray,ratioarea,ratioperim,'area',eachcolor)
687
+ contourssA,rgbcolor,invertedmask=findContoursFullImage(green2,maskDone,number,finalColorArray,ratioarea,ratioperim,'perimeter',eachcolor)
688
  SimilarAreaDictionary, colorsUsed , areas_Perimeters= FillDictionary(green2,SimilarAreaDictionary,maskDone,number,ratioarea,ratioperim,flag,finalColorArray,rgbcolor,eachcolor)
689
 
690
  a = SimilarAreaDictionary.to_numpy()
 
692
  # for component in zip(contourss,hierarchy):
693
  # contour = component[0]
694
  # currentHierarchy = component[1]
695
+ for contour in range(len(contourss)):
696
  shape=[]
697
 
698
  # cv2_imshow(imgStraight)
699
+ area1 = cv2.contourArea(contourss[contour][0])
700
+
701
+ # perimeter1 = cv2.arcLength(contourss[contour][0], True)
702
+ if (area1 > 2000 ): #check perimeter kman fl condition -- 2800
703
+ rect = cv2.minAreaRect(contourss[contour][0])
704
+
705
+ angleR,widthR ,heightR= StraightenImage(contourss[contour][0],imgArea1)
706
+ x, y , width, height = cv2.boundingRect(contourss[contour][0])
707
+ # (center, (width, height), angleR) = cv2.minAreaRect(contourss[contour][0])
708
+
709
  box = cv2.boxPoints(rect)
710
  box = box.astype('int')
711
  print(box)
712
 
713
+ x, y , width, height = cv2.boundingRect(contourss[contour][0])
714
+ perimeter = cv2.arcLength(contourss[contour][0], True)
715
+ approx = cv2.approxPolyDP(contourssA[contour][0], 0.0002* perimeter, True)
716
+ area1 = cv2.contourArea(approx)
717
+ approx = cv2.approxPolyDP(contourss[contour][0], 0.01 * perimeter, True) #0.0009
718
+ perimeter1 = cv2.arcLength(approx, True)
719
+
720
  for point in approx:
721
  x1, y1 = point[0]
722
 
 
746
  areaa=round(area1* ratioarea, 3) # true value of area of any shape/ area px value of same shape
747
 
748
  # elif flag=='perimeter':
749
+ perimeterr=round(perimeter1* ratioperim,3)
750
+ lengthShape=round(lengthShape* ratioperim,3)
751
  else:
752
  areaa=area1
753
  perimeterr=perimeter1
 
776
  passed=1
777
  # print(index)
778
  # cv2.drawContours(imgArea1, [contour], 0, (int(rgbcolor[2]), int(rgbcolor[1]), int(rgbcolor[0])), -1)
779
+ cv2.drawContours(imgArea1, [contourss[contour][0]], 0, ( int(SimilarAreaDictionary['B'].loc[i]), int(SimilarAreaDictionary['G'].loc[i]), int(SimilarAreaDictionary['R'].loc[i])), -1)
780
  annot = page.add_polygon_annot( points=shape ) # 'Polygon'
781
  annot.set_border(width=0.3, dashes=[2])
782
  annot.set_colors( fill=( int(SimilarAreaDictionary['R'].loc[i])/255 , int(SimilarAreaDictionary['G'].loc[i])/255 , int(SimilarAreaDictionary['B'].loc[i])/255 ) )
 
790
  # cv2.putText(imgtransparent1,'Length= '+str(height) , (x+50,y-20) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
791
  areasinImage.append(areaa)
792
 
793
+ cv2.putText(imgPerimeter1,'Perimeter: '+str(perimeterr)+ ' m', (x+70,y-30) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
794
+ cv2.putText(imgPerimeter1,'Area: '+str(areaa)+' m2', (x+50,y-40) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
795
 
796
 
797
+ # for i,row in SimilarAreaDictionary.iterrows():
798
+ # # print(row)
799
+ # if row[5] not in areasinImage: # column of area
800
+ # SimilarAreaDictionary = SimilarAreaDictionary.drop(SimilarAreaDictionary.loc[SimilarAreaDictionary.index==i].index)
801
 
802
  print(SimilarAreaDictionary)
803
  # display(totaldf)
 
805
  else:
806
 
807
  SimilarAreaDictionary= pd.DataFrame(columns=['Color','Rounded','Width','Height','Occurences','Area','Total Area','Perimeter','Total Perimeter','Length','Total Length','R','G','B'])
808
+ contourss=findContoursFullImage(green2,img,number,finalColorArray,ratioarea,ratioperim,'perimeter')
809
+ contourssA=findContoursFullImage(green2,img,number,finalColorArray,ratioarea,ratioperim,'area')
810
  SimilarAreaDictionary,colorsUsed , areas_Perimeters= FillDictionary(green2,SimilarAreaDictionary,img,number,ratioarea,ratioperim,flag,finalColorArray)
811
  # print('filled')
812
+ for contour in range(len(contourss)):
813
+ # imgArea1=img.copy()
814
+ area1 = cv2.contourArea(contourss[contour][0])
815
+ if (area1 >2000 ):
816
+ perimeter = cv2.arcLength(contourss[contour][0],True)
 
 
 
817
 
818
+ shape=[]
819
+ angleR,widthR ,heightR= StraightenImage(contourss[contour][0],imgArea1)
820
+ x, y , width, height = cv2.boundingRect(contourss[contour][0])
821
+ # approxo = cv2.approxPolyDP(contourss[contour],0.1*cv2.arcLength(contourss[contour],True),True)
822
+ # approx = cv2.approxPolyDP(contourss[contour][0], 0.005 * perimeter, True)
823
+ approx = cv2.approxPolyDP(contourssA[contour][0], 0.0002* perimeter, True)
824
+ area1 = cv2.contourArea(approx)
825
+ approx = cv2.approxPolyDP(contourss[contour][0], 0.01 * perimeter, True) #0.0009
826
+ perimeter1 = cv2.arcLength(approx, True)
827
  for point in approx:
828
  x1, y1 = point[0]
829
  shape.append([int(x1*ratio),int(y1*ratio)])
830
  allpoints.append(shape)
831
+ if (angleR != 90.0 and angleR != -90.0 and angleR != 0.0 and angleR != -0.0 ): #inclined b ay degree
832
  width=widthR
833
  height=heightR
834
  if width>height:
835
  lengthShape = width
836
  if height>width:
837
  lengthShape = height
838
+
839
+ widthMin= width-10
840
  widthMax= width+10
841
  heightMin=height-10
842
  heightMax= height+10
843
 
 
844
  if ratioarea !=0 and ratioperim!=0:
845
+ areaa=round(area1* ratioarea ,3) # true value of area of any shape/ area px value of same shape
846
+ perimeterr=round(perimeter1* ratioperim,3)
847
+ lengthShape=round(lengthShape* ratioperim,3)
 
 
 
848
  else:
 
849
  perimeterr=perimeter1
850
+ areaPerimeterMin= round(areaa,1) - 0.4
851
+ areaPerimeterMax= round(areaa,1) + 0.4
 
852
  masked=SimilarAreaDictionary.loc[SimilarAreaDictionary.index[((SimilarAreaDictionary['Rounded'] >=areaPerimeterMin) & (SimilarAreaDictionary['Rounded']<=areaPerimeterMax) )]]
853
  passed=0
 
 
854
  for i, row in masked.iterrows():
855
  if passed ==0:
 
856
  if ( SimilarAreaDictionary['Rounded'].loc[i] <= areaPerimeterMax and SimilarAreaDictionary['Rounded'].loc[i] >= areaPerimeterMin) :
857
  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 ) :
858
  SimilarAreaDictionary['Total Area'].loc[i]+=areaa
 
860
 
861
  SimilarAreaDictionary['Total Perimeter'].loc[i]+=perimeterr
862
  SimilarAreaDictionary['Perimeter'].loc[i]=perimeterr
863
+
864
  SimilarAreaDictionary['Total Length'].loc[i]+=lengthShape
865
  SimilarAreaDictionary['Length'].loc[i]=lengthShape
866
+
867
  passed=1
868
+ cv2.drawContours(imgArea1, [contourss[contour][0]], 0, ( int(SimilarAreaDictionary['B'].loc[i]), int(SimilarAreaDictionary['G'].loc[i]), int(SimilarAreaDictionary['R'].loc[i])), -1)
869
+
870
  annot = page.add_polygon_annot( points=shape ) # 'Polygon'
871
  annot.set_border(width=0.3, dashes=[2])
872
+ annot.set_colors( fill=( int(SimilarAreaDictionary['B'].loc[i])/255 , int(SimilarAreaDictionary['G'].loc[i])/255 , int(SimilarAreaDictionary['R'].loc[i])/255 ) )
873
  # annot.set_colors( fill=(1,0,1) )
874
  annot.set_opacity(0.5)
875
  annot.set_info(content='Area='+str(areaa)+' m2' +'\n \nPerimeter='+str(perimeterr)+' m',subject='ADR Team')#,title='uuum')
876
  # annot.set_line_ends(fitz.PDF_ANNOT_LE_DIAMOND, fitz.PDF_ANNOT_LE_CIRCLE)
877
  annot.update()
878
+ cv2.putText(imgtransparent1,'Area: '+str(areaa)+' m2' , (x+50,y-10) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
879
+ cv2.putText(imgtransparent1,'Perimeter: '+str(perimeterr)+' m' , (x+50,y-40) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
 
 
 
 
880
 
 
881
  alpha = 0.4 # Transparency factor.
882
  image_new1 = cv2.addWeighted(imgArea1, alpha, imgtransparent1, 1 - alpha, 0)
 
883
  # SimilarAreaDictionary.drop(['Rounded', 'Width','Height','R','G','B'], axis=1, inplace=True)
884
 
885
  print(SimilarAreaDictionary)
886
 
887
  # annotationsSave
 
888
  pdflink= db.dropbox_upload_file(doc=doc,pdfname=path,pdfpath=pdfpath)
 
 
 
 
 
 
889
  dbx=db.dropbox_connect()
890
  md, res =dbx.files_download(path= pdfpath+path)
891
  data = res.content