所以,我想创建一个结构,其中人脸识别将在后台线程上完成,并且流将通过正在进行的前台线程显示。
我仍然不明白为什么我的电脑以 20fps 而不是 30fps 显示流。
更新了代码(添加了 perf_counter 并添加了用户定义的函数)
请参阅下面的链接旧代码。
Old py codeimport cv2
from simple_facerec import SimpleFacerec
from datetime import datetime
from time import perf_counter
prev = 0
count = 0
vid_cod = ""
cap = ""
path =""
vid_name = ""
output = ""
sfr = ""
def vidCapture():
global cap,prev,count,sfr
while (cap.isOpened()):
ret, frame = cap.read()
# Detect Faces
startDetect = perf_counter()
face_locations, face_names = sfr.detect_known_faces(frame)
for face_loc, name in zip(face_locations, face_names):
y1, x2, y2, x1 = face_loc[0], face_loc[1], face_loc[2], face_loc[3]
#if the face is recognised then
if name != "Unknown" :
print("known Person detected :) => " + name)
stopDetect = perf_counter()
print(f"Time taken to identify face : {stopDetect-startDetect}")
#else if the face is unknown then
else :
stopDetect = perf_counter()
print(f"Time taken to identify face : {stopDetect-startDetect}")
print("Unknown Person detected :( => Frame captured...")
#sw the frames on the display
cv2.imsw("Frame", frame)
#write frames to output object so it can save the video
output.write(frame)
#to terminate the process press "x"
if cv2.waitKey(1) & 0XFF == ord('x'):
break
cap.release()
output.release()
cv2.destroyAllWindows()
def __init__():
global prev,count,vid_cod,vid_name,cap,path,output,sfr
startSfr = perf_counter()
# Encode faces from a folder
sfr = SimpleFacerec()
sfr.load_encoding_images("images/")
stopSfr = perf_counter()
loadSfr = stopSfr-startSfr
print(f"Time taken to load sfr : {loadSfr}")
startCam = perf_counter()
# Load Camera
cap = cv2.VideoCapture('https://192...:8080/videofeed')
#cap = cv2.VideoCapture(0)
stopCam = perf_counter()
loadCam = stopCam-startCam
print(f"Time taken to load cam : {loadCam}")
startCodec = perf_counter()
#Use MPEG video codec
vid_cod = cv2.VideoWriter_fourcc(*'MPEG')
stopCodec = perf_counter()
loadCodec = stopCodec-startCodec
print(f"Time taken to load codec : {loadCodec}")
startInit = perf_counter()
prev = 0
count = 0
#defining the path where video will be saved
path = 'C:/Users/Username/Doents/Recorded/'
#Encode video name with date and time
vid_name = str("recording_"+datetime.now().strftime("%b-%d-%Y_%H:%M").replace(":","_")+".avi")
#initialize video saving process as a "output" object
output = cv2.VideoWriter( str(path+vid_name) , vid_cod, 20.0 ,(640,480))
stopInit = perf_counter()
loadInit = stopInit - startInit
print(f"Time taken to load rest : {loadInit}")
vidCapture()
__init__()
下面的文件是在上面的代码中导入的 simplefacerec。
simple_facerec.py Note:当我将output
中的 fps 从 20 更改为 25 时,保存的视频长度会减少,当我将 fps 从 20 更改为 15 时,视频长度会增加。
在放入perf_counter()
后,我发现单独的人脸识别大约需要 0.5 秒。整个代码运行了大约 7 到 8 秒,其中只显示了 5 帧。
Time taken to load sfr : 2.3252885000001697
Time taken to load cam : 0.3193597999998019
Time taken to load codec : 1.3199998647905886e-05
Time taken to load rest : 0.0018557999974291306
known Person detected :) => Anirudhdhsinh Jadeja
Time taken to identify face : 0.4715902999996615
known Person detected :) => Anirudhdhsinh Jadeja
Time taken to identify face : 0.5326913999997487
known Person detected :) => Anirudhdhsinh Jadeja
Time taken to identify face : 0.4969602000019222
known Person detected :) => Anirudhdhsinh Jadeja
Time taken to identify face : 0.4868558000016492
known Person detected :) => Anirudhdhsinh Jadeja
Time taken to identify face : 0.4679383000002417
本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处
评论列表(42条)