使用计算机的前置摄像头进行人脸识别,如何使其更快?

时间:2019-06-26 02:58:34

标签: python opencv face-recognition

我使用face_recognitionOpenCVFace++ Search API使用计算机的前置摄像头制作一个简单的实时人脸识别程序。 该过程是:首先在检测到脸部时保存相机的画面,然后调用Face++ Search API来识别该人是否在我的脸中摆好姿势并打招呼。结果在fps方面非常差,我如何使其更快?

我认为重新编写代码(例如执行异步或使用某些多进程方式)可能会有所帮助。但是我对使用多进程或异步方法没有太多的经验,如果有人可以帮助我,我将非常感谢。

我正在使用Python 3.7,这是我正在使用的代码:

import face_recognition
import requests
import cv2
import pyttsx3


def search_face(file, key, secret):
    http_url = 'https://api-cn.faceplusplus.com/facepp/v3/search'

    form_data = {
        'api_key': key,
        'api_secret': secret,
        'outer_id': '***'
    } 

    file  = {
        'image_file': open(file, 'rb')
    }

    req = requests.post(url = http_url, data = form_data, files = file)
    result = req.json()

    if len(result['faces'])>0:
        return result['results'][0]['confidence'], result['results'][0]['user_id'] 


def say_hi(text):
    engine = pyttsx3.init()

    voices = engine.getProperty('voices')
    engine.setProperty('voice', voices[1].id)

    engine.say(text)
    engine.runAndWait()
    engine.stop

if __name__ == '__main__':

    key = '***'
    secret = '***'
    file = '/Desktop/test/test.jpg'

    cap = cv2.VideoCapture(0)

    if cap.isOpened():
        rval, frame = cap.read()
    else:
        rval = False

    #Initialize some variables
    face_locations = []
    process_this_frame = True

    while rval:
        rval, frame = cap.read()

        #Resize frame of video to 1/4 size for faster face recognition processing 
        small_frame = cv2.resize(frame, (0,0), fx = 0.25, fy = 0.25)

        #Convert the image from BGR color (which openCV uses) to RGB color (which face_recognition uses)
        rgb_small_frame = small_frame[:, :, ::-1]

        # Only process every other frame of video to save time
        if process_this_frame:
            #Find all the faces in the current frame of video
            face_locations = face_recognition.face_locations(rgb_small_frame)

        process_this_frame = not process_this_frame

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

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

        if len(face_locations) == 0:
            continue
        else:      
            #Display the results
            for (top, right, bottom, left) in face_locations:

                #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, 0), 2)

            #Save the frame
            cv2.imwrite(file, frame)

            confidence_level, user_id = search_face(file, key, secret)

            if confidence_level > 80:
                text = 'Hello, {}'.format(user_id)
                say_hi(text)
                print(user_id)
            else:
                text = 'Sorry, please try again!'
                say_hi(text)
                print('Sorry, {}'.format(confidence_level))


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

1 个答案:

答案 0 :(得分:0)

您不应使用API​​进行实时人脸识别。您可以在本地下载并集成一个face_recognition模型,然后对其进行重新训练以使其与您的面部相匹配。

或者这可能是一种解决方案:不要每秒发送全部30帧,而仅发送1或2,您会看到性能提高。