Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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 3.x 将k-最近邻(KNN)人脸识别算法与OpenCV实时集成_Python 3.x_Opencv_Real Time_Face Recognition_Knn - Fatal编程技术网

Python 3.x 将k-最近邻(KNN)人脸识别算法与OpenCV实时集成

Python 3.x 将k-最近邻(KNN)人脸识别算法与OpenCV实时集成,python-3.x,opencv,real-time,face-recognition,knn,Python 3.x,Opencv,Real Time,Face Recognition,Knn,我正在使用Python库,并试图将KNN算法更改为与OpenCV实时运行。为此,我“合并”了库作者(,)提供的另外两个算法 (已编辑:现在显示帧,直到检测到某张脸,然后崩溃) 到目前为止,我尝试的是: import numpy as np import cv2 import face_recognition import pickle def predict(frame, knn_clf=None, model_path=None, distance_threshold=0.6): "

我正在使用Python库,并试图将KNN算法更改为与OpenCV实时运行。为此,我“合并”了库作者(,)提供的另外两个算法

(已编辑:现在显示帧,直到检测到某张脸,然后崩溃) 到目前为止,我尝试的是:

import numpy as np
import cv2
import face_recognition
import pickle

def predict(frame, knn_clf=None, model_path=None, distance_threshold=0.6):
    """
    Recognizes faces in given image using a trained KNN classifier
    :param knn_clf: (optional) a knn classifier object. if not specified, model_save_path must be specified.
    :param model_path: (optional) path to a pickled knn classifier. if not specified, model_save_path must be knn_clf.
    :param distance_threshold: (optional) distance threshold for face classification. the larger it is, the more chance
           of mis-classifying an unknown person as a known one.
    :return: a list of names and face locations for the recognized faces in the image: [(name, bounding box), ...].
        For faces of unrecognized persons, the name 'unknown' will be returned.
    """

    if knn_clf is None and model_path is None:
        raise Exception("Must supply knn classifier either thourgh knn_clf or model_path")

    # Load a trained KNN model (if one was passed in)
    if knn_clf is None:
        with open(model_path, 'rb') as f:
            knn_clf = pickle.load(f)

    # find face locations from frame
    X_face_locations = face_recognition.face_locations(frame)

    # If no faces are found in the image, return an empty result.
    if len(X_face_locations) == 0:
        return []

    # Find encodings for faces in the frame
    faces_encodings = face_recognition.face_encodings(frame, known_face_locations=X_face_locations)

    # Use the KNN model to find the best matches for the test face
    closest_distances = knn_clf.kneighbors(faces_encodings, n_neighbors=1)
    are_matches = [closest_distances[0][i][0] <= distance_threshold for i in range(len(X_face_locations))]

    # Predict classes and remove classifications that aren't within the threshold
    return [(pred, loc) if rec else ("unknown", loc) for pred, loc, rec in zip(knn_clf.predict(faces_encodings), X_face_locations, are_matches)]


def show_labels_on_webcam(RGBFrame, predictions):
    """
    Shows the face recognition results visually.
    :param img_path: path to image to be recognized
    :param predictions: results of the predict function
    :return:
    """
    frame = RGBFrame

    for name, (top, right, bottom, left) in predictions:
        # 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
        print (frame.shape)
        print (frame.dtype)
        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)

# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(0)

while True:
    # Grab a single frame of video
    ret, frame = video_capture.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]

    predictions = predict(rgb_small_frame, model_path="trained_knn_model_1.clf")

    # Display results overlaid on webcam video
    print (rgb_small_frame.shape)
    print (rgb_small_frame.dtype)
    show_labels_on_webcam(rgb_small_frame, predictions)

    # 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()
将numpy导入为np
进口cv2
导入人脸识别
进口泡菜
def预测(帧,knn\U clf=无,模型路径=无,距离\U阈值=0.6):
"""
使用经过训练的KNN分类器识别给定图像中的人脸
:param knn_clf:(可选)knn分类器对象。如果未指定,则必须指定模型保存路径。
:param model_path:(可选)到pickle knn分类器的路径。如果未指定,model_save_path必须为knn_clf。
:param distance_threshold:(可选)人脸分类的距离阈值。它越大,可能性越大
把一个不认识的人误分类为一个已知的人。
:return:图像中已识别面的名称和面位置列表:[(名称,边界框),…]。
对于无法识别的人脸,将返回名称“unknown”。
"""
如果knn_clf为无且模型_路径为无:
引发异常(“必须提供knn分类器thourgh knn\u clf或model\u path”)
#加载经过训练的KNN模型(如果传入一个)
如果knn_clf为无:
开放式(模型_路径,'rb')为f:
knn_clf=酸洗负荷(f)
#从帧中查找面位置
X_面部位置=面部识别。面部位置(帧)
#如果在图像中未找到面,则返回空结果。
如果len(X_面_位置)==0:
返回[]
#查找帧中面的编码
面部编码=面部识别。面部编码(帧、已知面部位置=X面部位置)
#使用KNN模型为测试面找到最佳匹配
最近距离=knn\u clf.kneighbors(面编码,n\u邻居=1)

are_matches=[最近的_距离[0][i][0]我通过更改网络摄像头上的
显示标签(rgb_小框架,预测)
通过
显示网络摄像头上的标签(框架,预测)
。感谢@api55的提示!

错误发生前框架的形状和类型是什么?形状=(120,160,3);dtype=uint8我已经编辑了一些代码行,仍然收到一个错误:/
Traceback (most recent call last):
  File "withOpenCV.py", line 91, in <module>
    show_labels_on_webcam(rgb_small_frame, predictions)
  File "withOpenCV.py", line 62, in show_labels_on_webcam
    cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
TypeError: Layout of the output array img is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels)