-
Notifications
You must be signed in to change notification settings - Fork 0
/
findmatch.py
87 lines (74 loc) · 3.3 KB
/
findmatch.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import time
import logging
import logging.config
from urlparse import urljoin
from pymongo import MongoClient
from util import *
logFormatter = logging.Formatter(fmt='%(asctime)s %(levelname)s %(message)s', datefmt="%Y-%m-%d %H:%M:%S")
rootLogger = logging.getLogger('__name__')
rootLogger.setLevel(logging.DEBUG)
fileHandler = logging.FileHandler("run.log")
fileHandler.setFormatter(logFormatter)
rootLogger.addHandler(fileHandler)
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logFormatter)
rootLogger.addHandler(consoleHandler)
## query face
mywf = webFace()
q_imgurl = "http://img4.duitang.com/uploads/item/201507/28/20150728133323_HfVnr.jpeg" # yangmi
q_imgurl = 'http://imgbdb3.bendibao.com/bjbdb/20157/30/2015730235720677.jpg' # wujing 吴京
if len(sys.argv) > 1 and sys.argv[1].startswith("http"):
q_imgurl = sys.argv[1]
q_bgrImg = url_to_bgrImg(q_imgurl)
q_facerep = mywf.getRepsFromImg(q_bgrImg, multiple=False)[0]
rootLogger.info("query image url: %s" % (q_imgurl))
rootLogger.debug("query face rep: %s" % ([round(x, 2) for x in q_facerep]))
topN = 5
topN_imgurl = []
topN_dist = []
## create connection and access specific database
client = MongoClient("172.17.0.1", 27017)
db = client['webface']['post2'] # dabatase--collection
#db = client['webface']['mmjpg'] # dabatase--collection
rootLogger.info("########### starting search ##############")
starttime = time.time()
# cursor = db.find({})
cursor = db.find({'imgurls.0':{'$exists':True}}) # has at least one image
for web in cursor:
web_imgurls = []
web_facereps = []
## iterate over all images and get all the face reps
if 'reps' not in web:
rootLogger.debug("no valid image: %s" % (web['url']))
continue
for img_idx, face_reps in enumerate(web['reps']):
for face in face_reps:
web_imgurls.append(u' '.join([urljoin(web['url'], web['imgurls'][img_idx]), web['url'], web['title']]))
web_facereps.append(face)
rootLogger.debug("comparing with %d faces: %s %s" % (len(web_facereps), web['url'], web['title']))
if len(web_facereps) == 0:
continue
web_facereps = np.array(web_facereps)
## calculate distance of query face to all faces in this webpage
web_faceDists = calDist(q_facerep, web_facereps)
rootLogger.debug("dists=%s" %(web_faceDists))
## update the list of (top N) most similar faces
tmp_dists = np.concatenate([topN_dist, web_faceDists])
tmp_idx = np.argsort(tmp_dists)[::+1][:topN] # index of ordered items
if any(i >= topN for i in tmp_idx): # if there is any face with smaller distance
topN_dist = [tmp_dists[i] for i in tmp_idx]
tmp_imgurls = topN_imgurl + web_imgurls
topN_imgurl = [tmp_imgurls[i] for i in tmp_idx]
rootLogger.debug("updated topN dists: %s" % (topN_dist))
rootLogger.debug("updated topN imgurl: %s" % (repr(topN_imgurl).decode("unicode-escape")))
rootLogger.debug("-----")
rootLogger.info("search finished in %.3f seconds" % (time.time() - starttime))
## print the final result
rootLogger.info("########### final top %d face ##############" % (topN))
rootLogger.info("query image: %s" % (q_imgurl))
for i,dist in enumerate(topN_dist):
rootLogger.info("%.3f %s" % (dist, topN_imgurl[i].encode('utf-8')))
sys.exit(0)