def associate_detections_BS(_detections, _predictions): _assignments = Hungarian_method(_detections, _predictions) _missing = [] _insects = [i[0] for i in _predictions] _not_associated = np.zeros(shape=(0, 3)) for _nass in (_assignments[len(_insects):]): _not_associated = np.vstack([_not_associated, (_detections[_nass])]) _associations_BS = np.zeros(shape=(0, 4)) for ass in np.arange(len(_insects)): _record = _assignments[ass] if (_record <= len(_detections) - 1): _xc, _yc, _area = _detections[_assignments[ass]][0], _detections[ _assignments[ass]][1], _detections[_assignments[ass]][2] _dist = cal_dist(_xc, _yc, _predictions[ass][1], _predictions[ass][2]) if (_dist > max_dist_bs): _missing.append(_predictions[ass][0]) else: _associations_BS = np.vstack([ _associations_BS, (int(float(_predictions[ass][0])), int(_xc), int(_yc), int(_area)) ]) else: _missing.append(_predictions[ass][0]) return _associations_BS, _missing, _not_associated
def cal_cum_distance(_last_detections): cum_distance = 0 for _d in np.arange(0, len(_last_detections) - 1, 1): cum_distance += cal_dist(_last_detections[_d][0], _last_detections[_d][1], _last_detections[_d + 1][0], _last_detections[_d + 1][1]) return cum_distance
def Hungarian_method(_detections, _predictions): num_detections, num_predictions = len(_detections), len(_predictions) mat_shape = max(num_detections, num_predictions) hun_matrix = np.full((mat_shape, mat_shape), 0) for p in np.arange(num_predictions): for d in np.arange(num_detections): hun_matrix[p][d] = cal_dist(_predictions[p][1], _predictions[p][2], _detections[d][0], _detections[d][1]) row_ind, col_ind = linear_sum_assignment(hun_matrix) return col_ind
def verify_new_insect(_pot_new_insect, _associated_det_BS): recorded_BS = [] for bsdet in _associated_det_BS: for pni in np.arange(len(_pot_new_insect)): dist = cal_dist(bsdet[1], bsdet[2], int(float(_pot_new_insect[pni][0])), int(float(_pot_new_insect[pni][1]))) if dist <= max_dist_bs: recorded_BS.append(pni) _pot_new_insect = np.delete(_pot_new_insect, recorded_BS, 0) return _pot_new_insect
def cal_CoG_DL(result): _x_DL, _y_DL, _body_area, _radius = 0, 0, 0, 0 _x_TL = int(float(result[0])) _y_TL = int(float(result[1])) _x_BR = int(float(result[2])) _y_BR = int(float(result[3])) _x_DL = int(round((_x_TL + _x_BR) / 2)) _y_DL = int(round((_y_TL + _y_BR) / 2)) _radius = cal_dist(_x_TL, _y_TL, _x_DL, _y_DL) _body_area = cal_bodyArea_DL(_x_TL, _y_TL, _x_BR, _y_BR) return _x_DL, _y_DL, _body_area, _radius
def check_on_flower(_prediction, _on_flower=False): _x = _prediction[1] _y = _prediction[2] _flowers = get_flower_details() for flower in _flowers: dist_from_c = cal_dist(_x, _y, flower[0], flower[1]) if dist_from_c <= flower[2]: _on_flower = True break else: pass return _on_flower
def check_sight_coordinates(_record, outside_spot=True): if pt_cfg.POLYTRACK.DL_DARK_SPOTS: _x = int(float(_record[0])) _y = int(float(_record[1])) dark_spots = pt_cfg.POLYTRACK.RECORDED_DARK_SPOTS spot_radius = int(pt_cfg.POLYTRACK.DL_DARK_SPOTS_RADIUS) for spot in dark_spots: spot_x = spot[0] spot_y = spot[1] if (cal_dist(_x, _y, spot_x, spot_y) <= spot_radius): outside_spot = False break else: pass else: outside_spot = True return outside_spot
def associate_detections_DL(_detections, _predictions): _missing = [] _assignments = Hungarian_method(_detections, _predictions) _insects = [i[0] for i in _predictions] _not_associated = np.zeros(shape=(0, 5)) for _nass in (_assignments[len(_insects):]): _not_associated = np.vstack([_not_associated, (_detections[_nass])]) _associations_DL = np.zeros(shape=(0, 6)) for ass in np.arange(len(_insects)): _record = _assignments[ass] if (_record <= len(_detections) - 1): _xc, _yc, _area, _lable, _conf = _detections[_assignments[ass]][ 0], _detections[_assignments[ass]][1], _detections[ _assignments[ass]][2], _detections[_assignments[ass]][ 3], _detections[_assignments[ass]][4] _dist = cal_dist(_xc, _yc, _predictions[ass][1], _predictions[ass][2]) if (_dist > max_dist_dl): _missing.append(_predictions[ass][0]) else: _associations_DL = np.vstack([ _associations_DL, (_predictions[ass][0], _detections[_assignments[ass]][0], _detections[_assignments[ass]][1], _detections[_assignments[ass]][2], _detections[_assignments[ass]][3], _detections[_assignments[ass]][4]) ]) else: _missing.append(_predictions[ass][0]) return _associations_DL, _missing, _not_associated