Spaces:
Sleeping
Sleeping
Update Counting_Columns_2_1.py
Browse files- Counting_Columns_2_1.py +31 -9
Counting_Columns_2_1.py
CHANGED
|
@@ -9,6 +9,7 @@ import google_sheet_Legend
|
|
| 9 |
import pypdfium2 as pdfium
|
| 10 |
import fitz # PyMuPDF
|
| 11 |
import os
|
|
|
|
| 12 |
|
| 13 |
def get_text_from_pdf(input_pdf_path):
|
| 14 |
pdf_document = fitz.open('pdf',input_pdf_path)
|
|
@@ -167,7 +168,21 @@ def generate_legend(found_tuple):
|
|
| 167 |
data = word_freq
|
| 168 |
df = pd.DataFrame(data.items(), columns=['Column Type', 'Count'])
|
| 169 |
return df
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
'''def add_annotations_to_pdf(image, pdf_name, slctd_clm, columns_types_v):
|
| 172 |
image_width = image.shape[1]
|
| 173 |
image_height = image.shape[0]
|
|
@@ -191,7 +206,7 @@ def generate_legend(found_tuple):
|
|
| 191 |
page.set_rotation(rotationOld)
|
| 192 |
return pdf_document'''
|
| 193 |
|
| 194 |
-
def add_annotations_to_pdf(image, pdf_name,
|
| 195 |
image_width = image.shape[1]
|
| 196 |
image_height = image.shape[0]
|
| 197 |
# Create a new PDF document
|
|
@@ -202,21 +217,24 @@ def add_annotations_to_pdf(image, pdf_name, slctd_clm, columns_types_v):
|
|
| 202 |
if page.rotation!=0:
|
| 203 |
rotationangle = page.rotation
|
| 204 |
page.set_rotation(0)
|
| 205 |
-
for i in range(len(slctd_clm)):
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
| 207 |
p_midpoint = fitz.Point(x, y) * derotationMatrix
|
| 208 |
annot = page.add_circle_annot(
|
| 209 |
fitz.Rect(p_midpoint.x - 10, p_midpoint.y - 10, p_midpoint.x + 10,p_midpoint.y + 10) # Small circle
|
| 210 |
)
|
| 211 |
# ✅ Assign required Bluebeam metadata
|
| 212 |
-
annot.set_colors(stroke=
|
| 213 |
annot.set_border(width=2) # Border thickness
|
| 214 |
annot.set_opacity(1) # Fully visible
|
| 215 |
text = columns_types_v[i]
|
| 216 |
# ✅ Set annotation properties for Bluebeam Count detection
|
| 217 |
-
annot.set_info("name",
|
| 218 |
annot.set_info("subject", "Count") # ✅ Bluebeam uses "Count" for Count markups
|
| 219 |
-
annot.set_info("title",
|
| 220 |
annot.update() # Apply changes
|
| 221 |
page.set_rotation(rotationOld)
|
| 222 |
return pdf_document
|
|
@@ -237,12 +255,14 @@ def mainfun(pdf_name,pdfpath,planname):
|
|
| 237 |
imgResult = segment_brown(img)
|
| 238 |
outsu = threshold(imgResult)
|
| 239 |
column_points,mask_clmns, mask_walls = get_columns_info(outsu, img)
|
| 240 |
-
|
|
|
|
| 241 |
if len(column_points) > 10:
|
| 242 |
# BROWN COLUMNS
|
| 243 |
nearby, slctd_clm = getNearestText(text_points, column_points)
|
| 244 |
columns_types_v = getColumnsTypesKeyValue(nearby, txtpts_ky_vlu)
|
| 245 |
legend = generate_legend(columns_types_v)
|
|
|
|
| 246 |
|
| 247 |
else:
|
| 248 |
# BLUE COLUMNS
|
|
@@ -253,7 +273,9 @@ def mainfun(pdf_name,pdfpath,planname):
|
|
| 253 |
nearby, slctd_clm = getNearestText(text_points, column_points)
|
| 254 |
columns_types_v = getColumnsTypesKeyValue(nearby, txtpts_ky_vlu)
|
| 255 |
legend = generate_legend(columns_types_v)
|
| 256 |
-
|
|
|
|
|
|
|
| 257 |
page=pdf_document[0]
|
| 258 |
pix = page.get_pixmap() # render page to an image
|
| 259 |
pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
|
|
|
|
| 9 |
import pypdfium2 as pdfium
|
| 10 |
import fitz # PyMuPDF
|
| 11 |
import os
|
| 12 |
+
import random
|
| 13 |
|
| 14 |
def get_text_from_pdf(input_pdf_path):
|
| 15 |
pdf_document = fitz.open('pdf',input_pdf_path)
|
|
|
|
| 168 |
data = word_freq
|
| 169 |
df = pd.DataFrame(data.items(), columns=['Column Type', 'Count'])
|
| 170 |
return df
|
| 171 |
+
|
| 172 |
+
def color_groups(txtpts_ky_vlu):
|
| 173 |
+
unique_labels = list(set(txtpts_ky_vlu.values()))
|
| 174 |
+
def generate_rgb():
|
| 175 |
+
return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) # RGB tuple
|
| 176 |
+
key_colors = {key: generate_rgb() for key in unique_labels} # Assign a unique RGB color to each key
|
| 177 |
+
return key_colors
|
| 178 |
+
|
| 179 |
+
def get_drawing_info(txt_clmn,txtpts_ky_vlu,key_colors):
|
| 180 |
+
#Search for each word in the txt_clmn to get the word associated to it
|
| 181 |
+
huge_list_clmn_clr_loc = []
|
| 182 |
+
for text_location, column_location in txt_clmn:
|
| 183 |
+
word = txtpts_ky_vlu[text_location]
|
| 184 |
+
huge_list_clmn_clr_loc.append((text_location, column_location, word, key_colors[word]))
|
| 185 |
+
return huge_list_clmn_clr_loc #text_location, column_location, word, color
|
| 186 |
'''def add_annotations_to_pdf(image, pdf_name, slctd_clm, columns_types_v):
|
| 187 |
image_width = image.shape[1]
|
| 188 |
image_height = image.shape[0]
|
|
|
|
| 206 |
page.set_rotation(rotationOld)
|
| 207 |
return pdf_document'''
|
| 208 |
|
| 209 |
+
def add_annotations_to_pdf(image, pdf_name, huge_list_clmn_clr_loc):
|
| 210 |
image_width = image.shape[1]
|
| 211 |
image_height = image.shape[0]
|
| 212 |
# Create a new PDF document
|
|
|
|
| 217 |
if page.rotation!=0:
|
| 218 |
rotationangle = page.rotation
|
| 219 |
page.set_rotation(0)
|
| 220 |
+
#for i in range(len(slctd_clm)):
|
| 221 |
+
for text_loc, column_loc, word, color in huge_list_clmn_clr_loc:
|
| 222 |
+
x, y = column_loc
|
| 223 |
+
clr = (clr[0] / 255, clr[1] / 255, clr[2] / 255)
|
| 224 |
+
#x, y = slctd_clm[i]
|
| 225 |
p_midpoint = fitz.Point(x, y) * derotationMatrix
|
| 226 |
annot = page.add_circle_annot(
|
| 227 |
fitz.Rect(p_midpoint.x - 10, p_midpoint.y - 10, p_midpoint.x + 10,p_midpoint.y + 10) # Small circle
|
| 228 |
)
|
| 229 |
# ✅ Assign required Bluebeam metadata
|
| 230 |
+
annot.set_colors(stroke=clr, fill=(1, 1, 1)) # Set stroke color and fill white
|
| 231 |
annot.set_border(width=2) # Border thickness
|
| 232 |
annot.set_opacity(1) # Fully visible
|
| 233 |
text = columns_types_v[i]
|
| 234 |
# ✅ Set annotation properties for Bluebeam Count detection
|
| 235 |
+
annot.set_info("name", word) # Unique name for each count
|
| 236 |
annot.set_info("subject", "Count") # ✅ Bluebeam uses "Count" for Count markups
|
| 237 |
+
annot.set_info("title", word) # Optional
|
| 238 |
annot.update() # Apply changes
|
| 239 |
page.set_rotation(rotationOld)
|
| 240 |
return pdf_document
|
|
|
|
| 255 |
imgResult = segment_brown(img)
|
| 256 |
outsu = threshold(imgResult)
|
| 257 |
column_points,mask_clmns, mask_walls = get_columns_info(outsu, img)
|
| 258 |
+
key_colors = color_groups(txtpts_ky_vlu)
|
| 259 |
+
|
| 260 |
if len(column_points) > 10:
|
| 261 |
# BROWN COLUMNS
|
| 262 |
nearby, slctd_clm = getNearestText(text_points, column_points)
|
| 263 |
columns_types_v = getColumnsTypesKeyValue(nearby, txtpts_ky_vlu)
|
| 264 |
legend = generate_legend(columns_types_v)
|
| 265 |
+
huge_list_clmn_clr_loc = get_drawing_info(txt_clmn,txtpts_ky_vlu,key_colors)
|
| 266 |
|
| 267 |
else:
|
| 268 |
# BLUE COLUMNS
|
|
|
|
| 273 |
nearby, slctd_clm = getNearestText(text_points, column_points)
|
| 274 |
columns_types_v = getColumnsTypesKeyValue(nearby, txtpts_ky_vlu)
|
| 275 |
legend = generate_legend(columns_types_v)
|
| 276 |
+
huge_list_clmn_clr_loc = get_drawing_info(txt_clmn,txtpts_ky_vlu,key_colors)
|
| 277 |
+
|
| 278 |
+
pdf_document = add_annotations_to_pdf(img, pdf_name, huge_list_clmn_clr_loc)
|
| 279 |
page=pdf_document[0]
|
| 280 |
pix = page.get_pixmap() # render page to an image
|
| 281 |
pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
|