def init_new_vehicles(labels): # Iterate through labels in integrated heatmap for car_number in range(1, labels[1] + 1): # Find pixels with each car_number label value nonzero = (labels[0] == car_number).nonzero() # Identify x and y values of those pixels nonzeroy = np.array(nonzero[0]) nonzerox = np.array(nonzero[1]) # Define a bounding box based on min/max x and y xleft = np.min(nonzerox) ytop = np.min(nonzeroy) xright = np.max(nonzerox) ybot = np.max(nonzeroy) # Search over all vehicles for best overlap to associate max_iou = -1.0 for v in vehicle_list: if v.age_predicted < max_age_predicted: centerx_v = v.fit_centroid_x xleft_v = np.int(centerx_v - 0.5 * v.fit_width) xright_v = np.int(centerx_v + 0.5 * v.fit_width) centery_v = v.fit_centroid_y ytop_v = np.int(centery_v - 0.5 * v.fit_height) ybot_v = np.int(centery_v + 0.5 * v.fit_height) # Calc intersection over union: area_det = (xright - xleft + 1) * (ybot - ytop + 1) area_v = (xright_v - xleft_v + 1) * (ybot_v - ytop_v + 1) intersection = max( 0, min(xright, xright_v) - max(xleft, xleft_v)) * max( 0, min(ybot, ybot_v) - max(ytop, ytop_v)) union = area_det + area_v - intersection iou = intersection / union if iou > max_iou: max_iou = iou # If overlap is small -> init new track if max_iou < min_iou: new_vehicle = Vehicle() new_vehicle.centroid_x = 0.5 * (xleft + xright) new_vehicle.centroid_y = 0.5 * (ytop + ybot) new_vehicle.width = xright - xleft new_vehicle.height = ybot - ytop new_vehicle.fit_centroid_x = new_vehicle.centroid_x new_vehicle.fit_centroid_y = new_vehicle.centroid_y new_vehicle.fit_width = new_vehicle.width new_vehicle.fit_height = new_vehicle.height new_vehicle.allx.extend(np.arange(xleft, xright + 1)) new_vehicle.ally.extend(np.arange(ytop, ybot + 1)) new_vehicle.detected = True new_vehicle.age_predicted = 0 vehicle_list.append(new_vehicle)