refactor: video section
This commit is contained in:
70
src/hooks/use-frame-detection-map.ts
Normal file
70
src/hooks/use-frame-detection-map.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { useMemo } from 'react';
|
||||
import { DetectionData, DetectionType } from '@/types';
|
||||
|
||||
export const useFrameDetectionMap = (data: DetectionData, detectionType: DetectionType) => {
|
||||
const frameDetectionMap = useMemo(() => {
|
||||
const map = new Map<number, any[]>();
|
||||
const isCombined = detectionType === 'pot-sign-detection';
|
||||
const isPothole = detectionType === 'pothole-detection' || isCombined;
|
||||
const isSignboard = detectionType === 'sign-board-detection' || isCombined;
|
||||
|
||||
if (data.frames && Array.isArray(data.frames)) {
|
||||
data.frames.forEach((frameData) => {
|
||||
const frameId = frameData.frame_id;
|
||||
const flatDetections = (frameData as any).detections;
|
||||
if (flatDetections && Array.isArray(flatDetections)) {
|
||||
map.set(
|
||||
frameId,
|
||||
flatDetections.map((d: any) => ({
|
||||
...d,
|
||||
_detType: d.type === 'pothole' ? 'pothole' : 'signboard',
|
||||
pothole_id: d.type === 'pothole' ? d.detection_id : undefined,
|
||||
signboard_id: d.type !== 'pothole' ? d.detection_id : undefined,
|
||||
})),
|
||||
);
|
||||
} else {
|
||||
let detections: any[] = [];
|
||||
if (isPothole && (frameData as any).potholes) {
|
||||
detections = [
|
||||
...detections,
|
||||
...(frameData as any).potholes.map((p: any) => ({ ...p, _detType: 'pothole' })),
|
||||
];
|
||||
}
|
||||
if (isSignboard && (frameData as any).signboards) {
|
||||
detections = [
|
||||
...detections,
|
||||
...(frameData as any).signboards.map((s: any) => ({ ...s, _detType: 'signboard' })),
|
||||
];
|
||||
}
|
||||
if (detections.length > 0) map.set(frameId, detections);
|
||||
}
|
||||
});
|
||||
}
|
||||
return map;
|
||||
}, [data, detectionType]);
|
||||
|
||||
const getNearestDetections = (frame: number, sortedFrameIndices: number[], maxSkip = 3) => {
|
||||
const exact = frameDetectionMap.get(frame);
|
||||
if (exact) return exact;
|
||||
|
||||
let low = 0,
|
||||
high = sortedFrameIndices.length - 1,
|
||||
targetIndex = -1;
|
||||
|
||||
while (low <= high) {
|
||||
const mid = Math.floor((low + high) / 2);
|
||||
if (sortedFrameIndices[mid] <= frame) {
|
||||
targetIndex = sortedFrameIndices[mid];
|
||||
low = mid + 1;
|
||||
} else {
|
||||
high = mid - 1;
|
||||
}
|
||||
}
|
||||
|
||||
return targetIndex !== -1 && frame - targetIndex <= maxSkip
|
||||
? frameDetectionMap.get(targetIndex)
|
||||
: undefined;
|
||||
};
|
||||
|
||||
return { frameDetectionMap, getNearestDetections };
|
||||
};
|
||||
Reference in New Issue
Block a user