【问题标题】:OpenCV Python face recognition only one face recognitionOpenCV Python人脸识别只有一个人脸识别
【发布时间】:2018-06-14 01:42:14
【问题描述】:

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

处理后的图像: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


    【解决方案1】:

    您的预测应该在 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 循环中绘制预测
    • 因此您可以在一个函数中完成所有操作

    【讨论】:

    • 我尝试运行代码,因为您的代码不起作用。 TRACEBACK predict_img1 = predict_face(test_img1) draw_text(img, label, x, y - 5) # 将预测的标签绘制在框的顶部 cv2.putText(img, text, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.5, ( 0, 0, 255), 2) TypeError: bad argument type for built-in operation 为什么?
    • 你是从predict_face() 函数返回img 吗???.. 哪一行你到底得到了错误.. 我认为你没有返回img 这就是为什么@ 987654326@ 是 None 所以它会给你这个错误
    • 是的,你是对的,我解决了这个问题。谢谢。
    【解决方案2】:

    为什么要将原始图像传递给predict() 函数? 一旦你检测到这两张脸,你应该将它们(extracted from the original image using OpenCV functions)传递给预测函数。 通过这种方式,您的算法将起作用并且速度更快。

    祝你好运!

    【讨论】:

      猜你喜欢
      • 2019-08-10
      • 2017-02-26
      • 2017-02-03
      • 2015-07-05
      • 2019-05-10
      • 2013-12-24
      • 2020-10-07
      • 2021-08-16
      • 2016-09-18
      相关资源
      最近更新 更多