Spaces:
Sleeping
Sleeping
Update doors_fasterrcnn.py
Browse files- doors_fasterrcnn.py +99 -8
doors_fasterrcnn.py
CHANGED
|
@@ -100,7 +100,7 @@ def calculate_midpoint(p1, p2):
|
|
| 100 |
xm = int((x1 + x2) / 2)
|
| 101 |
ym = int((y1 + y2) / 2)
|
| 102 |
return (xm, ym)
|
| 103 |
-
def get_door_info(doors_info):
|
| 104 |
width_pixels = []
|
| 105 |
lines = []
|
| 106 |
sanda = []
|
|
@@ -214,6 +214,81 @@ def get_door_info(doors_info):
|
|
| 214 |
|
| 215 |
return width_pixels, lines, sanda, line_midpoint, singles, doubles, door_type
|
| 216 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
def pxl2meter(width_pixels, ratio):
|
| 218 |
real_width = []
|
| 219 |
for width in width_pixels:
|
|
@@ -350,31 +425,47 @@ def add_annotations_to_pdf(image, pdf_name, lines, sanda, char_width, line_midpo
|
|
| 350 |
|
| 351 |
def main_run(img_pillow,pdf_fullpath, weights_path, pdf_name,pdfpath,ratio): ####pdf_fullpath here is the data and not the path
|
| 352 |
img_pillow = convert2pillow(pdf_fullpath)
|
| 353 |
-
|
|
|
|
| 354 |
num_classes = 12 # classes + background
|
| 355 |
-
|
| 356 |
# Load the model with the specified number of classes
|
| 357 |
model = get_model(num_classes)
|
| 358 |
-
|
| 359 |
# Load the saved model's state dictionary with map_location to handle CPU
|
| 360 |
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
| 361 |
-
|
| 362 |
try:
|
| 363 |
model.load_state_dict(torch.load(weights_path, map_location=device), strict=False)
|
| 364 |
except RuntimeError as e:
|
| 365 |
print(f"Error loading model state_dict: {e}")
|
| 366 |
return
|
| 367 |
-
|
| 368 |
# Set the model to evaluation mode
|
| 369 |
model.eval()
|
| 370 |
-
|
| 371 |
# Move the model to the appropriate device
|
| 372 |
model.to(device)
|
| 373 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 374 |
# START INFERENCE
|
| 375 |
doors_info = ev_model(img_pillow, model, device, 0.8)
|
|
|
|
|
|
|
|
|
|
| 376 |
#width_pixels, lines, sanda, line_midpoint, doubleD_bbox, doubleD_width = get_door_info(doors_info)
|
| 377 |
-
width_pixels, lines, sanda, line_midpoint, single_count, double_count, door_type = get_door_info(doors_info)
|
| 378 |
real_width = pxl2meter(width_pixels, ratio)
|
| 379 |
#real_double = pxl2meter(doubleD_width, ratio)
|
| 380 |
char_width = width_as_char(real_width)
|
|
|
|
| 100 |
xm = int((x1 + x2) / 2)
|
| 101 |
ym = int((y1 + y2) / 2)
|
| 102 |
return (xm, ym)
|
| 103 |
+
def get_door_info(doors_info, doors_info_double):
|
| 104 |
width_pixels = []
|
| 105 |
lines = []
|
| 106 |
sanda = []
|
|
|
|
| 214 |
|
| 215 |
return width_pixels, lines, sanda, line_midpoint, singles, doubles, door_type
|
| 216 |
|
| 217 |
+
def get_door_info_double(doors_info_double, width_pixels, lines, sanda, line_midpoint, doubles, door_type):
|
| 218 |
+
for door_inf in doors_info_double:
|
| 219 |
+
xmin, ymin, xmax, ymax = door_inf[0]
|
| 220 |
+
|
| 221 |
+
#double_bottom
|
| 222 |
+
if door_inf[1] == 1:
|
| 223 |
+
point_st = (int(xmin), int(ymax) + 5)
|
| 224 |
+
point_end = (int(xmax),int(ymax) + 5)
|
| 225 |
+
lines.append((point_st,point_end))
|
| 226 |
+
|
| 227 |
+
sanda_st = (int(xmin), int(ymax))
|
| 228 |
+
sand_end = (int(xmax),int(ymax))
|
| 229 |
+
sanda.append((sanda_st, sand_end))
|
| 230 |
+
|
| 231 |
+
line_midpoint.append(calculate_midpoint(sanda_st,sand_end))
|
| 232 |
+
#for calculation
|
| 233 |
+
width = distance((xmin,ymax), (xmax,ymax))
|
| 234 |
+
width_pixels.append(width)
|
| 235 |
+
doubles +=1
|
| 236 |
+
door_type.append(1)
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
#double_upper
|
| 240 |
+
if door_inf[1] == 7:
|
| 241 |
+
#for drawing
|
| 242 |
+
point_st = (int(xmin),int(ymin) -5)
|
| 243 |
+
point_end = (int(xmax),int(ymin) - 5)
|
| 244 |
+
lines.append((point_st,point_end))
|
| 245 |
+
|
| 246 |
+
sanda_st = (int(xmin),int(ymin))
|
| 247 |
+
sand_end = (int(xmax),int(ymin))
|
| 248 |
+
sanda.append((sanda_st, sand_end))
|
| 249 |
+
line_midpoint.append(calculate_midpoint(sanda_st,sand_end))
|
| 250 |
+
#for calculation
|
| 251 |
+
width = distance((xmin,ymin), (xmax,ymin))
|
| 252 |
+
width_pixels.append(width)
|
| 253 |
+
doubles +=1
|
| 254 |
+
door_type.append(1)
|
| 255 |
+
|
| 256 |
+
#double_right
|
| 257 |
+
if door_inf[1] == 8:
|
| 258 |
+
#for drawing
|
| 259 |
+
point_st = (int(xmax) + 5,int(ymin))
|
| 260 |
+
point_end = (int(xmax) + 5,int(ymax))
|
| 261 |
+
lines.append((point_st,point_end))
|
| 262 |
+
|
| 263 |
+
sanda_st = (int(xmax), int(ymin))
|
| 264 |
+
sand_end = (int(xmax), int(ymax))
|
| 265 |
+
sanda.append((sanda_st, sand_end))
|
| 266 |
+
line_midpoint.append(calculate_midpoint(sanda_st,sand_end))
|
| 267 |
+
#for calculation
|
| 268 |
+
width = distance((xmax,ymin), (xmax,ymax))
|
| 269 |
+
width_pixels.append(width)
|
| 270 |
+
doubles +=1
|
| 271 |
+
door_type.append(1)
|
| 272 |
+
|
| 273 |
+
#double_left
|
| 274 |
+
if door_inf[1] == 4:
|
| 275 |
+
#for drawing
|
| 276 |
+
point_st = (int(xmin) -5,int(ymin))
|
| 277 |
+
point_end = (int(xmin) -5,int(ymax))
|
| 278 |
+
lines.append((point_st,point_end))
|
| 279 |
+
|
| 280 |
+
sanda_st = (int(xmin),int(ymin))
|
| 281 |
+
sand_end = (int(xmin),int(ymax))
|
| 282 |
+
sanda.append((sanda_st, sand_end))
|
| 283 |
+
line_midpoint.append(calculate_midpoint(sanda_st,sand_end))
|
| 284 |
+
#for calculation
|
| 285 |
+
width = distance((xmin,ymin), (xmin,ymax))
|
| 286 |
+
width_pixels.append(width)
|
| 287 |
+
doubles +=1
|
| 288 |
+
door_type.append(1)
|
| 289 |
+
|
| 290 |
+
return width_pixels, lines, sanda, line_midpoint, singles, doubles, door_type
|
| 291 |
+
|
| 292 |
def pxl2meter(width_pixels, ratio):
|
| 293 |
real_width = []
|
| 294 |
for width in width_pixels:
|
|
|
|
| 425 |
|
| 426 |
def main_run(img_pillow,pdf_fullpath, weights_path, pdf_name,pdfpath,ratio): ####pdf_fullpath here is the data and not the path
|
| 427 |
img_pillow = convert2pillow(pdf_fullpath)
|
| 428 |
+
|
| 429 |
+
# For Single Doors
|
| 430 |
num_classes = 12 # classes + background
|
|
|
|
| 431 |
# Load the model with the specified number of classes
|
| 432 |
model = get_model(num_classes)
|
|
|
|
| 433 |
# Load the saved model's state dictionary with map_location to handle CPU
|
| 434 |
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
|
|
|
| 435 |
try:
|
| 436 |
model.load_state_dict(torch.load(weights_path, map_location=device), strict=False)
|
| 437 |
except RuntimeError as e:
|
| 438 |
print(f"Error loading model state_dict: {e}")
|
| 439 |
return
|
|
|
|
| 440 |
# Set the model to evaluation mode
|
| 441 |
model.eval()
|
|
|
|
| 442 |
# Move the model to the appropriate device
|
| 443 |
model.to(device)
|
| 444 |
|
| 445 |
+
|
| 446 |
+
# For Double Doors
|
| 447 |
+
num_classes2 = 11 # classes + background
|
| 448 |
+
# Load the model with the specified number of classes
|
| 449 |
+
model2 = get_model(num_classes2)
|
| 450 |
+
# Load the saved model's state dictionary with map_location to handle CPU
|
| 451 |
+
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
| 452 |
+
try:
|
| 453 |
+
model.load_state_dict(torch.load(weights_path2, map_location=device), strict=False)
|
| 454 |
+
except RuntimeError as e:
|
| 455 |
+
print(f"Error loading model state_dict: {e}")
|
| 456 |
+
return
|
| 457 |
+
# Set the model to evaluation mode
|
| 458 |
+
model2.eval()
|
| 459 |
+
# Move the model to the appropriate device
|
| 460 |
+
model2.to(device)
|
| 461 |
+
|
| 462 |
# START INFERENCE
|
| 463 |
doors_info = ev_model(img_pillow, model, device, 0.8)
|
| 464 |
+
doors_info_double = ev_model(img_pillow, model2, device, 0.8)
|
| 465 |
+
|
| 466 |
+
|
| 467 |
#width_pixels, lines, sanda, line_midpoint, doubleD_bbox, doubleD_width = get_door_info(doors_info)
|
| 468 |
+
width_pixels, lines, sanda, line_midpoint, single_count, double_count, door_type = get_door_info(doors_info, doors_info_double)
|
| 469 |
real_width = pxl2meter(width_pixels, ratio)
|
| 470 |
#real_double = pxl2meter(doubleD_width, ratio)
|
| 471 |
char_width = width_as_char(real_width)
|