forked from yuxiwang66/Iris-Recognition-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculate_dist_mat_mmu2.py
117 lines (92 loc) · 3.39 KB
/
calculate_dist_mat_mmu2.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#------------------------------------------------------------------------------
# Libraries
#------------------------------------------------------------------------------
import os
import numpy as np
from glob import glob
from time import time
from random import shuffle
from matplotlib import pyplot as plt
from itertools import repeat
from multiprocessing import Pool, cpu_count
from fnc.extractFeature import extractFeature
from fnc.matching import calHammingDist
#------------------------------------------------------------------------------
# Parameters
#------------------------------------------------------------------------------
CASIA1_DIR = "/home/antiaegis/Downloads/Iris-Recognition/MMU2"
EYELASHES_THRES = 10
N_IMAGES = 5
#------------------------------------------------------------------------------
# Pool function of extracting feature
#------------------------------------------------------------------------------
def pool_func_extract_feature(args):
im_filename, eyelashes_thres, use_multiprocess = args
template, mask, im_filename = extractFeature(
im_filename=im_filename,
eyelashes_thres=eyelashes_thres,
use_multiprocess=use_multiprocess,
)
return template, mask, im_filename
#------------------------------------------------------------------------------
# Pool function of calculating Hamming distance
#------------------------------------------------------------------------------
def pool_func_calHammingDist(args):
template1, mask1, template2, mask2 = args
dist = calHammingDist(template1, mask1, template2, mask2)
return dist
#------------------------------------------------------------------------------
# Main execution
#------------------------------------------------------------------------------
# Get identities of MMU2 dataset
identities = glob(os.path.join(CASIA1_DIR, "**"))
identities = sorted([os.path.basename(identity) for identity in identities])
n_identities = len(identities)
print("Number of identities:", n_identities)
# Construct a dictionary of files
files_dict = {}
image_files = []
for identity in identities:
if identity=="50":
continue
files = glob(os.path.join(CASIA1_DIR, identity, "*.*"))
shuffle(files)
files_dict[identity] = files[:N_IMAGES]
# print("Identity %s: %d images" % (identity, len(files_dict[identity])))
image_files += files[:N_IMAGES]
n_image_files = len(image_files)
print("Number of image files:", n_image_files)
# Extract features
args = zip(image_files, repeat(EYELASHES_THRES), repeat(False))
pools = Pool(processes=cpu_count())
start_time = time()
features = list(pools.map(pool_func_extract_feature, args))
finish_time = time()
print("Extraction time: %.3f [s]" % (finish_time-start_time))
# Calculate the distances
args = []
for i in range(n_image_files):
for j in range(n_image_files):
if i>=j:
continue
arg = (features[i][0], features[i][1], features[j][0], features[j][1])
args.append(arg)
print("Number of pairs:", len(args))
start_time = time()
distances = pools.map(pool_func_calHammingDist, args)
finish_time = time()
print("Extraction time: %.3f [s]" % (finish_time-start_time))
# Construct a distance matrix
dist_mat = np.zeros([n_image_files, n_image_files])
k = 0
for i in range(n_image_files):
for j in range(n_image_files):
if i<j:
dist_mat[i, j] = distances[k]
k += 1
elif i>j:
dist_mat[i, j] = dist_mat[j, i]
np.save("dist_mat_mmu2.npy", dist_mat)
plt.figure()
plt.imshow(dist_mat)
plt.show()