0

Pythonのfacerecognitionライブラリを使って顔認証をしようと考えています。顔認証はできており、tkinterで画像を確認するところでエラーが出ています。
それがCode1です。Code1について説明すると、まず、'data/raw'ディレクトリから参照画像を読み込みます。参照画像から抽出した特徴量と、カメラからリアルタイムに取得した特徴量の距離を図ることで、一番近い認識結果を出します。その後、tkinterによりユーザーに対し認識結果を確認します。この時、Pythonが終了し、エラーが発生します。

-code1-

import cv2
import numpy as np
import face_recognition
import os

import tkinter as tk
from tkinter import *
from tkinter import ttk

def find_encodings(images):
    encode_list = []
    for img in images:
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        encode = face_recognition.face_encodings(img)[0]
        encode_list.append(encode)
    return encode_list
            

if __name__ == "__main__":
    name = None 

    path = 'data/raw'
    images = []
    classNames = []
    myList = os.listdir(path)

    for cls in myList:
        if os.path.splitext(cls)[1] != ".png":
            continue

        current_img = cv2.imread(f'{path}/{cls}')
        images.append(current_img)
        classNames.append(os.path.splitext(cls)[0])

    encode_list_known = find_encodings(images)

    cap = cv2.VideoCapture(1)
    while True:
        success, img = cap.read()
        img_resize = cv2.resize(img, (0, 0), None, 0.25, 0.25)
        img_resize = cv2.cvtColor(img_resize, cv2.COLOR_BGR2RGB)

        face_frame = face_recognition.face_locations(img_resize)
        encode_frame = face_recognition.face_encodings(img_resize, face_frame)

        for encode_face, face_loc in zip(encode_frame, face_frame):
            matches = face_recognition.compare_faces(encode_list_known, encode_face)
            face_dis = face_recognition.face_distance(encode_list_known, encode_face)
            match_idx = np.argmin(face_dis)
        
            if matches[match_idx]:
                name = classNames[match_idx].upper()
                y1, x2, y2, x1 = face_loc
                y1, x2, y2, x1 = y1*4, x2*4, y2*4, x1*4
                cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
                cv2.rectangle(img, (x1, y2-35), (x2, y2), (0,255,0),cv2.FILLED)
                cv2.putText(img, name, (x1+6, y2-6), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2)

        if name is not None:

            root = tk.Tk()
            root.geometry("300x150+550+300") 
            root.title('confirmation') 

            frame1 = ttk.Frame(root, padding=16) 
            label1 = ttk.Label(frame1, text=f"Are you {name}?")

            frame1.pack()
            label1.pack(side=TOP) 

            root.mainloop()

        cv2.imshow('WebCam', img)
        cv2.waitKey(1)



エラーメッセージは以下の内容です。

2022-08-31 20:18:09.967 Python[64018:537176] -[NSApplication macOSVersion]: unrecognized selector sent to instance 0x7fc35e1b0270
2022-08-31 20:18:09.968 Python[64018:537176] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication macOSVersion]: unrecognized selector sent to instance 0x7fc35e1b0270'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff206ffbeb __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x00007fff20438d92 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff2078273d -[NSObject(NSObject) __retain_OA] + 0
    3   CoreFoundation                      0x00007fff20667c8b ___forwarding___ + 1448
    4   CoreFoundation                      0x00007fff20667658 _CF_forwarding_prep_0 + 120
    5   libtk8.6.dylib                      0x000000011cd900db GetRGBA + 50
    6   libtk8.6.dylib                      0x000000011cd8fbf1 SetCGColorComponents + 124
    7   libtk8.6.dylib                      0x000000011cd8fff8 TkpGetColor + 543
    8   libtk8.6.dylib                      0x000000011ccef885 Tk_GetColor + 143
    9   libtk8.6.dylib                      0x000000011cce26ce Tk_Get3DBorder + 118
    10  libtk8.6.dylib                      0x000000011cce2556 Tk_Alloc3DBorderFromObj + 127
    11  libtk8.6.dylib                      0x000000011ccf0b88 DoObjConfig + 982
    12  libtk8.6.dylib                      0x000000011ccf069a Tk_InitOptions + 333
    13  libtk8.6.dylib                      0x000000011ccf0592 Tk_InitOptions + 69
    14  libtk8.6.dylib                      0x000000011cd17e95 CreateFrame + 1550
    15  libtk8.6.dylib                      0x000000011cd18119 TkListCreateFrame + 137
    16  libtk8.6.dylib                      0x000000011cd109a6 Initialize + 2002
    17  _tkinter.cpython-39-darwin.so       0x000000011cb6e724 Tcl_AppInit + 84
    18  _tkinter.cpython-39-darwin.so       0x000000011cb6907a Tkapp_New + 602
    19  _tkinter.cpython-39-darwin.so       0x000000011cb689a1 _tkinter_create + 561
    20  Python                              0x0000000104ba81a4 cfunction_vectorcall_FASTCALL + 91
    21  Python                              0x0000000104c532a2 call_function + 168
    22  Python                              0x0000000104c4caf5 _PyEval_EvalFrameDefault + 28149
    23  Python                              0x0000000104c44bee _PyEval_EvalCode + 410
    24  Python                              0x0000000104b5cced _PyFunction_Vectorcall + 376
    25  Python                              0x0000000104b5c43e _PyObject_FastCallDictTstate + 87
    26  Python                              0x0000000104bcaf0e slot_tp_init + 192
    27  Python                              0x0000000104bd036f type_call + 272
    28  Python                              0x0000000104b5c240 _PyObject_MakeTpCall + 127
    29  Python                              0x0000000104c53311 call_function + 279
    30  Python                              0x0000000104c4caf5 _PyEval_EvalFrameDefault + 28149
    31  Python                              0x0000000104c44bee _PyEval_EvalCode + 410
    32  Python                              0x0000000104c9ffc8 run_eval_code_obj + 128
    33  Python                              0x0000000104c9ff16 run_mod + 96
    34  Python                              0x0000000104c9d733 pyrun_file + 167
    35  Python                              0x0000000104c9d124 pyrun_simple_file + 271
    36  Python                              0x0000000104c9cfef PyRun_SimpleFileExFlags + 67
    37  Python                              0x0000000104cbcc0e pymain_run_file + 326
    38  Python                              0x0000000104cbc456 Py_RunMain + 990
    39  Python                              0x0000000104cbd514 pymain_main + 35
    40  Python                              0x0000000104cbd7ea Py_BytesMain + 42
    41  libdyld.dylib                       0x00007fff205a8f3d start + 1
)
libc++abi: terminating with uncaught exception of type NSException

私はCode1の最後のパートをCode2, Code3のように変えました。そしたら、両方とも動きました。

-code2-

        a = True
        if a == True:
            root = tk.Tk()
            root.geometry("300x150+550+300") 
            root.title('confirmation') 

            frame1 = ttk.Frame(root, padding=16) 
            label1 = ttk.Label(frame1, text=f"Are you {name}?")

            frame1.pack()
            label1.pack(side=TOP) 

            root.mainloop()

        cv2.imshow('WebCam', img)
        cv2.waitKey(1)

-code3-

        if name is not None:

            root = tk.Tk()
            root.geometry("300x150+550+300") 
            root.title('confirmation') 

            frame1 = ttk.Frame(root, padding=16) 
            label1 = ttk.Label(frame1, text=f"Are you {name}?")

            frame1.pack()
            label1.pack(side=TOP) 

            root.mainloop()

        else:
            print("aaa")
            continue
            root = tk.Tk()
            root.geometry("300x150+550+300") 
            root.title('confirmation') 

            frame1 = ttk.Frame(root, padding=16) 
            label1 = ttk.Label(frame1, text=f"Are you {name}?")

            frame1.pack()
            label1.pack(side=TOP) 

            root.mainloop()

        cv2.imshow('WebCam', img)
        cv2.waitKey(1)

なぜこのようなことが起きたのでしょうか。エラーは何に起因するものでしょうか。

1
  • なんとなく機械翻訳っぽさを感じますが、英語版に転送した方がよかったのでは、と思ったりも。
    – sayuri
    2022年9月5日 12:11

0

この質問に回答するには、ログインする必要があります。

のタグが付いた他の質問を参照する。