第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 tkinter 并排顯示多個圖像

使用 tkinter 并排顯示多個圖像

紅糖糍粑 2023-03-08 10:33:51
我寫了一個面部識別程序,現(xiàn)在我必須為它制作一個 GUI。我需要做的是顯示5張已識別人物的圖像。但我只能顯示其中一個。其他顯示為空。這是代碼:root = Tk()root.title("Test state")def train_button():    os.system('python3 extract_embeddings.py --dataset dataset --embeddings output/embeddings.pickle --detector face_detection_model --embedding-model openface_nn4.small2.v1.t7')    os.system('python3 train_model.py --embeddings output/embeddings.pickle --recognizer output/recognizer.pickle --le output/le.pickle')    messagebox.showinfo("INFO","Training completed")def select_photo():    global my_image    root.filename = filedialog.askopenfilename(initialdir="test",title ="Select a photo",filetypes=(("all files","*.*"),("png files","*.png"),("jpeg files","*.jpeg")))    output = subprocess.check_output("python3 recognize.py --detector face_detection_model --embedding-model openface_nn4.small2.v1.t7 --recognizer output/recognizer.pickle --le output/le.pickle --image "+root.filename, shell=True)    output = output.decode('utf-8')    pic_name = output.split('\n')[0]         my_image = ImageTk.PhotoImage(Image.open("images/"+pic_name+"/"+pic_name+"1.jpeg"))    my_image_label = Label(image = my_image).grid(row = 1 ,column = 0)    name_label = Label(text = pic_name).grid(row=2,column=0)    my_image1 = ImageTk.PhotoImage(Image.open("images/"+pic_name+"/"+pic_name+"2.jpeg"))    my_image_label1 = Label(image = my_image1).grid(row =1 ,column=1)    my_image2 = ImageTk.PhotoImage(Image.open("images/"+pic_name+"/"+pic_name+"3.jpeg"))    my_image_label2 = Label(image = my_image2).grid(row = 1,column = 2)button1 = Button(root,text = "Train" , command = train_button).grid(row=0 ,column=0)button2 = Button(root,text = "Recognise from image", command = select_photo).grid(row = 0 ,column=1)root.mainloop()這就是程序顯示圖像的方式 結(jié)果程序
查看完整描述

1 回答

?
素胚勾勒不出你

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超9個贊

PhotoImage這是在將圖像分配給函數(shù)中的局部變量時刪除圖像的錯誤。


你必須將它分配給全局變量(如果你有很多圖像,它可以是列表)或分配給你用來顯示它的小部件 -label.photo = image


我可以測試它,但這是用于label.photo = image解決此問題的版本


它還使用for-loop 來創(chuàng)建標(biāo)簽并將它們保存在列表中。


但是當(dāng)你使用label.photo = imagethen list 是沒有必要的。List 僅用于訪問標(biāo)簽以在創(chuàng)建新標(biāo)簽之前刪除舊標(biāo)簽。


import tkinter as tk # PEP8: `import *` is not preferred

from tkinter import filedialog


# --- functions ---


def train_button():

    os.system('python3 extract_embeddings.py --dataset dataset --embeddings output/embeddings.pickle --detector face_detection_model --embedding-model openface_nn4.small2.v1.t7')

    os.system('python3 train_model.py --embeddings output/embeddings.pickle --recognizer output/recognizer.pickle --le output/le.pickle')

    messagebox.showinfo("INFO","Training completed")


def select_photo():

    #global all_labels

    

    root.filename = filedialog.askopenfilename(initialdir="test", title ="Select a photo", filetypes=(("all files","*.*"),("png files","*.png"),("jpeg files","*.jpeg")))

    output = subprocess.check_output("python3 recognize.py --detector face_detection_model --embedding-model openface_nn4.small2.v1.t7 --recognizer output/recognizer.pickle --le output/le.pickle --image "+root.filename, shell=True)

    output = output.decode('utf-8')

    pic_name = output.split('\n')[0]


    # remove previous labels

    for label in all_labels:

        label.destroy()

    

    for number in range(3):

        #filename = f"images/{pic_name}/{pic_name}{number+1}.jpeg" # f-string (Python 3.6+)

        filename = "images/{}/{}{}.jpeg".format(pic_name, pic_name, number+1) # (Python 2.7, 3.0+)

        

        image = ImageTk.PhotoImage(Image.open(filename))

        

        label = tk.Label(image=image)

        label.photo = image   # assign to class variable to resolve problem with bug in `PhotoImage`

        

        label.grid(row=1, column=number)

        

        all_labels.append(label)


# --- main ---


all_labels = []


root = tk.Tk()


button1 = tk.Button(root, text="Train", command=train_button)

button1.grid(row=0, column=0)


button2 = tk.Button(root, text="Recognise from image", command=select_photo)

button2.grid(row=0, column=1)


root.mainloop()


查看完整回答
反對 回復(fù) 2023-03-08
  • 1 回答
  • 0 關(guān)注
  • 237 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號