-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWikiLookAlike.py
107 lines (87 loc) · 2.86 KB
/
WikiLookAlike.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from PIL import Image
import pandas as pd
from datetime import datetime
import wikipedia
import webbrowser
import argparse
from urllib.parse import quote
from face_search import find_best_matches
"""
This is the main file to compare a users image to the dataset encodings
"""
WIKI_BASE = 'https://en.wikipedia.org/wiki/'
def build_parser():
"""
Builds parser for command line arguments
"""
parser = argparse.ArgumentParser(description='Wiki Look-Alike: Find your look-alike on Wikipedia!')
parser.add_argument(
'user_filepath',
type=str,
help='filepath to user\'s image'
)
parser.add_argument(
'-s',
default=None,
type=str,
choices=['Male', 'Female'],
help='filter results to only show faces of the specified sex'
)
parser.add_argument(
'-d',
default=False,
type=bool,
help='go to first Wikipedia result even if link is ambigious'
)
parser.add_argument(
'-n',
default=1,
type=int,
help='number of matches to display to user'
)
parser.add_argument(
'-f',
default=True,
type=bool,
help='display user\'s face when match is found'
)
return parser
if __name__ == "__main__":
parser = build_parser()
args = parser.parse_args()
DATASET_PATH = r'./dataset.csv'
df = pd.read_csv(DATASET_PATH, sep='|')
start = datetime.now()
best = find_best_matches(df, args.user_filepath, up_to_n=args.n, gender=args.s, show_user_face=args.f)
delta = datetime.now() - start
print("\nTook ", delta.total_seconds(), "seconds to complete!\n")
# print found names
for idx, row in best.iterrows():
print(' ------------------------------------------------ ')
print()
print(f"Our search indicates that you look like {row['name']}.")
print()
if args.d:
url = wikipedia.page(title=row['name']).url
else:
try:
url = wikipedia.page(title=row['name'], auto_suggest=False).url
except wikipedia.exceptions.DisambiguationError:
print(f"However, the wikipedia page for that person is ambigious.")
print(f"Try navigating to the page for {row['name']} (born {row['dob']})")
print()
url = WIKI_BASE + quote(row['name'])
Image.open(row['path']).show()
webbrowser.open(url)
if (idx+1) != args.n:
user_continue = input("Type quit to exit the program, or enter to continue... ")
print()
if user_continue.lower().strip() == 'quit':
break
print(' ------------------------------------------------ ')
print()
print()
print()
print("That was the last result! Thanks for using WikiLookAlike!")
print()
print()