def search_index(index, img_hash, max_distance): hashes_with_dist = map(lambda h: (phash.hamming_distance(img_hash, int(h, 16)), h), index.keys()) in_threshold = filter(lambda t: t[0] <= max_distance, hashes_with_dist) ordered = sorted(in_threshold, key=lambda t: t[0]) return [index[key] for [_, key] in ordered]
def detectCard(self): """Detect the card from the active frame """ # The phash python bindings operate on files, so we have to write our # current frame to a file to continue cv2.imwrite('frame.jpg', self.frame) # Use phash on our frame ihash = phash.dct_imagehash('frame.jpg') idigest = phash.image_digest('frame.jpg') candidates = {} hashes = self.referencedb.get_hashes() for MultiverseID in hashes: if (MultiverseID in self.blacklist): continue hamd = phash.hamming_distance(ihash, int(hashes[MultiverseID])) if (hamd <= self.threshold): candidates[MultiverseID] = hamd if (not len(candidates)): print('No matches found') return None finalists = [] minV = min(candidates.values()) for MultiverseID in candidates: if (candidates[MultiverseID] == minV): finalists.append(MultiverseID) bestMatch = None correlations = {} for MultiverseID in finalists: hamd = candidates[MultiverseID] digest = phash.image_digest(self.referencedb.IMAGE_FILE % MultiverseID) corr = phash.cross_correlation(idigest, digest) if (bestMatch is None or corr > correlations[bestMatch]): bestMatch = MultiverseID correlations[MultiverseID] = corr return bestMatch
def detectCard(self): """Detect the card from the active frame """ # The phash python bindings operate on files, so we have to write our # current frame to a file to continue cv2.imwrite("frame.jpg", self.frame) # Use phash on our frame ihash = phash.dct_imagehash("frame.jpg") idigest = phash.image_digest("frame.jpg") candidates = {} hashes = self.referencedb.get_hashes() for MultiverseID in hashes: if MultiverseID in self.blacklist: continue hamd = phash.hamming_distance(ihash, int(hashes[MultiverseID])) if hamd <= self.threshold: candidates[MultiverseID] = hamd if not len(candidates): print("No matches found") return None finalists = [] minV = min(candidates.values()) for MultiverseID in candidates: if candidates[MultiverseID] == minV: finalists.append(MultiverseID) bestMatch = None correlations = {} for MultiverseID in finalists: hamd = candidates[MultiverseID] digest = phash.image_digest(self.referencedb.IMAGE_FILE % MultiverseID) corr = phash.cross_correlation(idigest, digest) if bestMatch is None or corr > correlations[bestMatch]: bestMatch = MultiverseID correlations[MultiverseID] = corr return bestMatch
def detectCard(self): """Detect the card from the active frame """ # The phash python bindings operate on files, so we have to write our # current frame to a file to continue cv2.imwrite('frame.jpg', self.frame) # Use phash on our frame ihash = phash.dct_imagehash('frame.jpg') idigest = phash.image_digest('frame.jpg') #print('1a') candidates = {} hashes = self.referencedb.get_hashes() #print('1a1') #print(hashes) for MultiverseID in hashes: #print('id %i' % MultiverseID) if (MultiverseID in self.blacklist): continue hamd = phash.hamming_distance(ihash, int(hashes[MultiverseID])) #print('1a11') #print('ham: %i tresh: %i id: %i' % (hamd, self.threshold, MultiverseID)) #print(hamd <= self.threshold) if (hamd <= self.threshold): #print('X') candidates[MultiverseID] = hamd #print('1a2') if (not len(candidates)): print('No matches found') return None #print('1a3') finalists = [] minV = min(candidates.values()) #print('1a4') for MultiverseID in candidates: if (candidates[MultiverseID] == minV): finalists.append(MultiverseID) #print('1b') bestMatch = None correlations = {} for MultiverseID in finalists: hamd = candidates[MultiverseID] #print(self.ROOT_PATH % self.referencedb.IMAGE_FILE % MultiverseID) s = self.referencedb.IMAGE_FILE % MultiverseID s = '%s/%s' % (self.ROOT_PATH, s) #print(s) #digest = phash.image_digest(self.referencedb.IMAGE_FILE % MultiverseID) digest = phash.image_digest(s) corr = phash.cross_correlation(idigest, digest) if (bestMatch is None or corr > correlations[bestMatch]): bestMatch = MultiverseID correlations[MultiverseID] = corr name, code, rarity = self.referencedb.get_card_info(MultiverseID) print('Candidate: ' + name + ' [' + code + '] ' + str(corr)) #print('1d') bestMatches = [] ACCURACY = 1000 print('Finalists:') print(finalists) for MultiverseID in finalists: if correlations[MultiverseID] + ACCURACY > correlations[bestMatch]: bestMatches.append(MultiverseID) return bestMatches #return more finallist return bestMatch
def distance(self, other: 'DctImageHash'): return phash.hamming_distance(self._hash, other._hash) / 64