使用电脑前置摄像头进行人脸识别,如何让它更快?



我使用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()

您不应该使用API进行实时识别。您可以在本地下载并集成一个face_recognition模型,您将重新培训以与您的脸匹配。

或这可能是一个解决方案:不要每秒发送30帧,但只有1或2,您会看到表演的增加。

最新更新