| 11971 | } |
| 11972 | |
| 11973 | bool sam3_refine_instance(sam3_tracker& tracker, sam3_state& state, |
| 11974 | const sam3_model& model, int instance_id, |
| 11975 | const std::vector<sam3_point>& pos_points, |
| 11976 | const std::vector<sam3_point>& neg_points) { |
| 11977 | const int D = model.hparams.neck_dim; |
| 11978 | sam3_masklet* tgt = nullptr; |
| 11979 | for (auto& ml : tracker.masklets) |
| 11980 | if (ml.instance_id == instance_id) { |
| 11981 | tgt = &ml; |
| 11982 | break; |
| 11983 | } |
| 11984 | if (!tgt) |
| 11985 | for (auto& ml : tracker.pending) |
| 11986 | if (ml.instance_id == instance_id) { |
| 11987 | tgt = &ml; |
| 11988 | break; |
| 11989 | } |
| 11990 | if (!tgt) { |
| 11991 | fprintf(stderr, "%s: instance %d not found\n", __func__, instance_id); |
| 11992 | return false; |
| 11993 | } |
| 11994 | sam3_pvs_params pvs; |
| 11995 | pvs.pos_points = pos_points; |
| 11996 | pvs.neg_points = neg_points; |
| 11997 | pvs.multimask = false; |
| 11998 | auto r = sam3_segment_pvs(state, model, pvs); |
| 11999 | if (r.detections.empty()) return false; |
| 12000 | // tracker.frame_index points to the *next* frame; the refinement applies |
| 12001 | // to the frame that was last tracked / encoded. |
| 12002 | int fi = std::max(0, tracker.frame_index - 1); |
| 12003 | const auto& rdet = r.detections[0]; |
| 12004 | tgt->last_score = rdet.score; |
| 12005 | tgt->last_seen = fi; |
| 12006 | std::vector<float> op(D); |
| 12007 | if (!rdet.sam_token.empty()) { |
| 12008 | sam3_extract_obj_ptr_cpu(model, rdet.sam_token.data(), rdet.mask.obj_score, op.data()); |
| 12009 | } else { |
| 12010 | std::fill(op.begin(), op.end(), 0.0f); |
| 12011 | } |
| 12012 | sam3_store_obj_ptr(tracker, model, instance_id, op.data(), fi); |
| 12013 | SAM3_LOG(2, "%s: refined instance %d\n", __func__, instance_id); |
| 12014 | return true; |
| 12015 | } |
| 12016 | |
| 12017 | int sam3_tracker_add_instance(sam3_tracker& tracker, sam3_state& state, |
| 12018 | const sam3_model& model, |
no test coverage detected