OpenCV Python人脸识别只能识别一张人脸

问题描述 投票:0回答:3

大家好当我运行下面的代码时,我们正在开发一个人脸识别应用程序,正如您在照片中看到的那样,它仅适用于一张脸(红色方块),我猜它不会扫描训练数据中的其他人脸我的预测函数只运行一次。不要在循环中。

处理后的图像:LINK

 # coding: utf-8
 import cv2
 import os
 import numpy as np
 suclular = ["Bilinmeyen", "Veli Eroglu", "Ali Eroglu"]


 def detect_face(img):
     # ALGORİMA için Gri Yapıyoruz.
     gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
     # yüz tanımlama için geereken haarcascade
     face_cascade = cv2.CascadeClassifier(
         'opencv-files/lbpcascade_frontalface.xml')
     faces = face_cascade.detectMultiScale(
         gray, scaleFactor=1.2, minNeighbors=5)  # YÜZ TANIMLAMA
     for (x, y, w, h) in faces:
         img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
     if (len(faces) == 0):
         return None, None  # Yuz bulunamazsa...
     (x, y, w, h) = faces[0]
     return gray[y:y + w, x:x + h], faces[0]

 face_recognizer = cv2.face.LBPHFaceRecognizer_create()
 face_recognizer.train(faces, np.array(labels))


 def draw_rectangle(img, rect):
     (x, y, w, h) = rect
     cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)


 def draw_text(img, text, x, y):
     cv2.putText(img, text, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 255), 2)


 def predict(test_img):
     img = test_img.copy()
     face, rect = detect_face(img)
     label, confidence = face_recognizer.predict(face)
     print(confidence)
     label_text = suclular[label]
     if confidence > 42 and confidence < 70:
         label_text = "Tespit Edilemedi."
         print(label_text)
     elif confidence > 70:
         label_text = "Bilinmiyor"
     draw_rectangle(img, rect)
     draw_text(img, label_text, rect[0], rect[1] - 5)

     return img


 print("Predicting images...")
 test_img1 = cv2.imread("test-data/test8jpg.jpg")
 predicted_img1 = predict(test_img1)
 print("Prediction complete")
 cv2.imshow("SONUC", cv2.resize(predicted_img1, (400, 500)))
 cv2.waitKey(0)
 cv2.destroyAllWindows()
 cv2.waitKey(1)
 cv2.destroyAllWindows()
python opencv image-processing face-recognition
3个回答
2
投票

您的预测应该在 for 循环中...您只从

detect_face
函数返回一个面,这是最后一个面,即使您循环遍历每个面并为每个面制作一个矩形...您应该做这样的事情:

def predict_face(img):
     # ALGORİMA için Gri Yapıyoruz.
     gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
     # yüz tanımlama için geereken haarcascade
     face_cascade = cv2.CascadeClassifier(
         'opencv-files/lbpcascade_frontalface.xml')
     faces = face_cascade.detectMultiScale(
         gray, scaleFactor=1.2, minNeighbors=5)  # YÜZ TANIMLAMA
     detected_faces = []
     i = 0
     for (x, y, w, h) in faces:
         img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
         detected_face = gray[y:y + w, x:x + h]
         label, confidence = face_recognizer.predict(detected_face)  # Prediction inside for loop
         draw_rectangle(img, faces[i])    # draw the red rectangles for every predicted face
         draw_text(img, label, x, y - 5)  # draw the predicted label on top of the box
         i += 1
  • 参见 for 循环中的第三行...我的预测在 for 循环内
  • 甚至在 for 循环中绘制矩形
  • 无需存储矩形并再次循环遍历它们来绘制它们
  • 在 for 循环中绘制预测
  • 所以你可以在一个函数中完成所有事情

0
投票

为什么要将原始图像传递给

predict()
函数? 一旦检测到两张脸,您应该将它们(使用 OpenCV 函数从原始图像中提取)传递给预测函数。 这样你的算法就会工作并且更快。

祝你好运!


0
投票

我已经有一个人脸检测应用程序 在这里

#fdm:
import time
import cv2 as cv
import mediapipe as mp
import numpy as np
import argparse
import imutils

def get_args():
    parser = argparse.ArgumentParser()
    
    parser.add_argument("-D", "--device", help='choose your input(availble: mp4 leave empty for camera)', default=0)
    parser.add_argument("-S", "--size", help='cap width', type=int, default=640)
    args = parser.parse_args()
    return args
args = get_args()

global cap_device
cap_device = args.device
global size
size = args.size
mp_face_detection = mp.solutions.face_detection
cap_device = args.device
cap = cv.VideoCapture(cap_device)
with mp_face_detection.FaceDetection(model_selection=1, min_detection_confidence=0.5) as face_detector:
    frame_counter = 0
    fonts = cv.FONT_HERSHEY_PLAIN
    start_time = time.time()
    while True:
        frame_counter += 1
        ret, frame = cap.read()
        if ret is False:
            break
        rgb_frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
        frame = imutils.resize(frame, width=size)
        results = face_detector.process(rgb_frame)
        frame_height, frame_width, c = frame.shape
        if results.detections:
            for face in results.detections:
                face_react = np.multiply(
                    [
                        face.location_data.relative_bounding_box.xmin,
                        face.location_data.relative_bounding_box.ymin,
                        face.location_data.relative_bounding_box.width,
                        face.location_data.relative_bounding_box.height,
                    ],
                    [frame_width, frame_height, frame_width, frame_height]).astype(int)
                
                cv.rectangle(frame, face_react, color=(255, 255, 255), thickness=2)
                key_points = np.array([(p.x, p.y) for p in face.location_data.relative_keypoints])
                key_points_coords = np.multiply(key_points,[frame_width,frame_height]).astype(int)
        
        fps = frame_counter / (time.time() - start_time)
        cv.putText(frame,f"FPS: {fps:.2f}",(10, 30),cv.FONT_HERSHEY_DUPLEX,0.7,(0, 255, 255),2,)
        key = cv.waitKey(1)
        if key == 27:
            break

        cv.imshow("face detection", frame)
    cap.release()
    cv.destroyAllWindows()

© www.soinside.com 2019 - 2024. All rights reserved.