两张人像对比是否是同一人- deep-person-reid

基于的项目:https://github.com/KaiyangZhou/deep-person-reid

安装

git clone https://github.com/KaiyangZhou/deep-person-reid.git

cd deep-person-reid/

pip install -r requirements.txt

python 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):
    # 直接传递图像路径给 FeatureExtractor
    features = extractor([image_path])  # Extract features
    return features[0]  # Return the feature vector

def compare_images(image_path1, image_path2):
    # Extract features from both images
    features1 = extract_feature(image_path1)
    features2 = extract_feature(image_path2)

    # Compute the cosine similarity
    similarity = 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_path1
    image_path2 = opt.image_path2
    
    similarity_score = compare_images(image_path1, image_path2)

    threshold = 0.7  # Adjust this threshold based on your use case
    if 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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值