Spaces:
Sleeping
Sleeping
Update doors_fasterrcnn.py
Browse files- doors_fasterrcnn.py +95 -64
doors_fasterrcnn.py
CHANGED
|
@@ -104,7 +104,7 @@ def calculate_midpoint(p1, p2):
|
|
| 104 |
def get_door_info(doors_info):
|
| 105 |
width_pixels = []
|
| 106 |
lines = []
|
| 107 |
-
sanda = []
|
| 108 |
line_midpoint = []
|
| 109 |
singles = 0
|
| 110 |
door_type = []
|
|
@@ -113,15 +113,19 @@ def get_door_info(doors_info):
|
|
| 113 |
#horz_bottom
|
| 114 |
if door_inf[1] == 2:
|
| 115 |
#for drawing
|
| 116 |
-
point_st = (int(xmin), int(ymax) + 5)
|
| 117 |
-
point_end = (int(xmax),int(ymax) + 5)
|
|
|
|
|
|
|
|
|
|
| 118 |
lines.append((point_st,point_end))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
-
|
| 121 |
-
sand_end = (int(xmax),int(ymax))
|
| 122 |
-
sanda.append((sanda_st, sand_end))
|
| 123 |
-
|
| 124 |
-
line_midpoint.append(calculate_midpoint(sanda_st,sand_end))
|
| 125 |
#for calculation
|
| 126 |
width = distance((xmin,ymax), (xmax,ymax))
|
| 127 |
width_pixels.append(width)
|
|
@@ -131,14 +135,18 @@ def get_door_info(doors_info):
|
|
| 131 |
#horz_upper
|
| 132 |
if door_inf[1] == 3:
|
| 133 |
#for drawing
|
| 134 |
-
point_st = (int(xmin),int(ymin)
|
| 135 |
-
point_end = (int(xmax),int(ymin)
|
|
|
|
|
|
|
|
|
|
| 136 |
lines.append((point_st,point_end))
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
|
|
|
| 142 |
#for calculation
|
| 143 |
width = distance((xmin,ymin), (xmax,ymin))
|
| 144 |
width_pixels.append(width)
|
|
@@ -148,14 +156,18 @@ def get_door_info(doors_info):
|
|
| 148 |
#vert_right
|
| 149 |
if door_inf[1] == 4:
|
| 150 |
#for drawing
|
| 151 |
-
point_st = (int(xmax) + 5,int(ymin))
|
| 152 |
-
point_end = (int(xmax) + 5,int(ymax))
|
| 153 |
-
lines.append((point_st,point_end))
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
line_midpoint.append(calculate_midpoint(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
#for calculation
|
| 160 |
width = distance((xmax,ymin), (xmax,ymax))
|
| 161 |
width_pixels.append(width)
|
|
@@ -164,38 +176,45 @@ def get_door_info(doors_info):
|
|
| 164 |
#vert_left
|
| 165 |
if door_inf[1] == 5:
|
| 166 |
#for drawing
|
| 167 |
-
point_st = (int(xmin)
|
| 168 |
-
point_end = (int(xmin)
|
|
|
|
|
|
|
|
|
|
| 169 |
lines.append((point_st,point_end))
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
|
|
|
| 175 |
#for calculation
|
| 176 |
width = distance((xmin,ymin), (xmin,ymax))
|
| 177 |
width_pixels.append(width)
|
| 178 |
singles +=1
|
| 179 |
door_type.append(0)
|
| 180 |
|
| 181 |
-
return width_pixels, lines,
|
| 182 |
|
| 183 |
-
def get_door_info_double(doors_info_double, width_pixels, lines,
|
| 184 |
doubles = 0
|
| 185 |
for door_inf in doors_info_double:
|
| 186 |
xmin, ymin, xmax, ymax = door_inf[0]
|
| 187 |
|
| 188 |
#double_bottom
|
| 189 |
if door_inf[1] == 1:
|
| 190 |
-
point_st = (int(xmin), int(ymax)
|
| 191 |
-
point_end = (int(xmax),int(ymax)
|
|
|
|
|
|
|
|
|
|
| 192 |
lines.append((point_st,point_end))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
-
|
| 195 |
-
sand_end = (int(xmax),int(ymax))
|
| 196 |
-
sanda.append((sanda_st, sand_end))
|
| 197 |
-
|
| 198 |
-
line_midpoint.append(calculate_midpoint(sanda_st,sand_end))
|
| 199 |
#for calculation
|
| 200 |
width = distance((xmin,ymax), (xmax,ymax))
|
| 201 |
width_pixels.append(width)
|
|
@@ -206,14 +225,18 @@ def get_door_info_double(doors_info_double, width_pixels, lines, sanda, line_mid
|
|
| 206 |
#double_upper
|
| 207 |
if door_inf[1] == 7:
|
| 208 |
#for drawing
|
| 209 |
-
point_st = (int(xmin),int(ymin)
|
| 210 |
-
point_end = (int(xmax),int(ymin)
|
|
|
|
|
|
|
|
|
|
| 211 |
lines.append((point_st,point_end))
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
|
|
|
| 217 |
#for calculation
|
| 218 |
width = distance((xmin,ymin), (xmax,ymin))
|
| 219 |
width_pixels.append(width)
|
|
@@ -223,14 +246,17 @@ def get_door_info_double(doors_info_double, width_pixels, lines, sanda, line_mid
|
|
| 223 |
#double_right
|
| 224 |
if door_inf[1] == 8:
|
| 225 |
#for drawing
|
| 226 |
-
point_st = (int(xmax) + 5,int(ymin))
|
| 227 |
-
point_end = (int(xmax) + 5,int(ymax))
|
|
|
|
|
|
|
|
|
|
| 228 |
lines.append((point_st,point_end))
|
| 229 |
-
|
| 230 |
-
sanda_st = (int(xmax), int(ymin))
|
| 231 |
-
sand_end = (int(xmax), int(ymax))
|
| 232 |
-
sanda.append((sanda_st, sand_end))
|
| 233 |
-
line_midpoint.append(calculate_midpoint(sanda_st,sand_end))
|
| 234 |
#for calculation
|
| 235 |
width = distance((xmax,ymin), (xmax,ymax))
|
| 236 |
width_pixels.append(width)
|
|
@@ -240,21 +266,25 @@ def get_door_info_double(doors_info_double, width_pixels, lines, sanda, line_mid
|
|
| 240 |
#double_left
|
| 241 |
if door_inf[1] == 4:
|
| 242 |
#for drawing
|
| 243 |
-
point_st = (int(xmin) -5,int(ymin))
|
| 244 |
-
point_end = (int(xmin) -5,int(ymax))
|
| 245 |
-
lines.append((point_st,point_end))
|
| 246 |
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
line_midpoint.append(calculate_midpoint(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
#for calculation
|
| 252 |
width = distance((xmin,ymin), (xmin,ymax))
|
| 253 |
width_pixels.append(width)
|
| 254 |
doubles +=1
|
| 255 |
door_type.append(1)
|
| 256 |
|
| 257 |
-
return width_pixels, lines,
|
| 258 |
|
| 259 |
def pxl2meter(width_pixels, ratio):
|
| 260 |
real_width = []
|
|
@@ -323,7 +353,7 @@ def adjustannotations(OutputPdfStage1):
|
|
| 323 |
print("Annotations updated and saved ")
|
| 324 |
return output_pdf_io.read()
|
| 325 |
|
| 326 |
-
def add_annotations_to_pdf(image, pdf_name, lines,
|
| 327 |
image_width, image_height = image.size
|
| 328 |
|
| 329 |
# Create a new PDF document
|
|
@@ -366,7 +396,8 @@ def add_annotations_to_pdf(image, pdf_name, lines, sanda, char_width, line_midpo
|
|
| 366 |
|
| 367 |
#Annotation for drawin lines as in the markups
|
| 368 |
for i in range(len(lines)):
|
| 369 |
-
l_points = [fitz.Point(*
|
|
|
|
| 370 |
annot = page.add_polyline_annot(l_points)
|
| 371 |
annot.set_border(width=2, dashes=None) # Optional border styling
|
| 372 |
annot.set_colors(stroke=(1, 0, 0)) # Set the line color to red
|
|
@@ -431,9 +462,9 @@ def main_run(img_pillow,pdf_fullpath, weights_path, weights_path2, pdf_name,pdfp
|
|
| 431 |
doors_info_double = ev_model(img_pillow, model2, device2, 0.8)
|
| 432 |
|
| 433 |
|
| 434 |
-
width_pixels, lines,
|
| 435 |
|
| 436 |
-
width_pixels, lines,
|
| 437 |
|
| 438 |
real_width = pxl2meter(width_pixels, ratio)
|
| 439 |
char_width = width_as_char(real_width)
|
|
|
|
| 104 |
def get_door_info(doors_info):
|
| 105 |
width_pixels = []
|
| 106 |
lines = []
|
| 107 |
+
#sanda = []
|
| 108 |
line_midpoint = []
|
| 109 |
singles = 0
|
| 110 |
door_type = []
|
|
|
|
| 113 |
#horz_bottom
|
| 114 |
if door_inf[1] == 2:
|
| 115 |
#for drawing
|
| 116 |
+
#point_st = (int(xmin), int(ymax) + 5)
|
| 117 |
+
#point_end = (int(xmax),int(ymax) + 5)
|
| 118 |
+
|
| 119 |
+
point_st = (int(xmin), int(ymax))
|
| 120 |
+
point_end = (int(xmax),int(ymax))
|
| 121 |
lines.append((point_st,point_end))
|
| 122 |
+
line_midpoint.append(calculate_midpoint(point_st,point_end))
|
| 123 |
+
|
| 124 |
+
#sanda_st = (int(xmin), int(ymax))
|
| 125 |
+
#sand_end = (int(xmax),int(ymax))
|
| 126 |
+
#sanda.append((sanda_st, sand_end))
|
| 127 |
|
| 128 |
+
#line_midpoint.append(calculate_midpoint(sanda_st,sand_end))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
#for calculation
|
| 130 |
width = distance((xmin,ymax), (xmax,ymax))
|
| 131 |
width_pixels.append(width)
|
|
|
|
| 135 |
#horz_upper
|
| 136 |
if door_inf[1] == 3:
|
| 137 |
#for drawing
|
| 138 |
+
point_st = (int(xmin),int(ymin))
|
| 139 |
+
point_end = (int(xmax),int(ymin))
|
| 140 |
+
|
| 141 |
+
#point_st = (int(xmin),int(ymin) -5)
|
| 142 |
+
#point_end = (int(xmax),int(ymin) - 5)
|
| 143 |
lines.append((point_st,point_end))
|
| 144 |
+
line_midpoint.append(calculate_midpoint(point_st, point_end))
|
| 145 |
+
|
| 146 |
+
#sanda_st = (int(xmin),int(ymin))
|
| 147 |
+
#sand_end = (int(xmax),int(ymin))
|
| 148 |
+
#sanda.append((sanda_st, sand_end))
|
| 149 |
+
#line_midpoint.append(calculate_midpoint(sanda_st,sand_end))
|
| 150 |
#for calculation
|
| 151 |
width = distance((xmin,ymin), (xmax,ymin))
|
| 152 |
width_pixels.append(width)
|
|
|
|
| 156 |
#vert_right
|
| 157 |
if door_inf[1] == 4:
|
| 158 |
#for drawing
|
| 159 |
+
#point_st = (int(xmax) + 5,int(ymin))
|
| 160 |
+
#point_end = (int(xmax) + 5,int(ymax))
|
|
|
|
| 161 |
|
| 162 |
+
point_st = (int(xmax), int(ymin))
|
| 163 |
+
point_end = (int(xmax), int(ymax))
|
| 164 |
+
lines.append((point_st,point_end))
|
| 165 |
+
line_midpoint.append(calculate_midpoint(point_st,point_end))
|
| 166 |
+
|
| 167 |
+
#sanda_st = (int(xmax), int(ymin))
|
| 168 |
+
#sand_end = (int(xmax), int(ymax))
|
| 169 |
+
#sanda.append((sanda_st, sand_end))
|
| 170 |
+
#line_midpoint.append(calculate_midpoint(sanda_st,sand_end))
|
| 171 |
#for calculation
|
| 172 |
width = distance((xmax,ymin), (xmax,ymax))
|
| 173 |
width_pixels.append(width)
|
|
|
|
| 176 |
#vert_left
|
| 177 |
if door_inf[1] == 5:
|
| 178 |
#for drawing
|
| 179 |
+
point_st = (int(xmin),int(ymin))
|
| 180 |
+
point_end = (int(xmin),int(ymax))
|
| 181 |
+
|
| 182 |
+
#point_st = (int(xmin) -5,int(ymin))
|
| 183 |
+
#point_end = (int(xmin) -5,int(ymax))
|
| 184 |
lines.append((point_st,point_end))
|
| 185 |
+
line_midpoint.append(calculate_midpoint(point_st,point_end))
|
| 186 |
+
|
| 187 |
+
#sanda_st = (int(xmin),int(ymin))
|
| 188 |
+
#sand_end = (int(xmin),int(ymax))
|
| 189 |
+
#sanda.append((sanda_st, sand_end))
|
| 190 |
+
#line_midpoint.append(calculate_midpoint(sanda_st,sand_end))
|
| 191 |
#for calculation
|
| 192 |
width = distance((xmin,ymin), (xmin,ymax))
|
| 193 |
width_pixels.append(width)
|
| 194 |
singles +=1
|
| 195 |
door_type.append(0)
|
| 196 |
|
| 197 |
+
return width_pixels, lines, line_midpoint, singles, door_type
|
| 198 |
|
| 199 |
+
def get_door_info_double(doors_info_double, width_pixels, lines, line_midpoint, door_type):
|
| 200 |
doubles = 0
|
| 201 |
for door_inf in doors_info_double:
|
| 202 |
xmin, ymin, xmax, ymax = door_inf[0]
|
| 203 |
|
| 204 |
#double_bottom
|
| 205 |
if door_inf[1] == 1:
|
| 206 |
+
point_st = (int(xmin), int(ymax))
|
| 207 |
+
point_end = (int(xmax),int(ymax))
|
| 208 |
+
|
| 209 |
+
#point_st = (int(xmin), int(ymax) + 5)
|
| 210 |
+
#point_end = (int(xmax),int(ymax) + 5)
|
| 211 |
lines.append((point_st,point_end))
|
| 212 |
+
line_midpoint.append(calculate_midpoint(point_st,point_end))
|
| 213 |
+
#sanda_st = (int(xmin), int(ymax))
|
| 214 |
+
#sand_end = (int(xmax),int(ymax))
|
| 215 |
+
#sanda.append((sanda_st, sand_end))
|
| 216 |
|
| 217 |
+
#line_midpoint.append(calculate_midpoint(sanda_st,sand_end))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
#for calculation
|
| 219 |
width = distance((xmin,ymax), (xmax,ymax))
|
| 220 |
width_pixels.append(width)
|
|
|
|
| 225 |
#double_upper
|
| 226 |
if door_inf[1] == 7:
|
| 227 |
#for drawing
|
| 228 |
+
point_st = (int(xmin),int(ymin))
|
| 229 |
+
point_end = (int(xmax),int(ymin))
|
| 230 |
+
|
| 231 |
+
#point_st = (int(xmin),int(ymin) -5)
|
| 232 |
+
#point_end = (int(xmax),int(ymin) - 5)
|
| 233 |
lines.append((point_st,point_end))
|
| 234 |
+
line_midpoint.append(calculate_midpoint(point_st,point_end))
|
| 235 |
+
|
| 236 |
+
#sanda_st = (int(xmin),int(ymin))
|
| 237 |
+
#sand_end = (int(xmax),int(ymin))
|
| 238 |
+
#sanda.append((sanda_st, sand_end))
|
| 239 |
+
#line_midpoint.append(calculate_midpoint(sanda_st,sand_end))
|
| 240 |
#for calculation
|
| 241 |
width = distance((xmin,ymin), (xmax,ymin))
|
| 242 |
width_pixels.append(width)
|
|
|
|
| 246 |
#double_right
|
| 247 |
if door_inf[1] == 8:
|
| 248 |
#for drawing
|
| 249 |
+
#point_st = (int(xmax) + 5,int(ymin))
|
| 250 |
+
#point_end = (int(xmax) + 5,int(ymax))
|
| 251 |
+
|
| 252 |
+
point_st = (int(xmax),int(ymin))
|
| 253 |
+
point_end = (int(xmax),int(ymax))
|
| 254 |
lines.append((point_st,point_end))
|
| 255 |
+
line_midpoint.append(calculate_midpoint(point_st,point_end))
|
| 256 |
+
#sanda_st = (int(xmax), int(ymin))
|
| 257 |
+
#sand_end = (int(xmax), int(ymax))
|
| 258 |
+
#sanda.append((sanda_st, sand_end))
|
| 259 |
+
#line_midpoint.append(calculate_midpoint(sanda_st,sand_end))
|
| 260 |
#for calculation
|
| 261 |
width = distance((xmax,ymin), (xmax,ymax))
|
| 262 |
width_pixels.append(width)
|
|
|
|
| 266 |
#double_left
|
| 267 |
if door_inf[1] == 4:
|
| 268 |
#for drawing
|
| 269 |
+
#point_st = (int(xmin) -5,int(ymin))
|
| 270 |
+
#point_end = (int(xmin) -5,int(ymax))
|
|
|
|
| 271 |
|
| 272 |
+
point_st = (int(xmin),int(ymin))
|
| 273 |
+
point_end = (int(xmin),int(ymax))
|
| 274 |
+
lines.append((point_st,point_end))
|
| 275 |
+
line_midpoint.append(calculate_midpoint(point_st,point_end))
|
| 276 |
+
|
| 277 |
+
#sanda_st = (int(xmin),int(ymin))
|
| 278 |
+
#sand_end = (int(xmin),int(ymax))
|
| 279 |
+
#sanda.append((sanda_st, sand_end))
|
| 280 |
+
#line_midpoint.append(calculate_midpoint(sanda_st,sand_end))
|
| 281 |
#for calculation
|
| 282 |
width = distance((xmin,ymin), (xmin,ymax))
|
| 283 |
width_pixels.append(width)
|
| 284 |
doubles +=1
|
| 285 |
door_type.append(1)
|
| 286 |
|
| 287 |
+
return width_pixels, lines, line_midpoint, doubles, door_type
|
| 288 |
|
| 289 |
def pxl2meter(width_pixels, ratio):
|
| 290 |
real_width = []
|
|
|
|
| 353 |
print("Annotations updated and saved ")
|
| 354 |
return output_pdf_io.read()
|
| 355 |
|
| 356 |
+
def add_annotations_to_pdf(image, pdf_name, lines, char_width, line_midpoint, door_type):
|
| 357 |
image_width, image_height = image.size
|
| 358 |
|
| 359 |
# Create a new PDF document
|
|
|
|
| 396 |
|
| 397 |
#Annotation for drawin lines as in the markups
|
| 398 |
for i in range(len(lines)):
|
| 399 |
+
l_points = [fitz.Point(*lines[i][0])* derotationMatrix, fitz.Point(*lines[i][1])* derotationMatrix]
|
| 400 |
+
#l_points = [fitz.Point(*sanda[i][0])* derotationMatrix, fitz.Point(*lines[i][0])* derotationMatrix, fitz.Point(*lines[i][1])* derotationMatrix, fitz.Point(*sanda[i][1])* derotationMatrix]
|
| 401 |
annot = page.add_polyline_annot(l_points)
|
| 402 |
annot.set_border(width=2, dashes=None) # Optional border styling
|
| 403 |
annot.set_colors(stroke=(1, 0, 0)) # Set the line color to red
|
|
|
|
| 462 |
doors_info_double = ev_model(img_pillow, model2, device2, 0.8)
|
| 463 |
|
| 464 |
|
| 465 |
+
width_pixels, lines, line_midpoint, single_count, door_type = get_door_info(doors_info)
|
| 466 |
|
| 467 |
+
width_pixels, lines, line_midpoint, double_count, door_type = get_door_info_double(doors_info_double, width_pixels, lines, sanda, line_midpoint, door_type)
|
| 468 |
|
| 469 |
real_width = pxl2meter(width_pixels, ratio)
|
| 470 |
char_width = width_as_char(real_width)
|