-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphoto_match_tinyimg2.py
45 lines (38 loc) · 1.81 KB
/
photo_match_tinyimg2.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
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 4 22:54:26 2015
@author: keith
"""
import scipy
from scipy import misc, ndimage
import numpy as np
class photoMatch(object):
def __init__(self, par={'fidelity': 5}):
print "Initializing an instance of the photo match that uses an average colour as its distance metric"
self.type = 'Tiny image, comparing mean'
N = par['fidelity']
self.compareSize = (N,N)
self.totalSize = N*N
self.fullSize = (75,75)
# reduces a photo (of any size) given as an MxNx3 scipy array (or should it be a PIL image?)
# down to some more easily compared representation
# baasically a coarse graining
def compactRepresentation(self, photo):
arr = scipy.ones((self.compareSize[0], self.compareSize[1], 3))*scipy.NaN
if len(photo.shape) == 3: # an MxNx3 array of of ANY size:
arr = photo
elif photo.size == scipy.prod(self.fullSize)*3: # must be NxNx3, with N as in self.fullSize
arr = photo.reshape((self.fullSize[0],self.fullSize[1],3))
return scipy.misc.imresize(arr, self.compareSize).reshape((1,self.totalSize*3))
# provide some distance between two compact representations of photos
# if photo1==photo2, distance should ideally be zeros
# here the L2-norm distance is used
def compactDistance(self, target, candidates):
#compare the candidates to the target accordin to some measure
targetarr = target.reshape((self.totalSize, 3))
candidatesarr = candidates.reshape((candidates.shape[0], self.totalSize, 3))
target_avg = scipy.mean(targetarr, axis=0)
candidates_avg = scipy.mean(candidatesarr, axis=1)
return scipy.sum((target_avg - candidates_avg)**2, axis=1)
def formatOutput(self, arr):
return arr