Marthee commited on
Commit
84f8638
·
verified ·
1 Parent(s): c13bf43

Update doors_fasterrcnn.py

Browse files
Files changed (1) hide show
  1. doors_fasterrcnn.py +116 -72
doors_fasterrcnn.py CHANGED
@@ -105,14 +105,16 @@ def get_door_info(doors_info):
105
  lines = []
106
  sanda = []
107
  line_midpoint = []
108
- doubleD_bbox = []
109
- doubleD_width = []
110
-
111
- single_door = 0
 
112
  for door_inf in doors_info:
113
  xmin, ymin, xmax, ymax = door_inf[0]
114
- #horz_bottom
115
- if door_inf[1] == 2:
 
116
  #for drawing
117
  point_st = (int(xmin), int(ymax) + 5)
118
  point_end = (int(xmax),int(ymax) + 5)
@@ -126,10 +128,16 @@ def get_door_info(doors_info):
126
  #for calculation
127
  width = distance((xmin,ymax), (xmax,ymax))
128
  width_pixels.append(width)
129
- #single_door +=1
130
-
131
- #horz_upper
132
- if door_inf[1] == 3:
 
 
 
 
 
 
133
  #for drawing
134
  point_st = (int(xmin),int(ymin) -5)
135
  point_end = (int(xmax),int(ymin) - 5)
@@ -142,9 +150,15 @@ def get_door_info(doors_info):
142
  #for calculation
143
  width = distance((xmin,ymin), (xmax,ymin))
144
  width_pixels.append(width)
145
-
146
- #vert_right
147
- if door_inf[1] == 4:
 
 
 
 
 
 
148
  #for drawing
149
  point_st = (int(xmax) + 5,int(ymin))
150
  point_end = (int(xmax) + 5,int(ymax))
@@ -157,8 +171,15 @@ def get_door_info(doors_info):
157
  #for calculation
158
  width = distance((xmax,ymin), (xmax,ymax))
159
  width_pixels.append(width)
160
- #vert_left
161
- if door_inf[1] == 5:
 
 
 
 
 
 
 
162
  #for drawing
163
  point_st = (int(xmin) -5,int(ymin))
164
  point_end = (int(xmin) -5,int(ymax))
@@ -171,20 +192,27 @@ def get_door_info(doors_info):
171
  #for calculation
172
  width = distance((xmin,ymin), (xmin,ymax))
173
  width_pixels.append(width)
 
 
 
 
 
 
 
174
 
175
  #double door
176
- if door_inf[1] == 1:
177
- doubleD_bbox.append([int(xmin), int(ymin), int(xmax), int(ymax)])
178
- bbox_width = xmax - xmin
179
- bbox_height = ymax - ymin
180
- if bbox_width > bbox_height:
181
- door_width = bbox_width
182
- doubleD_width.append(door_width)
183
- else:
184
- door_width = bbox_height
185
- doubleD_width.append(door_width)
186
-
187
- return width_pixels, lines, sanda, line_midpoint, doubleD_bbox, doubleD_width
188
 
189
  def pxl2meter(width_pixels, ratio):
190
  real_width = []
@@ -253,7 +281,7 @@ def adjustannotations(OutputPdfStage1):
253
  print("Annotations updated and saved ")
254
  return output_pdf_io.read()
255
 
256
- def add_annotations_to_pdf(image, pdf_name, lines, sanda, char_width, line_midpoint, doubleD_bbox, char_d_width):
257
  image_width, image_height = image.size
258
 
259
  # Create a new PDF document
@@ -284,14 +312,14 @@ def add_annotations_to_pdf(image, pdf_name, lines, sanda, char_width, line_midpo
284
  annot = page.add_freetext_annot(rect, text, fontsize=10, fontname="helv", text_color=(1, 0, 0))
285
  annot.update()
286
 
287
- for i in range(len(doubleD_bbox)):
288
  #a7seb ana el midpoint beta3 el bbox 3ashan a7ot el width feha
289
- x,y = calculate_midpoint((doubleD_bbox[i][0], doubleD_bbox[i][1]), (doubleD_bbox[i][2],doubleD_bbox[i][3]))
290
- p_midpoint = fitz.Point(x, y) * derotationMatrix
291
- rect = fitz.Rect(p_midpoint.x, p_midpoint.y, p_midpoint.x + 200, p_midpoint.y + 50)
292
- text = char_d_width[i]
293
- annot = page.add_freetext_annot(rect, text, fontsize=10, fontname="helv", text_color=(1, 0, 0))
294
- annot.update()
295
 
296
 
297
  #Annotation for drawin lines as in the markups
@@ -300,44 +328,59 @@ def add_annotations_to_pdf(image, pdf_name, lines, sanda, char_width, line_midpo
300
  annot = page.add_polyline_annot(l_points)
301
  annot.set_border(width=2, dashes=None) # Optional border styling
302
  annot.set_colors(stroke=(1, 0, 0)) # Set the line color to red
 
303
  annot.update()
304
- for i in range(len(doubleD_bbox)):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305
  # rect_coords should be a tuple: (x1, y1, x2, y2)
306
- tpl1 = (doubleD_bbox[i][0], doubleD_bbox[i][1])
307
- tpl2 = (doubleD_bbox[i][2], doubleD_bbox[i][3])
308
- tpl_derotated = fitz.Point(*tpl1) * derotationMatrix
309
- tpl2_derotated = fitz.Point(*tpl2) * derotationMatrix
310
- print("Derotated tuples:")
311
- print(tpl_derotated)
312
- print("Derotated tuples 2:")
313
- print(tpl2_derotated)
314
  #coordinates_derotated = [tpl_derotated[0],tpl_derotated[1],tpl2_derotated[0],tpl2_derotated[1]]
315
- x1, y1 = tpl_derotated
316
- x2, y2 = tpl2_derotated
317
- x1 = int(x1)
318
- y1 = int(y1)
319
- x2 = int(x2)
320
- y2 = int(y2)
321
- x1, x2 = min(x1, x2), max(x1, x2)
322
- y1, y2 = min(y1, y2), max(y1, y2)
323
 
324
 
325
- print("x1:")
326
- print(x1)
327
- print("y1:")
328
- print(y1)
329
- print("x2:")
330
- print(x2)
331
- print("y2:")
332
- print(y2)
333
 
334
- rect = fitz.Rect(x1, y1, x2, y2)
335
- print("RECT:")
336
- print(rect)
337
- rect_annot = page.add_rect_annot(rect)
338
- rect_annot.set_border(width=2) # Adjust border width
339
- rect_annot.set_colors(stroke=(1, 0, 0)) # Red rectangle
340
- rect_annot.update()
341
 
342
  # annot = page.add_line_annot(lines[i][0], lines[i][1])
343
  # # annot = page.add_line_annot(sanda[i][0], lines[i][0])
@@ -396,17 +439,18 @@ def main_run(img_pillow,pdf_fullpath, weights_path, pdf_name,pdfpath,ratio): ###
396
 
397
  # START INFERENCE
398
  doors_info = ev_model(img_pillow, model, device, 0.9)
399
- width_pixels, lines, sanda, line_midpoint, doubleD_bbox, doubleD_width = get_door_info(doors_info)
 
400
  real_width = pxl2meter(width_pixels, ratio)
401
- real_double = pxl2meter(doubleD_width, ratio)
402
  char_width = width_as_char(real_width)
403
- char_d_width = width_as_char(real_double)
404
 
405
- pdf_document = add_annotations_to_pdf(img_pillow, pdf_fullpath, lines, sanda, char_width, line_midpoint, doubleD_bbox, char_d_width)
406
  modified_pdf_data = pdf_document.tobytes()
407
  OutputPdfStage2=adjustannotations(modified_pdf_data)
408
  #Dataframe for Doors count
409
- doors_count = {'Type': ['Single Doors', 'Double Doors'], 'Quantity': [len(real_width), len(real_double)]}
410
  #doors_count = {'Type': ['Single Doors'], 'Quantity': [len(real_width)]}
411
  df_doors = pd.DataFrame(doors_count)
412
 
 
105
  lines = []
106
  sanda = []
107
  line_midpoint = []
108
+ #doubleD_bbox = []
109
+ #doubleD_width = []
110
+ singles = 0
111
+ doubles = 0
112
+ door_type = []
113
  for door_inf in doors_info:
114
  xmin, ymin, xmax, ymax = door_inf[0]
115
+ #horz_bottom #double_bottom
116
+ if door_inf[1] == 2 or door_inf[1] == 1:
117
+ #if door_inf[1] == 2:
118
  #for drawing
119
  point_st = (int(xmin), int(ymax) + 5)
120
  point_end = (int(xmax),int(ymax) + 5)
 
128
  #for calculation
129
  width = distance((xmin,ymax), (xmax,ymax))
130
  width_pixels.append(width)
131
+ if door_inf[1] == 2:
132
+ singles +=1
133
+ door_type.append(0)
134
+ if door_inf[1] == 1:
135
+ doubles +=1
136
+ door_type.append(1)
137
+
138
+
139
+ #horz_upper #double_upper
140
+ if door_inf[1] == 3 or door_inf[1] == 7:
141
  #for drawing
142
  point_st = (int(xmin),int(ymin) -5)
143
  point_end = (int(xmax),int(ymin) - 5)
 
150
  #for calculation
151
  width = distance((xmin,ymin), (xmax,ymin))
152
  width_pixels.append(width)
153
+ if door_inf[1] == 3:
154
+ singles +=1
155
+ door_type.append(0)
156
+ if door_inf[1] == 7:
157
+ doubles +=1
158
+ door_type.append(1)
159
+
160
+ #vert_right #double_right
161
+ if door_inf[1] == 5 or door_inf[1] == 8:
162
  #for drawing
163
  point_st = (int(xmax) + 5,int(ymin))
164
  point_end = (int(xmax) + 5,int(ymax))
 
171
  #for calculation
172
  width = distance((xmax,ymin), (xmax,ymax))
173
  width_pixels.append(width)
174
+ if door_inf[1] == 5:
175
+ singles +=1
176
+ door_type.append(0)
177
+ if door_inf[1] == 8:
178
+ doubles +=1
179
+ door_type.append(1)
180
+
181
+ #vert_left #double_left
182
+ if door_inf[1] == 6 or door_inf[1] == 4:
183
  #for drawing
184
  point_st = (int(xmin) -5,int(ymin))
185
  point_end = (int(xmin) -5,int(ymax))
 
192
  #for calculation
193
  width = distance((xmin,ymin), (xmin,ymax))
194
  width_pixels.append(width)
195
+ if door_inf[1] ==6:
196
+ singles +=1
197
+ door_type.append(0)
198
+ if door_inf[1] == 4:
199
+ doubles +=1
200
+ door_type.append(1)
201
+
202
 
203
  #double door
204
+ #if door_inf[1] == 1:
205
+ # doubleD_bbox.append([int(xmin), int(ymin), int(xmax), int(ymax)])
206
+ #bbox_width = xmax - xmin
207
+ #bbox_height = ymax - ymin
208
+ #if bbox_width > bbox_height:
209
+ # door_width = bbox_width
210
+ #doubleD_width.append(door_width)
211
+ #else:
212
+ # door_width = bbox_height
213
+ #doubleD_width.append(door_width)
214
+
215
+ return width_pixels, lines, sanda, line_midpoint, singles, doubles, door_type
216
 
217
  def pxl2meter(width_pixels, ratio):
218
  real_width = []
 
281
  print("Annotations updated and saved ")
282
  return output_pdf_io.read()
283
 
284
+ def add_annotations_to_pdf(image, pdf_name, lines, sanda, char_width, line_midpoint, door_type):
285
  image_width, image_height = image.size
286
 
287
  # Create a new PDF document
 
312
  annot = page.add_freetext_annot(rect, text, fontsize=10, fontname="helv", text_color=(1, 0, 0))
313
  annot.update()
314
 
315
+ #for i in range(len(doubleD_bbox)):
316
  #a7seb ana el midpoint beta3 el bbox 3ashan a7ot el width feha
317
+ #x,y = calculate_midpoint((doubleD_bbox[i][0], doubleD_bbox[i][1]), (doubleD_bbox[i][2],doubleD_bbox[i][3]))
318
+ #p_midpoint = fitz.Point(x, y) * derotationMatrix
319
+ #rect = fitz.Rect(p_midpoint.x, p_midpoint.y, p_midpoint.x + 200, p_midpoint.y + 50)
320
+ #text = char_d_width[i]
321
+ #annot = page.add_freetext_annot(rect, text, fontsize=10, fontname="helv", text_color=(1, 0, 0))
322
+ #annot.update()
323
 
324
 
325
  #Annotation for drawin lines as in the markups
 
328
  annot = page.add_polyline_annot(l_points)
329
  annot.set_border(width=2, dashes=None) # Optional border styling
330
  annot.set_colors(stroke=(1, 0, 0)) # Set the line color to red
331
+ annot.set_info(content=str(char_width[i])+' m',subject='Perimeter Measurement', title="ADR Team")
332
  annot.update()
333
+
334
+ for i in range(len(door_type)):
335
+ x, y = line_midpoint[i]
336
+ p_midpoint = fitz.Point(x, y) * derotationMatrix
337
+ if door_type[i] == 0:
338
+ text = "Single Door"
339
+ else:
340
+ text = "Double Door"
341
+ # Create an annotation (sticky note)
342
+ annot = page.add_text_annot((p_midpoint.x p_midpoint.y), text)
343
+ annot.set_border(width=0.2, dashes=(1, 2)) # Optional border styling
344
+ annot.set_colors(stroke=(1, 0, 0), fill=None) # Set the stroke color to red
345
+ annot.update()
346
+
347
+ #for i in range(len(doubleD_bbox)):
348
  # rect_coords should be a tuple: (x1, y1, x2, y2)
349
+ #tpl1 = (doubleD_bbox[i][0], doubleD_bbox[i][1])
350
+ #tpl2 = (doubleD_bbox[i][2], doubleD_bbox[i][3])
351
+ #tpl_derotated = fitz.Point(*tpl1) * derotationMatrix
352
+ #tpl2_derotated = fitz.Point(*tpl2) * derotationMatrix
353
+ #print("Derotated tuples:")
354
+ #print(tpl_derotated)
355
+ #print("Derotated tuples 2:")
356
+ #print(tpl2_derotated)
357
  #coordinates_derotated = [tpl_derotated[0],tpl_derotated[1],tpl2_derotated[0],tpl2_derotated[1]]
358
+ #x1, y1 = tpl_derotated
359
+ #x2, y2 = tpl2_derotated
360
+ #x1 = int(x1)
361
+ #y1 = int(y1)
362
+ #x2 = int(x2)
363
+ #y2 = int(y2)
364
+ #x1, x2 = min(x1, x2), max(x1, x2)
365
+ #y1, y2 = min(y1, y2), max(y1, y2)
366
 
367
 
368
+ #print("x1:")
369
+ #print(x1)
370
+ #print("y1:")
371
+ #print(y1)
372
+ #print("x2:")
373
+ #print(x2)
374
+ #print("y2:")
375
+ #print(y2)
376
 
377
+ #rect = fitz.Rect(x1, y1, x2, y2)
378
+ #print("RECT:")
379
+ #print(rect)
380
+ #rect_annot = page.add_rect_annot(rect)
381
+ #rect_annot.set_border(width=2) # Adjust border width
382
+ #rect_annot.set_colors(stroke=(1, 0, 0)) # Red rectangle
383
+ #rect_annot.update()
384
 
385
  # annot = page.add_line_annot(lines[i][0], lines[i][1])
386
  # # annot = page.add_line_annot(sanda[i][0], lines[i][0])
 
439
 
440
  # START INFERENCE
441
  doors_info = ev_model(img_pillow, model, device, 0.9)
442
+ #width_pixels, lines, sanda, line_midpoint, doubleD_bbox, doubleD_width = get_door_info(doors_info)
443
+ width_pixels, lines, sanda, line_midpoint, single_count, double_count, door_type = get_door_info(doors_info)
444
  real_width = pxl2meter(width_pixels, ratio)
445
+ #real_double = pxl2meter(doubleD_width, ratio)
446
  char_width = width_as_char(real_width)
447
+ #char_d_width = width_as_char(real_double)
448
 
449
+ pdf_document = add_annotations_to_pdf(img_pillow, pdf_fullpath, lines, sanda, char_width, line_midpoint, door_type)
450
  modified_pdf_data = pdf_document.tobytes()
451
  OutputPdfStage2=adjustannotations(modified_pdf_data)
452
  #Dataframe for Doors count
453
+ doors_count = {'Type': ['Single Doors', 'Double Doors'], 'Quantity': [single_count, double_count]}
454
  #doors_count = {'Type': ['Single Doors'], 'Quantity': [len(real_width)]}
455
  df_doors = pd.DataFrame(doors_count)
456