Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 1x 1x 1x 1x 1x 11x 4x 4x 3x 3x 4x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 27x 2x 2x 2x 2x 2x 27x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 5x 5x 5x 8x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 8x 2x 2x 8x 1x 1x 1x 1x 1x 8x | import type { QuadCorners } from '@/types/vision'
/** Channel name for cross-tab boundary sample notifications */
export const BOUNDARY_SAMPLE_CHANNEL = 'boundary-sample-saved'
export interface SaveBoundarySampleParams {
/** Base64 image data (PNG or JPEG, without data URL prefix) */
imageData: string
/** Corner coordinates in pixel space */
corners: QuadCorners
/** Frame width in pixels */
frameWidth: number
/** Frame height in pixels */
frameHeight: number
/** Optional device ID for organizing captures */
deviceId?: string
/** Optional session ID (for passive captures during practice) */
sessionId?: string
/** Optional player ID (for passive captures during practice) */
playerId?: string
}
export interface SaveBoundarySampleResult {
success: boolean
savedTo?: string
error?: string
}
/**
* Normalize corner coordinates from pixel space to 0-1 range
*/
export function normalizeCorners(corners: QuadCorners, width: number, height: number): QuadCorners {
return {
topLeft: {
x: corners.topLeft.x / width,
y: corners.topLeft.y / height,
},
topRight: {
x: corners.topRight.x / width,
y: corners.topRight.y / height,
},
bottomLeft: {
x: corners.bottomLeft.x / width,
y: corners.bottomLeft.y / height,
},
bottomRight: {
x: corners.bottomRight.x / width,
y: corners.bottomRight.y / height,
},
}
}
/**
* Strip data URL prefix from base64 string if present
*/
export function stripDataUrlPrefix(data: string): string {
if (data.startsWith('data:')) {
const commaIndex = data.indexOf(',')
if (commaIndex > 0) {
return data.substring(commaIndex + 1)
}
}
return data
}
/**
* Save a boundary frame sample for training.
*
* This is the core function used by both:
* - BoundaryDataCapture (explicit capture in vision training)
* - Passive capture during practice sessions
*
* @param params - The sample data to save
* @returns Promise with success/error status
*/
export async function saveBoundarySample(
params: SaveBoundarySampleParams
): Promise<SaveBoundarySampleResult> {
const { imageData, corners, frameWidth, frameHeight, deviceId, sessionId, playerId } = params
// Normalize corners from pixel coordinates to 0-1 range
const normalizedCorners = normalizeCorners(corners, frameWidth, frameHeight)
// Validate normalized corners are in valid range
const allCorners = [
normalizedCorners.topLeft,
normalizedCorners.topRight,
normalizedCorners.bottomLeft,
normalizedCorners.bottomRight,
]
for (const corner of allCorners) {
if (corner.x < 0 || corner.x > 1 || corner.y < 0 || corner.y > 1) {
return {
success: false,
error: `Corner out of bounds: (${corner.x}, ${corner.y})`,
}
}
}
// Strip data URL prefix if present
const cleanImageData = stripDataUrlPrefix(imageData)
try {
const response = await fetch('/api/vision-training/boundary-samples', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
imageData: cleanImageData,
corners: normalizedCorners,
frameWidth,
frameHeight,
deviceId,
sessionId,
playerId,
}),
})
const result = await response.json()
if (result.success) {
// Broadcast to other tabs that a new sample was saved
if (typeof BroadcastChannel !== 'undefined') {
try {
const channel = new BroadcastChannel(BOUNDARY_SAMPLE_CHANNEL)
channel.postMessage({
type: 'sample-saved',
savedTo: result.savedTo,
deviceId,
sessionId,
playerId,
timestamp: Date.now(),
})
channel.close()
} catch {
// BroadcastChannel may not be available in some contexts
}
}
return { success: true, savedTo: result.savedTo }
} else {
return { success: false, error: result.error || 'Failed to save' }
}
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : 'Network error',
}
}
}
|