1

我正在尝试在 Python 中创建一个文件资源管理器,它允许我从 Windows 系统上的任何目录中提取图像文件。到目前为止,我一直无法在 Python 中完成这个看似基本的任务。即使是我学校的教授也无法提供帮助。

我正在尝试制作的程序将对图像文件执行面部识别,将图像文件存储为变量,然后执行该文件的分析并将分析打印到一些 tkinter 标签。

我现在咬得比我能嚼的还多。我是一名新程序员,我已经编程了大约 3 个月。我在上人工智能课程,但是,该课程不使用结构化语言。我们学校似乎没有专家级的 Python 程序员,所以我肯定需要一些帮助。

任何有关 GUI 工具和面部识别的建议将不胜感激。以前使用的 API 是来自 Microsoft Azure 的cognitive_face API。寻找与cognitive_face 一样有效的免费开源API。

下面是一些基本代码。寻找任何可用的建议。

import subprocess
from tkinter import *



#Define Functions Here
def Open_File():
    subprocess.Popen(r'explorer /select,"C:\path\of\folder\file"')

def main_window(main):
    window.title('Facial Recognition')
    main.update_idletasks()
    width = 1024
    height = 768
    #find the center point of the width on the screen
    x = (main.winfo_screenwidth()//2)-(width//2)
    #find the center point of the height on the screen
    y = (main.winfo_screenheight()//2)-(height//2)

    main.geometry('{}x{}+{}+{}'.format(width,height,x,y))



#Define your Window
window = Tk()
main_window(window)

#Create Frames
topFrame = Frame(window)
topFrame.pack(side=LEFT)
bottomFrame = Frame(window)
bottomFrame.pack(side=RIGHT)

#Add Cascade Dropdown Menus
menu = Menu(window) #Add a Menu to the main window
window.config(menu=menu)

subMenu=Menu(menu)#declare a subMenu for menu
menu.add_cascade(label="File",menu=subMenu)#assign a dropdown for menu
subMenu.add_command(label="Open Image..",command =Open_File)
#Add a separator for your subMenu
subMenu.add_separator()
subMenu.add_command(label="Exit",command=exit)


#Add Buttons
##button1 = Button(topFrame,text="Open File...",fg="Grey",command=Open_File)
##button1.pack()
##button2 = Button(topFrame,text="Open Image...",fg="Dark Grey")
##button2.pack()


#Official Window loop continuously displays to screen
window.mainloop()
4

1 回答 1

0

这应该让您可以将文件传递给 OpenCV

from tkinter import *
from tkinter import filedialog

class Application(Frame):

    def __init__(self, master):

        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):

        menubar = Menu(root)

        #SETUP FILE MENU
        filemenu = Menu(menubar, tearoff=0)
        filemenu.add_command(label="Exit", command=root.quit)
        menubar.add_cascade(label="File", menu=filemenu)

        selectmenu = Menu(menubar, tearoff=0)
        selectmenu.add_command(label="Select Images", command= self.Find_Face)
        menubar.add_cascade(label="Select Images", menu=selectmenu)

        root.config(menu=menubar)

    #Define Functions Here
    def Open_File(self):
        self.filename =  filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
        return self.filename

    #Call open file from a different function and get the returned value
    def Find_Face(self):
        self.imagename = self.Open_File()
        print("File is: ", self.imagename)
        ##PASS THE FILE TO OPENCV AND PROCESS


#Official Window loop continuously displays to screen
root = Tk()
root.title("Facial Recognition")
root.geometry("1024x768")
app = Application(root)
root.resizable(width=FALSE, height=FALSE)
root.mainloop()

我经常使用 tkinter。这个构造函数是最容易构建的。我对其进行了一些修改,以展示如何从任何其他函数调用打开文件函数,然后将返回的值传递给您想要执行的任何处理。

这是一个看起来非常简单易用的python面部识别: https ://realpython.com/blog/python/face-recognition-with-python/

于 2018-03-04T17:46:56.377 回答