当我运行此代码时,框架会打开,但会变得迟钝,但是单独运行这些代码会完美运行

问题描述

一个代码使用OpenCV打开框架并保存图像,第二个代码使用机器学习算法来匹配面部。

代码简单易懂,但令人头疼。 这些代码功能在行中加上注释,以免混淆您。

import numpy as np
import cv2

from face_rec import classify_face

face_cascade = cv2.CascadeClassifier('cascades/data/haarcascade_frontalface_alt2.xml')
cap = cv2.VideoCapture(0)

def make_1080p():
    cap.set(3,1920)
    cap.set(4,1080)

def make_720p():
    cap.set(3,1280)
    cap.set(4,720)

def make_480p():
    cap.set(3,640)
    cap.set(4,480)

def change_res(width,height):
    cap.set(3,width)
    cap.set(4,height)

change_res(200,200)

def rescale_frame(frame,percent=75):
    width = int(frame.shape[1] * percent/ 100)
    height = int(frame.shape[0] * percent/ 100)
    dim = (width,height)
    return cv2.resize(frame,dim,interpolation =cv2.INTER_AREA)

while(True):

    # Capture frame-by-frame
    ret,frame = cap.read()

    frame75 = rescale_frame(frame,percent=75)
    gray  = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray,scaleFactor=1.2,minNeighbors=5)
    for (x,y,w,h) in faces:
        #print(x,h)
        roi_gray = gray[y:y+h,x:x+w] #(ycord_start,ycord_end)
        roi_color = frame[y:y+h+50,x:x+w+50]


        img_item = "sample.jpg"
        cv2.imwrite(img_item,roi_gray)

        color = (255,0) #BGR 0-255
        stroke = 2
        end_cord_x = x + w+50
        end_cord_y = y + h+50
        cv2.rectangle(frame,(x,y),(end_cord_x,end_cord_y),color,stroke)
    # display the resulting frame

    cv2.imshow('frame',frame)

    classify_face("sample.jpg")
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break

# When everything done,release the capture
cap.release()
cv2.destroyAllWindows()
import face_recognition as fr
import os
import cv2
import face_recognition
import numpy as np
from time import sleep


def get_encoded_faces():
    """
    looks through the faces folder and encodes all
    the faces

    :return: dict of (name,image encoded)
    """
    encoded = {}

    for dirpath,dnames,fnames in os.walk("./faces"):
        for f in fnames:
            if f.endswith(".jpg") or f.endswith(".png"):
                face = fr.load_image_file("faces/" + f)
                encoding = fr.face_encodings(face)[0]
                encoded[f.split(".")[0]] = encoding

    return encoded


def unkNown_image_encoded(img):
    """
    encode a face given the file name
    """
    face = fr.load_image_file("faces/" + img)
    encoding = fr.face_encodings(face)[0]

    return encoding


def classify_face(im):
    """
    will find all of the faces in a given image and label
    them if it kNows what they are

    :param im: str of file path
    :return: list of face names
    """
    faces = get_encoded_faces()
    faces_encoded = list(faces.values())
    kNown_face_names = list(faces.keys())

    img = cv2.imread(im,1)
    #img = cv2.resize(img,(0,0),fx=0.5,fy=0.5)
    #img = img[:,:,::-1]
 
    face_locations = face_recognition.face_locations(img)
    unkNown_face_encodings = face_recognition.face_encodings(img,face_locations)

    face_names = []
    for face_encoding in unkNown_face_encodings:
        # See if the face is a match for the kNown face(s)
        matches = face_recognition.compare_faces(faces_encoded,face_encoding)
        name = "UnkNown"

        # use the kNown face with the smallest distance to the new face
        face_distances = face_recognition.face_distance(faces_encoded,face_encoding)
        best_match_index = np.argmin(face_distances)
        if matches[best_match_index]:
            name = kNown_face_names[best_match_index]

        face_names.append(name)

    print(face_names)
#classify_face("sample.jpg")

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)