gpt4 book ai didi

python - 跟踪图像时迭代陷入循环(无限循环)

转载 作者:行者123 更新时间:2023-11-30 09:40:27 25 4
gpt4 key购买 nike

实际上,我试图使用机器学习构建一个人脸识别考勤系统,因为当我单击调用 TrackImages() 函数的 TrackImages 按钮时,我做了所有事情,人脸被正确跟踪和识别,但问题是,当程序进入循环,循环执行到attendence.loc[len(attendence)] = [Id,aa,date,timeStamp]这一行,然后再次从开头开始迭代。 While 循环而不是按照代码中所述在新创建的 csv 文件中标记出勤情况。该程序继续跟踪面部,而不是完成识别和标记考勤。请帮忙......

def TrackImages():
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read(r"TrainingImageLabel\Trainner.yml")
harcascadePath = "I:\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(harcascadePath);
df = pd.read_csv(r"StudentDetails\studentDetails.csv")
cam = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX
col_names = ['Id','Name','Date','Time']
attendence = pd.DataFrame(columns = col_names)
while True:
ret, im = cam.read()
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray, 1.2, 5)
for (x,y,w,h) in faces:
cv2.rectangle(im, (x,y), (x+w,y+h), (255,0,0),2)
Id, conf = recognizer.predict(gray[y:y+h,x:x+w])
if (conf <60):
ts = time.time()
date = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d')
timeStamp = datetime.datetime.fromtimestamp(ts).strftime('%H:%M:%S')
aa = df.loc[df['Id'] == Id]['Name'].values
tt = str(Id)+"-"+aa
attendence.loc[len(attendence)] = [Id,aa,date,timeStamp]
else:
Id = 'Unknown'
tt = str(Id)
if(conf>75):
noOfFile = len(os.listdir("ImagesUnknown"))+1
cv2.imwrite("ImagesUnknown\Image"+str(noOfFile)+".jpg", im[y:y+h,x:x+w])
cv2.putText(im,str(tt), (x,y+h), font, 1,(255,255,255),2)
attendence= attendence.drop_duplicates(subset = ['Id'], keep='first')
cv2.imshow('im',im)
if (cv2.waitKey(1) == ord('q')):
break
ts = time.time()
date = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d')
timeStamp = datetime.datetime.fromtimestamp(ts).strftime('%H:%M:%S')
Hour,Minute,Second = timeStamp.split(":")
fileName = "Attendence\Attendence_" +date+"_"+ Hour+"-"+Minute+"-"+Second+".csv"
attendence.to_csv(fileName,index=False)
cam.release()
cv2.destroyAllWindows()
res = attendence
message1.configure(text = res)

最佳答案

我认为问题出在你的缩进上。目前它的内容如下:

While True:
for some_stuff in stuff:
if (conf < 60):
add a timestamp record to attendence.loc
else:
set some Id

if (conf > 75):
write an image file

# This runs on every while loop
cv2.imshow('im',im)
if (cv2.waitKey(1) == ord('q')):
break

# This runs once while is no longer True (after pressing `q`)
ts = time.time()
date = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d')
timeStamp = datetime.datetime.fromtimestamp(ts).strftime('%H:%M:%S')
Hour,Minute,Second = timeStamp.split(":")
fileName = "Attendence\Attendence_" +date+"_"+ Hour+"-"+Minute+"-"+Second+".csv"
attendence.to_csv(fileName,index=False)

因此,如果您希望每次都运行最后一行,则需要将它们缩进一步以放入 while 循环内。然而,这不会对写入文件施加条件,无论如何都会完成。您可以创建一个函数来添加时间戳并写入文件:

def write_file(attendance):
ts = time.time()
date = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d')
timeStamp = datetime.datetime.fromtimestamp(ts).strftime('%H:%M:%S')
Hour,Minute,Second = timeStamp.split(":")
fileName = "Attendence\Attendence_" +date+"_"+ Hour+"-"+Minute+"-"+Second+".csv"
attendence.to_csv(fileName,index=False)

While True:
for some_stuff in stuff:
if (conf < 60):
add a timestamp record to attendence.loc
write_file(attendence)
else:
set some Id

if (conf > 75):
write an image file

# This runs on every while loop
cv2.imshow('im',im)
if (cv2.waitKey(1) == ord('q')):
break

关于python - 跟踪图像时迭代陷入循环(无限循环),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59036996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com