框架没有响应 face_recognition 库

问题描述

我在 python 3.7.7 中使用 anaconda 环境,一切正常,但我必须格式化我的笔记本电脑,我以与以前相同的方式安装了以下库并安装了它们,但是当我运行我的代码显示相机启动时帧没有响应,opencv imshow 正在工作,我认为问题出在人脸识别库上。

我安装的库

conda install -c conda-forge opencv
conda install -c conda-forge dlib
pip 安装 face_recognition
pip 安装 imutils

我的代码是:

from imutils.video import VideoStream
import face_recognition
import argparse
import imutils
import pickle
import time
import cv2
import numpy as np
import os

args = {
    "encodings": "models/encodings/encodings.pickle","output": "output/"+outputFileName(),"display": 1,"detection_method": "cnn","confidence": 0.5,"prototxt": "models/ssd/MobileNetSSD_deploy.prototxt","model": 'models/ssd/MobileNetSSD_deploy.caffemodel',"use_gpu": 0
}


# load the kNown faces and embeddings
data = pickle.loads(open(args["encodings"],"rb").read())

vs = VideoStream(src=0).start()

writer = None
time.sleep(2.0)

fps = FPS().start()

names2=[]
t11=[]

no_names_pf=10



i1=10
vidsave=False


# loop over frames from the video file stream
while True:
    frame = vs.read()
    
    rgb = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
    rgb = imutils.resize(frame,width=750)
    r = frame.shape[1] / float(rgb.shape[1])

    # detect the (x,y)-coordinates of the bounding Boxes
    # corresponding to each face in the input frame,then compute
    # the facial embeddings for each face
    Boxes = face_recognition.face_locations(rgb,model=args["detection_method"])
    encodings = face_recognition.face_encodings(rgb,Boxes)
    names = []

    # loop over the facial embeddings
    for encoding in encodings:
        # attempt to match each face in the input image to our kNown
        # encodings
        matches = face_recognition.compare_faces(data["encodings"],encoding)
        name = "UnkNown"

        # check to see if we have found a match
        if True in matches:
            # find the indexes of all matched faces then initialize a
            # dictionary to count the total number of times each face
            # was matched
            matchedIdxs = [i for (i,b) in enumerate(matches) if b]
            counts = {}

            # loop over the matched indexes and maintain a count for
            # each recognized face face
            for i in matchedIdxs:
                name = data["names"][i]
                counts[name] = counts.get(name,0) + 1

            # determine the recognized face with the largest number
            # of Votes (note: in the event of an unlikely tie Python
            # will select first entry in the dictionary)
            name = max(counts,key=counts.get)
        
        # update the list of names
        names.append(name)
        print(names)
        
        #___new code_____
        names2.append(name)
    
    #need to mod (capable of r of 1 person at the same time)
    if len(names2)>no_names_pf:
        
        confbool1=False
        for i in range(no_names_pf):
            i+=1
            i=i*-1
            if names2[-1] == names2[i-1]:
                confbool1=True
            else:
                confbool1=False
                break
        if confbool1==True:
            t11.append(name+'$$'+\
            str(datetime.datetime.Now().strftime("%d-%m-%Y %H:%M:%s")))
                
            if t11:
                print(t11[-1])
                
      

    
    
    #td01=frame[0:-2]
    framecopy=frame.copy()
    
    # loop over the recognized faces
    for ((top,right,bottom,left),name) in zip(Boxes,names):
        # rescale the face coordinates
        top = int(top * r)
        right = int(right * r)
        bottom = int(bottom * r)
        left = int(left * r)

        # draw the predicted face name on the image
        cv2.rectangle(frame,(left,top),(right,bottom),(0,255,0),2)
        y0 = top - 15 if top - 15 > 15 else top + 15
        cv2.putText(frame,name,y0),cv2.FONT_HERShey_SIMPLEX,0.75,2)
        
    

    # if the video writer is None *AND* we are supposed to write
    # the output video to disk initialize the writer
    
    
    
    if writer is None and args["output"] is not None:
        fourcc = cv2.VideoWriter_fourcc(*"MJPG")
        writer = cv2.VideoWriter(args["output"],fourcc,20,(frame.shape[1],frame.shape[0]),True)

  
    
   # if i1%10 == 0 :
    # print(vidsave)
    # print(label)
    # print('person' in label)
    

    #i1=i1+1
    vidsave=False

    # check to see if we are supposed to display the output frame to
    # the screen
    if args["display"] > 0:
        cv2.imshow("Frame",frame)
        key = cv2.waitKey(1) & 0xFF

        # if the `q` key was pressed,break from the loop
        if key == ord("q"):
            break

    #__________________________________________
    
    fps.update()

# stop the timer and display FPS information
fps.stop()
print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))



cv2.destroyAllWindows()
vs.stop()

if writer is not None:
    writer.release()
    
#___________________________________________________________________

# report handeling







appendtxt("output/"+datetime.datetime.Now()\
          .strftime("%d-%m-%Y")+" report.txt",'a\n')

    
    
    
this_year=datetime.datetime.Now().strftime("%Y")
this_month=datetime.datetime.Now().strftime("%m")
this_day=datetime.datetime.Now().strftime("%d")

# if not os.path.exists('output/'+this_year):
#     os.makedirs('output/'+this_year)

# if not os.path.exists('output/'+this_year+'/'+this_month):
#     os.makedirs('output/'+this_year+'/'+this_month)

# if not os.path.exists('output/'+this_year+'/'+this_month+'/'+this_day):
#     os.makedirs('output/'+this_year+'/'+this_month+'/'+this_day)





#image = cv2.imread("C:/Users/anwar/Desktop/lllsky.png")

# cv2.imshow("Image",frame)
# cv2.imwrite('output/aa.jpg',frame)
# cv2.waitKey(0)

# fc = framecopy[top-50:bottom+50,left-50:right+50 ]
# cv2.imshow("fc",fc)
# cv2.waitKey(0)

# cv2.destroyAllWindows()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)