开发者社区> 问答> 正文

试图使用face_gition自动识别人脸时,列表索引超出范围

我试图拯救未知的脸在视频和基于第一帧来识别它们出现在例如如果一个未知的脸出现在14帧应该被视为“新14”但是我越来越错误”IndexError:列表索引范围”,当一个新面孔出现。 这是我的代码和回溯。

import face_recognition
import cv2

input_movie = cv2.VideoCapture("video.mp4")
length = int(input_movie.get(cv2.CAP_PROP_FRAME_COUNT))

# Create an output movie file (make sure resolution/frame rate matches input video!)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
output_movie = cv2.VideoWriter('output.avi', fourcc, 29.97, (640, 360))


newimage = face_recognition.load_image_file("anchor.png")
new_face_encoding = face_recognition.face_encodings(newimage)[0]

known_faces = [
    new_face_encoding,

]

# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
frame_number = 0


def recog(frame_number, known_faces, face_names):
    toenc = []

    torec = face_recognition.load_image_file(r"New\Unknown%s.jpg" %str(frame_number))

    #if not len(torec):
     #   print("cannot find image")
    #torec = face_recognition.load_image_file(r"New\Unknown%s.jpg" %str(frame_number))
    toenc.append((face_recognition.face_encodings(torec))[0])
    if not len(toenc):
        print("can't be encoded")
    known_faces.append(toenc.pop())
    face_names.append("new %s" %str(frame_number))      

# Load some sample pictures and learn how to recognize them.

while True:
    # Grab a single frame of video
    ret, frame = input_movie.read()
    frame_number += 1

    # Quit when the input video file ends
    if not ret:
        break

    # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
    rgb_frame = frame[:, :, ::-1]

    # Find all the faces and face encodings in the current frame of video
    face_locations = face_recognition.face_locations(rgb_frame)
    face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)

    #face_names = []
    for face_encoding in face_encodings:
        # See if the face is a match for the known face(s)
        match = face_recognition.compare_faces(known_faces, face_encoding)


        # If you had more than 2 faces, you could make this logic a lot prettier
        # but I kept it simple for the demo
        name = "Unknown"

        face_names.append(name)

    # Label the results
    for (top, right, bottom, left), name in zip(face_locations, face_names):
        if not name:
            continue

        # Draw a box around the face
        unface = cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
        if name == "Unknown":
            res = frame[top:bottom, left:right]
            cv2.imwrite(r"New\Unknown%s.jpg" %str(frame_number), res)
            recog(frame_number, known_faces, face_names)

        cv2.rectangle(frame, (left, bottom - 25), (right, bottom), (0, 0, 255), cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.5, (255, 255, 255), 1)

    # Write the resulting image to the output video file
    print("Processing frame {} / {}".format(frame_number, length))
    #output_movie.write(frame)
    cv2.imshow("frame", frame)
    if( cv2.waitKey(27) & 0xFF == ord('q')):
        break

# All done!
input_movie.release()
cv2.destroyAllWindows()

输出

In [1]: runfile('D:/project_new/facerec_from_video_file.py', wdir='D:/project_new')
Processing frame 1 / 3291
Processing frame 2 / 3291
Processing frame 3 / 3291
Processing frame 4 / 3291
Processing frame 5 / 3291
Processing frame 6 / 3291
Processing frame 7 / 3291
Processing frame 8 / 3291
Processing frame 9 / 3291
Processing frame 10 / 3291
Processing frame 11 / 3291
Processing frame 12 / 3291
Traceback (most recent call last):

  File "<ipython-input-1-4b2c69ca71f8>", line 1, in <module>
    runfile('D:/project_new/facerec_from_video_file.py', wdir='D:/project_new')

  File "C:\Users\saber\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\Users\saber\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "D:/project_new/facerec_from_video_file.py", line 81, in <module>
    recog(frame_number, known_faces, face_names)

  File "D:/project_new/facerec_from_video_file.py", line 35, in recog
    toenc.append((face_recognition.face_encodings(torec))[0])

IndexError: list index out of range

问题来源StackOverflow 地址:/questions/59381438/indexerror-list-index-out-of-range-while-attempting-to-automatically-recognize

展开
收起
kun坤 2019-12-28 13:50:32 532 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载