-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathqueryDatabase.py
53 lines (46 loc) · 1.23 KB
/
queryDatabase.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
46
47
48
49
50
51
52
53
# coding=utf-8
import os
import sys
import imagehash
import numpy as np
import psycopg2
import math
def getValuesFromDb(con,cur):
global hashes, names
command = "select ahash,phash,psimplehash,phash,vertdhash,whash from binaryhashes"
cur.execute(command)
hashes=cur.fetchall()
con.commit()
command = "select name,set from binaryhashes"
cur.execute(command)
names =cur.fetchall()
con.commit()
def checkHashes(testHash,index,minDist):
global hashes, names
n=0
i=0
matches = [("","")]*200
for phash in hashes:
dbHash = str(phash[index])
hammingDist = hammingDistance(dbHash,testHash)
if (math.fabs(hammingDist)<=minDist):
matches[i] = (math.fabs(hammingDist),names[n])
i+=1
n+=1
sortedMatches = sorted(matches,key= lambda hamming: hamming[0])
for x in range(0,i):
print sortedMatches[x]
def hammingDistance(a,b):
count=0
a=str(a)
b=str(b)
if(len(a)!=len(b)):
print "Error: String lengths don't match"
sys.exit()
for n in range (0,len(a)):
if(a[n]!=b[n]):
count+=1
return count
con = psycopg2.connect(database='cardimages', user='Devon')
cur = con.cursor()
getValuesFromDb(con,cur)