def remove_overlaps(boxes, classes, scores, marking_list): ''' This function deletes the overlapping boxes that belong to the same crack class. It returns the boxes, classes and scores of the un-deleted (non-overlapping) boxes. ''' boxes, classes, scores = remove_b1(boxes, classes, scores, marking_list) k = boxes.shape[0] boxes_ = boxes remove_list = [] for i in range(k): if classes[ i] == 6: #not deleting the BC2/BC3 detections as they do not have overlaps continue for j in range(k): if classes[j] == 6: continue area1 = np_box_ops.area(np.array(([boxes_[j]]))) area2 = np_box_ops.area(np.array(([boxes_[i]]))) box1 = np_box_list.BoxList(np.array(([boxes_[j]]))) box2 = np_box_list.BoxList(np.array(([boxes_[i]]))) ioa1 = np.transpose(np_box_list_ops.ioa(box1, box2)) ioa2 = np.transpose(np_box_list_ops.ioa(box2, box1)) #A minimum overlap threshold to consider the overlapping boxes that need to be deleted if ioa1 > 0.65: #To check if its not the same detected box if ioa1 < 0.99: if classes[i] not in [5, 6]: #A check for overlaps using the ioa scores wrt (box1 , box2) and vice versa. #This is to delete the box with most overlap if abs(ioa1 - ioa2) <= 0.01: if ioa1 > ioa2: if i not in remove_list: if classes[i] == classes[j]: remove_list.append(i) else: if j not in remove_list: if classes[i] == classes[j]: remove_list.append(j) else: if classes[i] == classes[j]: remove_list.append(i) if classes[i] == classes[j] == 5: remove_list.append(i) elif ioa1 >= 0.99: if area1 != area2: if classes[i] == classes[j]: remove_list.append(i) boxes = np.delete(boxes, remove_list, 0) classes = np.delete(classes, remove_list, 0) scores = np.delete(scores, remove_list, 0) return boxes, classes, scores
def testArea(self): areas = np_box_ops.area(self.boxes1) expected_areas = np.array([6.0, 5.0], dtype=float) self.assertAllClose(expected_areas, areas)
def __init__(self, class_array, prob_array, bbox_array): self.class_array = class_array self.prob_array = prob_array self.bbox_array = bbox_array self.bbox_center_array = get_centers(self.bbox_array) self.bbox_area_array = area(self.bbox_array)
def cracks(boxes, classes, scores, marking_list): left_wheelpath, right_wheelpath, non_wheelpath, left_center_path, right_center_path, left_non_wheelpath, right_non_wheelpath = wheelpath( marking_list) right_center_list_b2 = [] left_center_list_b2 = [] left_center_list_b3 = [] right_center_list_b3 = [] right_list_b = [] left_list_b = [] center_b = list() right_list_l = [] left_list_l = [] box_list_right_load = [] box_list_left_load = [] b1_box_list = list() b1_box_list_longi = list() b1_class_list = list() box_list_block = list() rm_list = list() flag_LC3 = 0 flag_BC2 = 0 flag_BC3 = 0 box_left = np_box_list.BoxList(np.array(([left_wheelpath]))) box_right = np_box_list.BoxList(np.array(([right_wheelpath]))) box_non_wheel = np_box_list.BoxList(np.array(([non_wheelpath]))) box_left_center = np_box_list.BoxList(np.array(([left_center_path]))) box_right_center = np_box_list.BoxList(np.array(([right_center_path]))) box_non_wheel_left = np_box_list.BoxList(np.array(([left_non_wheelpath]))) box_non_wheel_right = np_box_list.BoxList(np.array( ([right_non_wheelpath]))) for i in range(boxes.shape[0]): label = classes[i] box = boxes[i] box2 = np_box_list.BoxList(np.array(([box]))) print('box2', box2) #Check for box overlaps with wheepaths and non-wheelpaths ioa_1 = np.transpose(np_box_list_ops.ioa(box_non_wheel, box2)) #non-wheelpath ioa_2 = np.transpose(np_box_list_ops.ioa(box_left, box2)) #Left wheelpath ioa_3 = np.transpose(np_box_list_ops.ioa(box_right, box2)) #Right wheelpath ioa_4 = np.transpose(np_box_list_ops.ioa(box_left_center, box2)) #Left center path ioa_5 = np.transpose(np_box_list_ops.ioa(box_right_center, box2)) #Right center path ioa_7 = np.transpose(np_box_list_ops.ioa(box_non_wheel_left, box2)) #Left nonwheelpath ioa_8 = np.transpose(np_box_list_ops.ioa(box_non_wheel_right, box2)) #Right nonwheelpath if classes[i] == 3: ioa_6 = np.transpose(np_box_list_ops.ioa(box2, box_non_wheel)) #LC3 if ioa_6 > 0.8: flag_LC3 = 1 if 3 not in classes: flag_LC3 = 1 #Check detected load cracks in the non wheelpath and change the label to BC1 if (ioa_1 > 0.8 or ioa_7 > 0.7) or ioa_8 > 0.7: if label in [1, 2, 3, 4]: classes[i] = 5 #creating a list of BC1 boxes and classes if (classes[i] == 6 or classes[i] == 5): if classes[i] == 5: b1_box_list.append(boxes[i]) b1_class_list.append(classes[i]) #Calculate the area of the bbox area = np_box_ops.area(np.array(([box]))) # appending the BC2/BC3 detections to left,right and center lists of the pavement dependig on their location and area. if classes[i] == 6: if ioa_4 > 0.7: if area > 190000: left_center_list_b2.append(classes[i]) #left_center_list_b2 box_list_block.append(boxes[i]) else: left_center_list_b3.append(classes[i]) #left_center_list_b3 box_list_block.append(boxes[i]) elif ioa_5 > 0.7: if area > 190000: right_center_list_b2.append(classes[i]) box_list_block.append(boxes[i]) #right_center_list_b2 else: right_center_list_b3.append(classes[i]) #right_center_list_b3 box_list_block.append(boxes[i]) #To check if the biggest box is in the center of the image if ((ioa_4 > 0.5 and ioa_5 > 0.5) or (area > 400000)): if area > 200000: if ioa_7 > 0.2 or ioa_8 > 0.2: center_b.append(classes[i]) #Check for load cracks else: #left_wheelpath if ioa_2 > 0.4: if classes[i] != 3: left_list_l.append(classes[i]) box_list_left_load.append(boxes[i]) #right_wheelpath elif ioa_3 > 0.4: if classes[i] != 3: right_list_l.append(classes[i]) box_list_right_load.append(boxes[i]) #special case LC3 with not much overlap because of wide box if classes[i] == 3: if ioa_2 > 0.3: left_list_l.append(classes[i]) box_list_left_load.append(boxes[i]) if ioa_3 > 0.3: right_list_l.append(classes[i]) box_list_right_load.append(boxes[i]) #Part of the code to post-process BC2/BC3 into BC2 and BC3 separately #check if LC4 is not present in detections if np.setdiff1d(classes, [1, 2, 3, 5, 6]).shape[0] == 0: if flag_LC3 == 1: #Checking if the nuumber of detected boxes is greater than 3 if (len(left_center_list_b2 + left_center_list_b3 + right_center_list_b2 + right_center_list_b3)) >= 4: if (len(left_center_list_b2 + left_center_list_b3) >= 1 and len(right_center_list_b2 + right_center_list_b3) >= 1): print('Block crack level 3 detected') flag_BC3 = 1 else: print('Block crack level 2 detected') flag_BC2 = 1 #check if BC2 is detected wrt area of the detected box being bigger than BC3 elif ((len(left_center_list_b2) != 0) or (len(right_center_list_b2) != 0)): if len(center_b) >= 1: print('Block crack level 2 detected') flag_BC2 = 1 #Checking if the nuumber of detected boxes is greater than or equal to 2 elif (len(left_center_list_b2 + left_center_list_b3 + right_center_list_b2 + right_center_list_b3)) >= 2: #Check if blocks are detected in both left and right parts of the pavement if (len(left_center_list_b2 + left_center_list_b3) >= 1 and len(right_center_list_b2 + right_center_list_b3) >= 1): print('Block crack level 3 detected') flag_BC3 = 1 else: print('Block crack level 2 detected') flag_BC2 = 1 elif (len(left_center_list_b2 + left_center_list_b3 + right_center_list_b2 + right_center_list_b3)) >= 1: print('Block crack level 2 detected') flag_BC2 = 1 #Check for blocks detected only in one part of the pavement (R or L) >= 2 elif len(left_center_list_b2 + left_center_list_b3) >= 2 or len( right_center_list_b2 + right_center_list_b3) >= 2: #if 6 in classes: flag_BC2 = 1 print('Block Crack Level 2 detected') # Part of the code to return crack extent calculation if not (flag_BC2 or flag_BC3): extent_right, extent_left, extent_b1 = extent(box_list_right_load, box_list_left_load, box_list_block, b1_box_list, flag_BC2, flag_BC3) left_list_l.sort() left_list_l.reverse() right_list_l.sort() right_list_l.reverse() if (left_list_l == [] and right_list_l == []): if extent_b1 != 0: return ('0', extent_left, '0', extent_right, 0, 0, '1', extent_b1) else: return ('0', extent_left, '0', extent_right, 0, 0, 0, 0) elif left_list_l == []: if len(right_list_l) >= 2: right = str(right_list_l[0]) + ',' + str(right_list_l[1]) else: right = str(right_list_l[0]) if extent_b1 != 0: return (0, extent_left, right, extent_right, 0, 0, '1', extent_b1) else: return (0, extent_left, right, extent_right, 0, 0, 0, 0) elif right_list_l == []: if len(left_list_l) >= 2: left = str(left_list_l[0]) + ',' + str(left_list_l[1]) else: left = str(left_list_l[0]) if extent_b1 != 0: return (left, extent_left, 0, extent_right, 0, 0, '1', extent_b1) else: return (left, extent_left, 0, extent_right, 0, 0, 0, 0) else: if len(left_list_l) >= 2: left = str(left_list_l[0]) + ',' + str(left_list_l[1]) else: left = str(left_list_l[0]) if len(right_list_l) >= 2: right = str(right_list_l[0]) + ',' + str(right_list_l[1]) else: right = str(right_list_l[0]) if extent_b1 != 0: return (left, extent_left, right, extent_right, 0, 0, '1', extent_b1) else: return (left, extent_left, right, extent_right, 0, 0, 0, 0) else: extent_block, extent_right, extent_left = extent( box_list_right_load, box_list_left_load, box_list_block, b1_box_list, flag_BC2, flag_BC3) left_list_l.sort() left_list_l.reverse() right_list_l.sort() right_list_l.reverse() if (left_list_l == [] and right_list_l == []): left = '0' right = '0' elif left_list_l == []: left = '0' if len(right_list_l) >= 2: right = str(right_list_l[0]) + ',' + str(right_list_l[1]) else: right = str(right_list_l[0]) elif right_list_l == []: right = '0' if len(left_list_l) >= 2: left = str(left_list_l[0]) + ',' + str(left_list_l[1]) else: left = str(left_list_l[0]) else: if len(left_list_l) >= 2: left = str(left_list_l[0]) + ',' + str(left_list_l[1]) else: left = str(left_list_l[0]) if len(right_list_l) >= 2: right = str(right_list_l[0]) + ',' + str(right_list_l[1]) else: right = str(right_list_l[0]) if flag_BC2: return (left, extent_left, right, extent_right, '2', str(extent_block), '0', '0') else: return (left, extent_left, right, extent_right, '3', str(extent_block), '0', '0')