Marthee commited on
Commit
eee2e4a
·
verified ·
1 Parent(s): db0a5b7

Update doors_fasterrcnn.py

Browse files
Files changed (1) hide show
  1. doors_fasterrcnn.py +111 -21
doors_fasterrcnn.py CHANGED
@@ -44,6 +44,11 @@ import google_sheet_Legend
44
  import torch
45
  import torchvision
46
  from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
 
 
 
 
 
47
 
48
  def convert2pillow(path):
49
  pdf = pdfium.PdfDocument(path)
@@ -178,46 +183,127 @@ def width_as_char(real_width):
178
  char_width.append(f"{width}m")
179
  return char_width
180
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
  def add_annotations_to_pdf(image, pdf_name, lines, sanda, char_width, line_midpoint):
182
  image_width, image_height = image.size
183
 
184
  # Create a new PDF document
185
- pdf_document = fitz.open()
186
-
 
 
 
 
 
 
 
187
  # Add a new page to the document with the same dimensions as the image
188
- page = pdf_document.new_page(width=image_width, height=image_height)
189
 
190
- # Insert the image into the PDF page
191
- image_stream = io.BytesIO()
192
- image.save(image_stream, format="PNG")
193
- page.insert_image(page.rect, stream=image_stream.getvalue())
194
 
195
 
196
  #Annotation for drawin lines as in the markups
197
  for i in range(len(line_midpoint)):
198
  x, y = line_midpoint[i]
 
 
199
  text = char_width[i]
200
- rect = fitz.Rect(x, y, x + 200, y + 50) # Adjust the width and height as needed
201
- annot = page.add_freetext_annot(rect, text, fontsize=10, fontname="helv", text_color=(1,0,0))
202
  annot.update()
 
203
  #Annotation for drawin lines as in the markups
204
  for i in range(len(lines)):
205
- annot = page.add_line_annot(lines[i][0], lines[i][1])
206
- annot = page.add_line_annot(sanda[i][0], lines[i][0])
207
- annot = page.add_line_annot(sanda[i][1], lines[i][1])
208
- annot.set_border(width=2, dashes=None) # Optional border styling
209
- annot.set_colors(stroke=(1, 0, 0)) # Set the line color to red
210
- annot.update()
 
 
 
 
 
 
211
 
 
 
 
 
 
212
 
213
- output_pdf_path = pdf_name+"_annotated.pdf"
 
 
 
 
 
214
  # Save the PDF with annotations
215
 
 
216
  return pdf_document
217
 
218
 
219
 
220
- def main_run(pdf_fullpath, weights_path, pdf_name,pdfpath,ratio): ####pdf_fullpath here is the data and not the path
221
  img_pillow = convert2pillow(pdf_fullpath)
222
 
223
  num_classes = 10 # classes + background
@@ -246,13 +332,15 @@ def main_run(pdf_fullpath, weights_path, pdf_name,pdfpath,ratio): ####pdf_fullpa
246
  real_width = pxl2meter(width_pixels, ratio)
247
  char_width = width_as_char(real_width)
248
 
249
- pdf_document = add_annotations_to_pdf(img_pillow, pdf_name, lines, sanda, char_width, line_midpoint)
250
-
 
251
  #Dataframe for Doors count
252
  #doors_count = {'Type': ['Single Doors', 'Double Doors'], 'Quantity': [len(sbox), len(dbox)]}
253
  doors_count = {'Type': ['Single Doors'], 'Quantity': [len(real_width)]}
254
  df_doors = pd.DataFrame(doors_count)
255
-
 
256
  page=pdf_document[0]
257
  pix = page.get_pixmap() # render page to an image
258
  pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
@@ -268,7 +356,9 @@ def main_run(pdf_fullpath, weights_path, pdf_name,pdfpath,ratio): ####pdf_fullpa
268
 
269
 
270
  # modify this return
271
- return annotatedimg, pdf_document , spreadsheet_url, list1, df_doors
 
 
272
 
273
  # model_path = '/content/drive/MyDrive/combined.pth'
274
  # #pdf_name = data
 
44
  import torch
45
  import torchvision
46
  from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
47
+ from PyPDF2 import PdfReader, PdfWriter
48
+ from PyPDF2.generic import TextStringObject, NameObject, ArrayObject, FloatObject
49
+ from PyPDF2.generic import NameObject, TextStringObject, DictionaryObject, FloatObject, ArrayObject
50
+ from PyPDF2 import PdfReader
51
+ from io import BytesIO
52
 
53
  def convert2pillow(path):
54
  pdf = pdfium.PdfDocument(path)
 
183
  char_width.append(f"{width}m")
184
  return char_width
185
 
186
+ def adjustannotations(OutputPdfStage1):
187
+ input_pdf_path = OutputPdfStage1
188
+ # output_pdf_path = "AnnotationAdjusted.pdf"
189
+
190
+ # Load the input PDF
191
+ pdf_bytes_io = BytesIO(OutputPdfStage1)
192
+
193
+ reader = PdfReader(pdf_bytes_io)
194
+ writer = PdfWriter()
195
+
196
+ # Append all pages to the writer
197
+ writer.append_pages_from_reader(reader)
198
+
199
+ # Add metadata (optional)
200
+ metadata = reader.metadata
201
+ writer.add_metadata(metadata)
202
+
203
+ # Iterate over pages
204
+ for page_index, page in enumerate(writer.pages):
205
+ if "/Annots" in page:
206
+ annotations = page["/Annots"]
207
+ for annot_index, annot in enumerate(annotations):
208
+ obj = annot.get_object()
209
+
210
+ # print("obj", obj)
211
+ # print(obj.get("/IT"))
212
+
213
+ if obj.get("/Subtype") == "/Line":
214
+ print("AWL ANNOT IF")
215
+ # Check the /IT value to differentiate annotations
216
+ if "/Contents" in obj and "m" in obj["/Contents"]:
217
+ print("Tany IF")
218
+ obj.update({
219
+ NameObject("/Measure"): DictionaryObject({
220
+ NameObject("/Type"): NameObject("/Measure"),
221
+ NameObject("/L"): DictionaryObject({
222
+ NameObject("/G"): FloatObject(1),
223
+ NameObject("/U"): TextStringObject("sq m"), # Unit of measurement for area
224
+ }),
225
+
226
+ }),
227
+ NameObject("/IT"): NameObject("/LineDimension"), # Use more distinctive name
228
+ NameObject("/Subj"): TextStringObject("Length Measurement"), # Intent explicitly for Area
229
+ })
230
+ print("After Update:", obj)
231
+
232
+ # # Save the modified PDF
233
+
234
+ output_pdf_io = BytesIO()
235
+ writer.write(output_pdf_io)
236
+ output_pdf_io.seek(0)
237
+
238
+ print("Annotations updated and saved ")
239
+ return output_pdf_io.read()
240
+
241
  def add_annotations_to_pdf(image, pdf_name, lines, sanda, char_width, line_midpoint):
242
  image_width, image_height = image.size
243
 
244
  # Create a new PDF document
245
+ pdf_document = fitz.open('pdf',pdf_name)
246
+ page=pdf_document[0]
247
+ rotationOld=page.rotation
248
+ derotationMatrix=page.derotation_matrix
249
+ print('rotationOld',rotationOld)
250
+ if page.rotation!=0:
251
+ rotationangle = page.rotation
252
+ page.set_rotation(0)
253
+ print('rotationnew',page.rotation)
254
  # Add a new page to the document with the same dimensions as the image
255
+ # page = pdf_document.new_page(width=image_width, height=image_height)
256
 
257
+ # # Insert the image into the PDF page
258
+ # image_stream = io.BytesIO()
259
+ # image.save(image_stream, format="PNG")
260
+ # page.insert_image(page.rect, stream=image_stream.getvalue())
261
 
262
 
263
  #Annotation for drawin lines as in the markups
264
  for i in range(len(line_midpoint)):
265
  x, y = line_midpoint[i]
266
+ p_midpoint = fitz.Point(x, y) * derotationMatrix
267
+ rect = fitz.Rect(p_midpoint.x, p_midpoint.y, p_midpoint.x + 200, p_midpoint.y + 50)
268
  text = char_width[i]
269
+ annot = page.add_freetext_annot(rect, text, fontsize=10, fontname="helv", text_color=(1, 0, 0))
 
270
  annot.update()
271
+
272
  #Annotation for drawin lines as in the markups
273
  for i in range(len(lines)):
274
+ # annot = page.add_line_annot(lines[i][0], lines[i][1])
275
+ # # annot = page.add_line_annot(sanda[i][0], lines[i][0])
276
+ # # annot = page.add_line_annot(sanda[i][1], lines[i][1])
277
+ # annot.set_border(width=2, dashes=None) # Optional border styling
278
+ # annot.set_colors(stroke=(1, 0, 0)) # Set the line color to red
279
+ # annot.update()
280
+
281
+ annot = page.add_line_annot(fitz.Point(*lines[i][0]) * derotationMatrix, fitz.Point(*lines[i][1]) * derotationMatrix)
282
+ annot.set_border(width=2, dashes=None) # Optional border styling
283
+ annot.set_colors(stroke=(1, 0, 0)) # Set the line color to red
284
+ annot.set_info(content=str(1.0)+' m',subject='Perimeter Measurement', title="ADR Team")
285
+ annot.update()
286
 
287
+ annot = page.add_line_annot(fitz.Point(*sanda[i][0]) * derotationMatrix, fitz.Point(*lines[i][0]) * derotationMatrix)
288
+ annot.set_border(width=2, dashes=None) # Optional border styling
289
+ annot.set_colors(stroke=(1, 0, 0)) # Set the line color to red
290
+ annot.set_info(content=str(1.0)+' m',subject='Perimeter Measurement', title="ADR Team")
291
+ annot.update()
292
 
293
+ annot = page.add_line_annot(fitz.Point(*sanda[i][1]) * derotationMatrix, fitz.Point(*lines[i][1]) * derotationMatrix)
294
+ annot.set_border(width=2, dashes=None) # Optional border styling
295
+ annot.set_colors(stroke=(1, 0, 0)) # Set the line color to red
296
+ annot.set_info(content=str(1.0)+' m',subject='Perimeter Measurement', title="ADR Team")
297
+ annot.update()
298
+ # output_pdf_path = pdf_name+"_annotated.pdf"
299
  # Save the PDF with annotations
300
 
301
+ page.set_rotation(rotationOld)
302
  return pdf_document
303
 
304
 
305
 
306
+ def main_run(img_pillow,pdf_fullpath, weights_path, pdf_name,pdfpath,ratio): ####pdf_fullpath here is the data and not the path
307
  img_pillow = convert2pillow(pdf_fullpath)
308
 
309
  num_classes = 10 # classes + background
 
332
  real_width = pxl2meter(width_pixels, ratio)
333
  char_width = width_as_char(real_width)
334
 
335
+ pdf_document = add_annotations_to_pdf(img_pillow, pdf_fullpath, lines, sanda, char_width, line_midpoint)
336
+ modified_pdf_data = pdf_document.tobytes()
337
+ OutputPdfStage2=adjustannotations(modified_pdf_data)
338
  #Dataframe for Doors count
339
  #doors_count = {'Type': ['Single Doors', 'Double Doors'], 'Quantity': [len(sbox), len(dbox)]}
340
  doors_count = {'Type': ['Single Doors'], 'Quantity': [len(real_width)]}
341
  df_doors = pd.DataFrame(doors_count)
342
+
343
+ doc2 =fitz.open('pdf',OutputPdfStage2)
344
  page=pdf_document[0]
345
  pix = page.get_pixmap() # render page to an image
346
  pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
 
356
 
357
 
358
  # modify this return
359
+ # OutputPdfStage2=OutputPdfStage2.read()
360
+ # doc2 =fitz.open('pdf',OutputPdfStage2)
361
+ return annotatedimg, doc2 , spreadsheet_url, list1, df_doors
362
 
363
  # model_path = '/content/drive/MyDrive/combined.pth'
364
  # #pdf_name = data