Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 计算机人脸识别&x27;s前置摄像头,如何使其更快?_Python_Opencv_Face Recognition - Fatal编程技术网

Python 计算机人脸识别&x27;s前置摄像头,如何使其更快?

Python 计算机人脸识别&x27;s前置摄像头,如何使其更快?,python,opencv,face-recognition,Python,Opencv,Face Recognition,我使用face\u recognition、OpenCV和face++Search API使用我电脑的前置摄像头制作了一个简单的实时人脸识别程序。 该过程首先是在检测到人脸时保存摄像头的帧,然后调用face++Search API来识别该人是否在我的面部集合中并打招呼。就fps而言,结果非常差,如何使其更快 我认为重新编写代码,比如执行异步或使用一些多进程方式可能会有所帮助。但我没有太多使用多进程或异步方法的经验,如果有人能在这方面帮助我,我将不胜感激 我使用的是Python 3.7,下面是我使

我使用
face\u recognition
OpenCV
face++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进行实时人脸识别。您可以下载并在本地集成一个人脸识别模型,您将重新训练该模型以匹配您的人脸

或者这可能是一个解决方案:不要每秒发送所有30帧,但只有1或2帧,您将看到性能提高