mitsuke-live
Version:
Real-time object detection library for web browsers using TensorFlow.js and YOLO
93 lines (92 loc) • 2.84 kB
JavaScript
import { convertDegreesToRadians } from "../helpers/math-helper.js";
import { Vector3, MathUtils } from "../node_modules/three/build/three.core.js";
function normalizeDetectionCoordinates(centerX, centerY, detectionWidth, detectionHeight) {
return {
x: centerX / detectionWidth * 2 - 1,
y: -(centerY / detectionHeight * 2 - 1)
};
}
function calculateWorldPosition(normalizedX, normalizedY, depth, aspectRatio, halfTanFov) {
const viewHeight = 2 * halfTanFov * depth;
const viewWidth = viewHeight * aspectRatio;
return new Vector3(
normalizedX * viewWidth * 0.5,
normalizedY * viewHeight * 0.5,
-depth
);
}
function calculateARTransform(detection, viewport, halfTanFov, scaleMultiplier = 1) {
const [x, y, , height] = detection.boundingBox;
const normalized = normalizeDetectionCoordinates(
x,
y,
viewport.detectionWidth,
viewport.detectionHeight
);
const aspectRatio = viewport.displayWidth / viewport.displayHeight;
const position = calculateWorldPosition(
normalized.x,
normalized.y,
detection.depth,
aspectRatio,
halfTanFov
);
const viewHeight = 2 * halfTanFov * detection.depth;
const worldHeight = height / viewport.detectionHeight * viewHeight;
const scale = worldHeight * scaleMultiplier;
const rotation = new Vector3(0, 0, 0);
if (detection.orientation) {
rotation.x = convertDegreesToRadians(detection.orientation.pitch);
rotation.y = convertDegreesToRadians(detection.orientation.yaw);
rotation.z = convertDegreesToRadians(detection.orientation.roll);
}
return {
position,
rotation,
scale: new Vector3(scale, scale, scale)
};
}
function createSmoothTransformState() {
return {
targetPosition: new Vector3(),
targetRotation: new Vector3(),
targetScale: new Vector3(1, 1, 1)
};
}
function updateSmoothTargets(state, detection, viewport, halfTanFov, scaleMultiplier = 1) {
const transform = calculateARTransform(
detection,
viewport,
halfTanFov,
scaleMultiplier
);
state.targetPosition.copy(transform.position);
state.targetRotation.copy(transform.rotation);
state.targetScale.copy(transform.scale);
}
function applySmoothTransform(object, state, lerpFactor = 0.15) {
object.position.lerp(state.targetPosition, lerpFactor);
object.rotation.x = MathUtils.lerp(
object.rotation.x,
state.targetRotation.x,
lerpFactor
);
object.rotation.y = MathUtils.lerp(
object.rotation.y,
state.targetRotation.y,
lerpFactor
);
object.rotation.z = MathUtils.lerp(
object.rotation.z,
state.targetRotation.z,
lerpFactor
);
object.scale.lerp(state.targetScale, lerpFactor);
}
export {
applySmoothTransform,
createSmoothTransformState,
normalizeDetectionCoordinates,
updateSmoothTargets
};
//# sourceMappingURL=three-helpers.js.map