Marthee commited on
Commit
249e9ba
·
verified ·
1 Parent(s): d9d91e4

Update Counting_Columns_2_1.py

Browse files
Files changed (1) hide show
  1. Counting_Columns_2_1.py +64 -3
Counting_Columns_2_1.py CHANGED
@@ -96,10 +96,37 @@ def get_columns_info(outsu4, img):
96
 
97
  def getTextsPoints(x):
98
  point_list = []
 
99
  for h in x:
100
- point_list.append((h[2],h[3]))
101
- return point_list
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
 
 
 
 
 
 
103
 
104
  def distance(point1, point2):
105
  x1, y1 = point1
@@ -139,6 +166,40 @@ def generate_legend(found_tuple):
139
  return df
140
 
141
  def mainfun(plan):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  texts_from_pdf = get_text_from_pdf(plan)
143
  img = convert2img(plan)
144
  imgResult = segment_brown(img)
@@ -166,4 +227,4 @@ def mainfun(plan):
166
  nearby = fix_rotation_90(pc_coordinates)
167
  columns_types = getColumnsTypes(nearby, texts_from_pdf)
168
  legend = generate_legend(columns_types)
169
- return legend
 
96
 
97
  def getTextsPoints(x):
98
  point_list = []
99
+ pt_clm = {}
100
  for h in x:
101
+ point_list.append(calculate_midpoint(h[1],h[0],h[3],h[2]))
102
+ pt_clm[calculate_midpoint(h[1],h[0],h[3],h[2])] = h[4]
103
+ return point_list, pt_clm
104
+
105
+ def fix_90_ky_val(pt_clm):
106
+ new_derotated = {}
107
+ for ky in pt_clm:
108
+ pts = fitz.Point(ky[0], ky[1]) * derotationMatrix
109
+ new_ky = ((int(pts.y),int(pts.x)))
110
+ new_derotated[new_ky] = pt_clm[ky]
111
+ return new_derotated
112
+
113
+ def calculate_midpoint(x1,y1,x2,y2):
114
+ xm = int((x1 + x2) / 2)
115
+ ym = int((y1 + y2) / 2)
116
+ return (xm, ym)
117
+
118
+ def getColumnsTypesKeyValue(nearbyy, pt_clm):
119
+ words = []
120
+ for i in range(len(nearbyy)):
121
+ words.append(pt_clm[nearbyy[i]])
122
+ return words
123
 
124
+ def fix_rotation_90(pc_coordinates, derotationMatrix):
125
+ coor = []
126
+ for coordinate in pc_coordinates:
127
+ pts = fitz.Point(coordinate[0], coordinate[1]) * derotationMatrix
128
+ coor.append((int(pts.y),int(pts.x)))
129
+ return coor
130
 
131
  def distance(point1, point2):
132
  x1, y1 = point1
 
166
  return df
167
 
168
  def mainfun(plan):
169
+ pdf_document = fitz.open(plan)
170
+ page = pdf_document[0]
171
+ rotation = page.rotation
172
+ derotationMatrix=page.derotation_matrix
173
+ texts_from_pdf = get_text_from_pdf(plan)
174
+ text_points, txtpts_ky_vlu = getTextsPoints(texts_from_pdf)
175
+ if rotation != 0:
176
+ if rotation ==90:
177
+ text_points = fix_rotation_90(text_points, derotationMatrix)
178
+ txtpts_ky_vlu = fix_90_ky_val(txtpts_ky_vlu)
179
+
180
+ img = convert2img(plan)
181
+ imgResult = segment_brown(img)
182
+ outsu = threshold(imgResult)
183
+ column_points,mask_clmns, mask_walls = get_columns_info(outsu, img)
184
+
185
+ if len(column_points) > 10:
186
+ # BROWN COLUMNS
187
+ nearby = getNearestText(text_points, column_points)
188
+ columns_types_v = getColumnsTypesKeyValue(nearby, txtpts_ky_vlu)
189
+ legend = generate_legend(columns_types_v)
190
+
191
+ else:
192
+ # BLUE COLUMNS
193
+ img_blue = changeGrayModify(img)
194
+ imgResult = segment_blue(img_blue)
195
+ outsu = threshold(imgResult)
196
+ column_points,mask_clmns, mask_walls = get_columns_info(outsu, img)
197
+ nearby = getNearestText(text_points, column_points)
198
+ columns_types_v = getColumnsTypesKeyValue(nearby, txtpts_ky_vlu)
199
+ legend = generate_legend(columns_types_v)
200
+ return legend
201
+
202
+ '''def mainfun(plan):
203
  texts_from_pdf = get_text_from_pdf(plan)
204
  img = convert2img(plan)
205
  imgResult = segment_brown(img)
 
227
  nearby = fix_rotation_90(pc_coordinates)
228
  columns_types = getColumnsTypes(nearby, texts_from_pdf)
229
  legend = generate_legend(columns_types)
230
+ return legend'''