1

Hi,Everyone We are working on a face recognition application when ı run the code below, as you can see in the photo it works only in one face(red square) it doesnt scan other faces in the traning-data ı guess my predict function only run one time. dont in the loop.

Processed Image : 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()
Veli Eroglu
  • 115
  • 3
  • 10

2 Answers2

2

You prediction should be with in the for loop... You are only returning one face from your detect_face function which is the last face even though you are looping through each face and making a rectangle for each face... You should do something like this:

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
  • See the third line in for loop ... My prediction is inside the for loop
  • Even draw the rectangles with in the for loop
  • No need to store the rectangles and loop through them again to draw them
  • Draw the prediction with in the for loop
  • So you do everything in one function
Jai
  • 3,211
  • 2
  • 17
  • 26
  • ı try run the code because your code dont work. TRACEBACK predicted_img1 = predict_face(test_img1) draw_text(img, label, x, y - 5) # draw the predicted label on top of the box cv2.putText(img, text, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 255), 2) TypeError: bad argument type for built-in operation Why ? – Veli Eroglu Jan 04 '18 at 15:25
  • are you returning the `img` from `predict_face()` function ???.. which line are you exactly getting the error.... I think you are not returning the `img` thats why `predicted_img1` is None so its throwing you that error – Jai Jan 04 '18 at 18:43
  • yeah you're right i solve this problem that's works. Thank You. – Veli Eroglu Jan 05 '18 at 07:38
0

Why are you passing the original image to the predict() function? Once you've detected the two faces, you should pass each of them (extracted from the original image using OpenCV functions) to the predict function. In this way your algorithm will work and be faster.

Good luck!

decadenza
  • 2,380
  • 3
  • 18
  • 31