xin chào mọi người , tui đang làm một project về nhận diện khuôn mặt và tui đang bị lỗi của numpy ( The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() ) tui đã thử một số cách nhưng vẫn ko đc hi vọng đc mn giúp đỡ . Đây là đoạn mã code của tui
from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk
import cv2
import numpy as np
import os
from os.path import isfile, join
from threading import Thread
from user_handler import UserData
import face_unlocker as FU
background, textColor = 'black', '#F6FAFB'
background, textColor = textColor, background
avatarChoosen = 0
choosedAvtrImage = None
user_name = ''
user_gender = ''
try:
face_classifier = cv2.CascadeClassifier('C:/VScode/AI---TroLyAo/master/Cascade/haarcascade_frontalface_default.xml')
except Exception as e:
print('Cascade File is missing...')
raise SystemExit
if os.path.exists('userData')==False:
os.mkdir('userData')
if os.path.exists('userData/faceData')==False:
os.mkdir('userData/faceData')
###### ROOT1 ########
def startLogin():
try:
result = FU.startDetecting()
if result:
user = UserData()
user.extractData()
userName = user.getName().split()[0]
welcLbl['text'] = 'Hi '+userName+',\nWelcome to the world of\nScience & Technology'
loginStatus['text'] = 'UNLOCKED'
loginStatus['fg'] = 'green'
faceStatus['text']='(Logged In)'
os.system('python modules/gui_assistant.py')
else:
print('Error Occurred')
except Exception as e:
print(e)
####### ROOT2 ########
def trainFace():
data_path = 'userData/faceData/'
onlyfiles = [f for f in os.listdir(data_path) if isfile(join(data_path, f))]
Training_data = []
Labels = []
for i, files in enumerate(onlyfiles):
image_path = data_path + onlyfiles[i]
images = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
Training_data.append(np.asarray(images, dtype=np.uint8))
Labels.append(i)
Labels = np.asarray(Labels, dtype=np.int32)
model = cv2.face.LBPHFaceRecognizer_create()
model.train(np.asarray(Training_data), np.asarray(Labels))
print('Model Trained Successfully !!!')
model.save('userData/trainer.yml')
print('Model Saved !!!')
def face_extractor(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(gray, 1.3, 5)
if faces == ():
return None
for (x, y, w, h) in faces:
cropped_face = img[y:y+h, x:x+w]
return cropped_face
cap = None
count = 0
def startCapturing():
global count, cap
ret , frame = cap.read()
if face_extractor(frame) is not None:
count += 1
face = cv2.resize(face_extractor(frame), (200, 200))
face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
file_name_path = 'userData/faceData/img' + str(count) + '.png'
cv2.imwrite(file_name_path, face)
print(count)
progress_bar['value'] = count
cv2.putText(face, str(count), (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2)
else:
pass
if count==100:
progress_bar.destroy()
lmain['image'] = defaultImg2
statusLbl['text'] = '(Face added successfully)'
cap.release()
cv2.destroyAllWindows()
Thread(target=trainFace).start()
addBtn['text'] = ' Next '
addBtn['command'] = lambda:raise_frame(root3)
return
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
frame = cv2.flip(frame, 1)
img = Image.fromarray(frame)
imgtk = ImageTk.PhotoImage(image=img)
lmain.imgtk = imgtk
lmain.configure(image=imgtk)
lmain.after(10, startCapturing)
def Add_Face():
global cap, user_name, user_gender
user_name = nameField.get()
user_gender = r.get()
if user_name != '' and user_gender!=0:
if agr.get()==1:
cap = cv2.VideoCapture(0)
startCapturing()
progress_bar.place(x=20, y=273)
statusLbl['text'] = ''
else:
statusLbl['text'] = '(Check the Condition)'
else:
statusLbl['text'] = '(Please fill the details)'
def SuccessfullyRegistered():
if avatarChoosen != 0:
gen = 'Male'
if user_gender==2: gen = 'Female'
u = UserData()
u.updateData(user_name, gen, avatarChoosen)
usernameLbl['text'] = user_name
raise_frame(root4)
def selectAVATAR(avt=0):
global avatarChoosen, choosedAvtrImage
avatarChoosen = avt
i=1
for avtr in (avtb1,avtb2,avtb3,avtb4,avtb5,avtb6,avtb7,avtb8):
if i==avt:
avtr['state'] = 'disabled'
userPIC['image'] = avtr['image']
else: avtr['state'] = 'normal'
i+=1