-
Notifications
You must be signed in to change notification settings - Fork 23
/
searchdemo.py
63 lines (52 loc) · 1.62 KB
/
searchdemo.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
54
55
56
57
58
59
60
61
62
63
import cherrypy, os, random
import cPickle as pickle
import imagesearch
import imtools
"""After ch07_buildindex.py has built an index in test.db, this queries it over
a web service.
"""
class SearchDemo(object):
def __init__(self):
self.imlist = imtools.get_imlist(
'/Users/thakis/Downloads/ukbench/first1000')[:100]
self.ndx = range(len(self.imlist))
with open('vocabulary.pkl', 'rb') as f:
self.voc = pickle.load(f)
self.maxresults = 15
self.header = """\
<!doctype html>
<html>
<head><title>Image search example</title></head>
<body>"""
self.footer = """\
</body>
</html>"""
@cherrypy.expose
def index(self, query=None):
self.searcher = imagesearch.Searcher('test.db', self.voc)
html = self.header
html += """\
<br>
Click an image to search. <a href="?query=">Random selection</a> of images.
<br><br>"""
if query:
res = self.searcher.query(query)[:self.maxresults]
for dist, ndx in res:
imname = self.searcher.get_filename(ndx)
html += '<a href="?query=%s">' % imname
html += '<img src="/img/%s" width=100>' % os.path.basename(imname)
html += '</a>'
else:
random.shuffle(self.ndx)
for i in self.ndx[:self.maxresults]:
imname = self.imlist[i]
html += '<a href="?query=%s">' % imname
html += '<img src="/img/%s" width=100>' % os.path.basename(imname)
html += '</a>'
html += self.footer
return html
config = { '/img': {
'tools.staticdir.on': True,
'tools.staticdir.dir': '/Users/thakis/Downloads/ukbench/first1000',
}}
cherrypy.quickstart(SearchDemo(), '/', config=config)