【发布时间】:2019-11-11 23:55:48
【问题描述】:
我有一个视频,https://www.youtube.com/watch?v=LdNrXndwyCc。 我正在尝试计算此视频中的未知人数,但有以下限制:
- 每次新的唯一面部检测都会增加未知计数。并将人脸编码存储到一个列表(facelist)中。
- 假设第一帧包含 2 个人,第二帧包含 4 个人。 代码将比较新面孔(新面孔编码)与旧面孔(面孔编码,存在于数组中)。并计算不在人脸列表中的新人脸的数量,并将此计数添加到未知人脸总数中。如果找到新面孔,则将面孔编码附加到列表中。
- 在新帧中,如果没有面部编码与面部列表的任何元素匹配,则清除面部列表。并将新的人脸编码附加到人脸列表中。未知数将根据新人数增加。
问题:
当一个人微笑,或将他的脸从前到左(或从前到右)。它检测到这张脸是新脸,并增加未知数
几帧后没有正确检测到新人脸
在 Python3、opencv2、face_recognition python 的库中试过。平台 Ubuntu 18.04
class FaceCount:
def __init__(self):
self.unknown_count=0
def face_distance_to_conf(self,face_distance, face_match_threshold=0.6):
if face_distance > face_match_threshold:
range = (1.0 - face_match_threshold)
linear_val = (1.0 - face_distance) / (range * 2.0)
return linear_val
else:
range = face_match_threshold
linear_val = 1.0 - (face_distance / (range * 2.0))
return linear_val + ((1.0 - linear_val) * math.pow((linear_val - 0.5) * 2, 0.2))
def countFaceThread(self,facelist,face_encodings):
matched_with_no_one=True
for face_encoding in face_encodings:
dup=False
for face in facelist:
match=face_recognition.compare_faces([face_encoding],face)[0]
face_distanc=face_recognition.face_distance([face_encoding],face)
percent=self.face_distance_to_conf(face_distanc)[0]
print(percent)
if match and percent>0.40:
dup=True
matched_with_no_one=False
break
#print('finished Comparing')
if not dup:
self.unknown_count+=1
print("unknown_count---->",self.unknown_count)
facelist.append(face_encoding)
if matched_with_no_one:
print("clearing facelist....")
facelist.clear()
print("unknown_count---->",self.unknown_count)
for f_encode in face_encodings:
facelist.append(f_encode)
def countUnknown(self):
cap = cv2.VideoCapture('livetest.webm')
cap.set(cv2.CAP_PROP_POS_MSEC,30)
facelist=[]
while(cap.isOpened()):
try:
#istart=time.time()
ret, frame = cap.read()
#print('frame reading time------->',time.time()-istart)
#start=time.time()
rgb_frame = frame[:, :, ::-1]
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
#print("detection time----------->",time.time()-start)
#start=time.time()
cv2.imshow('frame', frame)
for (top, right, bottom, left) in face_locations:
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.imshow('frame', frame)
#print("showing the detected frame time----------->",time.time()-start)
start=time.time()
if facelist and face_encodings:
t2=threading.Thread(target=self.countFaceThread,args=(facelist,face_encodings))
t2.start()
elif face_locations:
self.unknown_count+=len(face_locations)
print("unknown people------->",self.unknown_count)
for face in face_encodings:
facelist.append(face)
continue
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except Exception as e:
print(e)
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
if __name__=='__main__':
t1=threading.Thread(target=FaceCount().countUnknown)
t1.start()
https://www.youtube.com/watch?v=LdNrXndwyCc 播放此视频 在 0.02 秒内,该人应被视为未知人,并将计数增加一。但事实并非如此。当人微笑时它会增加
【问题讨论】:
-
您好,我没明白,您的具体问题是什么?
-
face_recognition.compare_faces() 这个函数给出了错误的结果。我在和同一个人的脸比较,一个是他的笑脸。一张是他正常的脸。如果他微笑,或者把头从前面移到另一边,那就是把他当作不同的人对待。如何证明人是同一个人?
标签: python python-3.x opencv face-recognition dlib