-
Notifications
You must be signed in to change notification settings - Fork 1
/
classify.py
84 lines (60 loc) · 2.01 KB
/
classify.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
from __future__ import print_function
import cv2
import glob
import urllib
import re
import numpy as np
def fname_to_name(fname, dirname):
fname = re.sub('^' + dirname + '\/', '', fname)
fname = re.sub('\.(jpg|png|jpeg)', '', fname)
fname = re.sub('-', ' ', fname)
fname = re.sub('_', '', fname)
return fname
def url_to_image(url):
# download the image, convert it to a NumPy array, and then read
# it into OpenCV format
resp = urllib.urlopen(url)
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
# return the image
return image
def euclidean_distance(img1, img2):
# Distance as sum of square of differences
dist = np.sum((img1.astype("float") - img2.astype("float")) ** 2)
# Normalize
dist /= (img1.shape[0] * img1.shape[1] * 1.0)
# Do not need to take the sqrt since we are comparing relatively
# and do not need to know absolute values.
return dist
def get_args():
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("img")
parser.add_argument("--url", action="store_true", default=False)
parser.add_argument("--comparisons_dir", default="comparisons")
parser.add_argument("-v", "--verbose", default=False, action="store_true")
return parser.parse_args()
def main():
args = get_args()
dirname = args.comparisons_dir
filenames = glob.glob(dirname + '/*')
img = args.img
if args.url:
img = url_to_image(img)
else:
img = cv2.imread(img)
best = None
best_val = float("Inf")
for filename in filenames:
img_ = cv2.imread(filename)
img_ = cv2.resize(img_, (img.shape[1], img.shape[0]))
dist = euclidean_distance(img, img_)
if args.verbose:
print(filename, dist)
if dist < best_val:
best_val = dist
best = filename
print("best:", fname_to_name(best, dirname), best_val)
return 0
if __name__ == "__main__":
main()