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 calculate_hashes(self): """Calculate the hashes for all the cards """ print('Calculating hashes...') try: cursor = self.connection.cursor() cursor.execute("""CREATE TABLE IF NOT EXISTS Hashes (MultiverseID INTEGER NOT NULL PRIMARY KEY, Hash TEXT NOT NULL)""") cursor.execute("DELETE FROM Hashes") self.connection.commit() cursor.execute("SELECT MultiverseID FROM Cards") cards = cursor.fetchall() if (len(cards)): pbar = ProgressBar( widgets=[ Percentage(), ' ', Bar(), ' ', ETA() ] ) for card in pbar(cards): MultiverseID = card[0] path = self.IMAGE_FILE % MultiverseID cursor.execute("""SELECT * FROM Hashes WHERE MultiverseID = ?""", (MultiverseID,)) if (cursor.fetchone() is None): print(path) ihash = phash.dct_imagehash(path) print(ihash) cursor.execute( """INSERT INTO Hashes (MultiverseID, Hash) VALUES(?, ?)""", (MultiverseID, str(ihash)) ) self.connection.commit() except sqlite3.Error, e: self.connection.rollback() print("Error %s:" % e.args[0]) sys.exit(1)
def calculate_hashes(self): """Calculate the hashes for all the cards """ print('Calculating hashes...') try: cursor = self.connection.cursor() cursor.execute("""CREATE TABLE IF NOT EXISTS Hashes (MultiverseID INTEGER NOT NULL PRIMARY KEY, Hash TEXT NOT NULL)""") cursor.execute("DELETE FROM Hashes") self.connection.commit() cursor.execute("SELECT MultiverseID FROM Cards") cards = cursor.fetchall() if (len(cards)): pbar = ProgressBar( widgets=[Percentage(), ' ', Bar(), ' ', ETA()]) for card in pbar(cards): MultiverseID = card[0] path = self.IMAGE_FILE % MultiverseID cursor.execute( """SELECT * FROM Hashes WHERE MultiverseID = ?""", (MultiverseID, )) if (cursor.fetchone() is None): ihash = phash.dct_imagehash(path) cursor.execute( """INSERT INTO Hashes (MultiverseID, Hash) VALUES(?, ?)""", (MultiverseID, str(ihash))) self.connection.commit() except sqlite3.Error, e: self.connection.rollback() print("Error %s:" % e.args[0]) sys.exit(1)
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 compute(self, filename): self._hash = phash.dct_imagehash(filename)
def dct_hash(filename): try: return phash.dct_imagehash(filename) except Exception as e: print('Error hashing image: {}'.format(e)) return None