-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_wrapper.py
More file actions
174 lines (133 loc) · 6.54 KB
/
Copy pathexample_wrapper.py
File metadata and controls
174 lines (133 loc) · 6.54 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import numpy as np
from pih_presets.utils import load_image
from pih_presets.deviation_presets import hamming_distance
from pih_presets.hashalgos_preset import dHash, aHash, pHash
"""Wrapper for aHash"""
def test_aHash(aOriginalImages, aComparativeImages, lThreshold=0.2, lHashSize=16, oCache=None):
# create dictionary of metadata
dicMetadata = {"algorithm": "aHash",
"hash_size": lHashSize, "threshold": lThreshold}
# compare every image
aDecisions = []
for i, sOriginalImagePath in enumerate(aOriginalImages):
sComparativeImagePath = aComparativeImages[i]
if oCache:
# create unique cache key for original and comparative key
aCacheKeyBase = ["aHash", lHashSize]
sOriginalImageCacheKey = oCache.calc_unique_key(
*aCacheKeyBase, sOriginalImagePath)
sComparativeImageCacheKey = oCache.calc_unique_key(
*aCacheKeyBase, sComparativeImagePath)
# check for existence of keys in cache
aHashOriginal = oCache.get(sOriginalImageCacheKey)
aHashComparative = oCache.get(sComparativeImageCacheKey)
# get images from path and calculate hash if not in hash already
# add to cache if calculated the first time and cache is active
if aHashOriginal is None:
aOriginalImage = load_image(sOriginalImagePath)
aHashOriginal = aHash(aOriginalImage, hash_size=lHashSize)
if oCache:
oCache.set(sOriginalImageCacheKey, aHashOriginal)
if aHashComparative is None:
aComparativeImage = load_image(sComparativeImagePath)
aHashComparative = aHash(aComparativeImage, hash_size=lHashSize)
if oCache:
oCache.set(sComparativeImageCacheKey, aHashOriginal)
# calculate deviation
dDeviation = hamming_distance(aHashComparative, aHashOriginal)
# make decision
bDecision = False
if(dDeviation <= lThreshold):
# images are considered to be the same
bDecision = True
# push decision to array of decisions
aDecisions.append(bDecision)
# return decision and dictionary of metadata
return aDecisions, dicMetadata
#------------------------------------------------------------------------------#
"""Wrapper for dHash"""
def test_dHash(aOriginalImages, aComparativeImages, lThreshold=0.2, lHashSize=16, oCache=None):
# create dictionary of metadata
dicMetadata = {"algorithm": "dHash",
"hash_size": lHashSize, "threshold": lThreshold}
# compare every image
aDecisions = []
for i, sOriginalImagePath in enumerate(aOriginalImages):
sComparativeImagePath = aComparativeImages[i]
if oCache:
# create unique cache key for original and comparative key
aCacheKeyBase = ["dHash", lHashSize]
sOriginalImageCacheKey = oCache.calc_unique_key(
*aCacheKeyBase, sOriginalImagePath)
sComparativeImageCacheKey = oCache.calc_unique_key(
*aCacheKeyBase, sComparativeImagePath)
# check for existence of keys in cache
aHashOriginal = oCache.get(sOriginalImageCacheKey)
aHashComparative = oCache.get(sComparativeImageCacheKey)
# get images from path and calculate hash if not in hash already
# add to cache if calculated the first time and cache is active
if aHashOriginal is None:
aOriginalImage = load_image(sOriginalImagePath)
aHashOriginal = aHash(aOriginalImage, hash_size=lHashSize)
if oCache:
oCache.set(sOriginalImageCacheKey, aHashOriginal)
if aHashComparative is None:
aComparativeImage = load_image(sComparativeImagePath)
aHashComparative = aHash(aComparativeImage, hash_size=lHashSize)
if oCache:
oCache.set(sComparativeImageCacheKey, aHashOriginal)
# calculate deviation
dDeviation = hamming_distance(aHashComparative, aHashOriginal)
# make decision
bDecision = False
if(dDeviation <= lThreshold):
# images are considered to be the same
bDecision = True
# push decision to array of decisions
aDecisions.append(bDecision)
# return decision and dictionary of metadata
return aDecisions, dicMetadata
#------------------------------------------------------------------------------#
"""Wrapper for pHash"""
def test_pHash(aOriginalImages, aComparativeImages, lThreshold=0.2, dSize=8, dFactor=4, oCache=None):
# create dictionary of metadata
dicMetadata = {"algorithm": "dHash",
"hash_size": dSize*dFactor, "threshold": lThreshold}
# compare every image
aDecisions = []
for i, sOriginalImagePath in enumerate(aOriginalImages):
sComparativeImagePath = aComparativeImages[i]
if oCache:
# create unique cache key for original and comparative key
aCacheKeyBase = ["pHash", dSize, dFactor]
sOriginalImageCacheKey = oCache.calc_unique_key(
*aCacheKeyBase, sOriginalImagePath)
sComparativeImageCacheKey = oCache.calc_unique_key(
*aCacheKeyBase, sComparativeImagePath)
# check for existence of keys in cache
aHashOriginal = oCache.get(sOriginalImageCacheKey)
aHashComparative = oCache.get(sComparativeImageCacheKey)
# get images from path and calculate hash if not in hash already
# add to cache if calculated the first time and cache is active
if aHashOriginal is None:
aOriginalImage = load_image(sOriginalImagePath)
aHashOriginal = pHash(aOriginalImage, dSize=dSize, dFactor=dFactor)
if oCache:
oCache.set(sOriginalImageCacheKey, aHashOriginal)
if aHashComparative is None:
aComparativeImage = load_image(sComparativeImagePath)
aHashComparative = pHash(
aComparativeImage, dSize=dSize, dFactor=dFactor)
if oCache:
oCache.set(sComparativeImageCacheKey, aHashOriginal)
# calculate deviation
dDeviation = hamming_distance(aHashComparative, aHashOriginal)
# make decision
bDecision = False
if(dDeviation <= lThreshold):
# images are considered to be the same
bDecision = True
# push decision to array of decisions
aDecisions.append(bDecision)
# return decision and dictionary of metadata
return aDecisions, dicMetadata