-
Notifications
You must be signed in to change notification settings - Fork 1
/
hash_code.py
86 lines (71 loc) · 2.37 KB
/
hash_code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# encoding: utf-8
"""
"""
import logging
import sys
sys.path.append('.')
import numpy as np
# import torch
# import tqdm
from torch.backends import cudnn
from identity_hushing.fastreid.config import get_cfg
from identity_hushing.fastreid.utils.logger import setup_logger
# from fastreid.data import build_reid_test_loader,build_reid_train_loader
from identity_hushing.demo.predictor import FeatureExtractionDemo
import torch.nn.functional as F
import hashlib
import glob
cudnn.benchmark = True
from PIL import Image
setup_logger(name="fastreid")
logger = logging.getLogger('fastreid.visualize_result')
import os
os.environ["CUDA_VISIBLE_DEVICES"] = '0,1' #use GPU with ID=0
def setup_cfg(args):
# load config from file and command-line arguments
cfg = get_cfg()
# add_partialreid_config(cfg)
cfg.merge_from_file(args.config_file)
cfg.merge_from_list(args.opts)
cfg.freeze()
return cfg
def postprocess(features):
# Normalize feature to compute cosine distance
features = F.normalize(features)
features = features.cpu().data.numpy()
return features
def get_hash(args):
# args = get_parser().parse_args()
cfg = setup_cfg(args)
demo = FeatureExtractionDemo(cfg, parallel=args.parallel)
logger.info("Start extracting image features")
hash_value=['']*1502
images1=glob.glob(args.dataset_path+'bounding_box_train/'+'*.jpg' )
images2=glob.glob(args.dataset_path+'query/'+'*.jpg' )
images3=glob.glob(args.dataset_path+'bounding_box_test/'+'*.jpg' )
files_list=[]
for i in images1:
files_list.append(i)
for i in images2:
files_list.append(i)
for i in images3:
files_list.append(i)
for i,filename in enumerate(files_list):
pid = int(filename.split('/')[-1].split('_')[0])
if pid<0 or hash_value[pid] !='':
continue
image = Image.open(filename).convert("RGB")
img=np.array(image)
img=img.transpose((1,0,2)) #hwc–>whc
feat =demo.run_on_image(img)
tmp = ""
for i in feat: #####
i=i.tolist()
tmp=''.join(str(i))
md = hashlib.md5()
md.update(tmp.encode('utf-8'))
hash_value[pid]= md.hexdigest()#mmh3.hash256(tmp,signed=False) [0] #md.hexdigest()
hash_np=np.array(hash_value)
np.savetxt(args.outdir + 'hash_128.txt',hash_np,fmt='%s')
if __name__ == '__main__':
get_hash()