In [336]:
from PIL import Image
from numpy import *
import numpy as np
from pylab import *
import os
from scipy.cluster.vq import *
from sqlite3 import dbapi2 as sqlite
import matplotlib.pyplot as plt
In [337]:
import pickle
import sift
import imagesearch
imagesearch = reload(imagesearch)
In [338]:
with open('caltech_imlist.pkl', 'rb') as f:
imlist = pickle.load(f)
featlist = pickle.load(f)
In [339]:
nbr_images = len(imlist)
In [340]:
with open('vocabulary.pkl', 'rb') as f:
voc = pickle.load(f)
In [341]:
db = 'test.db'
con = sqlite.connect(db)
In [342]:
tmp = con.execute("select wordid from imwords").fetchall()
word_list = [w[0] for w in set(tmp)]
target = word_list[40]
In [343]:
id = 73
locs, descr = sift.read_features_from_file(featlist[id])
iw = voc.project(descr)
In [344]:
target = 369
words, distance = vq(descr, voc.voc)
index = np.where(words==target)
print descr[index[0][0]]
print locs[index[0][0]]
figure()
plt.bar(range(128), descr[index[0][0]])
show()
In [345]:
id_list = con.execute("select rowid from imlist").fetchall()
id_list = [i[0] for i in id_list]
In [346]:
target_word = 45
cnt=1
figure(figsize=(16, 16))
for id in id_list[:80]:
locs, descr = sift.read_features_from_file(featlist[id-1])
words, distance = vq(descr, voc.voc)
index = np.where(words==target_word)
if len(index[0])<=0:
continue
subplot(10, 4, cnt)
d = descr[index][0]
l = range(128)
plt.bar(l, d)
cnt+=1
show()
In [332]:
target_word = 45
cnt=0
figure(figsize=(8, 8))
gray()
for id in id_list[:80]:
locs, descr = sift.read_features_from_file(featlist[id-1])
words, distance = vq(descr, voc.voc)
index = np.where(words==target_word)
if len(index[0])<=0:
continue
print id, ':', locs[id]
scale = uint(locs[id][2]*4+0.5)
im = array(Image.open(imlist[id-1]).convert('L'))
y1 = locs[id][1]-scale # locs is x, y, scale, angle
x1 = locs[id][0]-scale
y2 = y1 + scale*2
x2 = x1 + scale*2
y1 = y1 if (0<=y1) else 0
x1 = x1 if (0<=x1) else 0
y2 = y2 if (y2<im.shape[0]) else im.shape[0] # shape is ysize, xsize
x2 = x2 if (x2<im.shape[1]) else im.shape[1]
x1 = int(x1)
x2 = int(x2)
y1 = int(y1)
y2 = int(y2)
imsmall = im[y1:y2, x1:x2]
if x2-x1<scale*2:
imsmall = hstack((imsmall, zeros((imsmall.shape[0], int(scale*2-(x2-x1))))))
if y2-y1<scale*2:
imsmall = vstack((imsmall, zeros((int(scale*2-(y2-y1)), imsmall.shape[1]))))
subplot(4, 10, cnt+1)
imshow(imsmall)
axis('off')
cnt = cnt+1
show()
In [ ]: