-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmatchLeaves.py
executable file
·75 lines (55 loc) · 1.78 KB
/
matchLeaves.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
#!/usr/bin/env python
"""
CSE 40535
Brenden Kokoszka
Project
"""
from math import *
import sys
import os
import cv2
import pickle
import numpy as np
import scipy as sp, scipy.spatial
def matchLeaf(probe, gallery):
bestMatch = None
bestMatchDist = np.inf
dist = sp.spatial.distance.euclidean
for (name, descriptor) in gallery:
sys.stderr.write(' %s\n' % name)
dists = []
# For all pairs of probe and gallery subdescriptors
for pDesc in probe:
minDist = min([dist(pDesc.flat, gDesc.flat) for gDesc in descriptor])
dists.append(minDist)
dists.sort()
avgDist = sum(dists[:10])/10.0
if avgDist < bestMatchDist:
bestMatch = name
bestMatchDist = avgDist
return bestMatch
def main():
# Check for proper arguments
if len(sys.argv) != 3:
print 'Usage: %s [gallery directory] [probe directory]' % sys.argv[0]
return
galleryDir = sys.argv[1]
probeDir = sys.argv[2]
fileName = lambda name : '.'.join(name.split('.')[:-1])
# Read all the gallery descriptors into a list
gallery = []
for galFile in os.listdir(galleryDir):
if galFile == 'index.csv': continue
galPath = galleryDir + '/' + galFile
gallery.append((fileName(galFile), pickle.load(open(galPath, 'r'))))
# Find the best match for each probe
matches = {}
for probeFile in os.listdir(probeDir):
sys.stderr.write('matching %s\n' % probeFile)
probePath = probeDir + '/' + probeFile
probe = pickle.load(open(probePath, 'r'))
galMatch = matchLeaf(probe, gallery)
matches[int(fileName(probeFile))] = int(galMatch)
print pickle.dumps(matches)
if __name__ == '__main__':
main()