本文主要是介绍两张人像对比是否是同一人- deep-person-reid,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
基于的项目:https://github.com/KaiyangZhou/deep-person-reid
安装
git clone https://github.com/KaiyangZhou/deep-person-reid.gitcd deep-person-reid/pip install -r requirements.txtpython setup.py develop
权重下载
https://kaiyangzhou.github.io/deep-person-reid/MODEL_ZOO
reid.py
执行:
python reid.py --image_path1 input/1.jpg --image_path2 input/2.jpg
'''
python reid.py --image_path1 input/1.jpg --image_path2 input/2.jpg
'''
import torchreid
import torch
from PIL import Image
from torchreid.utils import FeatureExtractor
import argparse# Initialize the feature extractor with a pretrained model
extractor = FeatureExtractor(model_name='osnet_x1_0', # You can choose other models too, like 'resnet50', 'resnet101'model_path='osnet_x1_0_imagenet.pth', # None to use default pretrained model# device='cuda' # or 'cpu'device='cpu' # or 'cpu'
)def extract_feature(image_path):# 直接传递图像路径给 FeatureExtractorfeatures = extractor([image_path]) # Extract featuresreturn features[0] # Return the feature vectordef compare_images(image_path1, image_path2):# Extract features from both imagesfeatures1 = extract_feature(image_path1)features2 = extract_feature(image_path2)# Compute the cosine similaritysimilarity = torch.nn.functional.cosine_similarity(features1, features2, dim=0)return similarity.item()if __name__ == "__main__":parser = argparse.ArgumentParser()parser.add_argument('--image_path1', type=str, default='1.jpg')parser.add_argument('--image_path2', type=str, default='2.jpg')opt = parser.parse_args()image_path1 = opt.image_path1image_path2 = opt.image_path2similarity_score = compare_images(image_path1, image_path2)threshold = 0.7 # Adjust this threshold based on your use caseif similarity_score > threshold:print("The images are likely of the same person.")else:print("The images are likely of different people.")
结果
Model: osnet_x1_0
- params: 2,193,616
- flops: 978,878,352
Successfully loaded pretrained weights from “osnet_x1_0_imagenet.pth”
** The following layers are discarded due to unmatched keys or layer size: [‘classifier.weight’, ‘classifier.bias’]
The images are likely of different people.
Model: osnet_x1_0
- params: 2,193,616
- flops: 978,878,352
Successfully loaded pretrained weights from “osnet_x1_0_imagenet.pth”
** The following layers are discarded due to unmatched keys or layer size: [‘classifier.weight’, ‘classifier.bias’]
The images are likely of the same person.
这篇关于两张人像对比是否是同一人- deep-person-reid的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!