-
Notifications
You must be signed in to change notification settings - Fork 1
/
facelink.py
140 lines (110 loc) · 4.65 KB
/
facelink.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QLineEdit, QPushButton, QTextEdit
from PyQt5.QtGui import QIcon, QTextCursor
from PyQt5.QtCore import Qt
import time
import requests
import urllib.request
TESTING_MODE = True
APITOKEN = '<YOUR API TOKEN' # Your API Token
class FaceRecognitionApp(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("FaceLink")
self.setGeometry(100, 100, 400, 400)
self.setWindowIcon(QIcon("icon.png"))
self.label_url = QLabel("URL:")
self.textbox_url = QLineEdit()
self.label_output = QLabel("Output File Name:")
self.textbox_output = QLineEdit()
self.button_recognize = QPushButton("Recognize Face")
self.button_recognize.clicked.connect(self.recognize_face)
self.output_textbox = QTextEdit()
self.output_textbox.setReadOnly(True)
self.output_textbox.setLineWrapMode(QTextEdit.NoWrap)
layout = QVBoxLayout()
layout.addWidget(self.label_url)
layout.addWidget(self.textbox_url)
layout.addWidget(self.label_output)
layout.addWidget(self.textbox_output)
layout.addWidget(self.button_recognize)
layout.addWidget(self.output_textbox)
self.setLayout(layout)
self.setStyleSheet("""
QWidget {
background-color: #333333;
color: #ffffff;
}
QLabel {
color: #ffffff;
}
QLineEdit {
background-color: #555555;
color: #ffffff;
border: 1px solid #ffffff;
}
QPushButton {
background-color: #555555;
color: #ffffff;
border: none;
padding: 5px;
}
QPushButton:hover {
background-color: #777777;
}
QTextEdit {
background-color: #555555;
color: #ffffff;
border: 1px solid #ffffff;
padding: 5px;
}
""")
def recognize_face(self):
url = self.textbox_url.text()
output_file_name = self.textbox_output.text()
# Download the photo from the provided URL
urllib.request.urlretrieve(url, output_file_name)
def search_by_face(image_file):
if TESTING_MODE:
self.append_text('****** WELCOME, SEAERCHING IN PROGRESSS - MADBOT ******')
site = 'https://facecheck.id'
headers = {'accept': 'application/json', 'Authorization': APITOKEN}
files = {'images': open(image_file, 'rb'), 'id_search': None}
response = requests.post(site+'/api/upload_pic', headers=headers, files=files).json()
if response['error']:
return f"{response['error']} ({response['code']})", None
id_search = response['id_search']
self.append_text(response['message'] + ' id_search='+id_search)
json_data = {'id_search': id_search, 'with_progress': True, 'status_only': False, 'demo': TESTING_MODE}
while True:
response = requests.post(site+'/api/search', headers=headers, json=json_data).json()
if response['error']:
return f"{response['error']} ({response['code']})", None
if response['output']:
return None, response['output']['items']
self.append_text(f'{response["message"]} progress: {response["progress"]}%')
time.sleep(1)
# Search the Internet by face
error, urls_images = search_by_face(output_file_name)
if urls_images:
for im in urls_images: # Iterate search results
score = im['score'] # 0 to 100 score indicating how well the face matches the found image
url = im['url'] # URL to the webpage where the person was found
image_base64 = im['base64'] # Thumbnail image encoded as a base64 string
self.append_text(f"{score} <a href='{url}'>{url}</a> {image_base64[:32]}...")
else:
self.append_text(error)
def append_text(self, text):
cursor = self.output_textbox.textCursor()
cursor.movePosition(QTextCursor.End)
cursor.insertHtml(f"{text}<br>")
self.output_textbox.setTextCursor(cursor)
self.output_textbox.ensureCursorVisible()
if __name__ == "__main__":
TESTING_MODE = True
APITOKEN = '<YOUR APTI TOKRN>'
app = QApplication(sys.argv)
app.setStyle("Fusion")
window = FaceRecognitionApp()
window.show()
sys.exit(app.exec_())