def main(): parser = argparse.ArgumentParser() parser.add_argument('files', metavar="IMAGE_FILE", nargs="+") parser.add_argument('--threshold', type=float, default=0.90) args = parser.parse_args() if len(args.files) % 2 != 0: parser.error("Files must be provided in pairs") failures = 0 # TODO: don't punt on media type detection: for img_1, img_2 in grouper(2, args.files): digest_1 = image_digest(img_1) digest_2 = image_digest(img_2) pcc = cross_correlation(digest_1, digest_2) if pcc >= args.threshold: status = 'pass' log_f = sys.stdout else: status = 'FAIL' log_f = sys.stderr failures += 1 print('%s\t%s\t%s\t%0.3f' % (status, img_1, img_2, pcc), file=log_f) return 1 if failures else 0
def piece_recognizer(filename): """ take filename of piece, compare with ref table, find one of maximum correlation, return ref name """ # piece digest (pd) pd = image_digest(filename) # compute cross correlations ccs = map(lambda ref_im: (ref_im[0], cross_correlation(pd, ref_im[1])), ref_images) return max(ccs, key=lambda item: item[1])
def compareDirectoryImagesByPhash(self, inputFileName, path2, threshold): failures=0 total=0 files2 = glob.glob(path2+"/*.jpg") if (len(files2) == 0): print("No matching jpg files under "+ path2) return 0, 0, [] iterations=1 records = len(files2) log_f = sys.stdout print('**************** %s%s%s%d%s' % ('Compare ', inputFileName, " images against ", records, path2)) phashList = [] #iterations=10 complete = 1 #complete = iterations / 10 success_pct = 0 #for i, img_1 in enumerate(files): if (total != 0): success_pct = (total-failures)*100/total digest_1 = image_digest(inputFileName) # if (i % complete == 0): # print('%s\t%d\t%s\t%d' % ('current iteration ', i, "success_pct", success_pct), file=sys.stdout) for img_2 in files2: if (inputFileName == img_2): continue #print("Comparing with " + img_2) total = total + 1 #try: digest_2 = image_digest(img_2) pcc = cross_correlation(digest_1, digest_2) #except: # continue if (math.isnan(pcc) == False): phashList.append((pcc, img_2)) if pcc <= threshold: status = 'pass' #log_f = sys.stdout else: status = 'fail' failures += 1 #log_f = sys.stderr # failures += 1 #print('%s\t%s\t%s\t%0.3f' % (status, inputFileName, img_2, pcc)) else: print(" Got nan for "+img_2) failures += 1 success_pct = (total-failures)*100/total print('%s\t%s\t%d\t%s\t%d\t%s\t%d' % (inputFileName, 'total', total, "failures", failures, ' success_pct ', success_pct)) phashList.sort(reverse = True, key = lambda pcc:pcc[0]) #print(phashList) return failures, total, phashList
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 compare_two_hash(h1,h2): return phash.cross_correlation(h1,h2)