Многопоточное распознавание лиц Python в Opencv - PullRequest
0 голосов
/ 28 мая 2019

Я пытаюсь создать программу распознавания лиц с Python 3.6 с Opencv.

Я написал код, который;когда на экране мое лицо, вокруг меня появляется красный квадрат, включая мое имя.И он печатает мое имя ТОЛЬКО ОДИН РАЗ на консоли, если я продолжаю сидеть на своей камере, он говорит, что я могу идти, потому что он сделал мою фотографию.

import face_recognition
import cv2

video_capture = cv2.VideoCapture(0)

wicaledon_image = face_recognition.load_image_file("Wicaledon.jpg")
wicaledon_face_encoding = face_recognition.face_encodings(wicaledon_image)[0]

known_face_encodings = [
    wicaledon_face_encoding
]
known_face_names = [
    "Wicaledon"
]

# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
new_name = ''

while True:
    ret, frame = video_capture.read()

    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

    rgb_small_frame = small_frame[:, :, ::-1]

    if process_this_frame:

        face_locations = face_recognition.face_locations(rgb_small_frame)
        face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

        face_names = []
        for face_encoding in face_encodings:
            matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
            name = "Unknown"

            if True in matches:
                first_match_index = matches.index(True)
                name = known_face_names[first_match_index]

                if str(new_name) == str(name):
                    print("You can go Mr {}".format(name))
                else:
                    print("Hello {}".format(name))
            new_name = name

            face_names.append(name)

    process_this_frame = not process_this_frame

    for (top, right, bottom, left), name in zip(face_locations, face_names):
        # Scale back up face locations since the frame we detected in was scaled to 1/4 size
        top *= 4
        right *= 4
        bottom *= 4
        left *= 4

        # Draw a box around the face
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

        # Draw a label with a name below the face
        cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

    # Display the resulting image
    cv2.imshow('Video', frame)

    # Hit 'q' on the keyboard to quit!
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()

Проблема в том,пока на экране нет лица, FPS работает быстро.Но когда на экране есть лицо (даже неизвестное лицо), FPS работает очень медленно (я думаю, из-за того, что сделал много снимков).Я хочу как;когда мое лицо на экране, оно всегда должно показывать красный квадрат, но быстрый FPS и печатные предложения должны продолжаться.

Когда я проверял, что мне нужно использовать Thread, я не нашел решение, даже много попыток.Вы можете мне помочь?

...