代码拉取完成,页面将自动刷新
# -*- encoding: utf-8 -*-
# File Name: auto
# Author: 瓛
# @Time: 2021/5/10 22:32 5月
import tkinter as tk
import tkinter.messagebox
import face_recognition as face
import os
import time
import cv2
from PIL import Image
import smtplib
from email.message import EmailMessage
import rsa
import subprocess
from threading import Timer
class FaceRecognition:
def __init__(self):
self.run_time = 1
self.add_loop = False
self.number = 0
self.loop = True
self.window_loop = True
try:
with open('stats.txt', 'r') as stats:
if stats.read() == '0':
self.legal()
else:
self.run()
except FileNotFoundError:
stats = open('stats.txt', 'w')
stats.write('1')
def adding_face(self):
"""
scan user's face
store them
match them
"""
tk.messagebox.showinfo(title='Please Wait', message='Connecting Packages...\nWhen control the Face Capture,\n\
press p to capture a photo and q to quit\nYou can push your photo into file face_database instead')
capture = cv2.VideoCapture(0, cv2.CAP_DSHOW)
width = 1000
height = 618
w = 360
capture.set(cv2.CAP_PROP_FRAME_WIDTH, width)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
crop_w_start = (width - w) // 2
crop_h_start = (height - w) // 2
while True:
ret, frame = capture.read()
frame = frame[crop_h_start:crop_h_start + w,
crop_w_start:crop_w_start + w]
frame = cv2.flip(frame, 1, dst=None)
cv2.imshow("Face Capture", frame)
det_bp = cv2.waitKey(1) & 0xFF
if det_bp == ord('p'):
if det_bp == ord('p'):
way = os.getcwd()
cv2.imwrite("fdb_face.jpg", cv2.resize(frame, (224, 224)))
img = Image.open("fdb_face.jpg")
img.save(way + r'\face_database\fdb_face.jpg')
os.remove(way + r'\fdb_face.jpg')
capture.release()
cv2.destroyAllWindows()
return False
elif det_bp == ord('q'):
capture.release()
cv2.destroyAllWindows()
return False
if det_bp == ord('p'):
way = os.getcwd()
cv2.imwrite("fdb_face.jpg", cv2.resize(frame, (224, 224)))
img = Image.open("fdb_face.jpg")
img.save(way + r'\face_database\fdb_face.jpg')
os.remove(way + r'\fdb_face.jpg')
capture.release()
cv2.destroyAllWindows()
return False
elif det_bp == ord('q'):
capture.release()
cv2.destroyAllWindows()
return False
def run(self):
"""
during the running time
scan faces at regular time
yes timer reset
no run verify
"""
"""capture a picture containing faces"""
capture = cv2.VideoCapture(0, cv2.CAP_DSHOW)
ret, frame = capture.read()
way = os.getcwd()
frame = cv2.flip(frame, 1)
cv2.imwrite("current_face.jpg", frame)
img = Image.open("current_face.jpg")
img.save(way + r"\record\current_face.jpg")
os.remove(way + r"\current_face.jpg")
capture.release()
"""match the current face with those in the database"""
images = os.listdir('face_database')
image_to_be_matched = face.load_image_file(way + r"\record\current_face.jpg")
try:
image_to_be_matched_encoded = face.face_encodings(image_to_be_matched)[0]
for image in images:
current_image = face.load_image_file(way + r"\face_database\\" + image)
current_image_encoded = face.face_encodings(current_image)[0]
result = face.compare_faces([image_to_be_matched_encoded], current_image_encoded)
if result[0]:
# print("MATCHED!")
self.time_reset()
self.run_time = 1
else:
# print("NOT MATCHED!")
self.verify__init__()
self.run_time = 1
continue
except:
if self.run_time <= 5:
# print("CAPTURE IMAGE AGAIN!")
self.run_time += 1
self.run()
else:
self.run_time = 1
# self.time_reset()
subprocess.Popen("rundll32 powrprof.dll,SetSuspendState Hibernate")
def time_reset(self):
"""
timer counts down
when comes to zero
rerun
"""
regular_time = 5
count_down = regular_time
while count_down >= 0:
count_down -= 1
time.sleep(1)
if count_down < 0:
self.run()
self.time_reset()
def login__init__(self):
"""
create login in surface
including email and passcode input
passcode replaced with '*'
and login in and quit choices
"""
self.quit()
self.window = tk.Tk()
self.window.title('Welcome to FASS')
self.window.geometry('300x185')
tk.Label(self.window, font=('Times New Roman', 10), text='Email').place(x=115, y=20, width=70, height=25)
tk.Label(self.window, font=('Times New Roman', 10), text='Passcode\n(Any sentence you like can be\
your passcode)').place(x=20, y=70)
self.var_email = tk.StringVar()
enter_email = tk.Entry(self.window, textvariable=self.var_email)
enter_email.place(x=80, y=50, width=150, height=20)
self.var_passcode = tk.StringVar()
enter_passcode = tk.Entry(self.window, textvariable=self.var_passcode, show='*')
enter_passcode.place(x=80, y=110, width=150, height=20)
self.bt_login = tk.Button(self.window, font=('Times New Roman', 10), text='Login In', command=self.login)
self.bt_login.place(x=70, y=140, width=70, height=25)
self.bt_quit = tk.Button(self.window, font=('Times New Roman', 10), text='Quit', command=self.quit)
self.bt_quit.place(x=170, y=140, width=70, height=25)
# self.loop = True
# self.window_loop = True
while self.window_loop:
self.window.mainloop()
def rsaEncrypt(self, str):
"""
encrypt with rsa
not available now
"""
(pubkey, self.privkey) = rsa.newkeys(512)
content = str.encode("utf-8")
crypto = rsa.encrypt(content, pubkey)
return crypto, self.privkey
def rsaDecrypt(self, str, pk):
"""
decrypt with rsa
not available now
"""
content = rsa.decrypt(str, pk)
con = content.decode('utf-8')
return con
def login(self):
"""
let user to login in
when for the first
what user input will be remembered
and when login again
what input will be matched with the first time one
it show welcome and error as well
avoid user input null sentence
"""
email_loop = False
passcode_loop = False
email_exist = True
passcode_exist = True
getemail = self.var_email.get()
getpasscode = self.var_passcode.get()
if self.loop:
if getemail == '':
tk.messagebox.showerror(message='Email can not be empty!')
if getpasscode == '':
tk.messagebox.showerror(message='Passcode can not be empty!')
try:
with open('email.txt', 'r', encoding='utf-8') as email:
user_email = email.read()
if getemail == user_email:
email_loop = True
email.close()
except FileNotFoundError:
with open('email.txt', 'w', encoding='utf-8') as email:
email.write(getemail)
email_exist = False
email.close()
try:
with open('passcode.txt', 'r', encoding='utf-8') as passcode:
user_passcode = passcode.read()
if getpasscode == user_passcode:
passcode_loop = True
passcode.close()
except FileNotFoundError:
with open('passcode.txt', 'w', encoding='utf-8') as passcode:
passcode.write(getpasscode)
passcode_exist = False
passcode.close()
if email_exist and passcode_exist:
if email_loop and passcode_loop:
tk.messagebox.showinfo(title='Welcome', message='Success!')
self.add_loop = True
self.quit()
self.main_choices()
self.time_reset()
doc = open('stats.txt', 'w', encoding='utf-8')
doc.write('0')
else:
tk.messagebox.showerror(title='ERROR', message='Email or passcode is wrong!')
def legal(self):
"""
show legal license
"""
self.window = tk.Tk()
self.window.title('Legal license')
self.window.geometry('400x600')
license = open('License.txt', 'r', encoding='utf-8')
tk.Label(self.window, font=('Times New Roman', 10), text=license.read()).place(x=20, y=20)
self.bt_accept = tk.Button(self.window, font=('Times New Roman', 10), text='Accept', command=self.login__init__)
self.bt_accept.place(x=165, y=550, width=70, height=25)
self.window.mainloop()
def verify__init__(self):
"""
create verify surface
including enter passcode and enter and quit
"""
self.window = tk.Tk()
self.window.title('Verify')
self.window.geometry('300x185')
tk.Label(self.window, font=('Times New Roman', 10), text='Enter your passcode to verify').place(x=75, y=40)
self.passcode = tk.StringVar()
enter_passcode = tk.Entry(self.window, textvariable=self.passcode, show='*')
enter_passcode.place(x=80, y=70, width=150, height=20)
self.bt_login = tk.Button(self.window, font=('Times New Roman', 10), text='Enter', command=self.verify)
self.bt_login.place(x=110, y=100, width=70, height=25)
# self.bt_quit = tk.Button(self.window, font=('Times New Roman', 10), text='Quit', command=self.quit)
# self.bt_quit.place(x=170, y=100, width=70, height=25)
self.loop = True
self.window.mainloop()
def verify(self):
"""
when scaning a face FASS does not know
it will let user to verify his identity
for now including passcode-verify
typing-speed will come
"""
get_passcode = self.passcode.get()
while self.number < 2:
with open('passcode.txt', 'r', encoding='utf-8') as pc:
pcr = pc.read()
if get_passcode == pcr:
self.window.destroy()
self.time_reset()
pc.close()
self.number = 0
break
else:
self.number += 1
tk.messagebox.showerror(title='ERROR', message='You can try another ' + str(3 - self.number) + '!')
self.window.destroy()
self.verify__init__()
tk.messagebox.showerror(title='ERROR', message='VERIFY FAILS!')
try:
self.email_send()
self.sleep()
except:
self.sleep()
def sleep(self):
subprocess.Popen("rundll32 powrprof.dll,SetSuspendState Hibernate")
exit()
def email_send(self):
"""
if verify fails
send a email to
owner
"""
doc = open('email.txt', 'r')
self.login.email = doc.read()
email = EmailMessage()
email['from'] = 'FASS Guard'
email['to'] = f'{self.email}'
email['subject'] = 'Warning'
email.set_content("Your Fass verification failed, Please pay attention to your computer!")
with smtplib.SMTP(host='smtp.gmail.com', port=587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.login(f"{self.email}", f"{self.emailpasscode}")
smtp.send_message(email)
def quit(self):
"""
close current surfaces
"""
self.window.destroy()
self.window.quit()
def email__init__(self):
"""
create the email login UI surface
with Email and Email passcode
for user to input
which will be used to send email
"""
self.quit()
self.window = tk.Tk()
self.window.title('Email Account')
self.window.geometry('300x185')
tk.Label(self.window, font=('Times New Roman', 10), text='Email:').place(x=20, y=40)
tk.Label(self.window, font=('Times New Roman', 10), text='Email Passcode:').place(x=20, y=70)
self.email = tk.StringVar()
enter_email = tk.Entry(self.window, textvariable=self.email)
enter_email.place(x=120, y=40, width=150, height=20)
self.emailpasscode = tk.StringVar()
enter_email = tk.Entry(self.window, textvariable=self.emailpasscode, show='*')
enter_email.place(x=120, y=70, width=150, height=20)
self.bt_enter = tk.Button(self.window, font=('Times New Roman', 10), text='Enter', command=self.enter)
self.bt_enter.place(x=115, y=100, width=70, height=25)
self.bt_quit = tk.Button(self.window, font=('Times New Roman', 10), text='Quit', command=self.email_go)
self.bt_quit.place(x=200, y=100, width=70, height=25)
self.window.mainloop()
def email_go(self):
self.window.destroy()
self.main_choices()
def enter(self):
"""
close the current surface
and then run the program
"""
self.window.destroy()
self.main_choices()
def main_choices(self):
"""
show the main choices UI surface
for now including
add face
quit
readme
email
version information
and quit
"""
self.window = tk.Tk()
self.window.title('Main Choices')
self.window.geometry('300x185')
tk.Label(self.window, font=('Times New Roman', 10), text='Vision Enterprise 2.0.1 Beta').place(x=20, y=10)
self.bt_add_face = tk.Button(self.window, font=('Times New Roman', 10), text='Add Faces', command=self.adding_face)
self.bt_add_face.place(x=20, y=60, width=70, height=25)
self.bt_quit = tk.Button(self.window, font=('Times New Roman', 10), text='Quit', command=self.quit)
self.bt_quit.place(x=20, y=140, width=70, height=25)
self.bt_readme = tk.Button(self.window, font=('Times New Roman', 10), text='README', command=self.readme)
self.bt_readme.place(x=20, y=100, width=70, height=25)
self.bt_email = tk.Button(self.window, font=('Times New Roman', 10), text='Email', command=self.email__init__)
self.bt_email.place(x=120, y=60, width=70, height=25)
self.bt_vi = tk.Button(self.window, font=('Times New Roman', 10), text='Version Info', command=self.vi)
self.bt_vi.place(x=120, y=100, width=70, height=25)
self.window.mainloop()
def vi(self):
"""
show version information
"""
doc = open('version_info.txt', 'r', encoding='utf-8')
tk.messagebox.showinfo(title='VERSION INFORMATION', message=doc.read())
def readme(self):
"""
show README text
"""
doc = open('README.txt', 'r', encoding='utf-8')
tk.messagebox.showinfo(title='README', message=doc.read())
if __name__ == '__main__':
"""
run the program
"""
FR = FaceRecognition()
FR.__init__()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。