本コードではPySimpleGUIのウィンドウを2回表示させているのですが,一度目(show_candidate)はできても,二度目(show_result)のウィンドウにおいて,PySimpleGUI.PySimpleGUI.Imageのオブジェクトを表示させることができません(show_resultのresult_layoutのbox_imgをコメントアウトすればエラーは吐かれません).どちらも関数の先頭で画像リストをprintしているのですが,形状的にも問題なさそうに感じているので,困っております...
Python
1import numpy as np 2import PySimpleGUI as sg 3import cv2 4import sys 5import io 6from PIL import Image, ImageTk 7from tensorflow.python.keras.models import load_model 8 9 10def get_img_data(f, face_box, maxsize=(120, 120), first=False): 11 """Generate image data using PIL 12 """ 13 img = Image.open(f) 14 img = img.crop(face_box) 15 img.thumbnail(maxsize) 16 if first: # tkinter is inactive the first time 17 bio = io.BytesIO() 18 img.save(bio, format="PNG") 19 del img 20 return bio.getvalue() 21 return ImageTk.PhotoImage(img) 22 23 24def predict(path, face_box): 25 hoge_list = ['hoge', 'fuga', 'piyo', 'foo', 'bar', 'baz', 'toto'] 26 27 gray = Image.open(path).convert('L').crop(face_box) 28 img_resize = gray.resize((48, 48)) 29 img = np.array(img_resize).reshape(1, 48, 48, 1) 30 31 model = load_model('hoge.h5') 32 pre = model.predict(img).reshape(7) 33 arg = np.argsort(pre)[::-1] 34 pre_result = [] 35 for i in range(3): 36 pre_result.append('{}.{}: {}'.format(i + 1, hoge_list[arg[i]], pre[arg[i]])) 37 return pre_result 38 39 40def get_face_position(img_path): 41 face_cascade_path = './hoge/haarcascades/haarcascade_frontalface_default.xml' 42 face_cascade = cv2.CascadeClassifier(face_cascade_path) 43 img = cv2.imread(img_path) 44 img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 45 face_box = face_cascade.detectMultiScale(img_gray) 46 # face_box = face_cascade.detectMultiScale(img_gray, scaleFactor=1.05, minNeighbors=3, minSize=(120, 120)) 47 for i in range(face_box.shape[0]): 48 face_box[i][2] += face_box[i][0] 49 face_box[i][3] += face_box[i][1] 50 return face_box 51 52 53def show_result(pre_result, box_img): 54 # result_img = sg.Image(data=get_img_data(path, face_box[index], first=True)) 55 box_img = [box_img] 56 print(box_img) 57 result_layout = [[sg.Text('Here are the results.'), sg.Button('OK')], 58 box_img, 59 [sg.Text(pre_result[0])], 60 [sg.Text(pre_result[1])], 61 [sg.Text(pre_result[2])]] 62# ウィンドウの生成 63 result_window = sg.Window('Result', result_layout, return_keyboard_events=True, use_default_focus=False) 64 65# イベントループ 66 while True: 67 event, values = result_window.read() 68 if event == sg.WIN_CLOSED or event == 'OK': 69 break 70 71 result_window.close() 72 73 74def show_candidate(box_img_list): 75 print(box_img_list) 76 if len(box_img_list) > 1: 77 candidate_layout = [[sg.Text('Please enter a number between 1 and {}.'.format(len(box_img_list))), sg.InputText()], 78 [sg.Button('OK'), sg.Button('Cancel')], 79 box_img_list] 80 else: 81 candidate_layout = [[sg.Text('Is this the face?')], 82 [sg.Button('Yes'), sg.Button('No')], 83 box_img_list] 84 85 candidate_window = sg.Window('Choose the face', candidate_layout, return_keyboard_events=True, use_default_focus=False) 86 # loop reading the user input and displaying image, filename 87 while True: 88 event, values = candidate_window.read() 89 if event == sg.WIN_CLOSED or event == 'Cancel' or event == 'No': 90 break 91 elif event == 'Yes': 92 index = 1 93 # show_result(box_img_list[0]) 94 break 95 elif event == 'OK': 96 print('What you entered:{}'.format(values[0])) 97 index = int(values[0]) - 1 98 # show_result(box_img_list[index]) 99 break 100 101 candidate_window.close() 102 return index 103 104 105def main(): 106 for img_path in sys.argv: 107 if img_path == 'test.py': 108 continue 109 face_box = get_face_position(img_path) 110 box_img_list = [] 111 for i in range(face_box.shape[0]): 112 box_img_list.append(sg.Image(data=get_img_data(img_path, face_box[i], first=True))) 113 114 # print(box_img_list) 115 index = show_candidate(box_img_list) 116 pre = predict(img_path, face_box[index]) 117 show_result(pre, box_img_list[index]) 118 119 120if __name__ == '__main__': 121 main()
$ python test.py './img/test2.jpg' [<PySimpleGUI.PySimpleGUI.Image object at 0x7fd4382a4190>, <PySimpleGUI.PySimpleGUI.Image object at 0x7fd429862b20>, <PySimpleGUI.PySimpleGUI.Image object at 0x7fd43834c1c0>, <PySimpleGUI.PySimpleGUI.Image object at 0x7fd418797280>, <PySimpleGUI.PySimpleGUI.Image object at 0x7fd429862d30>, <PySimpleGUI.PySimpleGUI.Image object at 0x7fd43826fd60>] * Error performing wm_overrideredirect * What you entered:3 2021-01-09 17:25:51.179516: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations: AVX2 AVX512F FMA To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. 2021-01-09 17:25:51.192214: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fd3e2ebb0a0 initialized for platform Host (this does not guarantee that XLA will be used). Devices: 2021-01-09 17:25:51.192234: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version [<PySimpleGUI.PySimpleGUI.Image object at 0x7fd43834c1c0>] /Users/hoge/opt/anaconda3/envs/face/lib/python3.8/site-packages/PySimpleGUI/PySimpleGUI.py:7688: UserWarning: *** YOU ARE ATTEMPTING TO RESUSE AN ELEMENT IN YOUR LAYOUT! Once placed in a layout, an element cannot be used in another layout. *** warnings.warn('*** YOU ARE ATTEMPTING TO RESUSE AN ELEMENT IN YOUR LAYOUT! Once placed in a layout, an element cannot be used in another layout. ***', UserWarning)
よろしくお願いいたします.
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。