当前位置: 首页 > 知识库问答 >
问题:

在Python中打开一个文件图像,存储为变量,将图像显示到Python程序中,使用面部识别对图像进行分析

伊飞光
2023-03-14

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

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

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

非常感谢对图形用户界面工具和面部识别的任何建议。以前使用的应用编程接口是微软Azure的cognitive_face应用编程接口。寻找一个和cognitive_face一样有效的免费开源应用编程接口。

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

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()

共有1个答案

程俊誉
2023-03-14

这将使您能够将文件传递到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。这个构造函数是最容易构建的。我对它进行了一些修改,以展示如何从任何其他函数调用open file函数,并将返回的值传递到您想要执行的任何处理中。

下面是看起来很简单的使用python面部识别:https://realpython.com/blog/python/face-recognition-with-python/

 类似资料:
  • 问题内容: 我想将生成的图像存储在变量中,以将其用作嵌入式图像。 这些功能都不适合我的用例: -将其写入标准输出。这确实有帮助..因为我必须将图像嵌入html文件中。 /从命令行执行时不执行任何操作 问题答案: 您尝试过还是等效的?

  • 我试着用IPython。显示以下代码: 我还尝试将matplotlib与以下代码一起使用: 在这两种情况下,都不会显示任何内容,甚至不会显示错误消息。

  • 问题内容: 我需要拍摄图像并将其放置在新的生成的白色背景上,以便将其转换为可下载的桌面墙纸。因此该过程将进行: 生成具有1440x900尺寸的新全白图像 将现有图像放在顶部居中 另存为单个图像 在PIL中,我看到了该对象,但是没有任何迹象表明它可以将现有的图像数据绘制到另一个图像上。任何人都可以推荐的建议或链接? 问题答案: 这可以通过Image实例的方法来完成: 可以在Nadia Alramli

  • 下面的程序适用于目录中的第一个. jpg。当第二次调用时,它会得到一个“_tkinter。图片"pyImage2"不存在"异常。为什么它会得到错误?有没有办法重用第一个图像而不是创建第二个? 导入系统,如果是系统,则导入操作系统。版本信息[0]==2: 导入Tkinter Tkinter=Tkinter其他:从PIL导入图像,ImageTk导入Tkinter 这是控制台输出。Traceback(最

  • 问题内容: 我在Visual Studio 2013中运行Python 2.7。以前在Spyder中运行该代码正常,但是在运行时: 我最终遇到以下错误: 为什么会这样,我该如何解决? 如建议的那样,我已经在我的Python 2.7中使用了Pillow安装程序。但是奇怪的是,我最终得到了这个: 都失败了! 问题答案: 我有一个同样的问题。 代替 解决了这个问题

  • 本文向大家介绍Python-使用keras进行图像分类,包括了Python-使用keras进行图像分类的使用技巧和注意事项,需要的朋友参考一下 图像分类是一种使用某种方法将图像分类为各自类别的方法- 从头开始训练小型网络 使用VGG16微调模型的顶层 示例