Python 计算人体动作关键点相似度:余弦相似度实现及百分制转换
可以使用欧几里德距离或余弦相似度来计算两个动作之间的相似度。以下是使用余弦相似度的示例代码:
import numpy as np
def cosine_similarity(x, y):
'计算两个向量之间的余弦相似度'
dot_product = np.dot(x, y)
norm_x = np.linalg.norm(x)
norm_y = np.linalg.norm(y)
return dot_product / (norm_x * norm_y)
# 生成两个随机动作的关键点数据
action1 = np.random.rand(15, 2)
action2 = np.random.rand(15, 2)
# 计算两个动作之间的余弦相似度
similarity = cosine_similarity(action1.flatten(), action2.flatten())
# 将相似度转化为百分制
similarity_percent = similarity * 100
print('动作1:')
print(action1)
print('动作2:')
print(action2)
print('余弦相似度:', similarity)
print('相似度百分比:', similarity_percent)
输出示例:
动作1:
[[0.53081163 0.51505411]
[0.29102966 0.87409439]
[0.40486209 0.69696927]
[0.94669205 0.26611274]
[0.00738801 0.45793347]
[0.65369124 0.09219735]
[0.28700201 0.74247734]
[0.90442329 0.35132172]
[0.2442847 0.07100882]
[0.79086681 0.32584594]
[0.71780646 0.6336647 ]
[0.67420103 0.61341509]
[0.68487788 0.3242644 ]
[0.08148294 0.75466355]
[0.33496536 0.28635726]]
动作2:
[[0.16069534 0.63663107]
[0.22227613 0.29038259]
[0.96458339 0.55800626]
[0.0633725 0.98118591]
[0.89301994 0.45121297]
[0.42342101 0.63384537]
[0.85392177 0.43533878]
[0.51062239 0.50758182]
[0.73711609 0.95692685]
[0.06739501 0.90192686]
[0.24219056 0.49203182]
[0.13139481 0.17041411]
[0.0329004 0.80228337]
[0.63419345 0.15235264]
[0.98767876 0.64125684]]
余弦相似度: 0.7711047185626191
相似度百分比: 77.1104718562619
可以看到,两个随机动作的相似度约为77%。
原文地址: https://www.cveoy.top/t/topic/luYU 著作权归作者所有。请勿转载和采集!