feat(results): add live annotation overlay on raw video playback

This commit is contained in:
2026-07-08 17:24:20 +05:30
parent 23186ffd29
commit 7a82e17bbb
13 changed files with 540 additions and 57 deletions

View File

@@ -4,6 +4,7 @@ import { useEffect, useState } from 'react';
import { useQuery } from '@tanstack/react-query'; import { useQuery } from '@tanstack/react-query';
import { videoService } from '@/services/api'; import { videoService } from '@/services/api';
import type { VideoAnnotationFramesParams } from '@/types';
import { videoKeys } from '../queries/videoKeys'; import { videoKeys } from '../queries/videoKeys';
@@ -18,6 +19,20 @@ export function useVideoResultsQuery(videoId: string | undefined) {
}); });
} }
export function useVideoAnnotationFramesQuery(
videoId: string | undefined,
params: VideoAnnotationFramesParams,
) {
return useQuery({
queryKey: videoKeys.annotationFrames(videoId ?? '', params),
queryFn: () =>
videoService.getVideoAnnotationFrames(videoId as string, params),
enabled: Boolean(videoId),
staleTime: 1000 * 60 * 5,
gcTime: 1000 * 60 * 30,
});
}
/** /**
* Download the annotated video through the authenticated axios client and * Download the annotated video through the authenticated axios client and
* expose a local object URL that the native `<video>` element can play. * expose a local object URL that the native `<video>` element can play.
@@ -26,10 +41,10 @@ export function useVideoResultsQuery(videoId: string | undefined) {
* directly is what fixes the 401. The browser cannot attach the Bearer token to * directly is what fixes the 401. The browser cannot attach the Bearer token to
* a media request, but axios can. * a media request, but axios can.
*/ */
export function useAnnotatedVideoQuery(url: string | undefined) { export function useProtectedVideoQuery(url: string | undefined) {
const query = useQuery({ const query = useQuery({
queryKey: videoKeys.annotatedVideo(url ?? ''), queryKey: videoKeys.annotatedVideo(url ?? ''),
queryFn: () => videoService.getAnnotatedVideo(url as string), queryFn: () => videoService.getProtectedVideo(url as string),
enabled: Boolean(url), enabled: Boolean(url),
staleTime: Infinity, staleTime: Infinity,
gcTime: 1000 * 60 * 30, gcTime: 1000 * 60 * 30,
@@ -59,3 +74,5 @@ export function useAnnotatedVideoQuery(url: string | undefined) {
refetch: query.refetch, refetch: query.refetch,
}; };
} }
export const useAnnotatedVideoQuery = useProtectedVideoQuery;

View File

@@ -2,6 +2,10 @@ export const videoKeys = {
all: ['videos'] as const, all: ['videos'] as const,
results: () => [...videoKeys.all, 'results'] as const, results: () => [...videoKeys.all, 'results'] as const,
result: (videoId: string) => [...videoKeys.results(), videoId] as const, result: (videoId: string) => [...videoKeys.results(), videoId] as const,
annotationFrames: (
videoId: string,
params: { start_time_ms: number; end_time_ms: number },
) => [...videoKeys.result(videoId), 'annotation-frames', params] as const,
annotatedVideos: () => [...videoKeys.all, 'annotated'] as const, annotatedVideos: () => [...videoKeys.all, 'annotated'] as const,
annotatedVideo: (url: string) => annotatedVideo: (url: string) =>
[...videoKeys.annotatedVideos(), url] as const, [...videoKeys.annotatedVideos(), url] as const,

View File

@@ -184,9 +184,9 @@ export function TicketClassDetectionPreview({
size="sm" size="sm"
onClick={() => setCurrentIndex((index) => Math.max(index - 1, 0))} onClick={() => setCurrentIndex((index) => Math.max(index - 1, 0))}
disabled={isPreviousDisabled} disabled={isPreviousDisabled}
aria-label="Previous detection"
> >
<ChevronLeft className="size-4" /> <ChevronLeft className="size-4" />
Previous
</Button> </Button>
<div className="min-w-16 text-center text-sm font-medium text-foreground"> <div className="min-w-16 text-center text-sm font-medium text-foreground">
{detectionCount === 0 {detectionCount === 0
@@ -199,8 +199,8 @@ export function TicketClassDetectionPreview({
size="sm" size="sm"
onClick={() => setCurrentIndex((index) => index + 1)} onClick={() => setCurrentIndex((index) => index + 1)}
disabled={isNextDisabled} disabled={isNextDisabled}
aria-label="Next detection"
> >
Next
<ChevronRight className="size-4" /> <ChevronRight className="size-4" />
</Button> </Button>
</div> </div>

View File

@@ -0,0 +1,68 @@
'use client';
import { useRef } from 'react';
import type { MediaPlayerInstance } from '@vidstack/react';
import { useProtectedVideoQuery } from '@/app/(modules)/results/hooks/useVideoResults';
import AnnotatedVideoPlayer from '@/components/video/annotatedVideoPlayer';
import { useVideoAnnotationPlayback } from '@/components/video/useVideoAnnotationPlayback';
import type { TicketOverviewDetail } from '@/types';
type TicketInlineAnalysisVideoProps = {
ticket: TicketOverviewDetail;
};
export function TicketInlineAnalysisVideo({
ticket,
}: TicketInlineAnalysisVideoProps) {
const videoRef = useRef<MediaPlayerInstance | null>(null);
const videoId = ticket.video_id ?? ticket.video?.id ?? undefined;
const sourceUrl = ticket.video?.url ?? undefined;
const fps = ticket.video_metadata?.fps ?? 0;
const videoWidth = ticket.video_metadata?.resolution.width ?? 0;
const videoHeight = ticket.video_metadata?.resolution.height ?? 0;
const {
visibleDetections,
handleSeeked,
handleTimeUpdate,
handleVideoFrame,
} = useVideoAnnotationPlayback({
videoId,
logs: [],
fps,
videoRef,
});
const {
videoUrl,
isLoading: isVideoLoading,
isError: isVideoError,
refetch: refetchVideo,
} = useProtectedVideoQuery(sourceUrl);
if (!videoId || !sourceUrl) {
return (
<div className="rounded-lg border border-dashed bg-muted/20 px-4 py-8 text-sm text-muted-foreground">
Analysis video is unavailable for this ticket.
</div>
);
}
return (
<div className="space-y-2">
<AnnotatedVideoPlayer
videoRef={videoRef}
logs={[]}
visibleDetections={visibleDetections}
videoWidth={videoWidth}
videoHeight={videoHeight}
videoUrl={videoUrl}
isLoading={isVideoLoading}
isError={isVideoError}
onRetry={() => void refetchVideo()}
onTimeUpdate={handleTimeUpdate}
onVideoFrame={handleVideoFrame}
onSeeked={handleSeeked}
/>
</div>
);
}

View File

@@ -2,7 +2,6 @@
import { import {
Activity, Activity,
ArrowRight,
CalendarDays, CalendarDays,
Clock3, Clock3,
Gauge, Gauge,
@@ -10,7 +9,7 @@ import {
Monitor, Monitor,
UserRound, UserRound,
} from 'lucide-react'; } from 'lucide-react';
import { useRouter } from 'next/navigation'; import { useState } from 'react';
import { import {
Card, Card,
@@ -20,12 +19,20 @@ import {
CardTitle, CardTitle,
} from '@/components/ui/card'; } from '@/components/ui/card';
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import { getDefectVisual } from '@/constants/defectVisualConfig'; import { getDefectVisual } from '@/constants/defectVisualConfig';
import { PERMISSIONS } from '@/constants/permissions'; import { PERMISSIONS } from '@/constants/permissions';
import { PermissionGuard } from '@/guards'; import { PermissionGuard } from '@/guards';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
import type { TicketOverviewDetail } from '@/types'; import type { TicketOverviewDetail } from '@/types';
import { formatDate } from '@/utils/date'; import { formatDate } from '@/utils/date';
import { TicketInlineAnalysisVideo } from './TicketInlineAnalysisVideo';
type IconComponent = React.ComponentType<{ className?: string }>; type IconComponent = React.ComponentType<{ className?: string }>;
@@ -105,7 +112,7 @@ export function TicketOverviewCard({
}: { }: {
ticket: TicketOverviewDetail; ticket: TicketOverviewDetail;
}) { }) {
const router = useRouter(); const [isAnalysisOpen, setIsAnalysisOpen] = useState(false);
const videoMetadata = ticket.video_metadata; const videoMetadata = ticket.video_metadata;
const duration = const duration =
typeof videoMetadata?.duration_seconds === 'number' typeof videoMetadata?.duration_seconds === 'number'
@@ -125,11 +132,9 @@ export function TicketOverviewCard({
const uploaderName = ticket.uploader?.name || null; const uploaderName = ticket.uploader?.name || null;
const analysisVideoId = ticket.video_id || ticket.video?.id || null; const analysisVideoId = ticket.video_id || ticket.video?.id || null;
const videoName = ticket.video?.name || null; const videoName = ticket.video?.name || null;
const analysisTitle = videoName || ticket.ticket_name || 'Analysis Video';
const hasVideoMetrics = Boolean( const hasVideoMetrics = Boolean(
videoName || videoMetadata?.fps || duration || videoMetadata?.resolution?.label,
videoMetadata?.fps ||
duration ||
videoMetadata?.resolution?.label,
); );
return ( return (
@@ -178,10 +183,9 @@ export function TicketOverviewCard({
variant="ghost" variant="ghost"
size="sm" size="sm"
className="text-primary hover:bg-primary/10 hover:text-primary" className="text-primary hover:bg-primary/10 hover:text-primary"
onClick={() => router.push(`/results/${analysisVideoId}`)} onClick={() => setIsAnalysisOpen(true)}
> >
View Analysis View Analysis
<ArrowRight className="size-3.5" />
</Button> </Button>
</PermissionGuard> </PermissionGuard>
</CardAction> </CardAction>
@@ -210,9 +214,6 @@ export function TicketOverviewCard({
</div> </div>
{hasVideoMetrics ? ( {hasVideoMetrics ? (
<div className="grid gap-3 rounded-lg bg-muted/35 px-4 py-3 text-sm sm:grid-cols-3"> <div className="grid gap-3 rounded-lg bg-muted/35 px-4 py-3 text-sm sm:grid-cols-3">
{videoName ? (
<VideoMetric icon={Monitor} label={videoName} />
) : null}
{videoMetadata?.fps ? ( {videoMetadata?.fps ? (
<VideoMetric icon={Gauge} label={`${videoMetadata.fps} FPS`} /> <VideoMetric icon={Gauge} label={`${videoMetadata.fps} FPS`} />
) : null} ) : null}
@@ -229,6 +230,20 @@ export function TicketOverviewCard({
) : null} ) : null}
</CardContent> </CardContent>
</Card> </Card>
<Dialog open={isAnalysisOpen} onOpenChange={setIsAnalysisOpen}>
<DialogContent className="gap-3 p-3 sm:max-w-5xl sm:p-4">
<DialogHeader className="min-w-0 pr-8">
<DialogTitle className="truncate text-left text-base">
{analysisTitle}
</DialogTitle>
<DialogDescription className="truncate text-xs">
Video analysis with annotation overlay
</DialogDescription>
</DialogHeader>
<TicketInlineAnalysisVideo ticket={ticket} />
</DialogContent>
</Dialog>
</div> </div>
); );
} }

View File

@@ -1,8 +1,8 @@
import { getDefectVisual } from '@/constants/defectVisualConfig'; import { getDefectVisual } from '@/constants/defectVisualConfig';
import type { DetectionResultItem } from '@/types'; import type { BoundingBoxOverlayItem } from '@/types';
interface BoundingBoxOverlayProps { interface BoundingBoxOverlayProps {
detections: DetectionResultItem[]; detections: BoundingBoxOverlayItem[];
videoWidth: number; videoWidth: number;
videoHeight: number; videoHeight: number;
} }
@@ -23,7 +23,7 @@ export default function BoundingBoxOverlay({
viewBox={`0 0 ${videoWidth} ${videoHeight}`} viewBox={`0 0 ${videoWidth} ${videoHeight}`}
preserveAspectRatio="xMidYMid meet" preserveAspectRatio="xMidYMid meet"
> >
{detections.map(({ id, detection }) => { {detections.map((detection) => {
const { bounding_box: box } = detection; const { bounding_box: box } = detection;
const color = getDefectVisual(detection.class_name).boundingBoxColor; const color = getDefectVisual(detection.class_name).boundingBoxColor;
const label = `${detection.display_name} ${(detection.confidence * 100).toFixed(0)}%`; const label = `${detection.display_name} ${(detection.confidence * 100).toFixed(0)}%`;
@@ -31,7 +31,7 @@ export default function BoundingBoxOverlay({
const labelWidth = Math.max(150, label.length * 17); const labelWidth = Math.max(150, label.length * 17);
return ( return (
<g key={id}> <g key={detection.id}>
<rect <rect
x={box.x1} x={box.x1}
y={box.y1} y={box.y1}

View File

@@ -4,7 +4,7 @@ import { AlertTriangle, ImageIcon, Loader2 } from 'lucide-react';
import Image from 'next/image'; import Image from 'next/image';
import { useProtectedMediaObjectUrl } from '@/hooks/useProtectedMedia'; import { useProtectedMediaObjectUrl } from '@/hooks/useProtectedMedia';
import type { DetectionResultItem } from '@/types'; import type { BoundingBoxOverlayItem, DetectionResultItem } from '@/types';
import BoundingBoxOverlay from '@/components/annotation/boundingBoxOverlay'; import BoundingBoxOverlay from '@/components/annotation/boundingBoxOverlay';
@@ -22,6 +22,17 @@ export default function AnnotatedDetectionImage({
const { objectUrl, isLoading, isError } = useProtectedMediaObjectUrl( const { objectUrl, isLoading, isError } = useProtectedMediaObjectUrl(
detection?.detection.context_image_url, detection?.detection.context_image_url,
); );
const overlayDetections: BoundingBoxOverlayItem[] = detection
? [
{
id: detection.id,
class_name: detection.detection.class_name,
display_name: detection.detection.display_name,
confidence: detection.detection.confidence,
bounding_box: detection.detection.bounding_box,
},
]
: [];
const aspectRatio = const aspectRatio =
videoWidth > 0 && videoHeight > 0 videoWidth > 0 && videoHeight > 0
@@ -58,7 +69,7 @@ export default function AnnotatedDetectionImage({
className="object-contain" className="object-contain"
/> />
<BoundingBoxOverlay <BoundingBoxOverlay
detections={[detection]} detections={overlayDetections}
videoWidth={videoWidth} videoWidth={videoWidth}
videoHeight={videoHeight} videoHeight={videoHeight}
/> />

View File

@@ -2,7 +2,10 @@
import { RefObject, useEffect } from 'react'; import { RefObject, useEffect } from 'react';
import { AlertTriangle, Loader2 } from 'lucide-react'; import { AlertTriangle, Loader2 } from 'lucide-react';
import type { DetectionResultLog } from '@/types'; import type {
DetectionResultLog,
VideoAnnotationFrameDetection,
} from '@/types';
import { useDetectionThumbnails } from './useDetectionThumbnails'; import { useDetectionThumbnails } from './useDetectionThumbnails';
import BoundingBoxOverlay from '@/components/annotation/boundingBoxOverlay'; import BoundingBoxOverlay from '@/components/annotation/boundingBoxOverlay';
import { import {
@@ -21,7 +24,7 @@ import {
interface AnnotatedVideoPlayerProps { interface AnnotatedVideoPlayerProps {
videoRef: RefObject<MediaPlayerInstance | null>; videoRef: RefObject<MediaPlayerInstance | null>;
logs: DetectionResultLog[]; logs: DetectionResultLog[];
visibleLogs: DetectionResultLog[]; visibleDetections: VideoAnnotationFrameDetection[];
videoWidth: number; videoWidth: number;
videoHeight: number; videoHeight: number;
videoUrl: string | null; videoUrl: string | null;
@@ -60,7 +63,7 @@ function VideoFrameSync({
export default function AnnotatedVideoPlayer({ export default function AnnotatedVideoPlayer({
videoRef, videoRef,
logs, logs,
visibleLogs, visibleDetections,
videoWidth, videoWidth,
videoHeight, videoHeight,
videoUrl, videoUrl,
@@ -92,7 +95,7 @@ export default function AnnotatedVideoPlayer({
</MediaProvider> </MediaProvider>
<VideoFrameSync onVideoFrame={onVideoFrame} /> <VideoFrameSync onVideoFrame={onVideoFrame} />
<BoundingBoxOverlay <BoundingBoxOverlay
detections={visibleLogs} detections={visibleDetections}
videoWidth={videoWidth} videoWidth={videoWidth}
videoHeight={videoHeight} videoHeight={videoHeight}
/> />

View File

@@ -0,0 +1,242 @@
'use client';
import { useQueryClient } from '@tanstack/react-query';
import {
RefObject,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import type { MediaPlayerInstance } from '@vidstack/react';
import { useVideoAnnotationFramesQuery } from '@/app/(modules)/results/hooks/useVideoResults';
import { videoKeys } from '@/app/(modules)/results/queries/videoKeys';
import { videoService } from '@/services/api';
import type {
DetectionResultLog,
VideoAnnotationFrameDetection,
VideoAnnotationFramesParams,
VideoAnnotationFramesResponse,
} from '@/types';
const ANNOTATION_WINDOW_MS = 30000;
const ANNOTATION_PREFETCH_THRESHOLD_MS = 5000;
const buildWindowParams = (
startTimeMs: number,
): VideoAnnotationFramesParams => ({
start_time_ms: startTimeMs,
end_time_ms: startTimeMs + ANNOTATION_WINDOW_MS,
});
const getWindowStartMs = (currentTimeMs: number) =>
Math.floor(Math.max(currentTimeMs, 0) / ANNOTATION_WINDOW_MS) *
ANNOTATION_WINDOW_MS;
interface UseVideoAnnotationPlaybackParams {
videoId: string | undefined;
logs: DetectionResultLog[];
fps: number;
videoRef: RefObject<MediaPlayerInstance | null>;
}
export function useVideoAnnotationPlayback({
videoId,
logs,
fps,
videoRef,
}: UseVideoAnnotationPlaybackParams) {
const queryClient = useQueryClient();
const shouldPauseAfterSeekRef = useRef(false);
const visibleDetectionIdsRef = useRef('');
const [currentWindowStartMs, setCurrentWindowStartMs] = useState(0);
const [activeLog, setActiveLog] = useState<DetectionResultLog | undefined>();
const [visibleDetections, setVisibleDetections] = useState<
VideoAnnotationFrameDetection[]
>([]);
const sortedLogs = useMemo(
() =>
[...logs].sort(
(a, b) => a.frame.timestamp_seconds - b.frame.timestamp_seconds,
),
[logs],
);
const currentWindowParams = useMemo(
() => buildWindowParams(currentWindowStartMs),
[currentWindowStartMs],
);
const annotationFramesQuery = useVideoAnnotationFramesQuery(
videoId,
currentWindowParams,
);
useEffect(() => {
setActiveLog((current) => {
if (current && sortedLogs.some((log) => log.id === current.id)) {
return current;
}
return sortedLogs[0];
});
}, [sortedLogs]);
const prefetchWindow = useCallback(
(startTimeMs: number) => {
if (!videoId || startTimeMs < 0) return;
const params = buildWindowParams(startTimeMs);
void queryClient.prefetchQuery({
queryKey: videoKeys.annotationFrames(videoId, params),
queryFn: () => videoService.getVideoAnnotationFrames(videoId, params),
staleTime: 1000 * 60 * 5,
gcTime: 1000 * 60 * 30,
});
},
[queryClient, videoId],
);
const getWindowData = useCallback(
(startTimeMs: number) => {
if (!videoId) return undefined;
const params = buildWindowParams(startTimeMs);
return (
queryClient.getQueryData<VideoAnnotationFramesResponse>(
videoKeys.annotationFrames(videoId, params),
) ??
(startTimeMs === currentWindowStartMs
? annotationFramesQuery.data
: undefined)
);
},
[annotationFramesQuery.data, currentWindowStartMs, queryClient, videoId],
);
const updateActiveLog = useCallback(
(currentTimeSeconds: number) => {
if (sortedLogs.length === 0) {
setActiveLog(undefined);
return;
}
const currentLog = sortedLogs.findLast(
(log) => log.frame.timestamp_seconds <= currentTimeSeconds,
);
const nextActiveLog = currentLog ?? sortedLogs[0];
setActiveLog((previous) =>
previous?.id === nextActiveLog.id ? previous : nextActiveLog,
);
},
[sortedLogs],
);
const getVisibleDetections = useCallback(
(currentTimeSeconds: number) => {
const currentTimeMs = currentTimeSeconds * 1000;
const targetWindowStartMs = getWindowStartMs(currentTimeMs);
if (targetWindowStartMs !== currentWindowStartMs) {
setCurrentWindowStartMs(targetWindowStartMs);
}
const windowData = getWindowData(targetWindowStartMs);
if (!windowData) {
return [];
}
if (
windowData.window.has_more_after &&
currentTimeMs >=
windowData.window.end_time_ms - ANNOTATION_PREFETCH_THRESHOLD_MS
) {
prefetchWindow(windowData.window.end_time_ms);
}
const frameDurationMs = fps > 0 ? 1000 / fps : 0;
const currentFrame = windowData.frames.find((frame) => {
const frameEndMs =
frame.end_timestamp_ms ?? frame.timestamp_ms + frameDurationMs;
return (
currentTimeMs >= frame.timestamp_ms && currentTimeMs < frameEndMs
);
});
return currentFrame?.detections ?? [];
},
[currentWindowStartMs, fps, getWindowData, prefetchWindow],
);
const updateVisibleDetections = useCallback(
(currentTimeSeconds: number) => {
const nextDetections = getVisibleDetections(currentTimeSeconds);
const nextIds = nextDetections.map((detection) => detection.id).join('|');
if (nextIds === visibleDetectionIdsRef.current) return;
visibleDetectionIdsRef.current = nextIds;
setVisibleDetections(nextDetections);
},
[getVisibleDetections],
);
const handleSeek = useCallback(
(log: DetectionResultLog) => {
const video = videoRef.current;
if (!video) return;
const targetTimeSeconds = log.frame.timestamp_seconds;
const targetWindowStartMs = getWindowStartMs(targetTimeSeconds * 1000);
prefetchWindow(targetWindowStartMs);
setCurrentWindowStartMs(targetWindowStartMs);
shouldPauseAfterSeekRef.current = true;
video.pause();
video.currentTime = targetTimeSeconds;
setActiveLog(log);
updateVisibleDetections(targetTimeSeconds);
},
[prefetchWindow, updateVisibleDetections, videoRef],
);
const handleSeeked = useCallback(() => {
const video = videoRef.current;
if (!video || !shouldPauseAfterSeekRef.current) return;
shouldPauseAfterSeekRef.current = false;
video.pause();
}, [videoRef]);
const handleTimeUpdate = useCallback(() => {
const video = videoRef.current;
if (!video) return;
updateActiveLog(video.currentTime);
}, [updateActiveLog, videoRef]);
const handleVideoFrame = useCallback(
(mediaTime: number) => {
updateActiveLog(mediaTime);
updateVisibleDetections(mediaTime);
},
[updateActiveLog, updateVisibleDetections],
);
return {
activeLog,
annotationFramesQuery,
handleSeek,
handleSeeked,
handleTimeUpdate,
handleVideoFrame,
sortedLogs,
visibleDetections,
};
}

View File

@@ -1,36 +1,60 @@
'use client'; 'use client';
import { useMemo, useState } from 'react'; import { useRef } from 'react';
import { ImageIcon } from 'lucide-react'; import type { MediaPlayerInstance } from '@vidstack/react';
import { Film } from 'lucide-react';
import { useProtectedVideoQuery } from '@/app/(modules)/results/hooks/useVideoResults';
import type { CompletedVideoResult } from '@/types';
import DetectionLocationMap from './map/detectionLocationMap';
import { import {
Card, Card,
CardContent, CardContent,
CardDescription, CardDescription,
CardHeader, CardHeader,
CardTitle, CardTitle,
} from '@/components/ui/card'; } from './ui/card';
import { CompletedVideoResult } from '@/types';
import DetectionLocationMap from './map/detectionLocationMap';
import AnnotatedDetectionImage from './video/annotatedDetectionImage'; import AnnotatedDetectionImage from './video/annotatedDetectionImage';
import AnnotatedVideoPlayer from './video/annotatedVideoPlayer';
import CurrentDetectionBar from './video/currentDetectionBar'; import CurrentDetectionBar from './video/currentDetectionBar';
import DetectionLogs from './video/detectionLogs'; import DetectionLogs from './video/detectionLogs';
import ResultStatsGrid from './video/resultStatsGrid'; import ResultStatsGrid from './video/resultStatsGrid';
import { useVideoAnnotationPlayback } from './video/useVideoAnnotationPlayback';
type VideoPlayerSectionProps = { type VideoPlayerSectionProps = {
data: CompletedVideoResult; data: CompletedVideoResult;
}; };
export default function VideoPlayerSection({ data }: VideoPlayerSectionProps) { export default function VideoPlayerSection({ data }: VideoPlayerSectionProps) {
const sortedLogs = useMemo( const videoRef = useRef<MediaPlayerInstance | null>(null);
() => const {
[...data.logs].sort( activeLog,
(a, b) => a.frame.timestamp_seconds - b.frame.timestamp_seconds, handleSeek,
), handleSeeked,
[data.logs], handleTimeUpdate,
); handleVideoFrame,
const [selectedLogId, setSelectedLogId] = useState(sortedLogs[0]?.id); sortedLogs,
const activeLog = visibleDetections,
sortedLogs.find((log) => log.id === selectedLogId) ?? sortedLogs[0]; } = useVideoAnnotationPlayback({
videoId: data.video_id,
logs: data.logs,
fps: data.summary.fps,
videoRef,
});
const {
videoUrl,
isLoading: isVideoLoading,
isError: isVideoError,
refetch: refetchVideo,
} = useProtectedVideoQuery(data.raw_video_url);
const mediaAspectRatio =
data.summary.video_width > 0 && data.summary.video_height > 0
? `${data.summary.video_width} / ${data.summary.video_height}`
: '16 / 9';
const activeMapLabel = activeLog
? `${activeLog.detection.display_name} - Frame ${activeLog.frame.number}`
: 'Detection';
return ( return (
<div className="space-y-6"> <div className="space-y-6">
@@ -38,14 +62,15 @@ export default function VideoPlayerSection({ data }: VideoPlayerSectionProps) {
<CardHeader className="border-b pb-4"> <CardHeader className="border-b pb-4">
<div className="flex items-center gap-3"> <div className="flex items-center gap-3">
<div className="rounded bg-secondary p-2"> <div className="rounded bg-secondary p-2">
<ImageIcon className="h-5 w-5 text-primary" /> <Film className="h-5 w-5 text-primary" />
</div> </div>
<div> <div>
<CardTitle className="text-lg font-bold"> <CardTitle className="text-lg font-bold">
Detection Preview Detection Playback
</CardTitle> </CardTitle>
<CardDescription className="text-xs"> <CardDescription className="text-xs">
Selected detection image with its bounding box Raw video with frontend annotation overlay and selected
detection location
</CardDescription> </CardDescription>
</div> </div>
</div> </div>
@@ -53,17 +78,48 @@ export default function VideoPlayerSection({ data }: VideoPlayerSectionProps) {
<CardContent className="pt-6"> <CardContent className="pt-6">
<div className="grid grid-cols-1 gap-6 lg:grid-cols-3"> <div className="grid grid-cols-1 gap-6 lg:grid-cols-3">
<div className="space-y-6 lg:col-span-2"> <div className="space-y-6 lg:col-span-2">
<div className="space-y-2">
<p className="text-xs font-medium text-muted-foreground">
Detection Video
</p>
<AnnotatedVideoPlayer
videoRef={videoRef}
logs={sortedLogs}
visibleDetections={visibleDetections}
videoWidth={data.summary.video_width}
videoHeight={data.summary.video_height}
videoUrl={videoUrl}
isLoading={isVideoLoading}
isError={isVideoError}
onRetry={() => void refetchVideo()}
onTimeUpdate={handleTimeUpdate}
onVideoFrame={handleVideoFrame}
onSeeked={handleSeeked}
/>
</div>
<div className="grid gap-4 xl:grid-cols-[minmax(0,1.65fr)_minmax(280px,1fr)]">
<div className="space-y-2">
<p className="text-xs font-medium text-muted-foreground">
Annotated Image
</p>
<AnnotatedDetectionImage <AnnotatedDetectionImage
detection={activeLog} detection={activeLog}
videoWidth={data.summary.video_width} videoWidth={data.summary.video_width}
videoHeight={data.summary.video_height} videoHeight={data.summary.video_height}
/> />
</div>
<DetectionLocationMap <DetectionLocationMap
latitude={activeLog?.location.latitude ?? null} latitude={activeLog?.location.latitude ?? null}
longitude={activeLog?.location.longitude ?? null} longitude={activeLog?.location.longitude ?? null}
label={activeLog?.detection.display_name ?? 'Detection'} label={activeMapLabel}
title="Location on Map"
variant="plain"
aspectRatio={mediaAspectRatio}
showCoordinates={false}
/> />
</div>
<CurrentDetectionBar activeLog={activeLog} data={data} /> <CurrentDetectionBar activeLog={activeLog} data={data} />
</div> </div>
@@ -71,7 +127,7 @@ export default function VideoPlayerSection({ data }: VideoPlayerSectionProps) {
<DetectionLogs <DetectionLogs
logs={sortedLogs} logs={sortedLogs}
activeLogId={activeLog?.id} activeLogId={activeLog?.id}
onSelect={(log) => setSelectedLogId(log.id)} onSelect={handleSeek}
/> />
</div> </div>

View File

@@ -54,6 +54,8 @@ export const API_ROUTES = {
UPLOAD_EVENTS: (token: string) => UPLOAD_EVENTS: (token: string) =>
`/biz/api/v1/uploads/events?sse_token=${encodeURIComponent(token)}`, `/biz/api/v1/uploads/events?sse_token=${encodeURIComponent(token)}`,
RESULTS: (id: string) => `/biz/api/v1/results/${id}/completed`, RESULTS: (id: string) => `/biz/api/v1/results/${id}/completed`,
ANNOTATION_FRAMES: (id: string) =>
`/biz/api/v1/results/${id}/annotation-frames`,
DETECTIONS: (id: string) => `/biz/api/v1/results/${id}/detections`, DETECTIONS: (id: string) => `/biz/api/v1/results/${id}/detections`,
}, },
TICKETS: { TICKETS: {

View File

@@ -5,6 +5,8 @@ import {
PaginationParams, PaginationParams,
SseTokenResponse, SseTokenResponse,
UploadListResponse, UploadListResponse,
VideoAnnotationFramesParams,
VideoAnnotationFramesResponse,
VideoDetectionsParams, VideoDetectionsParams,
VideoDetectionsResponse, VideoDetectionsResponse,
} from '@/types'; } from '@/types';
@@ -81,15 +83,28 @@ export const videoService = {
return response.data; return response.data;
}, },
getVideoAnnotationFrames: async (
videoId: string,
params: VideoAnnotationFramesParams,
): Promise<VideoAnnotationFramesResponse> => {
const response = await axiosClient.get<VideoAnnotationFramesResponse>(
API_ROUTES.VIDEOS.ANNOTATION_FRAMES(videoId),
{
params,
},
);
return response.data;
},
/** /**
* Fetch the annotated video as a blob through the authenticated axios client. * Fetch a protected video as a blob through the authenticated axios client.
* *
* The browser's native `<video src>` request cannot carry the Bearer token, * The browser's native `<video src>` request cannot carry the Bearer token,
* which makes the protected media endpoint respond with 401. Proxying the * which makes the protected media endpoint respond with 401. Proxying the
* download through axios attaches the auth header (and benefits from the * download through axios attaches the auth header (and benefits from the
* refresh-on-401 interceptor) so the bytes can be played back locally. * refresh-on-401 interceptor) so the bytes can be played back locally.
*/ */
getAnnotatedVideo: async (url: string): Promise<Blob> => { getProtectedVideo: async (url: string): Promise<Blob> => {
const response = await axiosClient.get<Blob>(url, { const response = await axiosClient.get<Blob>(url, {
responseType: 'blob', responseType: 'blob',
}); });

View File

@@ -14,6 +14,14 @@ export type DetectionBoundingBox = {
height: number; height: number;
}; };
export type BoundingBoxOverlayItem = {
id: string | number;
class_name: string;
display_name: string;
confidence: number;
bounding_box: DetectionBoundingBox;
};
export type DetectionResultItem = { export type DetectionResultItem = {
id: string; id: string;
detection: { detection: {
@@ -66,3 +74,45 @@ export type VideoDetectionsResponse = {
sort: string; sort: string;
items: DetectionResultItem[]; items: DetectionResultItem[];
}; };
export type VideoAnnotationFramesParams = {
start_time_ms: number;
end_time_ms: number;
};
export type VideoAnnotationFrameDetection = BoundingBoxOverlayItem & {
location: {
latitude: number | null;
longitude: number | null;
};
};
export type VideoAnnotationFrame = {
frame_number: number;
timestamp_ms: number;
timestamp_seconds: number;
end_timestamp_ms?: number;
detections: VideoAnnotationFrameDetection[];
};
export type VideoAnnotationFramesResponse = {
video_id: string;
raw_video_url: string;
video: {
width: number;
height: number;
fps: number;
duration_seconds: number;
total_frames: number;
};
coordinate_format: 'xyxy_pixel' | string;
window: {
start_time_ms: number;
end_time_ms: number;
has_more_before: boolean;
has_more_after: boolean;
};
frame_count_in_window: number;
detection_count_in_window: number;
frames: VideoAnnotationFrame[];
};