1 Star 0 Fork 1

王泽昊/ClassificationModule

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
gui.py 9.52 KB
一键复制 编辑 原始数据 按行查看 历史
王泽昊 提交于 2021-12-31 00:28 . Version 1.0(Beta)
# GUI.py
"""
version 1.0 (Beta)
"""
import os.path
import shutil
import cv2
import classify
import torch
import tkinter as tk
import tkinter.messagebox
from tkinter.filedialog import *
import os
from PIL import Image, ImageTk
import datetime
import random
from time import time
# 导入图像处理函数库
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
default = "images"
default_album = "album_cache"
default_name = "result"
text_help = '''
该部分为帮助的文本显示部分:
使用方法
功能介绍
更多敬请期待
建议可以输在下方对话框zhong
(反正没人去看)
'''
version = "V1.0"
alb = classify.album.Album(hash_len=7)
obj = classify.object.Obj(device, default, name=default_name)
fac = classify.face.Face(device, confidence=0.45, path=os.path.join("images", default_name, "human"))
def tid_maker():
return '{0:%Y%m%d%H%M%S%f}'.format(datetime.datetime.now()) + ''.join(
[str(random.randint(1, 10)) for i in range(5)]) + '.jpg'
def add_album():
#输入路径
print('add_album')
def open_album():
print('open_album')
def save_photo():
print('save_photo')
# def fileSave():
# filenewpath = asksaveasfilename(defaultextension='.png') # 设置保存文件,并返回文件名,指定文件名后缀为.png
# filenewname.set(filenewpath) # 设置变量filenewname的值
# img.save(str(filenewname.get()))# 设置保存图片
def outputFile():
outputFilePath = askdirectory() # 选择目录,返回目录名
filepath = outputFilePath
# print('选取的相册路径为:'+outputFilePath)
# outputpath.set(outputFilePath)
alb.add_img_file(filepath)
def isWho(photoPath):
if len(photoPath) > 1:
path = photoPath
pic = fac.face_encode(path)
if len(pic) != 1:
tkinter.messagebox.showinfo("结果", "请使用单人照片!")
return
pic_code = pic[0]
out = fac.find_name(pic_code)
if out is None:
tkinter.messagebox.showinfo("结果", "数据中查无此人,你可以选择将该人脸信息进行注册")
return
else:
tkinter.messagebox.showinfo("结果", "我想他/她应该是这个人\n" + str(out.name))
result_Img = tk.Toplevel()
result_Img.geometry('600x600+50+10')
result_Img.title('图片显示')
path_face = os.path.abspath(out.path)
Img = Image.open(path_face)
img_png = ImageTk.PhotoImage(Img)
label_Img = tk.Label(result_Img, image=img_png)
label_Img.pack()
if out.name in range(200):
result_Img.mainloop()
flag = input("请问需要更名吗: [Y]/N")
if flag not in ['n', 'N']:
new = input("请输入他/她的名字:")
fac.giv_name(int(out.name), new)
result_Img.mainloop()
def select():
default_dir = r""
file_path = tkinter.filedialog.askopenfilename(title=u'选择文件', initialdir=(os.path.expanduser(default_dir)))
isWho(file_path)
def inputPeoplephoto():
window_peoplephoto = tk.Toplevel()
window_peoplephoto.geometry('200x100+50+10')
window_peoplephoto.title('识别照片')
inputPath = tk.Entry(window_peoplephoto)
inputPath.grid(row=0, column=0)
search_path = tk.Button(window_peoplephoto, text='选择路径', width=7, height=1, command=select)
search_path.grid(row=0, column=1)
getPath = tk.Button(window_peoplephoto, text='确认', width=12, height=1,
command=lambda: isWho(photoPath=inputPath.get()))
#print(photoPath)
getPath.grid(row=1, column=0, columnspan=2)
window_peoplephoto.mainloop()
def select_adjust_face():
default_dir = r""
file_path = tkinter.filedialog.askopenfilename(title=u'选择文件', initialdir=(os.path.expanduser(default_dir)))
show_fac(file_path)
def adjust_face():
window_peoplephoto1 = tk.Toplevel()
window_peoplephoto1.geometry('200x100+50+10')
window_peoplephoto1.title('特征分析')
inputPath1 = tk.Entry(window_peoplephoto1)
inputPath1.grid(row=0, column=0)
search_path1 = tk.Button(window_peoplephoto1, text='选择路径', width=7, height=1, command=select_adjust_face)
search_path1.grid(row=0, column=1)
getPath = tk.Button(window_peoplephoto1, text='确认', width=12, height=1,
command=lambda: show_fac(photoPath=inputPath1.get()))
#print(photoPath)
getPath.grid(row=1, column=0, columnspan=2)
window_peoplephoto1.mainloop()
def show_fac(photoPath):
if len(photoPath) > 1:
path = photoPath
locate = fac.face_locate(photoPath)
if len(locate) != 1:
tkinter.messagebox.showinfo("结果", "请使用单人照片!")
return
top, right, bottom, left = locate[0]
img_t = cv2.imread(path)
img_t = img_t[top: bottom, left: right]
path = os.path.join(os.path.abspath("bin"), tid_maker())
print(path)
cv2.imwrite(path, img_t)
features = fac.cha_outcome_file(path)
outcome = []
if features[1]:
outcome.append("他是男性\n")
else:
outcome.append("她是女性\n")
if features[2]:
outcome.append("这是一位年轻人\n")
else:
outcome.append("这应该不是一位年轻人\n")
if features[0]:
outcome.append("戴眼镜")
else:
outcome.append("不戴眼镜")
tkinter.messagebox.showinfo("特征", "".join(outcome))
def find_result():
print('Open File!')
default_dir = os.path.abspath("images")
os.system('start ' + default_dir)
# file_path = tkinter.filedialog.askopenfilename(title=u'选择文件', initialdir=(os.path.expanduser(default_dir)))
# image = Image.open(file_path)
# plt.imshow(image)
# plt.show()
def start():
global obj
global fac
obj.add_img_file(default_album)
fac.settle()
print('Settle Down!!!')
tkinter.messagebox.showinfo('处理结果', '处理结束')
def find_same():
global alb
lis = alb.find_same()
same_photo_path = askdirectory() # 选择目录,返回目录名
filepath = same_photo_path
if len(lis) == 0:
tkinter.messagebox.showinfo("结果", "相册中无相似的图片")
else:
num = 0
for i in lis:
os.makedirs(os.path.join(filepath, str(num)))
for j in i:
shutil.copy(j.get_abs_path(), os.path.join(filepath, str(num)))
num += 1
tkinter.messagebox.showinfo("结果", "相册整理结束")
os.system('start ' + filepath)
def main():
main_window = tk.Tk()
# main_window.iconbitmap("MyPictures/1.ico")
main_window.title('易识别' + version)
main_window.geometry('250x240+50+10')
main_window.resizable(0, 0)
menubar = tk.Menu(main_window) # 创建主菜单
ite_mainmenu = tk.Menu(menubar, tearoff=False)
ite_mainmenu.add_command(label='添加相册', command=outputFile)
ite_mainmenu.add_command(label='打开', command=outputFile)
ite_mainmenu.add_command(label='保存图片', command=save_photo)
menubar.add_cascade(label='主菜单', menu=ite_mainmenu)
menubar.add_command(label='帮助', command=show_help)
menubar.add_command(label='关于', command=show_about)
main_window.config(menu=menubar)
#菜单栏制作
btn_add_path = tk.Button(main_window, text='选择相册路径', width=10, height=4, command=outputFile)
btn_add_path.grid(row=0,column=1)
btn_iden_face = tk.Button(main_window, text='人脸识别', width=10, height=4, command=inputPeoplephoto)
btn_iden_face.grid(row=0, column=2)
btn_find_result = tk.Button(main_window, text='处理结果查询', width=10, height=4, command=find_result)
btn_find_result.grid(row=1, column=1)
btn_begin = tk.Button(main_window, text='开始处理', width=10, height=4, command=start)
btn_begin.grid(row=1, column=2)
btn_begin = tk.Button(main_window, text='图片相似性分析', width=12, height=4, command=find_same)
btn_begin.grid(row=0, column=3)
btn_begin = tk.Button(main_window, text='人脸特征分析', width=12, height=4, command=adjust_face)
btn_begin.grid(row=1, column=3)
btn_leave = tk.Button(main_window, text='退出', width=30, height=3, command=main_window.destroy)
btn_leave.grid(row=2, column=1, columnspan=3)
main_window.mainloop()
def show_about():
tk.messagebox.showinfo('关于', '易查询\n版本:' + version + '\n制作者:儒歇\n感谢:zd、wys'
'对本产品的赞助')
def show_help():
window_help = tk.Toplevel()
window_help.title('帮助')
window_help.geometry('400x400+300+200')
window_help.resizable(True, True)
entry=tk.Entry(window_help, show='*')
entry.pack()
#添加文本
lb = tk.Label(window_help, text=text_help,
width=100,
height=50,
#justify='left'
)
lb.pack()
window_help.mainloop()
def sign_in_register():
window_sign = tk.Toplevel()
window_sign.title('欢迎')
window_sign.geometry('400x250+300+200')
window_sign.resizable(0, 0)
window_sign.mainloop()
def behaviour_face():
window_face = tk.Tk()
window_face.title('人像模式')
window_face.geometry('500x600+300+200')
window_face.resizable(True, True)
window_face.mainloop()
if __name__ == '__main__':
main()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/gaetanWANG/ClassificationModule.git
git@gitee.com:gaetanWANG/ClassificationModule.git
gaetanWANG
ClassificationModule
ClassificationModule
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385