Problem running facial recognition test with Python - Opencv

Asked

Viewed 140 times

0

When executing the following test code:

import cv2
import os
import numpy as np
from PIL import Image

detector_face = cv2.CascadeClassifier(r"C:\Users\JokerDTR\Documents\SoftwaresProjeto\ReconhecimentoFacial\protótipo\haarcascade_frontalface_default.xml")
reconhecedor = cv2.face.EigenFaceRecognizer_create()
reconhecedor.read("classificadorEigen.yml")
# reconhecedor = cv2.face.FisherFaceRecognizer_create()
# reconhecedor.read("classificadorFisher.yml")
# reconhecedor = cv2.face.LBPHFaceRecognizer_create()
# reconhecedor.read("classificadorLBPH.yml")

total_acertos = 0
percentual_acerto = 0.0
total_confianca = 0.0

caminhos = [os.path.join('TESTE', f) for f in os.listdir('TESTE')]
for caminhos_imagem in caminhos:
    imagem_face = Image.open(caminhos_imagem).convert('L')
    imagem_np = np.array(imagem_face, 'uint8')
    faces_detectadas = detector_face.detectMultiScale(imagem_face)
    for (x, y, l, a) in faces_detectadas:
        idprevisto, confianca = reconhecedor.predict(imagemFaceNP)
        idatual = int(os.path.split(caminhos_imagem)[1].split(".")[0].replace("subject", ""))
        print(str(idatual) + " foi classificado como " + str(idprevisto) + " - " + str(confianca))
        if idprevisto == idatual:
            totalAcertos += 1
            totalConfianca += confianca
        cv2.rectangle(imagemFaceNP, (x, y), (x + l, y + a), (0, 0, 255), 2)
        cv2.imshow("Face", imagemFaceNP)
        cv2.waitKey(1000)
percentual_acerto = (total_acertos / 30) * 100
total_confianca = total_confianca / total_acertos
print("Percentual de acerto: " + str(percentual_acerto))
print("Total confiança: " + str(total_confianca))

I’m having the mistake:

Traceback (most recent call last):
  File "teste.py", line 22, in <module>
    faces_detectadas = detector_face.detectMultiScale(imagem_face)
TypeError: Expected cv::UMat for argument 'image'
  • Your image is like Matrix of the type CV_8U containing an image where objects are detected.? Or 8-bit unsigned integer (uchar). Instead of using the PIL library, use Opencv’s to open and convert the image, as Opencv is used for image processing...

  • your problem that you convert an image to an unsupported format with opencv. from a cv2.imread and then use color2gray function

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.