droboticflow/internal/api/annotation_boundary_grid.go

111 lines
4.0 KiB
Go

package api
import (
"droboticflow/internal/domain"
"fmt"
"math"
"sort"
)
// One half-open hardware boundary owns a media time and both camera indices.
// Adjacent intervals reference the same entry instead of independently rounding.
type annotationBoundaryGrid struct {
Origin int64
Scale float64
Timestamps []int64
Seconds []float64
LeftFrames []int
RightFrames []int
}
func newAnnotationBoundaryGrid(left, right []hardwarePTSRow, start, end int64, mediaDuration float64) (annotationBoundaryGrid, error) {
g := annotationBoundaryGrid{}
leftEnd, err := ptsCoverageEnd(left)
if err != nil {
return g, err
}
if len(right) < 2 || end <= start {
return g, fmt.Errorf("双目有效公共范围为空")
}
g.Origin = left[0].TimestampUS
g.Scale = mediaDuration / (float64(leftEnd-g.Origin) / 1e6)
if math.IsNaN(g.Scale) || math.IsInf(g.Scale, 0) || g.Scale < 0.5 || g.Scale > 2 {
return g, fmt.Errorf("视频与 PTS 时间映射无效")
}
index := func(rows []hardwarePTSRow, t int64) int {
return sort.Search(len(rows), func(i int) bool { return rows[i].TimestampUS >= t })
}
lastLeft, lastRight := index(left, end), index(right, end)
// Annotation sources are CFR outputs of the stereo preprocessing operator.
// Use their actual frame index, not hardware elapsed time as video seconds.
add := func(t int64, l, r int) {
g.Timestamps = append(g.Timestamps, t)
g.Seconds = append(g.Seconds, float64(l)*mediaDuration/float64(len(left)))
g.LeftFrames = append(g.LeftFrames, l)
g.RightFrames = append(g.RightFrames, r)
}
add(start, index(left, start), index(right, start))
for _, row := range left {
t := row.TimestampUS
if t <= start || t >= end {
continue
}
l, r := index(left, t), index(right, t)
last := len(g.Timestamps) - 1
// Do not expose two different boundaries for the same camera frame, or
// offer a cut that would leave either camera with an empty tail segment.
if l > g.LeftFrames[last] && r > g.RightFrames[last] && l < lastLeft && r < lastRight {
add(t, l, r)
}
}
if lastLeft <= g.LeftFrames[0] || lastRight <= g.RightFrames[0] {
return g, fmt.Errorf("公共范围必须包含左右相机有效帧")
}
add(end, lastLeft, lastRight)
return g, nil
}
func (g annotationBoundaryGrid) resolve(seconds float64) (int, error) {
i := sort.SearchFloat64s(g.Seconds, seconds)
if i == len(g.Seconds) {
i--
}
if i > 0 && math.Abs(g.Seconds[i-1]-seconds) < math.Abs(g.Seconds[i]-seconds) {
i--
}
if i < 0 || math.IsNaN(seconds) || math.Abs(g.Seconds[i]-seconds) > 1e-6 {
return 0, fmt.Errorf("边界 %.9f 未对齐统一帧边界,请在切片页重新定位后确认", seconds)
}
return i, nil
}
// Description coverage is optional for the current description-only workbench.
// Slice/media coverage is validated separately and must still be complete.
func validateDescriptionIntervals(slices []domain.SliceSegment, segments []domain.AnnotationSegment, requireCoverage bool) []validationIssue {
issues := []validationIssue{}
for i, slice := range slices {
if slice.IsDiscarded() {
continue
}
events := []domain.AnnotationSegment{}
for _, e := range segments {
if e.Start >= slice.Start-1e-6 && e.End <= slice.End+1e-6 {
events = append(events, e)
}
}
sort.Slice(events, func(i, j int) bool { return events[i].Start < events[j].Start })
cursor := slice.Start
for _, e := range events {
if e.Start < cursor-1e-6 || e.End <= e.Start || (requireCoverage && math.Abs(e.Start-cursor) > 1e-6) {
issues = append(issues, validationIssue{Field: fmt.Sprintf("sliceSegments.%d", i), Code: "annotation_coverage_invalid", Message: "已有动作区间必须有效且无重叠"})
break
}
cursor = e.End
}
if requireCoverage && (len(events) == 0 || math.Abs(cursor-slice.End) > 1e-6) {
issues = append(issues, validationIssue{Field: fmt.Sprintf("sliceSegments.%d", i), Code: "annotation_coverage_gap", Message: "动作描述未完整覆盖切片,请补充动作或显式标记无效片段"})
}
}
return issues
}