简体   繁体   English

找到人脸地标之间的欧几里德距离

[英]Find Eucledian distance between landmarks of faces

I've got multiple frames and I've detected the faces in each frame using Retinaface .我有多个帧,并且使用Retinaface检测到每个帧中的人Retinaface I would like to keep track of the faces using their landmarks.我想使用他们的地标来跟踪面部。

To find the similarity between 2 landmarks, I tried to calculate the Eucledian distance :为了找到 2 个地标之间的相似性,我尝试计算欧几里得距离:

Input :输入 :

landmark_1 = [1828, 911], [1887, 913], [1841, 942], [1832, 974], [1876, 976]
landmark_2 = [1827, 928], [1887, 926], [1848, 963], [1836, 992], [1884, 990]

After referring other links, I wrote the below function, but the values produced are very high :参考其他链接后,我编写了以下函数,但产生的值非常高:

def euclidean_dist(vector_x, vector_y):
    vector_x, vector_y = np.array(vector_x), np.array(vector_y)
    if len(vector_x) != len(vector_y):
        raise Exception('Vectors must be same dimensions')
    ans = sum((vector_x[dim] - vector_y[dim]) ** 2 for dim in range(len(vector_x)))
    return np.sqrt(np.sum(ans**2))

Output :输出 :

euclidean_dist(landmark_1, landmark_2)
>> 1424.9424549784458

(Expecting some smaller value in this case) (在这种情况下期望一些较小的值)

I guess the code can only be used for an one dimensional vector, but I'm really stuck here.我想代码只能用于一维向量,但我真的被困在这里。 Any help would be really appreciated.任何帮助将非常感激。

It looks like you're squaring the answer twice ( ans**2 ).看起来您将答案平方两次( ans**2 )。 But you can also simplify the function somewhat:但是你也可以稍微简化一下函数:

def euclidean_dist(vector_x, vector_y):
    vector_x, vector_y = np.array(vector_x), np.array(vector_y)
    return np.sqrt(np.sum((vector_x - vector_y)**2, axis=-1))

This will automatically raise an exception when the vectors are incompatible shapes.当向量是不兼容的形状时,这将自动引发异常。

EDIT: If you use axis=-1 it will sum over the last axis of the array, so you can use a 2-D array of vectors, for example.编辑:如果您使用axis=-1它将在数组的最后一个轴上求和,因此您可以使用二维向量数组,例如。

You can use linalg.nor too.您也可以使用linalg.nor

def euclidean_dist(vector_x, vector_y):
    distances = np.linalg.norm(np.array(vector_x)-np.array(vector_y), axis=1)
    return distances.tolist()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM