face-api/src/euclideanDistance.ts

13 lines
402 B
TypeScript
Raw Normal View History

2020-08-18 13:54:53 +02:00
export function euclideanDistance(arr1: number[] | Float32Array, arr2: number[] | Float32Array) {
2020-08-26 00:24:48 +02:00
if (arr1.length !== arr2.length)
2020-08-18 13:54:53 +02:00
throw new Error('euclideanDistance: arr1.length !== arr2.length')
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
const desc1 = Array.from(arr1)
const desc2 = Array.from(arr2)
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
return Math.sqrt(
desc1
.map((val, i) => val - desc2[i])
.reduce((res, diff) => res + Math.pow(diff, 2), 0)
)
2020-08-26 00:24:48 +02:00
}