Marthee commited on
Commit
3e78d8d
·
1 Parent(s): 9fba49a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -96
app.py CHANGED
@@ -1,43 +1,25 @@
1
- # -*- coding: utf-8 -*-
2
- """DEPLOY-PileCaps-ADR.ipynb
3
-
4
- Automatically generated by Colaboratory.
5
-
6
- Original file is located at
7
- https://colab.research.google.com/drive/19_mNwUtppW-iE_s3e-yhENpxjzx4mFpi
8
- """
9
- import os
10
- ##########################
11
- os.system('apt-get install poppler-utils libpoppler-cpp-dev')
12
- os.system('pip install -v -v python-poppler')
13
- os.system('pip install PyQt5')
14
- # os.system('brew install pkg-config poppler python')
15
-
16
- # os.system('brew install poppler')
17
- # os.system('pip install pdf2image')
18
-
19
- import numpy as np
20
  import cv2
21
  # from google.colab.patches import cv2_imshow
22
- from matplotlib import pyplot as plt
23
- from pdf2image import convert_from_path
24
 
25
  import math
26
  import fitz
27
  import pandas as pd
28
  import random
29
- # import imutils
30
- # from imutils import contours
31
- import colorsys
32
  from PIL import Image , ImageDraw, ImageFont , ImageColor
33
  import numpy as np
34
  import gradio as gr
35
  # from pdf_annotate import PdfAnnotator, Location, Appearance ,Metadata
36
- # import os
37
  from db import dropbox_upload_file
38
  from pathlib import Path
39
  from PreprocessingFoundation import rmv_text,rmv_dashedLines
40
- import popplerqt5
41
 
42
  #############################################################################################
43
 
@@ -64,47 +46,50 @@ def plan2img(plan):
64
 
65
  else:
66
  fname = plan
67
-
68
  #op='pictures/found.png'
69
- pages = convert_from_path(fname)#, poppler_path = r'/poppler-0.68.0/bin')
70
- imgg = np.array(pages[0])
71
- # #op='pictures/found.png'
72
- # doc = fitz.open(fname) # open document
73
- # for page in doc:
74
- # pix = page.get_pixmap() # render page to an image
75
- # pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
76
- # pl1=np.array(pl)
77
- # print(type(pl1))
78
- return imgg
79
 
 
 
 
 
 
 
 
80
 
 
 
 
 
 
81
 
82
  def detectCircles(imgOriginal ):
83
- print(type(imgOriginal))
84
- im=imgOriginal.copy()
 
 
 
 
 
 
 
 
 
85
 
86
- imgGry1 = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
87
- kernel=np.ones((3,3),np.uint8)
88
- er1=cv2.erode(imgGry1,kernel, iterations=2)
89
-
90
- er1=cv2.dilate(er1,kernel, iterations=1)
91
- gray_blurred = cv2.blur(er1, (3,3 ))
92
- # Apply Hough transform on the blurred image.
93
- # min distance between circles, Upper threshold for the internal Canny edge detector.
94
- detected_circles = cv2.HoughCircles( gray_blurred, cv2.HOUGH_GRADIENT, 1, 50, param1= 550,
95
- param2 =21, minRadius = 20, maxRadius = 40) #18 param2
96
-
97
- # Draw circles that are detected.
98
- if detected_circles is not None:
99
- # Convert the circle parameters a, b and r to integers.
100
- detected_circles = np.uint16(np.around(detected_circles))
101
- detected_circles = np.round(detected_circles[0, :]).astype("int")
102
- #DRAW CIRCLES
103
- for (x, y, r) in detected_circles:
104
- cv2.circle(im, (x, y), r, (255, 255, 255), 6)
105
- im=cv2.medianBlur(im,1)
106
- # cv2_imshow(im)
107
- return im
108
 
109
  def detectSmallCircles(img ):
110
  #Remove tiny TOC points that interfere with shapes
@@ -739,9 +724,9 @@ def drawAllContours(green2,img,number,finalColorArray,num1,num2,flag):
739
  approx = cv2.approxPolyDP(contour, 0.005 * perimeter1, True)
740
  for point in approx:
741
  x1, y1 = point[0]
742
- shape.append([int(x1*ratio2),int(y1*ratio2)])
743
  cv2.circle(imgArea1, (x1, y1), 4, (0, 255, 0), -1)
744
- allpoints.append(shape)
745
 
746
  if (angleR != 90.0 and angleR != -90.0 and angleR != 0.0 and angleR != -0.0 ): #inclined b ay degree
747
  width=widthR
@@ -857,7 +842,7 @@ def drawAllContours(green2,img,number,finalColorArray,num1,num2,flag):
857
  passed=1
858
  cv2.drawContours(imgArea1, [contour], 0, ( int(SimilarAreaDictionary['B'].loc[i]), int(SimilarAreaDictionary['G'].loc[i]), 255), -1)
859
  cv2.putText(imgtransparent1,'Area= '+str(areaa) + ' m', (x+50,y-10) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
860
- cv2.drawContours(imgArea1, [contour], 0, (0, 0, 255), -1)
861
  elif flag=='perimeter':
862
  masked=SimilarAreaDictionary.loc[SimilarAreaDictionary['Rounded'] ==round(perimeterr,1)]
863
  passed=0
@@ -869,7 +854,7 @@ def drawAllContours(green2,img,number,finalColorArray,num1,num2,flag):
869
  SimilarAreaDictionary['Total Perimeter'].loc[i]+=perimeterr
870
  SimilarAreaDictionary['Perimeter'].loc[i]=perimeterr
871
  passed=1
872
- cv2.drawContours(imgPerimeter1, [contour], 0, (0, 255, 255), 4)
873
  cv2.putText(imgPerimeter1,'width='+str(width), (x+50,y-10) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
874
  cv2.putText(imgPerimeter1,'height='+str(height), (x+30,y-30) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
875
 
@@ -913,8 +898,9 @@ def MainFunc(green2,img,dp,finalColorArray,number,num1,num2,flag):
913
  return Df2, imgPerimeter1,image_new1
914
 
915
  def PickColorContours(dp,img,radioButton,radioButton1, color,color1,color2,color3,color4,color5,color6,color7,num1=0,num2=0):
916
- img=np.array(img)
917
  green2=allpreSteps(img)
 
918
  colorArray=[color,color1,color2,color3,color4,color5,color6,color7]
919
  finalColorArray=[]
920
  for c in colorArray:
@@ -960,30 +946,40 @@ def PickColorContours(dp,img,radioButton,radioButton1, color,color1,color2,color
960
  return imgPerimeter1,Dictionary
961
 
962
  ''' General measurement function'''
963
- def getMeasurement(plan,SaveOP, dp,in2,in3,in4,in5,in6,in7,in8,in9,in10,in11):
 
964
  if plan==None:
965
- area=None
966
- # area,perim,df=None,None,None
967
- # elif 'foundation' in plan:
968
- # plan1='dropbox_plans/'+str(plan)
969
- # img=plan2img(plan1)
970
- # area,perim,df=IsolatedFoundations(img)
971
- #area,perim,df=img,None,None
972
- # elif 'piles' or 'pc' in plan: #any pile cap
973
- else:
974
  plan1='dropbox_plans/'+str(plan)
975
  img=plan2img(plan1)
976
- # print(type(img),'fhabfhbjabfka')
977
- # area,perim,df=PileCaps(img)
978
- # if num1==0 and num2 ==0 :
979
- # print('FOUNDIMGYEHEOO')
980
- area=PickColorContours(dp,img,in2,in3,in4,in5,in6,in7,in8,in9,in10,in11)
 
 
981
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
982
  # if SaveOP:
983
  # pushToDropbox(plan,area,perim,df)
984
 
985
  # return area,perim,df
986
- return area
987
  ##########################################################
988
  def update_dropdown(project):
989
 
@@ -1011,9 +1007,11 @@ with gr.Blocks() as mainBlock:
1011
  drop=gr.Dropdown(choices=None,interactive=True,label='project parts')
1012
  radio_button = gr.Dropdown(choices=['foundation','external','interior'], value=None, interactive=True,label='sections')
1013
 
1014
-
1015
- # with gr.Column():
1016
- # img1=gr.Image()
 
 
1017
  # img2=gr.Image()
1018
  # df=gr.Dataframe()
1019
 
@@ -1035,14 +1033,9 @@ with gr.Blocks() as mainBlock:
1035
  in9=gr.ColorPicker(label="color" )
1036
  in10=gr.ColorPicker(label="color" )
1037
  in11=gr.ColorPicker(label="color" )
1038
-
1039
- check=gr.Checkbox(label='SaveOutput')
1040
- show_button = gr.Button(value="Measure",elem_id='search')
1041
- clr_btn=gr.Button(value='Clear')
1042
  with gr.Column():
1043
- img1=gr.Image()
1044
- # with gr.Column():
1045
- # out1 = gr.Image(label="Image", type="pil", image_mode="RGBA")
1046
  #gr.Dataframe(label='Dictionary' ), # row_count = (5, "fixed")
1047
  # ]
1048
  # btn1 = gr.Button("Submit")
@@ -1059,13 +1052,13 @@ with gr.Blocks() as mainBlock:
1059
  gr.Image(label="Image", type="pil", image_mode="RGBA"),
1060
  gr.Dataframe(label='Dictionary' ), # row_count = (5, "fixed")
1061
  ]
1062
- # getMeasurement(plan,SaveOP)
1063
- show_button.click(fn=getMeasurement, inputs=[drop,check,dp,in2,in3,in4,in5,in6,in7,in8,in9,in10,in11],outputs=img1)
1064
  clr_btn.click(fn=clear,outputs=[project,radio_button,check,drop])
1065
  # btn1.click(fn=PickColorContours, inputs=[dp,in1,in2,in3,in4,in5,in6,in7,in8,in9,in10,in11], outputs=out1)
1066
  #secoond part
1067
- project.change(fn=update_dropdown, inputs=[project], outputs=drop)
1068
- btn.click(fn=PickColorContours, inputs=[dp,in2,in3,in4,in5,in6,in7,in8,in9,in10,in11,num1,num2], outputs=outputs1)
1069
 
1070
  mainBlock.launch(debug=True,enable_queue=True)
1071
 
 
1
+ # import numpy as np
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import cv2
3
  # from google.colab.patches import cv2_imshow
4
+ # from matplotlib import pyplot as plt
5
+ # from pdf2image import convert_from_path
6
 
7
  import math
8
  import fitz
9
  import pandas as pd
10
  import random
11
+ # # import imutils
12
+ # # from imutils import contours
13
+ # import colorsys
14
  from PIL import Image , ImageDraw, ImageFont , ImageColor
15
  import numpy as np
16
  import gradio as gr
17
  # from pdf_annotate import PdfAnnotator, Location, Appearance ,Metadata
18
+ import os
19
  from db import dropbox_upload_file
20
  from pathlib import Path
21
  from PreprocessingFoundation import rmv_text,rmv_dashedLines
22
+
23
 
24
  #############################################################################################
25
 
 
46
 
47
  else:
48
  fname = plan
 
49
  #op='pictures/found.png'
50
+ # pages = convert_from_path(fname)
51
+ # imgg = np.array(pages[0])
52
+
 
 
 
 
 
 
 
53
 
54
+ doc = fitz.open(path)
55
+ for page in doc:
56
+ pix = page.get_pixmap(dpi=200) # render page to an image
57
+ pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
58
+ pl1=np.array(pl)
59
+ # pix = page.get_pixmap() # render page to an image
60
+ return pl1
61
 
62
+ # path='/content/drive/MyDrive/Colab Notebooks/Pile caps plans/13886-CRH-B3-FN-DR-S-31001 - Copy.pdf'
63
+
64
+ # pip install aspose-pdf
65
+
66
+ # pip install img2pdf
67
 
68
  def detectCircles(imgOriginal ):
69
+ im=imgOriginal.copy()
70
+ imgGry1 = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
71
+ kernel=np.ones((3,3),np.uint8)
72
+ er1=cv2.erode(imgGry1,kernel, iterations=2)
73
+
74
+ er1=cv2.dilate(er1,kernel, iterations=1)
75
+ gray_blurred = cv2.blur(er1, (3,3 ))
76
+ # Apply Hough transform on the blurred image.
77
+ # min distance between circles, Upper threshold for the internal Canny edge detector.
78
+ detected_circles = cv2.HoughCircles( gray_blurred, cv2.HOUGH_GRADIENT, 1, 50, param1= 550,
79
+ param2 =21, minRadius = 20, maxRadius = 40) #18 param2
80
 
81
+ # Draw circles that are detected.
82
+ if detected_circles is not None:
83
+ # Convert the circle parameters a, b and r to integers.
84
+ detected_circles = np.uint16(np.around(detected_circles))
85
+ detected_circles = np.round(detected_circles[0, :]).astype("int")
86
+ #DRAW CIRCLES
87
+ for (x, y, r) in detected_circles:
88
+ cv2.circle(im, (x, y), r, (255, 255, 255), 6)
89
+ im=cv2.medianBlur(im,1)
90
+ print('circles')
91
+ cv2_imshow(im)
92
+ return im
 
 
 
 
 
 
 
 
 
 
93
 
94
  def detectSmallCircles(img ):
95
  #Remove tiny TOC points that interfere with shapes
 
724
  approx = cv2.approxPolyDP(contour, 0.005 * perimeter1, True)
725
  for point in approx:
726
  x1, y1 = point[0]
727
+ # shape.append([int(x1*ratio2),int(y1*ratio2)])
728
  cv2.circle(imgArea1, (x1, y1), 4, (0, 255, 0), -1)
729
+ # allpoints.append(shape)
730
 
731
  if (angleR != 90.0 and angleR != -90.0 and angleR != 0.0 and angleR != -0.0 ): #inclined b ay degree
732
  width=widthR
 
842
  passed=1
843
  cv2.drawContours(imgArea1, [contour], 0, ( int(SimilarAreaDictionary['B'].loc[i]), int(SimilarAreaDictionary['G'].loc[i]), 255), -1)
844
  cv2.putText(imgtransparent1,'Area= '+str(areaa) + ' m', (x+50,y-10) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
845
+ cv2.drawContours(imgArea1, [contour], 0, (0, 0, 255), 4)
846
  elif flag=='perimeter':
847
  masked=SimilarAreaDictionary.loc[SimilarAreaDictionary['Rounded'] ==round(perimeterr,1)]
848
  passed=0
 
854
  SimilarAreaDictionary['Total Perimeter'].loc[i]+=perimeterr
855
  SimilarAreaDictionary['Perimeter'].loc[i]=perimeterr
856
  passed=1
857
+ cv2.drawContours(imgPerimeter1, [contour], 0, (0, 0, 255), 4)
858
  cv2.putText(imgPerimeter1,'width='+str(width), (x+50,y-10) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
859
  cv2.putText(imgPerimeter1,'height='+str(height), (x+30,y-30) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
860
 
 
898
  return Df2, imgPerimeter1,image_new1
899
 
900
  def PickColorContours(dp,img,radioButton,radioButton1, color,color1,color2,color3,color4,color5,color6,color7,num1=0,num2=0):
901
+ print(type(img))
902
  green2=allpreSteps(img)
903
+
904
  colorArray=[color,color1,color2,color3,color4,color5,color6,color7]
905
  finalColorArray=[]
906
  for c in colorArray:
 
946
  return imgPerimeter1,Dictionary
947
 
948
  ''' General measurement function'''
949
+ def getMeasurement(plan,SaveOP, dp,in2,in3,in4,in5,in6,in7,in8,in9,in10,in11,num1=0,num2=0):
950
+
951
  if plan==None:
952
+ area,perim,df=None,None,None
953
+ elif 'foundation' in plan:
 
 
 
 
 
 
 
954
  plan1='dropbox_plans/'+str(plan)
955
  img=plan2img(plan1)
956
+ area,perim,df=IsolatedFoundations(img)
957
+ area,perim,df=img,None,None
958
+ elif 'piles' or 'pc' in plan: #any pile cap
959
+ # else:
960
+ plan1='dropbox_plans/'+str(plan)
961
+ # plan1='/content/drive/MyDrive/Colab Notebooks/Pile caps plans/13886-B3-NO_TEXT.pdf'
962
+ img=plan2img(plan1)
963
 
964
+ img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
965
+
966
+ if num1==0 and num2 ==0:
967
+ area=PickColorContours(dp,img,in2,in3,in4,in5,in6,in7,in8,in9,in10,in11)
968
+ area = cv2.cvtColor(area, cv2.COLOR_RGB2BGR)
969
+ # if SaveOP:
970
+ # pushToDropbox(plan,area,perim,df)
971
+ return area
972
+ else:
973
+ imgPerimeter1,Dictionary= PickColorContours(dp,img,in2,in3,in4,in5,in6,in7,in8,in9,in10,in11,num1,num2)
974
+ imgPerimeter1= cv2.cvtColor(imgPerimeter1, cv2.COLOR_RGB2BGR)
975
+ return imgPerimeter1,Dictionary
976
+ # else:
977
+ # areaPerimeterImg,df=PickColorContours(dp,img,in2,in3,in4,in5,in6,in7,in8,in9,in10,in11,num1,num2)
978
  # if SaveOP:
979
  # pushToDropbox(plan,area,perim,df)
980
 
981
  # return area,perim,df
982
+
983
  ##########################################################
984
  def update_dropdown(project):
985
 
 
1007
  drop=gr.Dropdown(choices=None,interactive=True,label='project parts')
1008
  radio_button = gr.Dropdown(choices=['foundation','external','interior'], value=None, interactive=True,label='sections')
1009
 
1010
+ check=gr.Checkbox(label='SaveOutput')
1011
+ show_button = gr.Button(value="Measure",elem_id='search')
1012
+ clr_btn=gr.Button(value='Clear')
1013
+ with gr.Column():
1014
+ img1=gr.Image()
1015
  # img2=gr.Image()
1016
  # df=gr.Dataframe()
1017
 
 
1033
  in9=gr.ColorPicker(label="color" )
1034
  in10=gr.ColorPicker(label="color" )
1035
  in11=gr.ColorPicker(label="color" )
1036
+
 
 
 
1037
  with gr.Column():
1038
+ out1 = gr.Image(label="Image", type="pil", image_mode="RGBA")
 
 
1039
  #gr.Dataframe(label='Dictionary' ), # row_count = (5, "fixed")
1040
  # ]
1041
  # btn1 = gr.Button("Submit")
 
1052
  gr.Image(label="Image", type="pil", image_mode="RGBA"),
1053
  gr.Dataframe(label='Dictionary' ), # row_count = (5, "fixed")
1054
  ]
1055
+ # getMeasurement(plan,SaveOP) #drop, check
1056
+ show_button.click(fn=getMeasurement, inputs=[ drop, check , dp,in2,in3,in4,in5,in6,in7,in8,in9,in10,in11],outputs=img1)
1057
  clr_btn.click(fn=clear,outputs=[project,radio_button,check,drop])
1058
  # btn1.click(fn=PickColorContours, inputs=[dp,in1,in2,in3,in4,in5,in6,in7,in8,in9,in10,in11], outputs=out1)
1059
  #secoond part
1060
+ # btn.click(fn=PickColorContours, inputs=[dp,in2,in3,in4,in5,in6,in7,in8,in9,in10,in11,num1,num2], outputs=outputs1)
1061
+ btn.click(fn=getMeasurement, inputs=[dp,in2,in3,in4,in5,in6,in7,in8,in9,in10,in11,num1,num2],outputs=outputs1)
1062
 
1063
  mainBlock.launch(debug=True,enable_queue=True)
1064