import sqlite3 import os from PIL import ImageTk from tkinter import * root=Tk() def sqlite_exec_sql(conn, sql): c = conn.cursor() c.execute(sql) conn.commit() print("Total number of rows changes :", conn.total_changes) def sqlite_query(conn, sql): c = conn.cursor() cursor = c.execute(sql) return cursor def creat_table(): conn = sqlite3.connect('face.db') SQL_CREATE_TABLE = \ ''' CREATE TABLE FACE( ID INTEGER PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, IMAGE_PATH CHAR(255) NOT NULL );''' # 新建表 sqlite_exec_sql(conn, SQL_CREATE_TABLE) print("Table created successfully") conn.close() def insert_face_data(name, img_path): conn = sqlite3.connect('face.db') SQL_INSET_DATA = \ "INSERT INTO FACE (NAME,IMAGE_PATH) VALUES ('" + name +"','"+ img_path + "')" # 插入数据 print(SQL_INSET_DATA) sqlite_exec_sql(conn, SQL_INSET_DATA) conn.close() def delete_face_data(id): conn = sqlite3.connect('face.db') SQL_DELETE_DATA = "DELETE from FACE where ID='"+str(id)+"';" # 插入数据 sqlite_exec_sql(conn, SQL_DELETE_DATA) conn.close() def delete_face_name(): conn = sqlite3.connect('face.db') SQL_DELETE_DATA = "DELETE from FACE where NAME='" + str() + "';" # 插入数据 sqlite_exec_sql(conn, SQL_DELETE_DATA) conn.close() def query_face_data_all(): conn = sqlite3.connect('face.db') SQL_QUERY_DATA_ALL = "SELECT * from FACE;" res = sqlite_query(conn, SQL_QUERY_DATA_ALL) temp = [] for item in res: temp.append({'id':item[0] ,"data":{"name":item[1],'img_path':item[2]}}) conn.close() return temp def query_face_data_by_name(name): conn = sqlite3.connect('face.db') SQL_QUERY_DATA_ALL = "SELECT * from FACE where name = '"+ name +"';" res = sqlite_query(conn, SQL_QUERY_DATA_ALL) temp = [] for item in res: temp.append({'id': item[0], "data": {"name": item[1], 'img_path': item[2]}}) conn.close() return temp def insert_face_img(path): def traverse(path, pathsDic): tempFilePath = '' tempFileName = '' tempPerName = '' filePath = [os.path.join(path, f) for f in os.listdir(path)] for itemPath in filePath: if os.path.isdir(itemPath): traverse(itemPath, pathsDic) elif os.path.isfile(itemPath): if itemPath.split("\\")[-1].split(".")[0][-4:] == '0001': tempFileName = itemPath.split("\\")[-1] tempFilePath = itemPath.split("\\")[0] + "/" +itemPath.split("\\")[1]+"/" +itemPath.split("\\")[2] tempPerName = itemPath.split("\\")[1] else: print("false") pathsDic.append({"path":tempFilePath, "imgName":tempFileName,"name":tempPerName}) return tempFilePath, tempFileName, tempPerName result = [] traverse(path, result) del result[-1] return result # def face_search(images): # if images == None: # print('null!!') # return 'none' # def face_recognition(images): # with sqlite3.connect('face.db') as conn: # if # add butten class search: def Show(self): self.root.title("Python Gui") self.messagebox.label(root,text="search", fg='blue', anchor="c") self.messagebox.pack(side = LEFT) self.E = Entry(root, bd =10 ) self.E.pack(side = RIGHT) def show_button(self): self.btn = Button(self.top, text="seach", command=insert_face_img) self.btn.pack() root.mainloop() # add class add: def printcoord(self): self.file = self.filedialog.askopenfilename(parent=root,initialdir="./", title="choose an image.") filename = ImageTk.PhotoImage(Image.open(self.file)) self.canvas.imags = filename self.canvas.create_image(0,0, anchor="nw", image=filename) def show_button(self): self.btn = Button(self.root, text="choose", command=insert_face_data) self.btn.pack() # delete class delete: def show_button(self): self.btn = Button(self.root, text="choose", command=delete_face_data) self.btn.pack() if __name__ == "__main__": unknown = "face_img/face.jpg" #Aaron_Eckhart_0001 # 照片所在文件夹 path = "./" # 遍历照片插入数据库 result = insert_face_img(path)
python zuoye
最新推荐文章于 2024-07-23 13:22:23 发布