Marthee commited on
Commit
e04c753
·
1 Parent(s): 590ae13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -48
app.py CHANGED
@@ -8,11 +8,12 @@ Original file is located at
8
  """
9
 
10
 
 
11
  import numpy as np
12
  import cv2
13
  # from google.colab.patches import cv2_imshow
14
  from matplotlib import pyplot as plt
15
- # from pdf2image import convert_from_path
16
 
17
  import math
18
  import fitz
@@ -30,6 +31,8 @@ from db import dropbox_upload_file
30
  from pathlib import Path
31
  from PreprocessingFoundation import rmv_text,rmv_dashedLines
32
 
 
 
33
  #############################################################################################
34
 
35
  '''push output to dropbox'''
@@ -63,11 +66,7 @@ def plan2img(plan):
63
  pl1=np.array(pl)
64
  return pl1
65
 
66
- # path='/content/drive/MyDrive/Colab Notebooks/Pile caps plans/13886-CRH-B3-FN-DR-S-31001 - Copy.pdf'
67
-
68
- # pip install aspose-pdf
69
 
70
- # pip install img2pdf
71
 
72
  def detectCircles(imgOriginal ):
73
  im=imgOriginal.copy()
@@ -239,7 +238,6 @@ def allpreSteps(imgOriginal):
239
  green2=ConnectBeamLines(noSmallDashes)
240
  # cv2_imshow(green2)
241
  return green2
242
- green2=allpreSteps(plan2img(plan))
243
 
244
  def ChangeBrightness(img,k):
245
  imgdarker = 255 * (img/255)**k # k>1 darker , k <1 lighter
@@ -345,7 +343,7 @@ def colorOrder(img,finalColorArray):
345
 
346
  return res
347
 
348
- def getinnerColor(BlackmaskDetected,detectedColors,finalColorArray,num1,num2,flag,eachcolor):
349
 
350
  countBlackMasks=0
351
  xored=detectedColors
@@ -391,7 +389,7 @@ def getinnerColor(BlackmaskDetected,detectedColors,finalColorArray,num1,num2,fla
391
 
392
  return xored,invertedmask , BlackmaskDetected
393
 
394
- def allLevelsofColor(BlackmaskDetected,levelonly, invertedmask,color,finalColorArray):
395
 
396
  firstLevel=levelonly
397
  firstLevel1=levelonly
@@ -451,7 +449,7 @@ def allLevelsofColor(BlackmaskDetected,levelonly, invertedmask,color,finalColorA
451
 
452
  return firstLevel1, BlackmaskDetected
453
 
454
- def getColoredContour(mask,finalColorArray,num1,num2,flag,eachcolor):
455
 
456
  ColoredContour, Coloredhierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
457
  Coloredhierarchy=Coloredhierarchy[0]
@@ -479,14 +477,14 @@ def getColoredContour(mask,finalColorArray,num1,num2,flag,eachcolor):
479
  if extrema == (0, 0) :#and extremaB==(0,0): # if image is not black --> has a colored mask within
480
  break
481
 
482
- levelOnly,invertedmask,BlackmaskDetected=getinnerColor(Blackmask,detectedColors,finalColorArray,num1,num2,flag,eachcolor) #mask inner levels b abyad
483
- firstLevel1, BlackmaskDetected1= allLevelsofColor(BlackmaskDetected,levelOnly, invertedmask,eachcolor,finalColorArray)
484
  # cv2_imshow(firstLevel1)
485
  return firstLevel1,invertedmask, BlackmaskDetected1
486
 
487
  """# contours"""
488
 
489
- def findContoursFullImage(img,number,finalColorArray,num1,num2,flag,color=[0,0,0]):
490
  if number == 0:
491
  thresh=preprocess(img,number,green2)
492
 
@@ -497,7 +495,7 @@ def findContoursFullImage(img,number,finalColorArray,num1,num2,flag,color=[0,0,0
497
  pil_image=Image.fromarray(mask)
498
  extrema = pil_image.convert("L").getextrema()
499
  if extrema != (0, 0): # if image is not black --> has a colored mask within
500
- coloredregions,invertedmask,BlackmaskDetected1=getColoredContour(mask,finalColorArray,num1,num2,flag,color)
501
 
502
  thresh=preprocess(coloredregions,number,green2)
503
  x=cv2.bitwise_and(thresh,thresh,mask=BlackmaskDetected1)
@@ -541,12 +539,12 @@ def StraightenImage(contour,imgArea):
541
  ##############
542
  return warped,angleR,width,height
543
 
544
- def getAreasPerimeter(img,number,num1,num2,flag,finalColorArray,color=[0,0,0]):
545
  appended=[]
546
  if number==0:
547
- contourss=findContoursFullImage(img,number,finalColorArray,num1,num2,flag,color)
548
  else:
549
- contourss=findContoursFullImage(img,number,finalColorArray,num1,num2,flag,color)[0]
550
 
551
  for contour in contourss:
552
 
@@ -571,10 +569,10 @@ def getAreasPerimeter(img,number,num1,num2,flag,finalColorArray,color=[0,0,0]):
571
 
572
  return appended
573
 
574
- def FillDictionary(SimilarAreaDictionary,img,number,num1,num2,flag,finalColorArray,rgbcolor=[0,0,0],color=[0,0,0]):
575
  #fills dictionary with key areas and number of occurences
576
 
577
- areas_Perimeters=sorted(getAreasPerimeter(img,number,num1,num2,flag,finalColorArray,color) )
578
 
579
  indices=[]
580
  for i in range(len(areas_Perimeters)):
@@ -643,25 +641,25 @@ def FillDictionary(SimilarAreaDictionary,img,number,num1,num2,flag,finalColorArr
643
  # apdf = PdfAnnotator('/content/drive/MyDrive/Colab Notebooks/Pile caps plans/13886-CRH-B2-FN-DR-S-21001.pdf')
644
 
645
  # ratio2=apdf.get_size(0)[0] / ImgShape[1]
646
- # ratio1 = apdf.get_size(0)[1] / ImgShape[0]
647
- # print(ratio1,ratio2)
648
 
649
- # apdf.add_annotation(
650
- # 'polygon',
651
 
652
- # Location(points= [ [1803*ratio2, 1929*ratio2], [1943*ratio2 ,1564*ratio2], [2370*ratio2, 1728*ratio2], [2229*ratio2 ,2094*ratio2]] ,page=0),
653
- # Appearance(stroke_color=(1, 0, 1), stroke_width=5,stroke_transparency=0.5,fill=(0,1,0),fill_transparency=0.2,),
654
- # # Metadata(Subj='hii'),#,Label='jnnf',label='jnnnf',name='ll',Name='mfds',note='mmf',Note='nzsf'), #Subj
655
- # )
656
 
657
- # apdf.add_annotation(
658
- # 'square',
659
- # Location(x1=1000,y1=100,x2=2000,y2=300 ,page=0),
660
- # Appearance(stroke_color=(1, 0, 1), stroke_width=5,fill=(0,1,0),fill_transparency=0.2),
661
- # Metadata(Subj='hii',Label='jnnf'), #Subj
662
- # )
663
 
664
- # apdf.write('bb.pdf') # or use overwrite=True if you feel lucky
665
 
666
  # pip install pdfrw
667
 
@@ -673,7 +671,7 @@ def FillDictionary(SimilarAreaDictionary,img,number,num1,num2,flag,finalColorArr
673
 
674
  # destpage.showPDFpage(r * fitz.Matrix(d=-1), src, sourcepage.number)
675
 
676
- def drawAllContours(img,number,finalColorArray,num1,num2,flag):
677
  # colors=[color,color1,color2,color3,color4,color5,color6,color7]
678
  # img=np.array(img)
679
  areasinImage=[]
@@ -704,8 +702,8 @@ def drawAllContours(img,number,finalColorArray,num1,num2,flag):
704
  if eachcolor==firstcolor: # 3shan a3rf el array of colors et3adet kam mara - to support embedded levels
705
  counter+=1
706
 
707
- contourss,rgbcolor,invertedmask=findContoursFullImage(maskDone,number,finalColorArray,num1,num2,flag,eachcolor)
708
- SimilarAreaDictionary= FillDictionary(SimilarAreaDictionary,maskDone,number,num1,num2,flag,finalColorArray,rgbcolor,eachcolor)
709
 
710
  a = SimilarAreaDictionary.to_numpy()
711
 
@@ -805,9 +803,9 @@ def drawAllContours(img,number,finalColorArray,num1,num2,flag):
805
 
806
  else:
807
  SimilarAreaDictionary= pd.DataFrame(columns=['Rounded','Width','Height','Perimeter','Occurences','Total Perimeter','R','G','B'])
808
- contourss=findContoursFullImage(img,number,finalColorArray,num1,num2,flag)
809
 
810
- SimilarAreaDictionary= FillDictionary(SimilarAreaDictionary,img,number,num1,num2,flag,finalColorArray)
811
 
812
  for contour in contourss:
813
 
@@ -878,9 +876,9 @@ def drawAllContours(img,number,finalColorArray,num1,num2,flag):
878
  # drawAllContours(img,0,[],1.11,25579,'area') #,[190,47,250] ,[47,251,255] ,[80,240,15],[253,163,40]
879
  # imgPerimeter1,image_new1,Dictionary=drawAllContours(img,220,[47,251,255],[251,163,47],1.105,27233.5,'area')
880
 
881
- def MainFunc(dp,finalColorArray,number,num1,num2,flag):
882
 
883
- imgPerimeter1,image_new1,Df2=drawAllContours(img,number,finalColorArray,num1,num2,flag)
884
  x = path.split("/")
885
  x=x.pop()
886
  Df2=Df2.T
@@ -902,6 +900,7 @@ def MainFunc(dp,finalColorArray,number,num1,num2,flag):
902
  return Df2, imgPerimeter1,image_new1
903
 
904
  def PickColorContours(dp,img,radioButton,radioButton1, color,color1,color2,color3,color4,color5,color6,color7,num1=0,num2=0):
 
905
  colorArray=[color,color1,color2,color3,color4,color5,color6,color7]
906
  finalColorArray=[]
907
  for c in colorArray:
@@ -914,30 +913,30 @@ def PickColorContours(dp,img,radioButton,radioButton1, color,color1,color2,color
914
  if radioButton=="Measure Full Image" :
915
 
916
  if radioButton1=='Area':
917
- imgPerimeter1,image_new1,df=drawAllContours(img,0,finalColorArray,num1,num2,'area')
918
  return image_new1
919
  else:
920
- imgPerimeter1,image_new1,df=drawAllContours(img,0,finalColorArray,num1,num2,'perimeter')
921
  return imgPerimeter1
922
  else:
923
  if radioButton1=='Area':
924
- imgPerimeter1,image_new1,df=drawAllContours(img,220,finalColorArray,num1,num2,'area') #rgbValue
925
  return image_new1
926
  else:
927
- imgPerimeter1,image_new1,df=drawAllContours(img,220,finalColorArray,num1,num2,'perimeter') #rgbValue
928
  return imgPerimeter1
929
  else:
930
  if radioButton=="Measure Full Image":
931
  if radioButton1=='Area':
932
- Dictionary, imgPerimeter1,image_new1=MainFunc(dp,finalColorArray,0,num1,num2,'area')
933
  else:
934
- Dictionary, imgPerimeter1,image_new1=MainFunc(dp,finalColorArray,0,num1,num2,'perimeter')
935
 
936
  else:
937
  if radioButton1=='Area':
938
- Dictionary, imgPerimeter1,image_new1=MainFunc(dp,finalColorArray,220,num1,num2,'area')
939
  else:
940
- Dictionary, imgPerimeter1,image_new1=MainFunc(dp,finalColorArray,220,num1,num2,'perimeter')
941
 
942
  Dictionary=Dictionary.T
943
  Dictionary.to_excel("output.xlsx")
 
8
  """
9
 
10
 
11
+
12
  import numpy as np
13
  import cv2
14
  # from google.colab.patches import cv2_imshow
15
  from matplotlib import pyplot as plt
16
+ from pdf2image import convert_from_path
17
 
18
  import math
19
  import fitz
 
31
  from pathlib import Path
32
  from PreprocessingFoundation import rmv_text,rmv_dashedLines
33
 
34
+
35
+
36
  #############################################################################################
37
 
38
  '''push output to dropbox'''
 
66
  pl1=np.array(pl)
67
  return pl1
68
 
 
 
 
69
 
 
70
 
71
  def detectCircles(imgOriginal ):
72
  im=imgOriginal.copy()
 
238
  green2=ConnectBeamLines(noSmallDashes)
239
  # cv2_imshow(green2)
240
  return green2
 
241
 
242
  def ChangeBrightness(img,k):
243
  imgdarker = 255 * (img/255)**k # k>1 darker , k <1 lighter
 
343
 
344
  return res
345
 
346
+ def getinnerColor(BlackmaskDetected,img,detectedColors,finalColorArray,num1,num2,flag,eachcolor):
347
 
348
  countBlackMasks=0
349
  xored=detectedColors
 
389
 
390
  return xored,invertedmask , BlackmaskDetected
391
 
392
+ def allLevelsofColor(BlackmaskDetected,img,levelonly, invertedmask,color,finalColorArray):
393
 
394
  firstLevel=levelonly
395
  firstLevel1=levelonly
 
449
 
450
  return firstLevel1, BlackmaskDetected
451
 
452
+ def getColoredContour(mask,img,finalColorArray,num1,num2,flag,eachcolor):
453
 
454
  ColoredContour, Coloredhierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
455
  Coloredhierarchy=Coloredhierarchy[0]
 
477
  if extrema == (0, 0) :#and extremaB==(0,0): # if image is not black --> has a colored mask within
478
  break
479
 
480
+ levelOnly,invertedmask,BlackmaskDetected=getinnerColor(Blackmask,img,detectedColors,finalColorArray,num1,num2,flag,eachcolor) #mask inner levels b abyad
481
+ firstLevel1, BlackmaskDetected1= allLevelsofColor(BlackmaskDetected,img,levelOnly, invertedmask,eachcolor,finalColorArray)
482
  # cv2_imshow(firstLevel1)
483
  return firstLevel1,invertedmask, BlackmaskDetected1
484
 
485
  """# contours"""
486
 
487
+ def findContoursFullImage(green2,img,number,finalColorArray,num1,num2,flag,color=[0,0,0]):
488
  if number == 0:
489
  thresh=preprocess(img,number,green2)
490
 
 
495
  pil_image=Image.fromarray(mask)
496
  extrema = pil_image.convert("L").getextrema()
497
  if extrema != (0, 0): # if image is not black --> has a colored mask within
498
+ coloredregions,invertedmask,BlackmaskDetected1=getColoredContour(mask,img,finalColorArray,num1,num2,flag,color)
499
 
500
  thresh=preprocess(coloredregions,number,green2)
501
  x=cv2.bitwise_and(thresh,thresh,mask=BlackmaskDetected1)
 
539
  ##############
540
  return warped,angleR,width,height
541
 
542
+ def getAreasPerimeter(green2,img,number,num1,num2,flag,finalColorArray,color=[0,0,0]):
543
  appended=[]
544
  if number==0:
545
+ contourss=findContoursFullImage(green2,img,number,finalColorArray,num1,num2,flag,color)
546
  else:
547
+ contourss=findContoursFullImage(green2,img,number,finalColorArray,num1,num2,flag,color)[0]
548
 
549
  for contour in contourss:
550
 
 
569
 
570
  return appended
571
 
572
+ def FillDictionary(green2,SimilarAreaDictionary,img,number,num1,num2,flag,finalColorArray,rgbcolor=[0,0,0],color=[0,0,0]):
573
  #fills dictionary with key areas and number of occurences
574
 
575
+ areas_Perimeters=sorted(getAreasPerimeter(green2,img,number,num1,num2,flag,finalColorArray,color) )
576
 
577
  indices=[]
578
  for i in range(len(areas_Perimeters)):
 
641
  # apdf = PdfAnnotator('/content/drive/MyDrive/Colab Notebooks/Pile caps plans/13886-CRH-B2-FN-DR-S-21001.pdf')
642
 
643
  # ratio2=apdf.get_size(0)[0] / ImgShape[1]
644
+ # # ratio1 = apdf.get_size(0)[1] / ImgShape[0]
645
+ # # print(ratio1,ratio2)
646
 
647
+ # # apdf.add_annotation(
648
+ # # 'polygon',
649
 
650
+ # # Location(points= [ [1803*ratio2, 1929*ratio2], [1943*ratio2 ,1564*ratio2], [2370*ratio2, 1728*ratio2], [2229*ratio2 ,2094*ratio2]] ,page=0),
651
+ # # Appearance(stroke_color=(1, 0, 1), stroke_width=5,stroke_transparency=0.5,fill=(0,1,0),fill_transparency=0.2,),
652
+ # # # Metadata(Subj='hii'),#,Label='jnnf',label='jnnnf',name='ll',Name='mfds',note='mmf',Note='nzsf'), #Subj
653
+ # # )
654
 
655
+ # # apdf.add_annotation(
656
+ # # 'square',
657
+ # # Location(x1=1000,y1=100,x2=2000,y2=300 ,page=0),
658
+ # # Appearance(stroke_color=(1, 0, 1), stroke_width=5,fill=(0,1,0),fill_transparency=0.2),
659
+ # # Metadata(Subj='hii',Label='jnnf'), #Subj
660
+ # # )
661
 
662
+ # # apdf.write('bb.pdf') # or use overwrite=True if you feel lucky
663
 
664
  # pip install pdfrw
665
 
 
671
 
672
  # destpage.showPDFpage(r * fitz.Matrix(d=-1), src, sourcepage.number)
673
 
674
+ def drawAllContours(green2,img,number,finalColorArray,num1,num2,flag):
675
  # colors=[color,color1,color2,color3,color4,color5,color6,color7]
676
  # img=np.array(img)
677
  areasinImage=[]
 
702
  if eachcolor==firstcolor: # 3shan a3rf el array of colors et3adet kam mara - to support embedded levels
703
  counter+=1
704
 
705
+ contourss,rgbcolor,invertedmask=findContoursFullImage(green2,maskDone,number,finalColorArray,num1,num2,flag,eachcolor)
706
+ SimilarAreaDictionary= FillDictionary(green2,SimilarAreaDictionary,maskDone,number,num1,num2,flag,finalColorArray,rgbcolor,eachcolor)
707
 
708
  a = SimilarAreaDictionary.to_numpy()
709
 
 
803
 
804
  else:
805
  SimilarAreaDictionary= pd.DataFrame(columns=['Rounded','Width','Height','Perimeter','Occurences','Total Perimeter','R','G','B'])
806
+ contourss=findContoursFullImage(green2,img,number,finalColorArray,num1,num2,flag)
807
 
808
+ SimilarAreaDictionary= FillDictionary(green2,SimilarAreaDictionary,img,number,num1,num2,flag,finalColorArray)
809
 
810
  for contour in contourss:
811
 
 
876
  # drawAllContours(img,0,[],1.11,25579,'area') #,[190,47,250] ,[47,251,255] ,[80,240,15],[253,163,40]
877
  # imgPerimeter1,image_new1,Dictionary=drawAllContours(img,220,[47,251,255],[251,163,47],1.105,27233.5,'area')
878
 
879
+ def MainFunc(green2,img,dp,finalColorArray,number,num1,num2,flag):
880
 
881
+ imgPerimeter1,image_new1,Df2=drawAllContours(green2,img,number,finalColorArray,num1,num2,flag)
882
  x = path.split("/")
883
  x=x.pop()
884
  Df2=Df2.T
 
900
  return Df2, imgPerimeter1,image_new1
901
 
902
  def PickColorContours(dp,img,radioButton,radioButton1, color,color1,color2,color3,color4,color5,color6,color7,num1=0,num2=0):
903
+ green2=allpreSteps(img)
904
  colorArray=[color,color1,color2,color3,color4,color5,color6,color7]
905
  finalColorArray=[]
906
  for c in colorArray:
 
913
  if radioButton=="Measure Full Image" :
914
 
915
  if radioButton1=='Area':
916
+ imgPerimeter1,image_new1,df=drawAllContours(green2,img,0,finalColorArray,num1,num2,'area')
917
  return image_new1
918
  else:
919
+ imgPerimeter1,image_new1,df=drawAllContours(green2,img,0,finalColorArray,num1,num2,'perimeter')
920
  return imgPerimeter1
921
  else:
922
  if radioButton1=='Area':
923
+ imgPerimeter1,image_new1,df=drawAllContours(green2,img,220,finalColorArray,num1,num2,'area') #rgbValue
924
  return image_new1
925
  else:
926
+ imgPerimeter1,image_new1,df=drawAllContours(green2,img,220,finalColorArray,num1,num2,'perimeter') #rgbValue
927
  return imgPerimeter1
928
  else:
929
  if radioButton=="Measure Full Image":
930
  if radioButton1=='Area':
931
+ Dictionary, imgPerimeter1,image_new1=MainFunc(green2,img,dp,finalColorArray,0,num1,num2,'area')
932
  else:
933
+ Dictionary, imgPerimeter1,image_new1=MainFunc(green2,img,dp,finalColorArray,0,num1,num2,'perimeter')
934
 
935
  else:
936
  if radioButton1=='Area':
937
+ Dictionary, imgPerimeter1,image_new1=MainFunc(green2,img,dp,finalColorArray,220,num1,num2,'area')
938
  else:
939
+ Dictionary, imgPerimeter1,image_new1=MainFunc(green2,img,dp,finalColorArray,220,num1,num2,'perimeter')
940
 
941
  Dictionary=Dictionary.T
942
  Dictionary.to_excel("output.xlsx")